2020-04-23 07:54:40 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-02-23 15:13:04 +01:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-04-23 07:54:40 +02:00
|
|
|
"github.com/caos/zitadel/internal/project/model"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAppendAddGrantEvent(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
project *Project
|
|
|
|
role *ProjectGrant
|
|
|
|
event *es_models.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
result *Project
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append add grant event",
|
|
|
|
args: args{
|
|
|
|
project: &Project{},
|
2021-01-22 13:31:52 +01:00
|
|
|
role: &ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}},
|
2020-04-23 07:54:40 +02:00
|
|
|
event: &es_models.Event{},
|
|
|
|
},
|
2021-01-22 13:31:52 +01:00
|
|
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
2020-04-23 07:54:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.role != nil {
|
|
|
|
data, _ := json.Marshal(tt.args.role)
|
|
|
|
tt.args.event.Data = data
|
|
|
|
}
|
|
|
|
tt.args.project.appendAddGrantEvent(tt.args.event)
|
|
|
|
if len(tt.args.project.Grants) != 1 {
|
|
|
|
t.Errorf("got wrong result should have one grant actual: %v ", len(tt.args.project.Grants))
|
|
|
|
}
|
|
|
|
if tt.args.project.Grants[0] == tt.result.Grants[0] {
|
|
|
|
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Grants[0], tt.args.project.Grants[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendChangeGrantEvent(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
project *Project
|
|
|
|
grant *ProjectGrant
|
|
|
|
event *es_models.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
result *Project
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append change grant event",
|
|
|
|
args: args{
|
2021-01-22 13:31:52 +01:00
|
|
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
|
|
|
grant: &ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"KeyChanged"}},
|
2020-04-23 07:54:40 +02:00
|
|
|
event: &es_models.Event{},
|
|
|
|
},
|
2021-01-22 13:31:52 +01:00
|
|
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"KeyChanged"}}}},
|
2020-04-23 07:54:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.grant != nil {
|
|
|
|
data, _ := json.Marshal(tt.args.grant)
|
|
|
|
tt.args.event.Data = data
|
|
|
|
}
|
|
|
|
tt.args.project.appendChangeGrantEvent(tt.args.event)
|
|
|
|
if len(tt.args.project.Grants) != 1 {
|
|
|
|
t.Errorf("got wrong result should have one grant actual: %v ", len(tt.args.project.Grants))
|
|
|
|
}
|
|
|
|
if tt.args.project.Grants[0] == tt.result.Grants[0] {
|
|
|
|
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Grants[0], tt.args.project.Grants[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendRemoveGrantEvent(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
project *Project
|
|
|
|
grant *ProjectGrant
|
|
|
|
event *es_models.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
result *Project
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append remove role event",
|
|
|
|
args: args{
|
2021-01-22 13:31:52 +01:00
|
|
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
|
|
|
grant: &ProjectGrant{GrantID: "ProjectGrantID"},
|
2020-04-23 07:54:40 +02:00
|
|
|
event: &es_models.Event{},
|
|
|
|
},
|
|
|
|
result: &Project{Grants: []*ProjectGrant{}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.grant != nil {
|
|
|
|
data, _ := json.Marshal(tt.args.grant)
|
|
|
|
tt.args.event.Data = data
|
|
|
|
}
|
|
|
|
tt.args.project.appendRemoveGrantEvent(tt.args.event)
|
|
|
|
if len(tt.args.project.Grants) != 0 {
|
|
|
|
t.Errorf("got wrong result should have no grant actual: %v ", len(tt.args.project.Grants))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendGrantStateEvent(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
project *Project
|
|
|
|
grant *ProjectGrantID
|
|
|
|
event *es_models.Event
|
|
|
|
state model.ProjectGrantState
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
result *Project
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append deactivate grant event",
|
|
|
|
args: args{
|
2021-01-22 13:31:52 +01:00
|
|
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
|
|
|
grant: &ProjectGrantID{GrantID: "ProjectGrantID"},
|
2020-04-23 07:54:40 +02:00
|
|
|
event: &es_models.Event{},
|
2020-06-23 14:47:47 +02:00
|
|
|
state: model.ProjectGrantStateInactive,
|
2020-04-23 07:54:40 +02:00
|
|
|
},
|
2021-01-22 13:31:52 +01:00
|
|
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}, State: int32(model.ProjectGrantStateInactive)}}},
|
2020-04-23 07:54:40 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append reactivate grant event",
|
|
|
|
args: args{
|
2021-01-22 13:31:52 +01:00
|
|
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
|
|
|
grant: &ProjectGrantID{GrantID: "ProjectGrantID"},
|
2020-04-23 07:54:40 +02:00
|
|
|
event: &es_models.Event{},
|
2020-06-23 14:47:47 +02:00
|
|
|
state: model.ProjectGrantStateActive,
|
2020-04-23 07:54:40 +02:00
|
|
|
},
|
2021-01-22 13:31:52 +01:00
|
|
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "ProjectGrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}, State: int32(model.ProjectGrantStateActive)}}},
|
2020-04-23 07:54:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.grant != nil {
|
|
|
|
data, _ := json.Marshal(tt.args.grant)
|
|
|
|
tt.args.event.Data = data
|
|
|
|
}
|
|
|
|
tt.args.project.appendGrantStateEvent(tt.args.event, tt.args.state)
|
|
|
|
if len(tt.args.project.Grants) != 1 {
|
|
|
|
t.Errorf("got wrong result should have one grant actual: %v ", len(tt.args.project.Grants))
|
|
|
|
}
|
|
|
|
if tt.args.project.Grants[0] == tt.result.Grants[0] {
|
|
|
|
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Grants[0], tt.args.project.Grants[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|