2021-03-19 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
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/eventstore/v1/models"
|
2024-09-26 07:14:33 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/senders"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/senders/mock"
|
2023-08-15 12:47:05 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-04-26 23:01:45 +00:00
|
|
|
"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_ChangeHumanPhone(t *testing.T) {
|
2024-09-26 07:14:33 +00:00
|
|
|
defaultGenerators := &SecretGenerators{
|
|
|
|
OTPSMS: &crypto.GeneratorConfig{
|
|
|
|
Length: 8,
|
|
|
|
Expiry: time.Hour,
|
|
|
|
IncludeLowerLetters: true,
|
|
|
|
IncludeUpperLetters: true,
|
|
|
|
IncludeDigits: true,
|
|
|
|
IncludeSymbols: true,
|
|
|
|
},
|
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
type fields struct {
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
defaultSecretGenerators *SecretGenerators
|
|
|
|
newEncryptedCodeWithDefault encryptedCodeWithDefaultFunc
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx context.Context
|
|
|
|
email *domain.Phone
|
|
|
|
resourceOwner string
|
|
|
|
secretGenerator crypto.Generator
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.Phone
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid phone, invalid argument error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "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-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
PhoneNumber: "+41711234567",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "phone not changed, precondition error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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,
|
|
|
|
"+41711234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
PhoneNumber: "+41711234567",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsPreconditionFailed,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "verified phone changed, ok",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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,
|
|
|
|
"+41711234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+41719876543",
|
|
|
|
),
|
|
|
|
user.NewHumanPhoneVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
PhoneNumber: "+41719876543",
|
2021-03-19 10:12:56 +00:00
|
|
|
IsPhoneVerified: true,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41719876543",
|
|
|
|
IsPhoneVerified: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-05-26 07:24:52 +00:00
|
|
|
{
|
|
|
|
name: "phone changed to verified, ok",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-05-26 07:24:52 +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,
|
|
|
|
"+41711234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPhoneVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41711234567",
|
|
|
|
IsPhoneVerified: true,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41711234567",
|
|
|
|
IsPhoneVerified: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "phone changed to verified, ok",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-10-19 10:19:10 +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,
|
|
|
|
"+41711234567",
|
2023-05-26 07:24:52 +00:00
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2023-05-26 07:24:52 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41711234567",
|
|
|
|
IsPhoneVerified: true,
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41711234567",
|
|
|
|
IsPhoneVerified: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
{
|
|
|
|
name: "phone changed with code, ok",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-09-26 07:14:33 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigTwilioAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
"",
|
|
|
|
"sid",
|
|
|
|
"senderNumber",
|
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "id", Crypted: []byte("crypted")},
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+41711234567",
|
|
|
|
),
|
|
|
|
user.NewHumanPhoneCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
2024-09-26 07:14:33 +00:00
|
|
|
"",
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2024-09-26 07:14:33 +00:00
|
|
|
userEncryption: crypto.NewMockEncryptionAlgorithm(gomock.NewController(t)),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
|
|
|
newEncryptedCodeWithDefault: mockEncryptedCodeWithDefault("a", time.Hour),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41711234567",
|
|
|
|
},
|
|
|
|
resourceOwner: "org1",
|
|
|
|
secretGenerator: GetMockSecretGenerator(t),
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41711234567",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "phone changed with code (external), 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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigTwilioAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
"",
|
|
|
|
"sid",
|
|
|
|
"senderNumber",
|
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "id", Crypted: []byte("crypted")},
|
|
|
|
"verifyServiceSID",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+41711234567",
|
|
|
|
),
|
|
|
|
user.NewHumanPhoneCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
nil,
|
|
|
|
0,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.NewMockEncryptionAlgorithm(gomock.NewController(t)),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
email: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
},
|
2022-04-12 14:20:17 +00:00
|
|
|
PhoneNumber: "+41711234567",
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
2022-02-16 15:49:17 +00:00
|
|
|
resourceOwner: "org1",
|
|
|
|
secretGenerator: GetMockSecretGenerator(t),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.Phone{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: "user1",
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
PhoneNumber: "+41711234567",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
defaultSecretGenerators: tt.fields.defaultSecretGenerators,
|
|
|
|
newEncryptedCodeWithDefault: tt.fields.newEncryptedCodeWithDefault,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
got, err := r.ChangeHumanPhone(tt.args.ctx, tt.args.email, tt.args.resourceOwner, tt.args.secretGenerator)
|
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_VerifyHumanPhone(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
phoneCodeVerifier func(ctx context.Context, id string) (senders.CodeGenerator, error)
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
code string
|
|
|
|
resourceOwner string
|
|
|
|
secretGenerator crypto.Generator
|
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-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
code: "aa",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(
|
2021-03-19 10:12:56 +00:00
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
code: "aa",
|
|
|
|
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 not existing, not found error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid code, invalid argument error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
user.NewHumanPhoneCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
2024-09-26 07:14:33 +00:00
|
|
|
"",
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPhoneVerificationFailedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
code: "test",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
secretGenerator: GetMockSecretGenerator(t),
|
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: "valid code, ok",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPhoneCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("a"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
2024-09-26 07:14:33 +00:00
|
|
|
"",
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPhoneVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
code: "a",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
secretGenerator: GetMockSecretGenerator(t),
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-09-26 07:14:33 +00:00
|
|
|
{
|
|
|
|
name: "valid code (external), 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.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+411234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPhoneCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
nil,
|
|
|
|
0,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPhoneCodeSentEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&senders.CodeGeneratorInfo{
|
|
|
|
ID: "id",
|
|
|
|
VerificationID: "verificationID",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPhoneVerifiedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
phoneCodeVerifier: func(ctx context.Context, id string) (senders.CodeGenerator, error) {
|
|
|
|
sender := mock.NewMockCodeGenerator(gomock.NewController(t))
|
|
|
|
sender.EXPECT().VerifyCode("verificationID", "a")
|
|
|
|
return sender, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
code: "a",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
secretGenerator: GetMockSecretGenerator(t),
|
|
|
|
},
|
|
|
|
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-09-26 07:14:33 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
phoneCodeVerifier: tt.fields.phoneCodeVerifier,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
got, err := r.VerifyHumanPhone(tt.args.ctx, tt.args.userID, tt.args.code, tt.args.resourceOwner, tt.args.secretGenerator)
|
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 {
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_CreateVerificationCodeHumanPhone(t *testing.T) {
|
2024-09-26 07:14:33 +00:00
|
|
|
defaultGenerators := &SecretGenerators{
|
|
|
|
OTPSMS: &crypto.GeneratorConfig{
|
|
|
|
Length: 8,
|
|
|
|
Expiry: time.Hour,
|
|
|
|
IncludeLowerLetters: true,
|
|
|
|
IncludeUpperLetters: true,
|
|
|
|
IncludeDigits: true,
|
|
|
|
IncludeSymbols: true,
|
|
|
|
},
|
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
type fields struct {
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore func(*testing.T) *eventstore.Eventstore
|
|
|
|
userEncryption crypto.EncryptionAlgorithm
|
|
|
|
defaultSecretGenerators *SecretGenerators
|
|
|
|
newEncryptedCodeWithDefault encryptedCodeWithDefaultFunc
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2023-08-15 12:47:05 +00:00
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
resourceOwner 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-09-26 07:14:33 +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-09-26 07:14:33 +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: "phone already verified, precondition error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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-09-26 07:14:33 +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",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
2024-09-26 07:14:33 +00:00
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
2023-08-15 12:47:05 +00:00
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
2024-09-26 07:14:33 +00:00
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigTwilioAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
"",
|
|
|
|
"sid",
|
|
|
|
"senderNumber",
|
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "id", Crypted: []byte("crypted")},
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
2023-08-15 12:47:05 +00:00
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPhoneCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "enc",
|
|
|
|
KeyID: "id",
|
|
|
|
Crypted: []byte("12345678"),
|
|
|
|
},
|
|
|
|
time.Hour*1,
|
2024-09-26 07:14:33 +00:00
|
|
|
"",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
|
|
|
newEncryptedCodeWithDefault: mockEncryptedCodeWithDefault("12345678", time.Hour),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "new code (external), 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.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+411234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigTwilioAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
"",
|
|
|
|
"sid",
|
|
|
|
"senderNumber",
|
|
|
|
&crypto.CryptoValue{CryptoType: crypto.TypeEncryption, Algorithm: "enc", KeyID: "id", Crypted: []byte("crypted")},
|
|
|
|
"verifyServiceSID",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusher(
|
|
|
|
instance.NewSMSConfigActivatedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&instance.NewAggregate("instanceID").Aggregate,
|
|
|
|
"id",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPhoneCodeAddedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
nil,
|
|
|
|
0,
|
|
|
|
"id",
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
2024-09-26 07:14:33 +00:00
|
|
|
userEncryption: crypto.CreateMockEncryptionAlg(gomock.NewController(t)),
|
|
|
|
defaultSecretGenerators: defaultGenerators,
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2023-08-15 12:47:05 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
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-09-26 07:14:33 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
|
|
|
userEncryption: tt.fields.userEncryption,
|
|
|
|
defaultSecretGenerators: tt.fields.defaultSecretGenerators,
|
|
|
|
newEncryptedCodeWithDefault: tt.fields.newEncryptedCodeWithDefault,
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2023-08-15 12:47:05 +00:00
|
|
|
got, err := r.CreateHumanPhoneVerificationCode(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
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 {
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_PhoneVerificationCodeSent(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-09-26 07:14:33 +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
|
2024-09-26 07:14:33 +00:00
|
|
|
generatorInfo *senders.CodeGeneratorInfo
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
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-09-26 07:14:33 +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-09-26 07:14:33 +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-09-26 07:14:33 +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.NewHumanPhoneCodeSentEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
2024-09-26 07:14:33 +00:00
|
|
|
&senders.CodeGeneratorInfo{},
|
2023-10-19 10:19:10 +00:00
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
2024-09-26 07:14:33 +00:00
|
|
|
generatorInfo: &senders.CodeGeneratorInfo{},
|
|
|
|
},
|
|
|
|
res: res{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code sent (external), 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.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+411234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
|
|
|
user.NewHumanPhoneCodeSentEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
&senders.CodeGeneratorInfo{
|
|
|
|
ID: "generatorID",
|
|
|
|
VerificationID: "verificationID",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
userID: "user1",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
generatorInfo: &senders.CodeGeneratorInfo{
|
|
|
|
ID: "generatorID",
|
|
|
|
VerificationID: "verificationID",
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
},
|
|
|
|
res: res{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
err := r.HumanPhoneVerificationCodeSent(tt.args.ctx, tt.args.resourceOwner, tt.args.userID, tt.args.generatorInfo)
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandSide_RemoveHumanPhone(t *testing.T) {
|
|
|
|
type fields struct {
|
2024-09-26 07:14:33 +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 {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
2023-08-02 16:57:53 +00:00
|
|
|
{
|
|
|
|
name: "userid missing, invalid argument error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(),
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
ctx: context.Background(),
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "user not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-08-02 16:57:53 +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,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "phone not existing, precondition error",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: expectEventstore(
|
2023-08-02 16:57:53 +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",
|
|
|
|
resourceOwner: "org1",
|
|
|
|
},
|
|
|
|
res: res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2023-08-02 16:57:53 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-19 10:12:56 +00:00
|
|
|
{
|
|
|
|
name: "remove phone, ok",
|
|
|
|
fields: fields{
|
2024-09-26 07:14:33 +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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
eventFromEventPusherWithCreationDateNow(
|
|
|
|
user.NewHumanPhoneChangedEvent(context.Background(),
|
|
|
|
&user.NewAggregate("user1", "org1").Aggregate,
|
|
|
|
"+411234567",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanPhoneRemovedEvent(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{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &Commands{
|
2024-09-26 07:14:33 +00:00
|
|
|
eventstore: tt.fields.eventstore(t),
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
got, err := r.RemoveHumanPhone(tt.args.ctx, tt.args.userID, tt.args.resourceOwner)
|
|
|
|
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 {
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|