mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 19:14:23 +00:00
5abd5b0505
* feat(management): service accounts * chore: current go version * init * refactor: apis * feat(internal): start impl of service account * chore: start impl of machine/human users * code compiles * fix: tests * fix: tests * fix: add new event types to switches * chore: add cases to event types * fix(management): definitive proto messages * fix: machine/human * fix: add missing tables as todos * fix: remove unused permissions * fix: refactoring * fix: refactor * fix: human registered * fix: user id * fix: logid * fix: proto remove //equal * chore(management): remove no comment * fix: human mfas * fix: user subobjects * chore: rename existing to better name * fix: username in user (#634) * fix: username in user * fix: username * fix remove unused code * fix add validations * fix: use new user in all apis * fix: regexp for username in api * fix: fill user data for human and machine (#638) * fix: fill Display name grant/member handlers fix: add description to grant/member objects in api fix: check if user is human in login * fix: remove description from member and grant * chore: remove todos * feat: machine keys * fix: implement missing parts * feat: machine key management view * fix: remove keys from machine view * fix: set default expiration date * fix: get key by ids * feat: add machine keys in proto * feat: machine keys * fix: add migration * fix: mig * fix: correct method name * feat: user search * feat: user search * fix: log ids * fix partial authconfig prompt, domain c perm * membership read check * contributor refresh trigger, observe org write * fix: migrations * fix(console): machine build (#660) * frontend 1 * fix html bindings * trailing comma * user permissions, project deactivate * fix(console): human view (#661) * fix search user view, user detail form * rm log * feat(console): user services list and create (#663) * fix search user view, user detail form * rm log * machine list * generic table component * create user service * proove table for undefined values * tmp disable user link if machine * lint * lint styles * user table lint * Update console/src/assets/i18n/de.json Co-authored-by: Florian Forster <florian@caos.ch> * feat(console): service user detail view, keys cr_d, fix search user autocomplete (#664) * service users for sidenav, routing * i18n * back routes * machine detail form * update machine detail, fix svc user grants * keys table * add key dialog, timestamp creation * check permission on create, delete, fix selection * lint ts, scss * Update console/src/assets/i18n/de.json * Apply suggestions from code review Co-authored-by: Florian Forster <florian@caos.ch> * allow user grants for project.write * management service * fix mgmt service * feat: Machine keys (#655) * fix: memberships (#633) * feat: add iam members to memberships * fix: search project grants * fix: rename * feat: idp and login policy configurations (#619) * feat: oidc config * fix: oidc configurations * feat: oidc idp config * feat: add oidc config test * fix: tests * fix: tests * feat: translate new events * feat: idp eventstore * feat: idp eventstore * fix: tests * feat: command side idp * feat: query side idp * feat: idp config on org * fix: tests * feat: authz idp on org * feat: org idps * feat: login policy * feat: login policy * feat: login policy * feat: add idp func on login policy * feat: add validation to loginpolicy and idp provider * feat: add default login policy * feat: login policy on org * feat: login policy on org * fix: id config handlers * fix: id config handlers * fix: create idp on org * fix: create idp on org * fix: not existing idp config * fix: default login policy * fix: add login policy on org * fix: idp provider search on org * fix: test * fix: remove idp on org * fix: test * fix: test * fix: remove admin idp * fix: logo src as byte * fix: migration * fix: tests * Update internal/iam/repository/eventsourcing/iam.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/iam/repository/eventsourcing/iam_test.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/iam/repository/eventsourcing/iam_test.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/iam/repository/eventsourcing/model/login_policy.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/iam/repository/eventsourcing/model/login_policy.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/org/repository/eventsourcing/org_test.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/iam/repository/eventsourcing/model/login_policy_test.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/iam/repository/eventsourcing/model/login_policy_test.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * fix: pr comments * fix: tests * Update types.go * fix: merge request changes * fix: reduce optimization Co-authored-by: Silvan <silvan.reusser@gmail.com> Co-authored-by: Livio Amstutz <livio.a@gmail.com> * fix: reread user mfas, preferred loginname as otp account name (#636) * fix: reread user mfas * fix: use preferred login name as otp account name * fix: tests * fix: reduce (#635) * fix: management reduce optimization * fix: reduce optimization * fix: reduce optimization * fix: merge master * chore(deps): bump github.com/gorilla/schema from 1.1.0 to 1.2.0 (#627) Bumps [github.com/gorilla/schema](https://github.com/gorilla/schema) from 1.1.0 to 1.2.0. - [Release notes](https://github.com/gorilla/schema/releases) - [Commits](https://github.com/gorilla/schema/compare/v1.1.0...v1.2.0) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump github.com/gorilla/mux from 1.7.4 to 1.8.0 (#624) Bumps [github.com/gorilla/mux](https://github.com/gorilla/mux) from 1.7.4 to 1.8.0. - [Release notes](https://github.com/gorilla/mux/releases) - [Commits](https://github.com/gorilla/mux/compare/v1.7.4...v1.8.0) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps): bump github.com/DATA-DOG/go-sqlmock from 1.4.1 to 1.5.0 (#591) Bumps [github.com/DATA-DOG/go-sqlmock](https://github.com/DATA-DOG/go-sqlmock) from 1.4.1 to 1.5.0. - [Release notes](https://github.com/DATA-DOG/go-sqlmock/releases) - [Commits](https://github.com/DATA-DOG/go-sqlmock/compare/v1.4.1...v1.5.0) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore: auto assign issues and PR to ZTIADEL project board (#643) * Create main.yml * Update main.yml Co-authored-by: Livio Amstutz <livio.a@gmail.com> * fix(console): project grant members, update deps (#645) * fix: searchprojectgrantmembers * chore(deps-dev): bump @angular/cli from 10.0.6 to 10.0.7 in /console (#622) Bumps [@angular/cli](https://github.com/angular/angular-cli) from 10.0.6 to 10.0.7. - [Release notes](https://github.com/angular/angular-cli/releases) - [Commits](https://github.com/angular/angular-cli/compare/v10.0.6...v10.0.7) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps-dev): bump @angular-devkit/build-angular in /console (#626) Bumps [@angular-devkit/build-angular](https://github.com/angular/angular-cli) from 0.1000.6 to 0.1000.7. - [Release notes](https://github.com/angular/angular-cli/releases) - [Commits](https://github.com/angular/angular-cli/commits) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Max Peintner <max@caos.ch> * chore(deps-dev): bump @types/jasmine from 3.5.12 to 3.5.13 in /console (#623) Bumps [@types/jasmine](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jasmine) from 3.5.12 to 3.5.13. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jasmine) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps-dev): bump ts-node from 8.10.2 to 9.0.0 in /console (#629) Bumps [ts-node](https://github.com/TypeStrong/ts-node) from 8.10.2 to 9.0.0. - [Release notes](https://github.com/TypeStrong/ts-node/releases) - [Commits](https://github.com/TypeStrong/ts-node/compare/v8.10.2...v9.0.0) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * update packlock Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore: delete main.yml (#648) * fix: usergrant (#650) * fix(console): mfa refresh after verification, member eventemitter (#651) * refresh mfa * fix: detail link from contributors * lint * feat: add domain verification notification (#649) * fix: dont (re)generate client secret with auth type none * fix(cors): allow Origin from request * feat: add origin allow list and fix some core issues * rename migration * fix UserIDsByDomain * feat: send email to users after domain claim * username * check origin on userinfo * update oidc pkg * fix: add migration 1.6 * change username * change username * remove unique email aggregate * change username in mgmt * search global user by login name * fix test * change user search in angular * fix tests * merge * userview in angular * fix merge * Update pkg/grpc/management/proto/management.proto Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> * Update internal/notification/static/i18n/de.yaml Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> * fix Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> * fix: translation (#647) * fix: translation * fix: translation * fix: translation * fix: remove unused code * fix: log err * fix: migration numbers (#652) * chore: issue / feature templates (#642) * feat: machine keys * fix: implement missing parts * feat: machine key management view * fix: remove keys from machine view * feat: global org read (#657) * fix: set default expiration date * fix: get key by ids * feat: add machine keys in proto * feat: machine keys * fix: add migration * fix: mig * fix: correct method name * feat: user search * feat: user search * fix: log ids * fix: migrations * fix(console): machine build (#660) * frontend 1 * fix html bindings * trailing comma * fix(console): human view (#661) * fix search user view, user detail form * rm log * feat(console): user services list and create (#663) * fix search user view, user detail form * rm log * machine list * generic table component * create user service * proove table for undefined values * tmp disable user link if machine * lint * lint styles * user table lint * Update console/src/assets/i18n/de.json Co-authored-by: Florian Forster <florian@caos.ch> * feat(console): service user detail view, keys cr_d, fix search user autocomplete (#664) * service users for sidenav, routing * i18n * back routes * machine detail form * update machine detail, fix svc user grants * keys table * add key dialog, timestamp creation * check permission on create, delete, fix selection * lint ts, scss * Update console/src/assets/i18n/de.json * Apply suggestions from code review Co-authored-by: Florian Forster <florian@caos.ch> * refactor: protos * fix(management): key expiration date * fix: check if user is human * fix: marshal key details * fix: correct generate login names * fix: logid Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Max Peintner <max@caos.ch> Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com> Co-authored-by: Florian Forster <florian@caos.ch> * fix: naming * refactor: findings * fix: username * fix: mfa upper case * fix: tests * fix: add translations * reactivatemyorg req typeö * fix: projectType for console * fix: user changes * fix: translate events * fix: event type translation * fix: remove unused types Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com> Co-authored-by: Max Peintner <max@caos.ch> Co-authored-by: Florian Forster <florian@caos.ch> Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2278 lines
67 KiB
Go
2278 lines
67 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
)
|
|
|
|
func TestProjectByIDQuery(t *testing.T) {
|
|
type args struct {
|
|
id string
|
|
sequence uint64
|
|
}
|
|
type res struct {
|
|
filterLen int
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project by id query ok",
|
|
args: args{
|
|
id: "AggregateID",
|
|
sequence: 1,
|
|
},
|
|
res: res{
|
|
filterLen: 3,
|
|
},
|
|
},
|
|
{
|
|
name: "project by id query, no id",
|
|
args: args{
|
|
sequence: 1,
|
|
},
|
|
res: res{
|
|
filterLen: 3,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
query, err := ProjectByIDQuery(tt.args.id, tt.args.sequence)
|
|
if !tt.res.wantErr && query == nil {
|
|
t.Errorf("query should not be nil")
|
|
}
|
|
if !tt.res.wantErr && len(query.Filters) != tt.res.filterLen {
|
|
t.Errorf("got wrong filter len: expected: %v, actual: %v ", tt.res.filterLen, len(query.Filters))
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectQuery(t *testing.T) {
|
|
type args struct {
|
|
sequence uint64
|
|
}
|
|
type res struct {
|
|
filterLen int
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project query ok",
|
|
args: args{
|
|
sequence: 1,
|
|
},
|
|
res: res{
|
|
filterLen: 2,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
query := ProjectQuery(tt.args.sequence)
|
|
if query == nil {
|
|
t.Errorf("query should not be nil")
|
|
}
|
|
if len(query.Filters) != tt.res.filterLen {
|
|
t.Errorf("got wrong filter len: expected: %v, actual: %v ", tt.res.filterLen, len(query.Filters))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
aggCreator *models.AggregateCreator
|
|
project *model.Project
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
aggType models.AggregateType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "create aggregate",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
project: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID", Sequence: 1}},
|
|
},
|
|
res: res{
|
|
eventLen: 0,
|
|
aggType: model.ProjectAggregate,
|
|
},
|
|
},
|
|
{
|
|
name: "project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 0,
|
|
aggType: model.ProjectAggregate,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectAggregate(tt.args.ctx, tt.args.aggCreator, tt.args.project)
|
|
|
|
if !tt.res.wantErr && agg == nil {
|
|
t.Errorf("agg should not be nil")
|
|
}
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectCreateAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
new *model.Project
|
|
member *model.ProjectMember
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project update aggregate ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
new: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
member: &model.ProjectMember{UserID: "UserID"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 2,
|
|
eventType: []models.EventType{model.ProjectAdded, model.ProjectMemberAdded},
|
|
},
|
|
},
|
|
{
|
|
name: "new project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
new: nil,
|
|
member: &model.ProjectMember{UserID: "UserID"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "new member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
new: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
member: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectCreateAggregate(tt.args.aggCreator, tt.args.new, tt.args.member)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
|
|
if !tt.res.wantErr {
|
|
for i := range agg.Events {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventType[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectUpdateAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.Project
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project update aggregate ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName_Changed", State: int32(proj_model.ProjectStateActive)},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "new project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectUpdateAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectDeactivateAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project deactivate aggregate ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectDeactivated,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectDeactivated,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectDeactivateAggregate(tt.args.aggCreator, tt.args.existingProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectReactivateAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project reactivate aggregate ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateInactive)},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectReactivated,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectReactivated,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectReactivateAggregate(tt.args.aggCreator, tt.args.existingProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectMemberAddedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
new *model.ProjectMember
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "projectmember added ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
new: &model.ProjectMember{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectMemberAddedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.new)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectMemberChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectMember
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "projectmember changed ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectMember{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectMemberChangedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectMemberRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectMember
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "projectmember removed ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectMember{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberRemoved,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectMemberRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectMemberRemovedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectRoleAddedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject []*model.ProjectRole
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "projectrole added ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: []*model.ProjectRole{{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectRoleAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "projectrole multiple added ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: []*model.ProjectRole{
|
|
{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"},
|
|
{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key2"},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 2,
|
|
eventType: model.ProjectRoleAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectRoleAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectRoleAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectRoleAddedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject...)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectRoleChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectRole
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "projectmember changed ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectRole{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectRoleChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectRoleChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectRoleChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectRoleChangedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectRoleRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectRole
|
|
grants []*model.ProjectGrant
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "projectrole changed ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectRole{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ProjectRoleRemoved},
|
|
},
|
|
},
|
|
{
|
|
name: "projectrole changed with grant",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Grants: []*model.ProjectGrant{{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"ROLE"}}},
|
|
},
|
|
newProject: &model.ProjectRole{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"},
|
|
grants: []*model.ProjectGrant{{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{}}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 2,
|
|
eventTypes: []models.EventType{model.ProjectRoleRemoved, model.ProjectGrantCascadeChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectRoleRemovedAggregate(tt.args.ctx, tt.args.aggCreator, tt.args.existingProject, tt.args.newProject, tt.args.grants)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if agg != nil {
|
|
for i := range agg.Events {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectAppAddedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.Application
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "add oidc application",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "Name",
|
|
OIDCConfig: &model.OIDCConfig{AppID: "AppID", ClientID: "ClientID"},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 2,
|
|
eventTypes: []models.EventType{model.ApplicationAdded, model.OIDCConfigAdded},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ApplicationAddedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectAppChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.Application
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "change app",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Applications: []*model.Application{
|
|
{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
newProject: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "NameChanged",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ApplicationChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ApplicationChangedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectAppRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.Application
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "remove app",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Applications: []*model.Application{
|
|
{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
newProject: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "Name",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ApplicationRemoved},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ApplicationRemovedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectAppDeactivatedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.Application
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "deactivate app",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Applications: []*model.Application{
|
|
{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
newProject: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "Name",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ApplicationDeactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ApplicationDeactivatedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectAppReactivatedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.Application
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "deactivate app",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Applications: []*model.Application{
|
|
{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
newProject: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "Name",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ApplicationReactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ApplicationReactivatedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOIDCConfigchangAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.OIDCConfig
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "deactivate app",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Applications: []*model.Application{
|
|
{AppID: "AppID", Name: "Name", OIDCConfig: &model.OIDCConfig{AppID: "AppID", AuthMethodType: 1}},
|
|
}},
|
|
newProject: &model.OIDCConfig{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppID",
|
|
AuthMethodType: 2,
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.OIDCConfigChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := OIDCConfigChangedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOIDCConfigSecretChangeAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.OIDCConfig
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "change client secret",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Applications: []*model.Application{
|
|
{AppID: "AppID", Name: "Name", OIDCConfig: &model.OIDCConfig{AppID: "AppID", AuthMethodType: 1}},
|
|
}},
|
|
newProject: &model.OIDCConfig{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppID",
|
|
ClientSecret: &crypto.CryptoValue{},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.OIDCConfigSecretChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
newProject: &model.OIDCConfig{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppID",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := OIDCConfigSecretChangedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject.AppID, tt.args.newProject.ClientSecret)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantAddedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrant
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "projectgrant added ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectGrant{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, GrantID: "GrantID", GrantedOrgID: "OrgID"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "grant nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantAddedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrant
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "change project grant",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Grants: []*model.ProjectGrant{
|
|
{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID", RoleKeys: []string{"Key"}},
|
|
}},
|
|
newProject: &model.ProjectGrant{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
GrantID: "GrantID",
|
|
GrantedOrgID: "GrantedOrgID",
|
|
RoleKeys: []string{"KeyChanged"},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ProjectGrantChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "projectgrant nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantChangedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrant
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "remove app",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Grants: []*model.ProjectGrant{
|
|
{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID"},
|
|
}},
|
|
newProject: &model.ProjectGrant{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
GrantID: "GrantID",
|
|
GrantedOrgID: "GrantedOrgID",
|
|
RoleKeys: []string{"KeyChanged"},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ProjectGrantRemoved},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "projectgrant nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantRemovedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantDeactivatedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrant
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "deactivate project grant",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateActive),
|
|
Grants: []*model.ProjectGrant{
|
|
{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID"},
|
|
}},
|
|
newProject: &model.ProjectGrant{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
GrantID: "GrantID",
|
|
GrantedOrgID: "GrantedOrgID",
|
|
RoleKeys: []string{"KeyChanged"},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ProjectGrantDeactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "grant nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantDeactivatedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantReactivatedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrant
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventTypes []models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "reactivate project grant",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.ProjectStateInactive),
|
|
Grants: []*model.ProjectGrant{
|
|
{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID"},
|
|
}},
|
|
newProject: &model.ProjectGrant{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
GrantID: "GrantID",
|
|
GrantedOrgID: "GrantedOrgID",
|
|
RoleKeys: []string{"KeyChanged"},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{model.ProjectGrantReactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "grant nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateInactive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantReactivatedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
for i := 0; i < tt.res.eventLen; i++ {
|
|
if !tt.res.wantErr && agg.Events[i].Type != tt.res.eventTypes[i] {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventTypes[i], agg.Events[i].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[i].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
}
|
|
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantMemberAddedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrantMember
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project grant member added ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectGrantMember{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, GrantID: "GrantID", UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantMemberAddedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantMemberChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrantMember
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project grant member changed ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectGrantMember{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, UserID: "UserID", Roles: []string{"RolesChanged"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantMemberChangedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectGrantMemberRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existingProject *model.Project
|
|
newProject *model.ProjectGrantMember
|
|
aggCreator *models.AggregateCreator
|
|
}
|
|
type res struct {
|
|
eventLen int
|
|
eventType models.EventType
|
|
wantErr bool
|
|
errFunc func(err error) bool
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
res res
|
|
}{
|
|
{
|
|
name: "project grant member removed ok",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: &model.ProjectGrantMember{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberRemoved,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: authz.NewMockContext("orgID", "userID"),
|
|
existingProject: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.ProjectStateActive)},
|
|
newProject: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: model.ProjectGrantMemberRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectGrantMemberRemovedAggregate(tt.args.aggCreator, tt.args.existingProject, tt.args.newProject)(tt.args.ctx)
|
|
|
|
if !tt.res.wantErr && len(agg.Events) != tt.res.eventLen {
|
|
t.Errorf("got wrong event len: expected: %v, actual: %v ", tt.res.eventLen, len(agg.Events))
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Type != tt.res.eventType {
|
|
t.Errorf("got wrong event type: expected: %v, actual: %v ", tt.res.eventType, agg.Events[0].Type.String())
|
|
}
|
|
if !tt.res.wantErr && agg.Events[0].Data == nil {
|
|
t.Errorf("should have data in event")
|
|
}
|
|
if tt.res.wantErr && !tt.res.errFunc(err) {
|
|
t.Errorf("got wrong err: %v ", err)
|
|
}
|
|
})
|
|
}
|
|
}
|