2023-06-20 15:34:06 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2023-06-20 15:34:06 +00:00
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-04-24 15:50:58 +00:00
|
|
|
|
2023-06-20 15:34:06 +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/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-06-20 15:34:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommands_RequestPasswordReset(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
checkPermission domain.PermissionCheck
|
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-SAFdda", "Errors.User.IDMissing"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-SAF4f", "Errors.User.NotFound"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not initialized",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanInitialCodeAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
2024-04-24 15:50:58 +00:00
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("code")}, 10*time.Second, ""),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Sfe4g", "Errors.User.NotInitialised"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
}
|
|
|
|
_, _, err := c.RequestPasswordReset(tt.args.ctx, tt.args.userID)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_requestPasswordReset
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_RequestPasswordResetReturnCode(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
checkPermission domain.PermissionCheck
|
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-SAFdda", "Errors.User.IDMissing"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-SAF4f", "Errors.User.NotFound"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not initialized",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanInitialCodeAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
2024-04-24 15:50:58 +00:00
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("code")}, 10*time.Second, ""),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Sfe4g", "Errors.User.NotInitialised"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
}
|
|
|
|
_, _, err := c.RequestPasswordResetReturnCode(tt.args.ctx, tt.args.userID)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_requestPasswordReset
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_RequestPasswordResetURLTemplate(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
checkPermission domain.PermissionCheck
|
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
urlTmpl string
|
|
|
|
notificationType domain.NotificationType
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid template",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
urlTmpl: "{{",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "DOMAIN-oGh5e", "Errors.User.InvalidURLTemplate"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-SAFdda", "Errors.User.IDMissing"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-SAF4f", "Errors.User.NotFound"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not initialized",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanInitialCodeAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
2024-04-24 15:50:58 +00:00
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("code")}, 10*time.Second, ""),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Sfe4g", "Errors.User.NotInitialised"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
2023-12-08 14:30:55 +00:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
}
|
|
|
|
_, _, err := c.RequestPasswordResetURLTemplate(tt.args.ctx, tt.args.userID, tt.args.urlTmpl, tt.args.notificationType)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_requestPasswordReset
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_requestPasswordReset(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
checkPermission domain.PermissionCheck
|
|
|
|
eventstore func(t *testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode encrypedCodeFunc
|
2023-06-20 15:34:06 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
returnCode bool
|
|
|
|
urlTmpl string
|
|
|
|
notificationType domain.NotificationType
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
details *domain.ObjectDetails
|
|
|
|
code *string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowInvalidArgument(nil, "COMMAND-SAFdda", "Errors.User.IDMissing"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowNotFound(nil, "COMMAND-SAF4f", "Errors.User.NotFound"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not initialized",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanInitialCodeAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
2024-04-24 15:50:58 +00:00
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("code")}, 10*time.Second, ""),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Sfe4g", "Errors.User.NotInitialised"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code generated",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCodeAddedEventV2(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
10*time.Minute,
|
|
|
|
domain.NotificationTypeEmail,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("code", 10*time.Minute),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
details: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
code: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code generated template",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCodeAddedEventV2(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
10*time.Minute,
|
|
|
|
domain.NotificationTypeEmail,
|
|
|
|
"https://example.com/password/changey?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
false,
|
|
|
|
),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("code", 10*time.Minute),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
urlTmpl: "https://example.com/password/changey?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
details: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
code: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code generated template sms",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCodeAddedEventV2(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
10*time.Minute,
|
|
|
|
domain.NotificationTypeSms,
|
|
|
|
"https://example.com/password/changey?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
false,
|
|
|
|
),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("code", 10*time.Minute),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
urlTmpl: "https://example.com/password/changey?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
notificationType: domain.NotificationTypeSms,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
details: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
code: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code generated returned",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: expectEventstore(
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
"username", "firstname", "lastname", "nickname", "displayname",
|
|
|
|
language.English, domain.GenderUnspecified, "email", false),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPasswordCodeAddedEventV2(context.Background(), &user.NewAggregate("userID", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("code"),
|
|
|
|
},
|
|
|
|
10*time.Minute,
|
|
|
|
domain.NotificationTypeEmail,
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
),
|
2023-06-20 15:34:06 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
2024-04-05 09:35:49 +00:00
|
|
|
newCode: mockEncryptedCode("code", 10*time.Minute),
|
2023-06-20 15:34:06 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "userID",
|
|
|
|
returnCode: true,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
details: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
code: gu.Ptr("code"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
2024-04-05 09:35:49 +00:00
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
newEncryptedCode: tt.fields.newCode,
|
2023-06-20 15:34:06 +00:00
|
|
|
}
|
|
|
|
got, gotPlainCode, err := c.requestPasswordReset(tt.args.ctx, tt.args.userID, tt.args.returnCode, tt.args.urlTmpl, tt.args.notificationType)
|
|
|
|
require.ErrorIs(t, err, tt.res.err)
|
|
|
|
assert.Equal(t, tt.res.details, got)
|
|
|
|
assert.Equal(t, tt.res.code, gotPlainCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|