mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
8464cfa4fe
* feat: eventstore repository * fix: remove gorm * version * feat: pkg * feat: add some files for project * feat: eventstore without eventstore-lib * rename files * gnueg * fix: key json * fix: add object * fix: change imports * fix: internal models * fix: some imports * fix: global model * feat: add global view functions * fix: add some functions on repo * feat(eventstore): sdk * fix(eventstore): search query * fix(eventstore): rename app to eventstore * delete empty test * remove unused func * merge master * fix(eventstore): tests * fix(models): delete unused struct * fix: some funcitons * feat(eventstore): implemented push events * fix: move project eventstore to project package * fix: change project eventstore funcs * feat(eventstore): overwrite context data * fix: change project eventstore * fix: add project repo to mgmt server * feat(types): SQL-config * fix: commented code * feat(eventstore): options to overwrite editor * feat: auth interceptor and cockroach migrations * fix: migrations * fix: fix filter * fix: not found on getbyid * fix: use global sql config * fix: add sequence * fix: add some tests * fix(eventstore): nullable sequence * fix: add some tests * merge * fix: add some tests * fix(migrations): correct statements for sequence * fix: add some tests * fix: add some tests * fix: changes from mr * fix: changes from mr * fix: add some tests * Update internal/eventstore/models/field.go Co-Authored-By: livio-a <livio.a@gmail.com> * fix(eventstore): code quality * fix: add types to aggregate/Event-types * fix: try tests * fix(eventstore): rename modifier* to editor* * fix(eventstore): delete editor_org * fix(migrations): remove editor_org field, rename modifier_* to editor_* * fix: query tests * fix: use prepare funcs * fix: go mod * fix: generate files * fix(eventstore): tests * fix(eventstore): rename modifier to editor * fix(migrations): add cluster migration, fix(migrations): fix typo of host in clean clsuter * fix(eventstore): move health * fix(eventstore): AggregateTypeFilter aggregateType as param * code quality * fix: go tests * feat: add member funcs * feat: add member model * feat: add member events * feat: add member repo model * fix: better error func testing * fix: project member funcs * fix: add tests * fix: add tests * feat: implement member requests * fix: merge master * fix: merge master * fix: read existing in project repo * fix: fix tests * feat: add internal cache * feat: add cache mock * fix: return values of cache mock * feat: add project role * fix: add cache config * fix: add role to eventstore * fix: use eventstore sdk * fix: use eventstore sdk * fix: add project role grpc requests * fix: fix getby id * fix: changes for mr * fix: change value to interface * feat: add app event creations * fix: searchmethods * Update internal/project/model/project_member.go Co-Authored-By: Silvan <silvan.reusser@gmail.com> * fix: use get project func * fix: append events * fix: check if value is string on equal ignore case * fix: add changes test * fix: add go mod * fix: add some tests * fix: return err not nil * fix: return err not nil * fix: add aggregate funcs and tests * fix: add oidc aggregate funcs and tests * fix: add oidc * fix: add some tests * fix: tests * fix: oidc validation * fix: generate client secret * fix: generate client id * fix: test change app * fix: deactivate/reactivate application * fix: change oidc config * fix: change oidc config secret * 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 * fix: problems after merger * fix: new commit * fix: remove enum converter * Update internal/project/model/project_grant.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/project/model/project_grant.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * Update internal/project/model/project.go Co-Authored-By: Livio Amstutz <livio.a@gmail.com> * fix: better sub object handling * fix: imports Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: livio-a <livio.a@gmail.com>
2221 lines
63 KiB
Go
2221 lines
63 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"context"
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
"testing"
|
|
|
|
"github.com/caos/zitadel/internal/api/auth"
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
proj_model "github.com/caos/zitadel/internal/project/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: auth.NewMockContext("orgID", "userID"),
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
project: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID", Sequence: 1}},
|
|
},
|
|
res: res{
|
|
eventLen: 0,
|
|
aggType: proj_model.ProjectAggregate,
|
|
},
|
|
},
|
|
{
|
|
name: "project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 0,
|
|
aggType: proj_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
|
|
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: auth.NewMockContext("orgID", "userID"),
|
|
new: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "new project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectAdded,
|
|
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.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 TestProjectUpdateAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName_Changed", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "new project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectDeactivateAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectDeactivated,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing)(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
|
|
existing *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_INACTIVE)},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectReactivated,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing)(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
|
|
existing *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectMember{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectMemberAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectMemberAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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
|
|
existing *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 changed ok",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectMember{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectMemberChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectMemberChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectMemberRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *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 removed ok",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectMember{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectMemberRemoved,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectMemberRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectRoleAddedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectRole{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectRoleAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectRoleAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectRoleChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectRole{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectRoleChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectRoleChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectRoleRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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 changed ok",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectRole{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Key: "Key"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectRoleRemoved,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectRoleRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectRoleRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
agg, err := ProjectRoleRemovedAggregate(tt.args.aggCreator, tt.args.existing, 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 TestProjectAppAddedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &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{proj_model.ApplicationAdded, proj_model.OIDCConfigAdded},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Applications: []*model.Application{
|
|
&model.Application{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
new: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "NameChanged",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{proj_model.ApplicationChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Applications: []*model.Application{
|
|
&model.Application{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
new: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "Name",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{proj_model.ApplicationRemoved},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Applications: []*model.Application{
|
|
&model.Application{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
new: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "Name",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{proj_model.ApplicationDeactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Applications: []*model.Application{
|
|
&model.Application{AppID: "AppID", Name: "Name"},
|
|
}},
|
|
new: &model.Application{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppId",
|
|
Name: "Name",
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{proj_model.ApplicationReactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Applications: []*model.Application{
|
|
&model.Application{AppID: "AppID", Name: "Name", OIDCConfig: &model.OIDCConfig{AppID: "AppID", AuthMethodType: 1}},
|
|
}},
|
|
new: &model.OIDCConfig{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppID",
|
|
AuthMethodType: 2,
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{proj_model.OIDCConfigChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "app nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Applications: []*model.Application{
|
|
&model.Application{AppID: "AppID", Name: "Name", OIDCConfig: &model.OIDCConfig{AppID: "AppID", AuthMethodType: 1}},
|
|
}},
|
|
new: &model.OIDCConfig{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "AggregateID"},
|
|
AppID: "AppID",
|
|
ClientSecret: &crypto.CryptoValue{},
|
|
},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventTypes: []models.EventType{proj_model.OIDCConfigSecretChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
new: &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.existing, tt.args.new.AppID, tt.args.new.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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectGrant{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, GrantID: "GrantID", GrantedOrgID: "OrgID"},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "grant nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectGrantChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Grants: []*model.ProjectGrant{
|
|
&model.ProjectGrant{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID", RoleKeys: []string{"Key"}},
|
|
}},
|
|
new: &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{proj_model.ProjectGrantChanged},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "projectgrant nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Grants: []*model.ProjectGrant{
|
|
&model.ProjectGrant{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID"},
|
|
}},
|
|
new: &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{proj_model.ProjectGrantRemoved},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "projectgrant nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_ACTIVE),
|
|
Grants: []*model.ProjectGrant{
|
|
&model.ProjectGrant{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID"},
|
|
}},
|
|
new: &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{proj_model.ProjectGrantDeactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "grant nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{
|
|
ObjectRoot: models.ObjectRoot{AggregateID: "ID"},
|
|
Name: "ProjectName",
|
|
State: int32(proj_model.PROJECTSTATE_INACTIVE),
|
|
Grants: []*model.ProjectGrant{
|
|
&model.ProjectGrant{GrantID: "GrantID", GrantedOrgID: "GrantedOrgID"},
|
|
}},
|
|
new: &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{proj_model.ProjectGrantReactivated},
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "grant nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_INACTIVE)},
|
|
new: 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.existing, 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))
|
|
}
|
|
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
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectGrantMember{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, GrantID: "GrantID", UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantMemberAdded,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantMemberAdded,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectGrantMemberChangedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectGrantMember{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, UserID: "UserID", Roles: []string{"RolesChanged"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantMemberChanged,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantMemberChanged,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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 TestProjectGrantMemberRemovedAggregate(t *testing.T) {
|
|
type args struct {
|
|
ctx context.Context
|
|
existing *model.Project
|
|
new *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: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: &model.ProjectGrantMember{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, UserID: "UserID", Roles: []string{"Roles"}},
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantMemberRemoved,
|
|
},
|
|
},
|
|
{
|
|
name: "existing project nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_model.ProjectGrantMemberRemoved,
|
|
wantErr: true,
|
|
errFunc: caos_errs.IsPreconditionFailed,
|
|
},
|
|
},
|
|
{
|
|
name: "member nil",
|
|
args: args{
|
|
ctx: auth.NewMockContext("orgID", "userID"),
|
|
existing: &model.Project{ObjectRoot: models.ObjectRoot{AggregateID: "ID"}, Name: "ProjectName", State: int32(proj_model.PROJECTSTATE_ACTIVE)},
|
|
new: nil,
|
|
aggCreator: models.NewAggregateCreator("Test"),
|
|
},
|
|
res: res{
|
|
eventLen: 1,
|
|
eventType: proj_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.existing, 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)
|
|
}
|
|
})
|
|
}
|
|
}
|