2020-05-11 10:16:29 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-02-23 14:13:04 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/project/model"
|
|
|
|
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mockAppData(app *es_model.Application) []byte {
|
|
|
|
data, _ := json.Marshal(app)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockOIDCConfigData(config *es_model.OIDCConfig) []byte {
|
|
|
|
data, _ := json.Marshal(config)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplicationAppendEvent(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
event *es_models.Event
|
|
|
|
app *ApplicationView
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
result *ApplicationView
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "append added app event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.ApplicationAdded, Data: mockAppData(&es_model.Application{Name: "AppName"})},
|
|
|
|
app: &ApplicationView{},
|
|
|
|
},
|
2020-06-23 12:47:47 +00:00
|
|
|
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append changed app event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.ApplicationChanged, Data: mockAppData(&es_model.Application{Name: "AppNameChanged"})},
|
2020-06-23 12:47:47 +00:00
|
|
|
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2020-06-23 12:47:47 +00:00
|
|
|
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppNameChanged", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append deactivate app event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.ApplicationDeactivated},
|
2020-06-23 12:47:47 +00:00
|
|
|
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2020-06-23 12:47:47 +00:00
|
|
|
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateInactive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append reactivate app event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.ApplicationReactivated},
|
2020-06-23 12:47:47 +00:00
|
|
|
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateInactive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2020-06-23 12:47:47 +00:00
|
|
|
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append added oidc config event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.OIDCConfigAdded, Data: mockOIDCConfigData(&es_model.OIDCConfig{ClientID: "clientID"})},
|
2020-06-23 12:47:47 +00:00
|
|
|
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2020-06-23 12:47:47 +00:00
|
|
|
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", IsOIDC: true, OIDCClientID: "clientID", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append changed oidc config event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: es_model.OIDCConfigAdded, Data: mockOIDCConfigData(&es_model.OIDCConfig{ClientID: "clientIDChanged"})},
|
2020-06-23 12:47:47 +00:00
|
|
|
app: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", OIDCClientID: "clientID", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
2020-06-23 12:47:47 +00:00
|
|
|
result: &ApplicationView{ProjectID: "AggregateID", Name: "AppName", IsOIDC: true, OIDCClientID: "clientIDChanged", State: int32(model.AppStateActive)},
|
2020-05-11 10:16:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.args.app.AppendEvent(tt.args.event)
|
|
|
|
if tt.args.app.ProjectID != tt.result.ProjectID {
|
|
|
|
t.Errorf("got wrong result projectID: expected: %v, actual: %v ", tt.result.ProjectID, tt.args.app.ProjectID)
|
|
|
|
}
|
|
|
|
if tt.args.app.Name != tt.result.Name {
|
|
|
|
t.Errorf("got wrong result name: expected: %v, actual: %v ", tt.result.Name, tt.args.app.Name)
|
|
|
|
}
|
|
|
|
if tt.args.app.State != tt.result.State {
|
|
|
|
t.Errorf("got wrong result state: expected: %v, actual: %v ", tt.result.State, tt.args.app.State)
|
|
|
|
}
|
|
|
|
if tt.args.app.IsOIDC != tt.result.IsOIDC {
|
|
|
|
t.Errorf("got wrong result IsOIDC: expected: %v, actual: %v ", tt.result.IsOIDC, tt.args.app.IsOIDC)
|
|
|
|
}
|
|
|
|
if tt.args.app.OIDCClientID != tt.result.OIDCClientID {
|
|
|
|
t.Errorf("got wrong result OIDCClientID: expected: %v, actual: %v ", tt.result.OIDCClientID, tt.args.app.OIDCClientID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|