zitadel/internal/command/user_human_profile_test.go
Elio Bischof dd33538c0a
feat: restrict languages (#6931)
* feat: return 404 or 409 if org reg disallowed

* fix: system limit permissions

* feat: add iam limits api

* feat: disallow public org registrations on default instance

* add integration test

* test: integration

* fix test

* docs: describe public org registrations

* avoid updating docs deps

* fix system limits integration test

* silence integration tests

* fix linting

* ignore strange linter complaints

* review

* improve reset properties naming

* redefine the api

* use restrictions aggregate

* test query

* simplify and test projection

* test commands

* fix unit tests

* move integration test

* support restrictions on default instance

* also test GetRestrictions

* self review

* lint

* abstract away resource owner

* fix tests

* configure supported languages

* fix allowed languages

* fix tests

* default lang must not be restricted

* preferred language must be allowed

* change preferred languages

* check languages everywhere

* lint

* test command side

* lint

* add integration test

* add integration test

* restrict supported ui locales

* lint

* lint

* cleanup

* lint

* allow undefined preferred language

* fix integration tests

* update main

* fix env var

* ignore linter

* ignore linter

* improve integration test config

* reduce cognitive complexity

* compile

* check for duplicates

* remove useless restriction checks

* review

* revert restriction renaming

* fix language restrictions

* lint

* generate

* allow custom texts for supported langs for now

* fix tests

* cleanup

* cleanup

* cleanup

* lint

* unsupported preferred lang is allowed

* fix integration test

* finish reverting to old property name

* finish reverting to old property name

* load languages

* refactor(i18n): centralize translators and fs

* lint

* amplify no validations on preferred languages

* fix integration test

* lint

* fix resetting allowed languages

* test unchanged restrictions
2023-12-05 11:12:01 +00:00

328 lines
7.8 KiB
Go

package command
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/text/language"
"github.com/zitadel/zitadel/internal/domain"
zitadel_errs "github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/zitadel/internal/repository/user"
)
func TestCommandSide_ChangeHumanProfile(t *testing.T) {
type fields struct {
eventstore *eventstore.Eventstore
}
type args struct {
ctx context.Context
address *domain.Profile
resourceOwner string
}
type res struct {
want *domain.Profile
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.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
FirstName: "firstname",
LastName: "lastname",
NickName: "nickname",
DisplayName: "displayname",
PreferredLanguage: AllowedLanguage,
Gender: domain.GenderFemale,
},
resourceOwner: "org1",
},
res: res{
err: zitadel_errs.IsPreconditionFailed,
},
},
{
name: "profile 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",
AllowedLanguage,
domain.GenderFemale,
"email",
true,
),
),
),
),
},
args: args{
ctx: context.Background(),
address: &domain.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
FirstName: "firstname",
LastName: "lastname",
NickName: "nickname",
DisplayName: "displayname",
PreferredLanguage: AllowedLanguage,
Gender: domain.GenderFemale,
},
resourceOwner: "org1",
},
res: res{
err: zitadel_errs.IsPreconditionFailed,
},
},
{
name: "profile changed, ok",
fields: fields{
eventstore: eventstoreExpect(
t,
expectFilter(
eventFromEventPusher(
user.NewHumanAddedEvent(context.Background(),
&user.NewAggregate("user1", "org1").Aggregate,
"username",
"firstname",
"lastname",
"nickname",
"displayname",
DisallowedLanguage,
domain.GenderUnspecified,
"email",
true,
),
),
),
expectPush(
newProfileChangedEvent(context.Background(),
"user1", "org1",
"firstname2",
"lastname2",
"nickname2",
"displayname2",
AllowedLanguage,
domain.GenderMale,
),
),
),
},
args: args{
ctx: context.Background(),
address: &domain.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
FirstName: "firstname2",
LastName: "lastname2",
NickName: "nickname2",
DisplayName: "displayname2",
PreferredLanguage: AllowedLanguage,
Gender: domain.GenderMale,
},
resourceOwner: "org1",
},
res: res{
want: &domain.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
ResourceOwner: "org1",
},
FirstName: "firstname2",
LastName: "lastname2",
NickName: "nickname2",
DisplayName: "displayname2",
PreferredLanguage: AllowedLanguage,
Gender: domain.GenderMale,
},
},
},
{
name: "undefined preferred language, ok",
fields: fields{
eventstore: eventstoreExpect(
t,
expectFilter(
eventFromEventPusher(
user.NewHumanAddedEvent(context.Background(),
&user.NewAggregate("user1", "org1").Aggregate,
"username",
"firstname",
"lastname",
"nickname",
"displayname",
DisallowedLanguage,
domain.GenderUnspecified,
"email",
true,
),
),
),
expectPush(
newProfileChangedEvent(context.Background(),
"user1", "org1",
"firstname2",
"lastname2",
"nickname2",
"displayname2",
language.Und,
domain.GenderMale,
),
),
),
},
args: args{
ctx: context.Background(),
address: &domain.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
FirstName: "firstname2",
LastName: "lastname2",
NickName: "nickname2",
DisplayName: "displayname2",
Gender: domain.GenderMale,
},
resourceOwner: "org1",
},
res: res{
want: &domain.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
ResourceOwner: "org1",
},
FirstName: "firstname2",
LastName: "lastname2",
NickName: "nickname2",
DisplayName: "displayname2",
PreferredLanguage: language.Und,
Gender: domain.GenderMale,
},
},
}, {
name: "unsupported preferred language, ok",
fields: fields{
eventstore: eventstoreExpect(
t,
expectFilter(
eventFromEventPusher(
user.NewHumanAddedEvent(context.Background(),
&user.NewAggregate("user1", "org1").Aggregate,
"username",
"firstname",
"lastname",
"nickname",
"displayname",
DisallowedLanguage,
domain.GenderUnspecified,
"email",
true,
),
),
),
expectPush(
newProfileChangedEvent(context.Background(),
"user1", "org1",
"firstname2",
"lastname2",
"nickname2",
"displayname2",
UnsupportedLanguage,
domain.GenderMale,
),
),
),
},
args: args{
ctx: context.Background(),
address: &domain.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
},
FirstName: "firstname2",
LastName: "lastname2",
NickName: "nickname2",
DisplayName: "displayname2",
PreferredLanguage: UnsupportedLanguage,
Gender: domain.GenderMale,
},
resourceOwner: "org1",
},
res: res{
want: &domain.Profile{
ObjectRoot: models.ObjectRoot{
AggregateID: "user1",
ResourceOwner: "org1",
},
FirstName: "firstname2",
LastName: "lastname2",
NickName: "nickname2",
DisplayName: "displayname2",
PreferredLanguage: UnsupportedLanguage,
Gender: domain.GenderMale,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Commands{
eventstore: tt.fields.eventstore,
}
got, err := r.ChangeHumanProfile(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 newProfileChangedEvent(ctx context.Context, userID, resourceOwner, fistName, lastName, nickName, displayName string, lang language.Tag, gender domain.Gender) *user.HumanProfileChangedEvent {
event, _ := user.NewHumanProfileChangedEvent(ctx,
&user.NewAggregate(userID, resourceOwner).Aggregate,
[]user.ProfileChanges{
user.ChangeFirstName(fistName),
user.ChangeLastName(lastName),
user.ChangeNickName(nickName),
user.ChangeDisplayName(displayName),
user.ChangePreferredLanguage(lang),
user.ChangeGender(gender),
},
)
return event
}