2023-04-25 09:02:29 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-22 12:56:43 +02:00
|
|
|
"go.uber.org/mock/gomock"
|
2023-04-25 09:02:29 +02:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-04-25 09:02:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommands_ChangeUserEmail(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
2023-05-05 17:34:53 +02:00
|
|
|
checkPermission domain.PermissionCheck
|
2023-04-25 09:02:29 +02:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
email string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing email",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "EMAIL-spblu", "Errors.User.Email.Empty"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not changed",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Uch5e", "Errors.User.Email.NotChanged"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
}
|
|
|
|
_, err := c.ChangeUserEmail(context.Background(), tt.args.userID, tt.args.resourceOwner, tt.args.email, crypto.CreateMockEncryptionAlg(gomock.NewController(t)))
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_changeUserEmailWithGenerator
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_ChangeUserEmailURLTemplate(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
2023-05-05 17:34:53 +02:00
|
|
|
checkPermission domain.PermissionCheck
|
2023-04-25 09:02:29 +02:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
email string
|
|
|
|
urlTmpl string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid template",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email-changed@test.ch",
|
|
|
|
urlTmpl: "{{",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "DOMAIN-oGh5e", "Errors.User.InvalidURLTemplate"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "permission missing",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
urlTmpl: "https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not changed",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
urlTmpl: "https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Uch5e", "Errors.User.Email.NotChanged"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
}
|
|
|
|
_, err := c.ChangeUserEmailURLTemplate(context.Background(), tt.args.userID, tt.args.resourceOwner, tt.args.email, crypto.CreateMockEncryptionAlg(gomock.NewController(t)), tt.args.urlTmpl)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_changeUserEmailWithGenerator
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_ChangeUserEmailReturnCode(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
2023-05-05 17:34:53 +02:00
|
|
|
checkPermission domain.PermissionCheck
|
2023-04-25 09:02:29 +02:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
email string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing email",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "EMAIL-spblu", "Errors.User.Email.Empty"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
}
|
|
|
|
_, err := c.ChangeUserEmailReturnCode(context.Background(), tt.args.userID, tt.args.resourceOwner, tt.args.email, crypto.CreateMockEncryptionAlg(gomock.NewController(t)))
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_changeUserEmailWithGenerator
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_ChangeUserEmailVerified(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
2023-05-05 17:34:53 +02:00
|
|
|
checkPermission domain.PermissionCheck
|
2023-04-25 09:02:29 +02:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
email string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *domain.Email
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-0Gzs3", "Errors.User.Email.IDMissing"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email-changed@test.ch",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing email",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "EMAIL-spblu", "Errors.User.Email.Empty"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email changed",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 12:19:10 +02:00
|
|
|
user.NewHumanEmailChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"email-changed@test.ch",
|
|
|
|
),
|
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-04-25 09:02:29 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email-changed@test.ch",
|
|
|
|
},
|
|
|
|
want: &domain.Email{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
EmailAddress: "email-changed@test.ch",
|
|
|
|
IsEmailVerified: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
}
|
|
|
|
got, err := c.ChangeUserEmailVerified(context.Background(), tt.args.userID, tt.args.resourceOwner, tt.args.email)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
2023-10-19 12:19:10 +02:00
|
|
|
assert.Equal(t, tt.want, got)
|
2023-04-25 09:02:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
2023-05-05 17:34:53 +02:00
|
|
|
checkPermission domain.PermissionCheck
|
2023-04-25 09:02:29 +02:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
email string
|
|
|
|
returnCode bool
|
|
|
|
urlTmpl string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *domain.Email
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing user",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
returnCode: false,
|
|
|
|
urlTmpl: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-0Gzs3", "Errors.User.Email.IDMissing"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing permission",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckNotAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
returnCode: false,
|
|
|
|
urlTmpl: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "AUTHZ-HKJD33", "Errors.PermissionDenied"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing email",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "",
|
|
|
|
returnCode: false,
|
|
|
|
urlTmpl: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "EMAIL-spblu", "Errors.User.Email.Empty"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not changed",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email@test.ch",
|
|
|
|
returnCode: false,
|
|
|
|
urlTmpl: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-Uch5e", "Errors.User.Email.NotChanged"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email changed",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 12:19:10 +02:00
|
|
|
user.NewHumanEmailChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"email-changed@test.ch",
|
|
|
|
),
|
|
|
|
user.NewHumanEmailCodeAddedEventV2(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
"", false,
|
|
|
|
),
|
2023-04-25 09:02:29 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email-changed@test.ch",
|
|
|
|
returnCode: false,
|
|
|
|
urlTmpl: "",
|
|
|
|
},
|
|
|
|
want: &domain.Email{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
EmailAddress: "email-changed@test.ch",
|
|
|
|
IsEmailVerified: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email changed, return code",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 12:19:10 +02:00
|
|
|
user.NewHumanEmailChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"email-changed@test.ch",
|
|
|
|
),
|
|
|
|
user.NewHumanEmailCodeAddedEventV2(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
"", true,
|
|
|
|
),
|
2023-04-25 09:02:29 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email-changed@test.ch",
|
|
|
|
returnCode: true,
|
|
|
|
urlTmpl: "",
|
|
|
|
},
|
|
|
|
want: &domain.Email{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
EmailAddress: "email-changed@test.ch",
|
|
|
|
IsEmailVerified: false,
|
|
|
|
PlainCode: gu.Ptr("a"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email changed, URL template",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 12:19:10 +02:00
|
|
|
user.NewHumanEmailChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"email-changed@test.ch",
|
|
|
|
),
|
|
|
|
user.NewHumanEmailCodeAddedEventV2(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
"https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}", false,
|
|
|
|
),
|
2023-04-25 09:02:29 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
checkPermission: newMockPermissionCheckAllowed(),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
email: "email-changed@test.ch",
|
|
|
|
returnCode: false,
|
|
|
|
urlTmpl: "https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}",
|
|
|
|
},
|
|
|
|
want: &domain.Email{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
EmailAddress: "email-changed@test.ch",
|
|
|
|
IsEmailVerified: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
checkPermission: tt.fields.checkPermission,
|
|
|
|
}
|
|
|
|
got, err := c.changeUserEmailWithGenerator(context.Background(), tt.args.userID, tt.args.resourceOwner, tt.args.email, GetMockSecretGenerator(t), tt.args.returnCode, tt.args.urlTmpl)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
assert.Equal(t, got, tt.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_VerifyUserEmail(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
code string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "a",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-0Gzs3", "Errors.User.Email.IDMissing"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing code",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-Fia4a", "Errors.User.Code.Empty"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong code",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSecretGeneratorAddedEvent(context.Background(),
|
|
|
|
&instance.NewAggregate("inst1").Aggregate,
|
|
|
|
domain.SecretGeneratorTypeVerifyEmailCode,
|
|
|
|
12, time.Minute, true, true, true, true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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.NewHumanEmailCodeAddedEventV2(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
"", false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 12:19:10 +02:00
|
|
|
user.NewHumanEmailVerificationFailedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-04-25 09:02:29 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "wrong",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-eis9R", "Errors.User.Code.Invalid"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
_, err := c.VerifyUserEmail(context.Background(), tt.args.userID, tt.args.resourceOwner, tt.args.code, crypto.CreateMockEncryptionAlg(gomock.NewController(t)))
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_verifyUserEmailWithGenerator
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_verifyUserEmailWithGenerator(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
code string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "a",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-0Gzs3", "Errors.User.Email.IDMissing"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing code",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
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{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-Fia4a", "Errors.User.Code.Empty"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "good code",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
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.NewHumanEmailCodeAddedEventV2(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
"", false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 12:19:10 +02:00
|
|
|
user.NewHumanEmailVerificationFailedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-04-25 09:02:29 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "wrong",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-eis9R", "Errors.User.Code.Invalid"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrong code",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"username",
|
|
|
|
"firstname",
|
|
|
|
"lastname",
|
|
|
|
"nickname",
|
|
|
|
"displayname",
|
|
|
|
language.German,
|
|
|
|
domain.GenderUnspecified,
|
|
|
|
"email@test.ch",
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanEmailCodeAddedEventV2(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
|
|
|
"", false,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 12:19:10 +02:00
|
|
|
user.NewHumanEmailVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-04-25 09:02:29 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
code: "a",
|
|
|
|
},
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
got, err := c.verifyUserEmailWithGenerator(context.Background(), tt.args.userID, tt.args.resourceOwner, tt.args.code, GetMockSecretGenerator(t))
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
assert.Equal(t, got, tt.want)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_NewUserEmailEvents(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
userID string
|
|
|
|
resourceOwner string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "missing userID",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "COMMAND-0Gzs3", "Errors.User.Email.IDMissing"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(t, expectFilter()),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-ieJ2e", "Errors.User.Email.NotFound"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not initialized",
|
|
|
|
fields: fields{
|
|
|
|
eventstore: eventstoreExpect(
|
|
|
|
t,
|
|
|
|
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.NewHumanInitialCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
nil, time.Hour*1,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
2023-12-08 16:30:55 +02:00
|
|
|
wantErr: zerrors.ThrowPreconditionFailed(nil, "COMMAND-uz0Uu", "Errors.User.NotInitialised"),
|
2023-04-25 09:02:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
_, err := c.NewUserEmailEvents(context.Background(), tt.args.userID, tt.args.resourceOwner)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
|
|
// successful cases are tested in TestCommands_changeUserEmailWithGenerator
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|