2020-05-18 10:06:36 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func now() time.Time {
|
|
|
|
return time.Now().UTC().Round(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendEvent(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
event *es_models.Event
|
|
|
|
userView *UserSessionView
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
result *UserSessionView
|
|
|
|
}{
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append user password check succeeded event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.UserPasswordCheckSucceeded},
|
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: now()},
|
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append human password check succeeded event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.HumanPasswordCheckSucceeded},
|
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: now()},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user password check failed event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.UserPasswordCheckFailed},
|
|
|
|
userView: &UserSessionView{PasswordVerification: now()},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: time.Time{}},
|
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append human password check failed event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.HumanPasswordCheckFailed},
|
|
|
|
userView: &UserSessionView{PasswordVerification: now()},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: time.Time{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user password changed event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.UserPasswordChanged},
|
|
|
|
userView: &UserSessionView{PasswordVerification: now()},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: time.Time{}},
|
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append human password changed event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.HumanPasswordChanged},
|
|
|
|
userView: &UserSessionView{PasswordVerification: now()},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: time.Time{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user otp check succeeded event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.MFAOTPCheckSucceeded},
|
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: now()},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append human otp check succeeded event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
2020-09-15 13:04:02 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.HumanMFAOTPCheckSucceeded},
|
2020-05-18 10:06:36 +00:00
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: now()},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append user otp check failed event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
2020-09-15 13:04:02 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.MFAOTPCheckFailed},
|
2020-11-04 10:26:10 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: now()},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: time.Time{}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append human otp check failed event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
2020-09-15 13:04:02 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.HumanMFAOTPCheckFailed},
|
2020-11-04 10:26:10 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: now()},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: time.Time{}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append user otp removed event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.MFAOTPRemoved},
|
2020-11-04 10:26:10 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: now()},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: time.Time{}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append human otp removed event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.HumanMFAOTPRemoved},
|
2020-11-04 10:26:10 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: now()},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: time.Time{}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user signed out event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.SignedOut},
|
2020-11-04 10:26:10 +00:00
|
|
|
userView: &UserSessionView{PasswordVerification: now(), SecondFactorVerification: now()},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: time.Time{}, SecondFactorVerification: time.Time{}, State: 1},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2020-09-15 13:04:02 +00:00
|
|
|
{
|
|
|
|
name: "append human signed out event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{CreationDate: now(), Type: es_model.HumanSignedOut},
|
2020-11-04 10:26:10 +00:00
|
|
|
userView: &UserSessionView{PasswordVerification: now(), SecondFactorVerification: now()},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2020-11-04 10:26:10 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: time.Time{}, SecondFactorVerification: time.Time{}, State: 1},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.args.userView.AppendEvent(tt.args.event)
|
|
|
|
assert.Equal(t, tt.result, tt.args.userView)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|