2020-05-18 09:32:16 +00:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-08-26 07:56:23 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/id"
|
2020-07-28 07:42:21 +00:00
|
|
|
|
2020-05-18 09:32:16 +00:00
|
|
|
mock_cache "github.com/caos/zitadel/internal/cache/mock"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/mock"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
)
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func GetMockedEventstore(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *IAMEventstore {
|
|
|
|
return &IAMEventstore{
|
|
|
|
Eventstore: mockEs,
|
|
|
|
iamCache: GetMockCache(ctrl),
|
|
|
|
idGenerator: GetSonyFlacke(),
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func GetMockedEventstoreWithCrypto(ctrl *gomock.Controller, mockEs *mock.MockEventstore) *IAMEventstore {
|
|
|
|
return &IAMEventstore{
|
|
|
|
Eventstore: mockEs,
|
|
|
|
iamCache: GetMockCache(ctrl),
|
|
|
|
idGenerator: GetSonyFlacke(),
|
|
|
|
secretCrypto: crypto.NewBCrypt(10),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func GetMockCache(ctrl *gomock.Controller) *IAMCache {
|
2020-05-18 09:32:16 +00:00
|
|
|
mockCache := mock_cache.NewMockCache(ctrl)
|
|
|
|
mockCache.EXPECT().Get(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
|
|
|
mockCache.EXPECT().Set(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
2020-08-26 07:56:23 +00:00
|
|
|
return &IAMCache{iamCache: mockCache}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSonyFlacke() id.Generator {
|
|
|
|
return id.SonyFlakeGenerator
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func GetMockIamByIDOK(ctrl *gomock.Controller) *IAMEventstore {
|
|
|
|
data, _ := json.Marshal(model.IAM{GlobalOrgID: "GlobalOrgID"})
|
2020-05-18 09:32:16 +00:00
|
|
|
events := []*es_models.Event{
|
2020-08-26 07:56:23 +00:00
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IAMSetupStarted},
|
2020-05-18 09:32:16 +00:00
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.GlobalOrgSet, Data: data},
|
|
|
|
}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
return GetMockedEventstore(ctrl, mockEs)
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func GetMockIamByIDNoEvents(ctrl *gomock.Controller) *IAMEventstore {
|
2020-05-18 09:32:16 +00:00
|
|
|
events := []*es_models.Event{}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
return GetMockedEventstore(ctrl, mockEs)
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func GetMockManipulateIam(ctrl *gomock.Controller) *IAMEventstore {
|
2020-05-18 09:32:16 +00:00
|
|
|
events := []*es_models.Event{
|
2020-08-26 07:56:23 +00:00
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IAMSetupStarted},
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
mockEs.EXPECT().AggregateCreator().Return(es_models.NewAggregateCreator("TEST"))
|
|
|
|
mockEs.EXPECT().PushAggregates(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
return GetMockedEventstore(ctrl, mockEs)
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func GetMockManipulateIamWithCrypto(ctrl *gomock.Controller) *IAMEventstore {
|
|
|
|
events := []*es_models.Event{
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IAMSetupStarted},
|
|
|
|
}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
mockEs.EXPECT().AggregateCreator().Return(es_models.NewAggregateCreator("TEST"))
|
|
|
|
mockEs.EXPECT().PushAggregates(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
return GetMockedEventstoreWithCrypto(ctrl, mockEs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMockManipulateIamWithMember(ctrl *gomock.Controller) *IAMEventstore {
|
|
|
|
memberData, _ := json.Marshal(model.IAMMember{UserID: "UserID", Roles: []string{"Role"}})
|
2020-05-18 09:32:16 +00:00
|
|
|
events := []*es_models.Event{
|
2020-08-26 07:56:23 +00:00
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IAMSetupStarted},
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IAMMemberAdded, Data: memberData},
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
mockEs.EXPECT().AggregateCreator().Return(es_models.NewAggregateCreator("TEST"))
|
|
|
|
mockEs.EXPECT().PushAggregates(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
return GetMockedEventstore(ctrl, mockEs)
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
|
|
|
|
func GetMockManipulateIamWithOIDCIdp(ctrl *gomock.Controller) *IAMEventstore {
|
|
|
|
idpData, _ := json.Marshal(model.IDPConfig{IDPConfigID: "IDPConfigID", Name: "Name"})
|
|
|
|
oidcData, _ := json.Marshal(model.OIDCIDPConfig{IDPConfigID: "IDPConfigID", ClientID: "ClientID"})
|
|
|
|
events := []*es_models.Event{
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IAMSetupStarted},
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IDPConfigAdded, Data: idpData},
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.OIDCIDPConfigAdded, Data: oidcData},
|
|
|
|
}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
mockEs.EXPECT().AggregateCreator().Return(es_models.NewAggregateCreator("TEST"))
|
|
|
|
mockEs.EXPECT().PushAggregates(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
return GetMockedEventstore(ctrl, mockEs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMockManipulateIamWithLoginPolicy(ctrl *gomock.Controller) *IAMEventstore {
|
|
|
|
policyData, _ := json.Marshal(model.LoginPolicy{AllowRegister: true, AllowUsernamePassword: true, AllowExternalIdp: true})
|
|
|
|
idpProviderData, _ := json.Marshal(model.IDPProvider{IDPConfigID: "IDPConfigID", Type: 1})
|
|
|
|
events := []*es_models.Event{
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.IAMSetupStarted},
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.LoginPolicyAdded, Data: policyData},
|
|
|
|
&es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: model.LoginPolicyIDPProviderAdded, Data: idpProviderData},
|
|
|
|
}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
mockEs.EXPECT().AggregateCreator().Return(es_models.NewAggregateCreator("TEST"))
|
|
|
|
mockEs.EXPECT().PushAggregates(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
return GetMockedEventstore(ctrl, mockEs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMockManipulateIamNotExisting(ctrl *gomock.Controller) *IAMEventstore {
|
2020-05-18 09:32:16 +00:00
|
|
|
events := []*es_models.Event{}
|
|
|
|
mockEs := mock.NewMockEventstore(ctrl)
|
|
|
|
mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil)
|
|
|
|
mockEs.EXPECT().AggregateCreator().Return(es_models.NewAggregateCreator("TEST"))
|
|
|
|
mockEs.EXPECT().PushAggregates(gomock.Any(), gomock.Any()).Return(nil)
|
|
|
|
return GetMockedEventstore(ctrl, mockEs)
|
|
|
|
}
|