2020-05-18 11:32:16 +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-05-18 11:32:16 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAppendAddMemberEvent(t *testing.T) {
|
|
|
|
type args struct {
|
2020-08-26 09:56:23 +02:00
|
|
|
iam *IAM
|
|
|
|
member *IAMMember
|
2020-05-18 11:32:16 +02:00
|
|
|
event *es_models.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2020-08-26 09:56:23 +02:00
|
|
|
result *IAM
|
2020-05-18 11:32:16 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append add member event",
|
|
|
|
args: args{
|
2020-08-26 09:56:23 +02:00
|
|
|
iam: &IAM{},
|
|
|
|
member: &IAMMember{UserID: "UserID", Roles: []string{"Role"}},
|
2020-05-18 11:32:16 +02:00
|
|
|
event: &es_models.Event{},
|
|
|
|
},
|
2020-08-26 09:56:23 +02:00
|
|
|
result: &IAM{Members: []*IAMMember{&IAMMember{UserID: "UserID", Roles: []string{"Role"}}}},
|
2020-05-18 11:32:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.member != nil {
|
|
|
|
data, _ := json.Marshal(tt.args.member)
|
|
|
|
tt.args.event.Data = data
|
|
|
|
}
|
|
|
|
tt.args.iam.appendAddMemberEvent(tt.args.event)
|
|
|
|
if len(tt.args.iam.Members) != 1 {
|
|
|
|
t.Errorf("got wrong result should have one member actual: %v ", len(tt.args.iam.Members))
|
|
|
|
}
|
|
|
|
if tt.args.iam.Members[0] == tt.result.Members[0] {
|
|
|
|
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Members[0], tt.args.iam.Members[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendChangeMemberEvent(t *testing.T) {
|
|
|
|
type args struct {
|
2020-08-26 09:56:23 +02:00
|
|
|
iam *IAM
|
|
|
|
member *IAMMember
|
2020-05-18 11:32:16 +02:00
|
|
|
event *es_models.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2020-08-26 09:56:23 +02:00
|
|
|
result *IAM
|
2020-05-18 11:32:16 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append change member event",
|
|
|
|
args: args{
|
2020-08-26 09:56:23 +02:00
|
|
|
iam: &IAM{Members: []*IAMMember{&IAMMember{UserID: "UserID", Roles: []string{"Role"}}}},
|
|
|
|
member: &IAMMember{UserID: "UserID", Roles: []string{"ChangedRole"}},
|
2020-05-18 11:32:16 +02:00
|
|
|
event: &es_models.Event{},
|
|
|
|
},
|
2020-08-26 09:56:23 +02:00
|
|
|
result: &IAM{Members: []*IAMMember{&IAMMember{UserID: "UserID", Roles: []string{"ChangedRole"}}}},
|
2020-05-18 11:32:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.member != nil {
|
|
|
|
data, _ := json.Marshal(tt.args.member)
|
|
|
|
tt.args.event.Data = data
|
|
|
|
}
|
|
|
|
tt.args.iam.appendChangeMemberEvent(tt.args.event)
|
|
|
|
if len(tt.args.iam.Members) != 1 {
|
|
|
|
t.Errorf("got wrong result should have one member actual: %v ", len(tt.args.iam.Members))
|
|
|
|
}
|
|
|
|
if tt.args.iam.Members[0] == tt.result.Members[0] {
|
|
|
|
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.Members[0], tt.args.iam.Members[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendRemoveMemberEvent(t *testing.T) {
|
|
|
|
type args struct {
|
2020-08-26 09:56:23 +02:00
|
|
|
iam *IAM
|
|
|
|
member *IAMMember
|
2020-05-18 11:32:16 +02:00
|
|
|
event *es_models.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2020-08-26 09:56:23 +02:00
|
|
|
result *IAM
|
2020-05-18 11:32:16 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append remove member event",
|
|
|
|
args: args{
|
2020-08-26 09:56:23 +02:00
|
|
|
iam: &IAM{Members: []*IAMMember{&IAMMember{UserID: "UserID", Roles: []string{"Role"}}}},
|
|
|
|
member: &IAMMember{UserID: "UserID"},
|
2020-05-18 11:32:16 +02:00
|
|
|
event: &es_models.Event{},
|
|
|
|
},
|
2020-08-26 09:56:23 +02:00
|
|
|
result: &IAM{Members: []*IAMMember{}},
|
2020-05-18 11:32:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.member != nil {
|
|
|
|
data, _ := json.Marshal(tt.args.member)
|
|
|
|
tt.args.event.Data = data
|
|
|
|
}
|
|
|
|
tt.args.iam.appendRemoveMemberEvent(tt.args.event)
|
|
|
|
if len(tt.args.iam.Members) != 0 {
|
|
|
|
t.Errorf("got wrong result should have no member actual: %v ", len(tt.args.iam.Members))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|