2020-05-18 10:06:36 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2024-05-22 15:26:02 +00:00
|
|
|
"database/sql"
|
2020-12-07 11:09:10 +00:00
|
|
|
"encoding/json"
|
2020-05-18 10:06:36 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2024-05-28 08:59:49 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
es_model "github.com/zitadel/zitadel/internal/user/repository/eventsourcing/model"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.UserV1PasswordCheckSucceededType},
|
2020-05-18 10:06:36 +00:00
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append human password check succeeded event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.HumanPasswordCheckSucceededType},
|
2020-09-15 13:04:02 +00:00
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user password check failed event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.UserV1PasswordCheckFailedType},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append human password check failed event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.HumanPasswordCheckFailedType},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), PasswordVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user password changed event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
2020-12-07 11:09:10 +00:00
|
|
|
event: &es_models.Event{
|
|
|
|
CreationDate: now(),
|
2023-10-19 10:19:10 +00:00
|
|
|
Typ: user.UserV1PasswordChangedType,
|
2020-12-07 11:09:10 +00:00
|
|
|
Data: func() []byte {
|
|
|
|
d, _ := json.Marshal(&es_model.Password{
|
|
|
|
Secret: &crypto.CryptoValue{Crypted: []byte("test")},
|
|
|
|
})
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{UserAgentID: "id", PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{UserAgentID: "id", ChangeDate: now(), PasswordVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append human password changed event",
|
|
|
|
args: args{
|
2020-12-07 11:09:10 +00:00
|
|
|
event: &es_models.Event{
|
|
|
|
CreationDate: now(),
|
2023-10-19 10:19:10 +00:00
|
|
|
Typ: user.HumanPasswordChangedType,
|
2020-12-07 11:09:10 +00:00
|
|
|
Data: func() []byte {
|
|
|
|
d, _ := json.Marshal(&es_model.PasswordChange{
|
|
|
|
Password: es_model.Password{
|
|
|
|
Secret: &crypto.CryptoValue{Crypted: []byte("test")},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{UserAgentID: "id", PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{UserAgentID: "id", ChangeDate: now(), PasswordVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
2020-12-07 11:09:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append human password changed event same user agent",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{
|
|
|
|
CreationDate: now(),
|
2023-10-19 10:19:10 +00:00
|
|
|
Typ: user.HumanPasswordChangedType,
|
2020-12-07 11:09:10 +00:00
|
|
|
Data: func() []byte {
|
|
|
|
d, _ := json.Marshal(&es_model.PasswordChange{
|
|
|
|
Password: es_model.Password{
|
|
|
|
Secret: &crypto.CryptoValue{Crypted: []byte("test")},
|
|
|
|
},
|
|
|
|
UserAgentID: "id",
|
|
|
|
})
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{UserAgentID: "id", PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-12-07 11:09:10 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{UserAgentID: "id", ChangeDate: now(), PasswordVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-12-07 11:09:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user otp verified event",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{
|
|
|
|
CreationDate: now(),
|
2023-10-19 10:19:10 +00:00
|
|
|
Typ: user.HumanMFAOTPVerifiedType,
|
2020-12-07 11:09:10 +00:00
|
|
|
Data: nil,
|
|
|
|
},
|
|
|
|
userView: &UserSessionView{UserAgentID: "id"},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{UserAgentID: "id", ChangeDate: now()},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user otp verified event same user agent",
|
|
|
|
args: args{
|
|
|
|
event: &es_models.Event{
|
|
|
|
CreationDate: now(),
|
2023-10-19 10:19:10 +00:00
|
|
|
Typ: user.HumanMFAOTPVerifiedType,
|
2020-12-07 11:09:10 +00:00
|
|
|
Data: func() []byte {
|
|
|
|
d, _ := json.Marshal(&es_model.OTPVerified{
|
|
|
|
UserAgentID: "id",
|
|
|
|
})
|
|
|
|
return d
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
userView: &UserSessionView{UserAgentID: "id"},
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{UserAgentID: "id", ChangeDate: now(), SecondFactorVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user otp check succeeded event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.UserV1MFAOTPCheckSucceededType},
|
2020-09-15 13:04:02 +00:00
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: sql.NullTime{Time: now(), Valid: true}},
|
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{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.HumanMFAOTPCheckSucceededType},
|
2020-05-18 10:06:36 +00:00
|
|
|
userView: &UserSessionView{},
|
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: sql.NullTime{Time: now(), Valid: true}},
|
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{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.UserV1MFAOTPCheckFailedType},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
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{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.HumanMFAOTPCheckFailedType},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
2020-09-15 13:04:02 +00:00
|
|
|
name: "append user otp removed event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.UserV1MFAOTPRemovedType},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append human otp removed event",
|
|
|
|
args: args{
|
2023-10-19 10:19:10 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.HumanMFAOTPRemovedType},
|
2024-05-22 15:26:02 +00:00
|
|
|
userView: &UserSessionView{SecondFactorVerification: sql.NullTime{Time: now(), Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
2024-05-22 15:26:02 +00:00
|
|
|
result: &UserSessionView{ChangeDate: now(), SecondFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true}},
|
2020-09-15 13:04:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "append user signed out event",
|
2020-05-18 10:06:36 +00:00
|
|
|
args: args{
|
2024-05-22 15:26:02 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.UserV1SignedOutType},
|
|
|
|
userView: &UserSessionView{
|
|
|
|
PasswordVerification: sql.NullTime{Time: now(), Valid: true},
|
|
|
|
SecondFactorVerification: sql.NullTime{Time: now(), Valid: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{
|
|
|
|
ChangeDate: now(),
|
|
|
|
PasswordVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
SecondFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
ExternalLoginVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
PasswordlessVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
MultiFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
2024-05-28 08:59:49 +00:00
|
|
|
State: sql.Null[domain.UserSessionState]{V: domain.UserSessionStateTerminated},
|
2020-05-18 10:06:36 +00:00
|
|
|
},
|
|
|
|
},
|
2020-09-15 13:04:02 +00:00
|
|
|
{
|
|
|
|
name: "append human signed out event",
|
|
|
|
args: args{
|
2024-05-22 15:26:02 +00:00
|
|
|
event: &es_models.Event{CreationDate: now(), Typ: user.HumanSignedOutType},
|
|
|
|
userView: &UserSessionView{
|
|
|
|
PasswordVerification: sql.NullTime{Time: now(), Valid: true},
|
|
|
|
SecondFactorVerification: sql.NullTime{Time: now(), Valid: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
result: &UserSessionView{
|
|
|
|
ChangeDate: now(),
|
|
|
|
PasswordVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
SecondFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
ExternalLoginVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
PasswordlessVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
|
|
|
MultiFactorVerification: sql.NullTime{Time: time.Time{}, Valid: true},
|
2024-05-28 08:59:49 +00:00
|
|
|
State: sql.Null[domain.UserSessionState]{V: domain.UserSessionStateTerminated},
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|