mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-19 14:27:32 +00:00
8a5badddf6
* fix: change oidc config * fix: change oidc config secret * begin models * begin repo * fix: implement grpc app funcs * fix: add application requests * fix: converter * fix: converter * fix: converter and generate clientid * fix: tests * feat: project grant aggregate * feat: project grant * fix: project grant check if role existing * fix: project grant requests * fix: project grant fixes * fix: project grant member model * fix: project grant member aggregate * fix: project grant member eventstore * fix: project grant member requests * feat: user model * begin repo * repo models and more * feat: user command side * lots of functions * user command side * profile requests * commit before rebase on user * save * local config with gopass and more * begin new auth command (user centric) * Update internal/user/model/user.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/address.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/address.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/email.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/email.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/email.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/mfa.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/mfa.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/password.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/password.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/password.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/phone.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/phone.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/phone.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/user.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/user.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/model/user.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/usergrant/repository/eventsourcing/model/user_grant.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/usergrant/repository/eventsourcing/model/user_grant.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/usergrant/repository/eventsourcing/user_grant.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/user_test.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/user/repository/eventsourcing/eventstore_mock_test.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * changes from mr review * save files into basedir * changes from mr review * changes from mr review * move to auth request * Update internal/usergrant/repository/eventsourcing/cache.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/usergrant/repository/eventsourcing/cache.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * changes requested on mr * fix generate codes * fix return if no events * password code * email verification step * more steps * lot of mfa * begin tests * more next steps * auth api * auth api (user) * auth api (user) * auth api (user) * differ requests * merge * tests * fix compilation error * mock for id generator * Update internal/user/repository/eventsourcing/model/password.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/user/repository/eventsourcing/model/user.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * requests of mr * check email * begin separation of command and query * otp * change packages * some cleanup and fixes * tests for auth request / next steps * add VerificationLifetimes to config and make it run * tests * fix code challenge validation * cleanup * fix merge * begin view * repackaging tests and configs * fix startup config for auth * add migration * add PromptSelectAccount * fix copy / paste * remove user_agent files * fixes * fix sequences in user_session * token commands * token queries and signout * fix * fix set password test * add token handler and table * handle session init * add session state * add user view test cases * change VerifyMyMfaOTP * some fixes * fix user repo in auth api * cleanup * add user session view test * fix merge * begin oidc * user agent and more * config * keys * key command and query * add login statics * key handler * start login * login handlers * lot of fixes * merge oidc * add missing exports * add missing exports * fix some bugs * authrequestid in htmls * getrequest * update auth request * fix userid check * add username to authrequest * fix user session and auth request handling * fix UserSessionsByAgentID * fix auth request tests * fix user session on UserPasswordChanged and MfaOtpRemoved * fix MfaTypesSetupPossible * handle mfa * fill username * auth request query checks new events * fix userSessionByIDs * fix tokens * fix userSessionByIDs test * add user selection * init code * user code creation date * add init user step * add verification failed types * add verification failures * verify init code * user init code handle * user init code handle * fix userSessionByIDs * update logging * user agent cookie * browserinfo from request * add DeleteAuthRequest * add static login files to binary * add login statik to build * move generate to separate file and remove statik.go files * remove static dirs from startup.yaml * generate into separate namespaces * merge master * auth request code * auth request type mapping * fix keys * improve tokens * improve register and basic styling * fix ailerons font * improve password reset * add audience to token * all oidc apps as audience * fix test nextStep * fix email texts * remove "not set" * lot of style changes * improve copy to clipboard * fix footer * add cookie handler * remove placeholders * fix compilation after merge * fix auth config * remove comments * typo * use new secrets store * change default pws to match default policy * fixes * add todo * enable login * fix db name * Auth queries (#179) * my usersession * org structure/ auth handlers * working user grant spooler * auth internal user grants * search my project orgs * remove permissions file * my zitadel permissions * my zitadel permissions * remove unused code * authz * app searches in view * token verification * fix user grant load * fix tests * fix tests * read configs * remove unused const * remove todos * env variables * app_name * working authz * search projects * global resourceowner * Update internal/api/auth/permissions.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/api/auth/permissions.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * model2 rename * at least it works * check token expiry * search my user grants * remove token table from authz Co-authored-by: Livio Amstutz <livio.a@gmail.com> * fix test * fix ports and enable console Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com> Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> Co-authored-by: Silvan <silvan.reusser@gmail.com>
1028 lines
27 KiB
Go
1028 lines
27 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"context"
|
|
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/caos/zitadel/internal/api/auth"
|
|
"github.com/caos/zitadel/internal/errors"
|
|
es_mock "github.com/caos/zitadel/internal/eventstore/mock"
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
"github.com/golang/mock/gomock"
|
|
)
|
|
|
|
type testOrgEventstore struct {
|
|
OrgEventstore
|
|
mockEventstore *es_mock.MockEventstore
|
|
}
|
|
|
|
func newTestEventstore(t *testing.T) *testOrgEventstore {
|
|
mock := mockEventstore(t)
|
|
return &testOrgEventstore{OrgEventstore: OrgEventstore{Eventstore: mock}, mockEventstore: mock}
|
|
}
|
|
|
|
func (es *testOrgEventstore) expectFilterEvents(events []*es_models.Event, err error) *testOrgEventstore {
|
|
es.mockEventstore.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, err)
|
|
return es
|
|
}
|
|
|
|
func (es *testOrgEventstore) expectPushEvents(startSequence uint64, err error) *testOrgEventstore {
|
|
es.mockEventstore.EXPECT().PushAggregates(gomock.Any(), gomock.Any()).DoAndReturn(
|
|
func(_ context.Context, aggregates ...*es_models.Aggregate) error {
|
|
for _, aggregate := range aggregates {
|
|
for _, event := range aggregate.Events {
|
|
event.Sequence = startSequence
|
|
startSequence++
|
|
}
|
|
}
|
|
return err
|
|
})
|
|
return es
|
|
}
|
|
|
|
func (es *testOrgEventstore) expectAggregateCreator() *testOrgEventstore {
|
|
es.mockEventstore.EXPECT().AggregateCreator().Return(es_models.NewAggregateCreator("test"))
|
|
return es
|
|
}
|
|
|
|
func mockEventstore(t *testing.T) *es_mock.MockEventstore {
|
|
ctrl := gomock.NewController(t)
|
|
e := es_mock.NewMockEventstore(ctrl)
|
|
|
|
return e
|
|
}
|
|
|
|
func TestOrgEventstore_OrgByID(t *testing.T) {
|
|
type fields struct {
|
|
Eventstore *testOrgEventstore
|
|
}
|
|
type res struct {
|
|
expectedSequence uint64
|
|
isErr func(error) bool
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
org *org_model.Org
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "no input org",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
org: nil,
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
},
|
|
},
|
|
{
|
|
name: "no aggregate id in input org",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
org: &org_model.Org{ObjectRoot: es_models.ObjectRoot{Sequence: 4}},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "no events found success",
|
|
fields: fields{Eventstore: newTestEventstore(t).expectFilterEvents([]*es_models.Event{}, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
org: &org_model.Org{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "hodor"}},
|
|
},
|
|
res: res{
|
|
expectedSequence: 4,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "filter fail",
|
|
fields: fields{Eventstore: newTestEventstore(t).expectFilterEvents([]*es_models.Event{}, errors.ThrowInternal(nil, "EVENT-SAa1O", "message"))},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
org: &org_model.Org{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "hodor"}},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsInternal,
|
|
},
|
|
},
|
|
{
|
|
name: "new events found and added success",
|
|
fields: fields{Eventstore: newTestEventstore(t).expectFilterEvents([]*es_models.Event{
|
|
{Sequence: 6},
|
|
}, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
org: &org_model.Org{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "hodor-org", ChangeDate: time.Now(), CreationDate: time.Now()}},
|
|
},
|
|
res: res{
|
|
expectedSequence: 6,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tt.fields.Eventstore.OrgByID(tt.args.ctx, tt.args.org)
|
|
if tt.res.isErr == nil && err != nil {
|
|
t.Errorf("no error expected got:%T %v", err, err)
|
|
}
|
|
if tt.res.isErr != nil && !tt.res.isErr(err) {
|
|
t.Errorf("wrong error got %T: %v", err, err)
|
|
}
|
|
if got == nil && tt.res.expectedSequence != 0 {
|
|
t.Errorf("org should be nil but was %v", got)
|
|
t.FailNow()
|
|
}
|
|
if tt.res.expectedSequence != 0 && tt.res.expectedSequence != got.Sequence {
|
|
t.Errorf("org should have sequence %d but had %d", tt.res.expectedSequence, got.Sequence)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrgEventstore_DeactivateOrg(t *testing.T) {
|
|
type fields struct {
|
|
Eventstore *testOrgEventstore
|
|
}
|
|
type res struct {
|
|
expectedSequence uint64
|
|
isErr func(error) bool
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
orgID string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "no input org",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "",
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
},
|
|
},
|
|
{
|
|
name: "push failed",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectFilterEvents([]*es_models.Event{orgCreatedEvent()}, nil).
|
|
expectAggregateCreator().
|
|
expectPushEvents(0, errors.ThrowInternal(nil, "EVENT-S8WzW", "test"))},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "hodor",
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsInternal,
|
|
},
|
|
},
|
|
{
|
|
name: "push correct",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectFilterEvents([]*es_models.Event{orgCreatedEvent()}, nil).
|
|
expectAggregateCreator().
|
|
expectPushEvents(6, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "hodor",
|
|
},
|
|
res: res{
|
|
expectedSequence: 6,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "org already inactive error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectFilterEvents([]*es_models.Event{orgCreatedEvent(), orgInactiveEvent()}, nil).
|
|
expectAggregateCreator().
|
|
expectPushEvents(6, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "hodor",
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tt.fields.Eventstore.DeactivateOrg(tt.args.ctx, tt.args.orgID)
|
|
if tt.res.isErr == nil && err != nil {
|
|
t.Errorf("no error expected got:%T %v", err, err)
|
|
}
|
|
if tt.res.isErr != nil && !tt.res.isErr(err) {
|
|
t.Errorf("wrong error got %T: %v", err, err)
|
|
}
|
|
if got == nil && tt.res.expectedSequence != 0 {
|
|
t.Errorf("org should be nil but was %v", got)
|
|
t.FailNow()
|
|
}
|
|
if tt.res.expectedSequence != 0 && tt.res.expectedSequence != got.Sequence {
|
|
t.Errorf("org should have sequence %d but had %d", tt.res.expectedSequence, got.Sequence)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrgEventstore_ReactivateOrg(t *testing.T) {
|
|
type fields struct {
|
|
Eventstore *testOrgEventstore
|
|
}
|
|
type res struct {
|
|
expectedSequence uint64
|
|
isErr func(error) bool
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
orgID string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "no input org",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "",
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
},
|
|
},
|
|
{
|
|
name: "push failed",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectFilterEvents([]*es_models.Event{orgCreatedEvent(), orgInactiveEvent()}, nil).
|
|
expectAggregateCreator().
|
|
expectPushEvents(0, errors.ThrowInternal(nil, "EVENT-S8WzW", "test"))},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "hodor",
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsInternal,
|
|
},
|
|
},
|
|
{
|
|
name: "push correct",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectFilterEvents([]*es_models.Event{orgCreatedEvent(), orgInactiveEvent()}, nil).
|
|
expectAggregateCreator().
|
|
expectPushEvents(6, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "hodor",
|
|
},
|
|
res: res{
|
|
expectedSequence: 6,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "org already active error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectFilterEvents([]*es_models.Event{orgCreatedEvent()}, nil).
|
|
expectAggregateCreator().
|
|
expectPushEvents(6, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
orgID: "hodor",
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tt.fields.Eventstore.ReactivateOrg(tt.args.ctx, tt.args.orgID)
|
|
if tt.res.isErr == nil && err != nil {
|
|
t.Errorf("no error expected got:%T %v", err, err)
|
|
}
|
|
if tt.res.isErr != nil && !tt.res.isErr(err) {
|
|
t.Errorf("wrong error got %T: %v", err, err)
|
|
}
|
|
if got == nil && tt.res.expectedSequence != 0 {
|
|
t.Errorf("org should be nil but was %v", got)
|
|
t.FailNow()
|
|
}
|
|
if tt.res.expectedSequence != 0 && tt.res.expectedSequence != got.Sequence {
|
|
t.Errorf("org should have sequence %d but had %d", tt.res.expectedSequence, got.Sequence)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrgEventstore_OrgMemberByIDs(t *testing.T) {
|
|
type fields struct {
|
|
Eventstore *testOrgEventstore
|
|
}
|
|
type res struct {
|
|
expectedSequence uint64
|
|
isErr func(error) bool
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
member *org_model.OrgMember
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "no input member",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: nil,
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "no aggregate id in input member",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{ObjectRoot: es_models.ObjectRoot{Sequence: 4}, UserID: "asdf"},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "no aggregate id in input member",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "asdf"}},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "no events found success",
|
|
fields: fields{Eventstore: newTestEventstore(t).expectFilterEvents([]*es_models.Event{}, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "plants"}, UserID: "banana"},
|
|
},
|
|
res: res{
|
|
expectedSequence: 4,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "filter fail",
|
|
fields: fields{Eventstore: newTestEventstore(t).expectFilterEvents([]*es_models.Event{}, errors.ThrowInternal(nil, "EVENT-SAa1O", "message"))},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "plants"}, UserID: "banana"},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsInternal,
|
|
},
|
|
},
|
|
{
|
|
name: "new events found and added success",
|
|
fields: fields{Eventstore: newTestEventstore(t).expectFilterEvents([]*es_models.Event{
|
|
{Sequence: 6, Data: []byte("{\"userId\": \"banana\", \"roles\": [\"bananaa\"]}"), Type: model.OrgMemberChanged},
|
|
}, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "plants", ChangeDate: time.Now(), CreationDate: time.Now()}, UserID: "banana"},
|
|
},
|
|
res: res{
|
|
expectedSequence: 6,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "not member of org error",
|
|
fields: fields{Eventstore: newTestEventstore(t).expectFilterEvents([]*es_models.Event{
|
|
{Sequence: 6, Data: []byte("{\"userId\": \"banana\", \"roles\": [\"bananaa\"]}"), Type: model.OrgMemberAdded},
|
|
{Sequence: 7, Data: []byte("{\"userId\": \"apple\"}"), Type: model.OrgMemberRemoved},
|
|
}, nil)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{ObjectRoot: es_models.ObjectRoot{Sequence: 4, AggregateID: "plants", ChangeDate: time.Now(), CreationDate: time.Now()}, UserID: "apple"},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsNotFound,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tt.fields.Eventstore.OrgMemberByIDs(tt.args.ctx, tt.args.member)
|
|
if tt.res.isErr == nil && err != nil {
|
|
t.Errorf("no error expected got:%T %v", err, err)
|
|
}
|
|
if tt.res.isErr != nil && !tt.res.isErr(err) {
|
|
t.Errorf("wrong error got %T: %v", err, err)
|
|
}
|
|
if got == nil && tt.res.expectedSequence != 0 {
|
|
t.Errorf("org should be nil but was %v", got)
|
|
t.FailNow()
|
|
}
|
|
if tt.res.expectedSequence != 0 && tt.res.expectedSequence != got.Sequence {
|
|
t.Errorf("org should have sequence %d but had %d", tt.res.expectedSequence, got.Sequence)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrgEventstore_AddOrgMember(t *testing.T) {
|
|
type fields struct {
|
|
Eventstore *testOrgEventstore
|
|
}
|
|
type res struct {
|
|
expectedSequence uint64
|
|
isErr func(error) bool
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
member *org_model.OrgMember
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "no input member",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: nil,
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "push failed",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
}, nil).
|
|
expectAggregateCreator().
|
|
expectPushEvents(0, errors.ThrowInternal(nil, "EVENT-S8WzW", "test"))},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
Sequence: 4,
|
|
AggregateID: "hodor-org",
|
|
},
|
|
UserID: "hodor",
|
|
Roles: []string{"nix"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsInternal,
|
|
},
|
|
},
|
|
{
|
|
name: "push correct",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
}, nil).
|
|
expectPushEvents(6, nil),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
Sequence: 4,
|
|
AggregateID: "hodor-org",
|
|
},
|
|
UserID: "hodor",
|
|
Roles: []string{"nix"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 6,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "member already exists error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "hodor", "roles": ["master"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil).
|
|
expectPushEvents(0, errors.ThrowAlreadyExists(nil, "EVENT-yLTI6", "weiss nöd wie teste")),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
Sequence: 4,
|
|
AggregateID: "hodor-org",
|
|
},
|
|
UserID: "hodor",
|
|
Roles: []string{"nix"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorAlreadyExists,
|
|
},
|
|
},
|
|
{
|
|
name: "member deleted success",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectPushEvents(10, nil).
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "hodor", "roles": ["master"]}`),
|
|
Sequence: 6,
|
|
},
|
|
{
|
|
Type: model.OrgMemberRemoved,
|
|
Data: []byte(`{"userId": "hodor"}`),
|
|
Sequence: 10,
|
|
},
|
|
}, nil),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
Sequence: 4,
|
|
AggregateID: "hodor-org",
|
|
},
|
|
UserID: "hodor",
|
|
Roles: []string{"nix"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 10,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "org not exists error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents(nil, nil).
|
|
expectPushEvents(0, errors.ThrowAlreadyExists(nil, "EVENT-yLTI6", "weiss nöd wie teste")),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{
|
|
Sequence: 4,
|
|
AggregateID: "hodor-org",
|
|
},
|
|
UserID: "hodor",
|
|
Roles: []string{"nix"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorAlreadyExists,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := tt.fields.Eventstore.AddOrgMember(tt.args.ctx, tt.args.member)
|
|
if tt.res.isErr == nil && err != nil {
|
|
t.Errorf("no error expected got:%T %v", err, err)
|
|
}
|
|
if tt.res.isErr != nil && !tt.res.isErr(err) {
|
|
t.Errorf("wrong error got %T: %v", err, err)
|
|
}
|
|
if got == nil && tt.res.expectedSequence != 0 {
|
|
t.Errorf("org should not be nil but was %v", got)
|
|
t.FailNow()
|
|
}
|
|
if tt.res.expectedSequence != 0 && tt.res.expectedSequence != got.Sequence {
|
|
t.Errorf("org should have sequence %d but had %d", tt.res.expectedSequence, got.Sequence)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrgEventstore_ChangeOrgMember(t *testing.T) {
|
|
type fields struct {
|
|
Eventstore *testOrgEventstore
|
|
}
|
|
type res struct {
|
|
isErr func(error) bool
|
|
expectedSequence uint64
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
member *org_model.OrgMember
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "no input member",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: nil,
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member not found error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "brudi", "roles": ["master of desaster"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{AggregateID: "hodor-org", Sequence: 5},
|
|
UserID: "hodor",
|
|
Roles: []string{"master"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsNotFound,
|
|
},
|
|
},
|
|
{
|
|
name: "member found no changes error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "hodor", "roles": ["master"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{AggregateID: "hodor-org", Sequence: 5},
|
|
UserID: "hodor",
|
|
Roles: []string{"master"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
},
|
|
},
|
|
{
|
|
name: "push error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "hodor", "roles": ["master"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil).
|
|
expectPushEvents(0, errors.ThrowInternal(nil, "PEVENT-3wqa2", "test")),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{AggregateID: "hodor-org", Sequence: 5},
|
|
UserID: "hodor",
|
|
Roles: []string{"master of desaster"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 0,
|
|
isErr: errors.IsInternal,
|
|
},
|
|
},
|
|
{
|
|
name: "change success",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "hodor", "roles": ["master"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil).
|
|
expectPushEvents(7, nil),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{AggregateID: "hodor-org", Sequence: 5},
|
|
UserID: "hodor",
|
|
Roles: []string{"master of desaster"},
|
|
},
|
|
},
|
|
res: res{
|
|
expectedSequence: 7,
|
|
isErr: nil,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
es := &OrgEventstore{
|
|
Eventstore: tt.fields.Eventstore,
|
|
}
|
|
got, err := es.ChangeOrgMember(tt.args.ctx, tt.args.member)
|
|
if tt.res.isErr == nil && err != nil {
|
|
t.Errorf("no error expected got:%T %v", err, err)
|
|
}
|
|
if tt.res.isErr != nil && !tt.res.isErr(err) {
|
|
t.Errorf("wrong error got %T: %v", err, err)
|
|
}
|
|
if got == nil && tt.res.expectedSequence != 0 {
|
|
t.Errorf("org should not be nil but was %v", got)
|
|
t.FailNow()
|
|
}
|
|
if tt.res.expectedSequence != 0 && tt.res.expectedSequence != got.Sequence {
|
|
t.Errorf("org should have sequence %d but had %d", tt.res.expectedSequence, got.Sequence)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrgEventstore_RemoveOrgMember(t *testing.T) {
|
|
type fields struct {
|
|
Eventstore *testOrgEventstore
|
|
}
|
|
type res struct {
|
|
isErr func(error) bool
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
member *org_model.OrgMember
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "no input member",
|
|
fields: fields{Eventstore: newTestEventstore(t)},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: nil,
|
|
},
|
|
res: res{
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
},
|
|
},
|
|
{
|
|
name: "member not found error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "brudi", "roles": ["master of desaster"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{AggregateID: "hodor-org", Sequence: 5},
|
|
UserID: "hodor",
|
|
Roles: []string{"master"},
|
|
},
|
|
},
|
|
res: res{
|
|
isErr: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "push error",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "hodor", "roles": ["master"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil).
|
|
expectPushEvents(0, errors.ThrowInternal(nil, "PEVENT-3wqa2", "test")),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{AggregateID: "hodor-org", Sequence: 5},
|
|
UserID: "hodor",
|
|
},
|
|
},
|
|
res: res{
|
|
isErr: errors.IsInternal,
|
|
},
|
|
},
|
|
{
|
|
name: "remove success",
|
|
fields: fields{Eventstore: newTestEventstore(t).
|
|
expectAggregateCreator().
|
|
expectFilterEvents([]*es_models.Event{
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgAdded,
|
|
Sequence: 4,
|
|
Data: []byte("{}"),
|
|
},
|
|
{
|
|
AggregateID: "hodor-org",
|
|
Type: model.OrgMemberAdded,
|
|
Data: []byte(`{"userId": "hodor", "roles": ["master"]}`),
|
|
Sequence: 6,
|
|
},
|
|
}, nil).
|
|
expectPushEvents(7, nil),
|
|
},
|
|
args: args{
|
|
ctx: auth.NewMockContext("user", "org"),
|
|
member: &org_model.OrgMember{
|
|
ObjectRoot: es_models.ObjectRoot{AggregateID: "hodor-org", Sequence: 5},
|
|
UserID: "hodor",
|
|
},
|
|
},
|
|
res: res{
|
|
isErr: nil,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
es := &OrgEventstore{
|
|
Eventstore: tt.fields.Eventstore,
|
|
}
|
|
err := es.RemoveOrgMember(tt.args.ctx, tt.args.member)
|
|
if tt.res.isErr == nil && err != nil {
|
|
t.Errorf("no error expected got:%T %v", err, err)
|
|
}
|
|
if tt.res.isErr != nil && !tt.res.isErr(err) {
|
|
t.Errorf("wrong error got %T: %v", err, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func orgCreatedEvent() *es_models.Event {
|
|
return &es_models.Event{
|
|
AggregateID: "hodor-org",
|
|
AggregateType: model.OrgAggregate,
|
|
AggregateVersion: "v1",
|
|
CreationDate: time.Now().Add(-1 * time.Minute),
|
|
Data: []byte(`{"name": "hodor-org", "domain":"hodor.org"}`),
|
|
EditorService: "testsvc",
|
|
EditorUser: "testuser",
|
|
ID: "sdlfö4t23kj",
|
|
ResourceOwner: "hodor-org",
|
|
Sequence: 32,
|
|
Type: model.OrgAdded,
|
|
}
|
|
}
|
|
|
|
func orgInactiveEvent() *es_models.Event {
|
|
return &es_models.Event{
|
|
AggregateID: "hodor-org",
|
|
AggregateType: model.OrgAggregate,
|
|
AggregateVersion: "v1",
|
|
CreationDate: time.Now().Add(-1 * time.Minute),
|
|
Data: nil,
|
|
EditorService: "testsvc",
|
|
EditorUser: "testuser",
|
|
ID: "sdlfö4t23kj",
|
|
ResourceOwner: "hodor-org",
|
|
Sequence: 52,
|
|
Type: model.OrgDeactivated,
|
|
}
|
|
}
|