zitadel/internal/command/user_human_adress_test.go
Tim Möhlmann f680dd934d
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors

* rename package errors to gerrors

* fix error related linting issues

* fix zitadel error assertion

* fix gosimple linting issues

* fix deprecated linting issues

* resolve gci linting issues

* fix import structure

---------

Co-authored-by: Elio Bischof <elio@zitadel.com>
2023-12-08 15:30:55 +01:00

205 lines
4.5 KiB
Go

package command
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
"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/user"
"github.com/zitadel/zitadel/internal/zerrors"
)
func TestCommandSide_ChangeHumanAddress(t *testing.T) {
type fields struct {
eventstore *eventstore.Eventstore
}
type args struct {
ctx context.Context
address *domain.Address
resourceOwner string
}
type res struct {
want *domain.Address
err func(error) bool
}
tests := []struct {
name string
fields fields
args args
res res
}{
{
name: "user not existing, precondition error",
fields: fields{
eventstore: eventstoreExpect(
t,
expectFilter(),
),
},
args: args{
ctx: context.Background(),
address: &domain.Address{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
Country: "Switzerland",
Locality: "St. Gallen",
PostalCode: "9000",
},
resourceOwner: "org1",
},
res: res{
err: zerrors.IsPreconditionFailed,
},
},
{
name: "address not changed, precondition error",
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",
true,
),
),
eventFromEventPusher(
newAddressChangedEvent(context.Background(),
"user1", "org1",
"country",
"locality",
"postalcode",
"region",
"street",
),
),
),
),
},
args: args{
ctx: context.Background(),
address: &domain.Address{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
Country: "country",
Locality: "locality",
PostalCode: "postalcode",
Region: "region",
StreetAddress: "street",
},
resourceOwner: "org1",
},
res: res{
err: zerrors.IsPreconditionFailed,
},
},
{
name: "address changed, ok",
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",
true,
),
),
),
expectPush(
newAddressChangedEvent(context.Background(),
"user1", "org1",
"country",
"locality",
"postalcode",
"region",
"street",
),
),
),
},
args: args{
ctx: context.Background(),
address: &domain.Address{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
Country: "country",
Locality: "locality",
PostalCode: "postalcode",
Region: "region",
StreetAddress: "street",
},
resourceOwner: "org1",
},
res: res{
want: &domain.Address{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
ResourceOwner: "org1",
},
Country: "country",
Locality: "locality",
PostalCode: "postalcode",
Region: "region",
StreetAddress: "street",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Commands{
eventstore: tt.fields.eventstore,
}
got, err := r.ChangeHumanAddress(tt.args.ctx, tt.args.address)
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 newAddressChangedEvent(ctx context.Context, userID, resourceOwner, country, locality, postalCode, region, street string) *user.HumanAddressChangedEvent {
event, _ := user.NewAddressChangedEvent(ctx,
&user.NewAggregate(userID, resourceOwner).Aggregate,
[]user.AddressChanges{
user.ChangeCountry(country),
user.ChangeLocality(locality),
user.ChangePostalCode(postalCode),
user.ChangeRegion(region),
user.ChangeStreetAddress(street),
},
)
return event
}