2021-03-19 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-20 15:34:06 +00:00
|
|
|
"errors"
|
2023-07-14 06:49:57 +00:00
|
|
|
"io"
|
2021-03-19 10:12:56 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-07-14 06:49:57 +00:00
|
|
|
"github.com/zitadel/passwap"
|
2023-11-22 10:56:43 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2021-03-19 10:12:56 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-03-19 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommandSide_SetOneTimePassword(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2024-04-05 09:35:49 +00:00
|
|
|
userPasswordHasher *crypto.Hasher
|
2023-07-14 06:49:57 +00:00
|
|
|
checkPermission domain.PermissionCheck
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
password string
|
2021-06-03 11:28:24 +00:00
|
|
|
oneTime bool
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
2023-06-20 15:34:06 +00:00
|
|
|
{
|
|
|
|
name: "missing permission, error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-06-20 15:34:06 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
oneTime: true,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: func(err error) bool {
|
2023-12-08 14:30:55 +00:00
|
|
|
return errors.Is(err, zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"))
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
{
|
2021-06-03 11:28:24 +00:00
|
|
|
name: "change password onetime, ok",
|
2021-03-19 10:12:56 +00:00
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
true,
|
|
|
|
"",
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
2021-06-03 11:28:24 +00:00
|
|
|
oneTime: true,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change password no one time, ok",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-06-03 11:28:24 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
),
|
2021-06-03 11:28:24 +00:00
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
2021-06-03 11:28:24 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
oneTime: false,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: tt.fields.userPasswordHasher,
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2021-06-03 11:28:24 +00:00
|
|
|
got, err := r.SetPassword(tt.args.ctx, tt.args.resourceOwner, tt.args.userID, tt.args.password, tt.args.oneTime)
|
2021-03-19 10:12:56 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:34:06 +00:00
|
|
|
func TestCommandSide_SetPasswordWithVerifyCode(t *testing.T) {
|
2021-03-19 10:12:56 +00:00
|
|
|
type fields struct {
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2023-07-14 06:49:57 +00:00
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
2024-04-05 09:35:49 +00:00
|
|
|
userPasswordHasher *crypto.Hasher
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2024-05-02 09:50:13 +00:00
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
code string
|
|
|
|
resourceOwner string
|
|
|
|
password string
|
|
|
|
userAgentID string
|
|
|
|
changeRequired bool
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
code: "aa",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "string",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid code, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
domain.NotificationTypeEmail,
|
2024-04-24 15:50:58 +00:00
|
|
|
"",
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-06-20 15:34:06 +00:00
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2023-06-20 15:34:06 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
code: "test",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "set password, ok",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
domain.NotificationTypeEmail,
|
2024-04-24 15:50:58 +00:00
|
|
|
"",
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2023-06-20 15:34:06 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
code: "a",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-30 14:36:34 +00:00
|
|
|
{
|
|
|
|
name: "set password with userAgentID, ok",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2024-01-30 14:36:34 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
domain.NotificationTypeEmail,
|
2024-04-24 15:50:58 +00:00
|
|
|
"",
|
2024-01-30 14:36:34 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"userAgent1",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
code: "a",
|
|
|
|
userAgentID: "userAgent1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-02 09:50:13 +00:00
|
|
|
{
|
|
|
|
name: "set password with changeRequired, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPasswordCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
domain.NotificationTypeEmail,
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
true,
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
code: "a",
|
|
|
|
userAgentID: "",
|
|
|
|
changeRequired: true,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: tt.fields.userPasswordHasher,
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2024-05-02 09:50:13 +00:00
|
|
|
got, err := r.SetPasswordWithVerifyCode(tt.args.ctx, tt.args.resourceOwner, tt.args.userID, tt.args.code, tt.args.password, tt.args.userAgentID, tt.args.changeRequired)
|
2021-03-19 10:12:56 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
2023-06-20 15:34:06 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_ChangePassword(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-04-05 09:35:49 +00:00
|
|
|
userPasswordHasher *crypto.Hasher
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2024-05-02 09:50:13 +00:00
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
oldPassword string
|
|
|
|
newPassword string
|
|
|
|
userAgentID string
|
|
|
|
changeRequired bool
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
2023-07-14 06:49:57 +00:00
|
|
|
expect []expect
|
2021-03-19 10:12:56 +00:00
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
2023-07-14 06:49:57 +00:00
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{},
|
2021-03-19 10:12:56 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
oldPassword: "password",
|
|
|
|
newPassword: "password1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
expect: []expect{},
|
2021-03-19 10:12:56 +00:00
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-07-14 06:49:57 +00:00
|
|
|
name: "old password missing, invalid argument error",
|
|
|
|
fields: fields{},
|
2021-03-19 10:12:56 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
newPassword: "password1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
expect: []expect{},
|
2021-03-19 10:12:56 +00:00
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-07-14 06:49:57 +00:00
|
|
|
name: "new password missing, invalid argument error",
|
|
|
|
fields: fields{},
|
2021-03-19 10:12:56 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
oldPassword: "password",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
expect: []expect{},
|
2021-03-19 10:12:56 +00:00
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-07-14 06:49:57 +00:00
|
|
|
name: "user not existing, precondition error",
|
|
|
|
fields: fields{},
|
2021-03-19 10:12:56 +00:00
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
oldPassword: "password",
|
|
|
|
newPassword: "password1",
|
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
expect: []expect{
|
|
|
|
expectFilter(),
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "existing password empty, precondition error",
|
|
|
|
fields: fields{
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
oldPassword: "password",
|
|
|
|
newPassword: "password1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
expect: []expect{
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-02 09:50:13 +00:00
|
|
|
{
|
|
|
|
name: "password not matching complexity policy, invalid argument error",
|
|
|
|
fields: fields{
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
oldPassword: "password-old",
|
|
|
|
newPassword: "password1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
expect: []expect{
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password-old",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
1,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
{
|
2023-07-14 06:49:57 +00:00
|
|
|
name: "password not matching, invalid argument error",
|
2021-03-19 10:12:56 +00:00
|
|
|
fields: fields{
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
oldPassword: "password-old",
|
|
|
|
newPassword: "password1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
expect: []expect{
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change password, ok",
|
|
|
|
fields: fields{
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
oldPassword: "password",
|
|
|
|
newPassword: "password1",
|
|
|
|
},
|
|
|
|
expect: []expect{
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
2024-05-02 09:50:13 +00:00
|
|
|
&org.NewAggregate("org1").Aggregate,
|
2023-07-14 06:49:57 +00:00
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password1",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-30 14:36:34 +00:00
|
|
|
{
|
|
|
|
name: "change password with userAgentID, ok",
|
|
|
|
fields: fields{
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
oldPassword: "password",
|
|
|
|
newPassword: "password1",
|
|
|
|
userAgentID: "userAgent1",
|
|
|
|
},
|
|
|
|
expect: []expect{
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password1",
|
|
|
|
false,
|
|
|
|
"userAgent1",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-02 09:50:13 +00:00
|
|
|
{
|
|
|
|
name: "change password with changeRequired, ok",
|
|
|
|
fields: fields{
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
oldPassword: "password",
|
|
|
|
newPassword: "password1",
|
|
|
|
userAgentID: "",
|
|
|
|
changeRequired: true,
|
|
|
|
},
|
|
|
|
expect: []expect{
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewPasswordComplexityPolicyAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password1",
|
|
|
|
true,
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2023-07-14 06:49:57 +00:00
|
|
|
eventstore: eventstoreExpect(t, tt.expect...),
|
|
|
|
userPasswordHasher: tt.fields.userPasswordHasher,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2024-05-02 09:50:13 +00:00
|
|
|
got, err := r.ChangePassword(tt.args.ctx, tt.args.resourceOwner, tt.args.userID, tt.args.oldPassword, tt.args.newPassword, tt.args.userAgentID, tt.args.changeRequired)
|
2021-03-19 10:12:56 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_RequestSetPassword(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2024-07-17 04:43:07 +00:00
|
|
|
newCode encrypedCodeFunc
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2024-07-17 04:43:07 +00:00
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
notifyType domain.NotificationType
|
|
|
|
authRequestID string
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-22 13:40:25 +00:00
|
|
|
name: "user initial, precondition error",
|
2021-03-19 10:12:56 +00:00
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
2021-03-22 13:40:25 +00:00
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanInitialCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
nil, time.Hour*1,
|
2024-04-24 15:50:58 +00:00
|
|
|
"",
|
2021-03-22 13:40:25 +00:00
|
|
|
),
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+411234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "new code, ok",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanInitializedCheckSucceededEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate)),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
domain.NotificationTypeEmail,
|
2024-04-24 15:50:58 +00:00
|
|
|
"",
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2024-07-17 04:43:07 +00:00
|
|
|
newCode: mockEncryptedCode("a", 1*time.Hour),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2024-07-17 04:43:07 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-04-24 15:50:58 +00:00
|
|
|
{
|
|
|
|
name: "new code with authRequestID, ok",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanInitializedCheckSucceededEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate)),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPasswordCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
domain.NotificationTypeEmail,
|
|
|
|
"authRequestID",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-07-17 04:43:07 +00:00
|
|
|
newCode: mockEncryptedCode("a", 1*time.Hour),
|
2024-04-24 15:50:58 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2024-07-17 04:43:07 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authRequestID: "authRequestID",
|
2024-04-24 15:50:58 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-07-17 04:43:07 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
newEncryptedCode: tt.fields.newCode,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2024-07-17 04:43:07 +00:00
|
|
|
got, err := r.RequestSetPassword(tt.args.ctx, tt.args.userID, tt.args.resourceOwner, tt.args.notifyType, tt.args.authRequestID)
|
2021-03-19 10:12:56 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.Equal(t, tt.res.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_PasswordCodeSent(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code sent, ok",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+411234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCodeSentEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
err := r.PasswordCodeSent(tt.args.ctx, tt.args.resourceOwner, tt.args.userID)
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_CheckPassword(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
2024-04-05 09:35:49 +00:00
|
|
|
userPasswordHasher *crypto.Hasher
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
password string
|
|
|
|
authReq *domain.AuthRequest
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
password: "password",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
2021-11-08 07:42:07 +00:00
|
|
|
{
|
|
|
|
name: "login policy not found, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-11-08 07:42:07 +00:00
|
|
|
expectFilter(),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-11-08 07:42:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "login policy login password not allowed, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-11-08 07:42:07 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("org1").Aggregate,
|
2021-11-08 07:42:07 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2022-05-16 13:39:09 +00:00
|
|
|
false,
|
2022-10-06 11:30:14 +00:00
|
|
|
false,
|
2022-10-17 19:19:15 +00:00
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2021-11-08 07:42:07 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
2022-05-16 13:39:09 +00:00
|
|
|
"",
|
2022-02-21 15:05:02 +00:00
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
2021-11-08 07:42:07 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-11-08 07:42:07 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
{
|
|
|
|
name: "user not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-11-08 07:42:07 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("org1").Aggregate,
|
2021-11-08 07:42:07 +00:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2022-05-16 13:39:09 +00:00
|
|
|
false,
|
2022-10-06 11:30:14 +00:00
|
|
|
false,
|
2022-10-17 19:19:15 +00:00
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2021-11-08 07:42:07 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
2022-05-16 13:39:09 +00:00
|
|
|
"",
|
2022-02-21 15:05:02 +00:00
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
2021-11-08 07:42:07 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
2023-11-08 13:19:13 +00:00
|
|
|
{
|
|
|
|
name: "user locked, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-11-08 13:19:13 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
domain.PasswordlessTypeNotAllowed,
|
|
|
|
"",
|
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewUserLockedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2023-11-08 13:19:13 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
{
|
|
|
|
name: "existing password empty, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-11-08 07:42:07 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("org1").Aggregate,
|
2021-11-08 07:42:07 +00:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2022-05-16 13:39:09 +00:00
|
|
|
false,
|
2022-10-06 11:30:14 +00:00
|
|
|
false,
|
2022-10-17 19:19:15 +00:00
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2021-11-08 07:42:07 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
2022-05-16 13:39:09 +00:00
|
|
|
"",
|
2022-02-21 15:05:02 +00:00
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
2021-11-08 07:42:07 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
password: "password",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-08-11 06:36:32 +00:00
|
|
|
name: "password not matching lockout policy not relevant, precondition error",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-11-08 07:42:07 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("org1").Aggregate,
|
2021-11-08 07:42:07 +00:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2022-05-16 13:39:09 +00:00
|
|
|
false,
|
2022-10-06 11:30:14 +00:00
|
|
|
false,
|
2022-10-17 19:19:15 +00:00
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2021-11-08 07:42:07 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
2022-05-16 13:39:09 +00:00
|
|
|
"",
|
2022-02-21 15:05:02 +00:00
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
2021-11-08 07:42:07 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-08-11 06:36:32 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
2023-07-14 06:49:57 +00:00
|
|
|
"$plain$x$password",
|
2021-08-11 06:36:32 +00:00
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
2023-11-08 13:19:13 +00:00
|
|
|
expectFilter(),
|
2024-05-30 22:08:48 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLockoutPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
0, 0, false,
|
|
|
|
)),
|
|
|
|
),
|
2021-08-11 06:36:32 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCheckFailedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "request1",
|
|
|
|
UserAgentID: "agent1",
|
|
|
|
},
|
|
|
|
),
|
2021-08-11 06:36:32 +00:00
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
2021-08-11 06:36:32 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
password: "password1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authReq: &domain.AuthRequest{
|
|
|
|
ID: "request1",
|
|
|
|
AgentID: "agent1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-08-11 06:36:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password not matching, max password attempts reached - user locked, precondition error",
|
2021-03-19 10:12:56 +00:00
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-11-08 07:42:07 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("org1").Aggregate,
|
2021-11-08 07:42:07 +00:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2022-05-16 13:39:09 +00:00
|
|
|
false,
|
2022-10-06 11:30:14 +00:00
|
|
|
false,
|
2022-10-17 19:19:15 +00:00
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2021-11-08 07:42:07 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
2022-05-16 13:39:09 +00:00
|
|
|
"",
|
2022-02-21 15:05:02 +00:00
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
2021-11-08 07:42:07 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
2023-07-14 06:49:57 +00:00
|
|
|
"$plain$x$password",
|
2021-03-19 10:12:56 +00:00
|
|
|
false,
|
2023-11-08 13:19:13 +00:00
|
|
|
""),
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
2023-11-08 13:19:13 +00:00
|
|
|
expectFilter(),
|
2024-05-30 22:08:48 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLockoutPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
1, 1, false,
|
|
|
|
)),
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCheckFailedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "request1",
|
|
|
|
UserAgentID: "agent1",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
user.NewUserLockedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
password: "password1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
authReq: &domain.AuthRequest{
|
|
|
|
ID: "request1",
|
|
|
|
AgentID: "agent1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "check password, ok",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2021-11-08 07:42:07 +00:00
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
2022-04-20 14:59:37 +00:00
|
|
|
&org.NewAggregate("org1").Aggregate,
|
2021-11-08 07:42:07 +00:00
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2022-05-16 13:39:09 +00:00
|
|
|
false,
|
2022-10-06 11:30:14 +00:00
|
|
|
false,
|
2022-10-17 19:19:15 +00:00
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2021-11-08 07:42:07 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
2022-05-16 13:39:09 +00:00
|
|
|
"",
|
2022-02-21 15:05:02 +00:00
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
2021-11-08 07:42:07 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2021-03-19 10:12:56 +00:00
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
2023-07-14 06:49:57 +00:00
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
2023-11-08 13:19:13 +00:00
|
|
|
expectFilter(),
|
2023-07-14 06:49:57 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCheckSucceededEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "request1",
|
|
|
|
UserAgentID: "agent1",
|
|
|
|
},
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
authReq: &domain.AuthRequest{
|
|
|
|
ID: "request1",
|
|
|
|
AgentID: "agent1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "check password, ok, updated hash",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-07-14 06:49:57 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2023-07-14 06:49:57 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
|
|
|
"",
|
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$v$password",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
2023-11-08 13:19:13 +00:00
|
|
|
expectFilter(),
|
2023-07-14 06:49:57 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCheckSucceededEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "request1",
|
|
|
|
UserAgentID: "agent1",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
user.NewHumanPasswordHashUpdatedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
authReq: &domain.AuthRequest{
|
|
|
|
ID: "request1",
|
|
|
|
AgentID: "agent1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{},
|
|
|
|
},
|
2023-11-08 13:19:13 +00:00
|
|
|
{
|
|
|
|
name: "check password ok, locked in the mean time",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-11-08 13:19:13 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
domain.PasswordlessTypeNotAllowed,
|
|
|
|
"",
|
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPasswordChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
false,
|
|
|
|
"")),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewUserLockedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
authReq: &domain.AuthRequest{
|
|
|
|
ID: "request1",
|
|
|
|
AgentID: "agent1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2023-11-08 13:19:13 +00:00
|
|
|
},
|
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
{
|
|
|
|
name: "regression test old version event",
|
|
|
|
fields: fields{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-07-14 06:49:57 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
org.NewLoginPolicyAddedEvent(context.Background(),
|
|
|
|
&org.NewAggregate("org1").Aggregate,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2023-07-20 04:06:16 +00:00
|
|
|
false,
|
2023-07-14 06:49:57 +00:00
|
|
|
domain.PasswordlessTypeNotAllowed,
|
|
|
|
"",
|
|
|
|
time.Hour*1,
|
|
|
|
time.Hour*2,
|
|
|
|
time.Hour*3,
|
|
|
|
time.Hour*4,
|
|
|
|
time.Hour*5,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
&user.HumanPasswordChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
user.HumanPasswordChangedType,
|
|
|
|
),
|
|
|
|
Secret: &crypto.CryptoValue{
|
2021-03-19 10:12:56 +00:00
|
|
|
CryptoType: crypto.TypeHash,
|
2023-07-14 06:49:57 +00:00
|
|
|
Algorithm: "plain",
|
2021-03-19 10:12:56 +00:00
|
|
|
KeyID: "",
|
2023-07-14 06:49:57 +00:00
|
|
|
Crypted: []byte("$plain$v$password"),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
2023-07-14 06:49:57 +00:00
|
|
|
ChangeRequired: false,
|
|
|
|
},
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
2023-11-08 13:19:13 +00:00
|
|
|
expectFilter(),
|
2021-03-19 10:12:56 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCheckSucceededEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&user.AuthRequestInfo{
|
|
|
|
ID: "request1",
|
|
|
|
UserAgentID: "agent1",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
user.NewHumanPasswordHashUpdatedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"$plain$x$password",
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: mockPasswordHasher("x"),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
password: "password",
|
|
|
|
authReq: &domain.AuthRequest{
|
|
|
|
ID: "request1",
|
|
|
|
AgentID: "agent1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: res{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-04-24 15:50:58 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2023-07-14 06:49:57 +00:00
|
|
|
userPasswordHasher: tt.fields.userPasswordHasher,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2024-05-30 22:08:48 +00:00
|
|
|
err := r.HumanCheckPassword(tt.args.ctx, tt.args.resourceOwner, tt.args.userID, tt.args.password, tt.args.authReq)
|
2021-03-19 10:12:56 +00:00
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-07-14 06:49:57 +00:00
|
|
|
|
|
|
|
func Test_convertPasswapErr(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "nil",
|
|
|
|
args: args{nil},
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mismatch",
|
|
|
|
args: args{passwap.ErrPasswordMismatch},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(passwap.ErrPasswordMismatch, "COMMAND-3M0fs", "Errors.User.Password.Invalid"),
|
2023-07-14 06:49:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no change",
|
|
|
|
args: args{passwap.ErrPasswordNoChange},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(passwap.ErrPasswordNoChange, "COMMAND-Aesh5", "Errors.User.Password.NotChanged"),
|
2023-07-14 06:49:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "other",
|
|
|
|
args: args{io.ErrClosedPipe},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInternal(io.ErrClosedPipe, "COMMAND-CahN2", "Errors.Internal"),
|
2023-07-14 06:49:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
err := convertPasswapErr(tt.args.err)
|
|
|
|
assert.ErrorIs(t, err, tt.wantErr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|