2020-05-11 10:16:29 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-03-31 09:36:26 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/project/model"
|
|
|
|
es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
2020-05-11 10:16:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func mockProjectData(project *es_model.Project) []byte {
|
|
|
|
data, _ := json.Marshal(project)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockProjectGrantData(grant *es_model.ProjectGrant) []byte {
|
|
|
|
data, _ := json.Marshal(grant)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func TestProjectGrantAppendEvent(t *testing.T) {
|
2020-05-11 10:16:29 +00:00
|
|
|
type args struct {
|
|
|
|
event *es_models.Event
|
2020-06-15 12:50:39 +00:00
|
|
|
project *ProjectGrantView
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2020-06-15 12:50:39 +00:00
|
|
|
result *ProjectGrantView
|
2020-05-11 10:16:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append added project grant event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantAddedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "GrantedOrgID", RoleKeys: []string{"Role"}})},
|
2020-06-15 12:50:39 +00:00
|
|
|
project: &ProjectGrantView{},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2022-08-31 07:52:43 +00:00
|
|
|
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append change project grant event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantChangedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID", RoleKeys: []string{"RoleChanged"}})},
|
2022-08-31 07:52:43 +00:00
|
|
|
project: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2022-08-31 07:52:43 +00:00
|
|
|
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"RoleChanged"}},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append deactivate project grant event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantDeactivatedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID"})},
|
2022-08-31 07:52:43 +00:00
|
|
|
project: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2022-08-31 07:52:43 +00:00
|
|
|
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateInactive), GrantedRoleKeys: []string{"Role"}},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append reactivate project grant event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.GrantReactivatedType, ResourceOwner: "GrantedOrgID", Data: mockProjectGrantData(&es_model.ProjectGrant{GrantID: "ProjectGrantID"})},
|
2022-08-31 07:52:43 +00:00
|
|
|
project: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateInactive), GrantedRoleKeys: []string{"Role"}},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2022-08-31 07:52:43 +00:00
|
|
|
result: &ProjectGrantView{ProjectID: "AggregateID", ResourceOwner: "GrantedOrgID", OrgID: "GrantedOrgID", State: int32(model.ProjectStateActive), GrantedRoleKeys: []string{"Role"}},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.args.project.AppendEvent(tt.args.event)
|
|
|
|
if tt.args.project.ProjectID != tt.result.ProjectID {
|
|
|
|
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.project.ProjectID)
|
|
|
|
}
|
|
|
|
if tt.args.project.OrgID != tt.result.OrgID {
|
|
|
|
t.Errorf("got wrong result orgID: expected: %v, actual: %v ", tt.result.OrgID, tt.args.project.OrgID)
|
|
|
|
}
|
|
|
|
if tt.args.project.ResourceOwner != tt.result.ResourceOwner {
|
|
|
|
t.Errorf("got wrong result ResourceOwner: expected: %v, actual: %v ", tt.result.ResourceOwner, tt.args.project.ResourceOwner)
|
|
|
|
}
|
|
|
|
if tt.args.project.Name != tt.result.Name {
|
|
|
|
t.Errorf("got wrong result name: expected: %v, actual: %v ", tt.result.Name, tt.args.project.Name)
|
|
|
|
}
|
|
|
|
if tt.args.project.State != tt.result.State {
|
|
|
|
t.Errorf("got wrong result state: expected: %v, actual: %v ", tt.result.State, tt.args.project.State)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tt.args.project.GrantedRoleKeys, tt.result.GrantedRoleKeys) {
|
|
|
|
t.Errorf("got wrong result state: expected: %v, actual: %v ", tt.result.GrantedRoleKeys, tt.args.project.GrantedRoleKeys)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|