mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-20 06:47:31 +00:00
170 lines
5.2 KiB
Go
170 lines
5.2 KiB
Go
|
package model
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||
|
"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{},
|
||
|
role: &ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}},
|
||
|
event: &es_models.Event{},
|
||
|
},
|
||
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
||
|
},
|
||
|
}
|
||
|
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{
|
||
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
||
|
grant: &ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"KeyChanged"}},
|
||
|
event: &es_models.Event{},
|
||
|
},
|
||
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"KeyChanged"}}}},
|
||
|
},
|
||
|
}
|
||
|
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{
|
||
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
||
|
grant: &ProjectGrant{GrantID: "GrantID"},
|
||
|
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{
|
||
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
||
|
grant: &ProjectGrantID{GrantID: "GrantID"},
|
||
|
event: &es_models.Event{},
|
||
|
state: model.PROJECTGRANTSTATE_INACTIVE,
|
||
|
},
|
||
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}, State: int32(model.PROJECTGRANTSTATE_INACTIVE)}}},
|
||
|
},
|
||
|
{
|
||
|
name: "append reactivate grant event",
|
||
|
args: args{
|
||
|
project: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}}}},
|
||
|
grant: &ProjectGrantID{GrantID: "GrantID"},
|
||
|
event: &es_models.Event{},
|
||
|
state: model.PROJECTGRANTSTATE_ACTIVE,
|
||
|
},
|
||
|
result: &Project{Grants: []*ProjectGrant{&ProjectGrant{GrantID: "GrantID", GrantedOrgID: "OrgID", RoleKeys: []string{"Key"}, State: int32(model.PROJECTGRANTSTATE_ACTIVE)}}},
|
||
|
},
|
||
|
}
|
||
|
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])
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|