mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
254 lines
5.3 KiB
Go
254 lines
5.3 KiB
Go
|
package command
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/caos/zitadel/internal/api/authz"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
|
||
|
"github.com/caos/zitadel/internal/domain"
|
||
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
||
|
"github.com/caos/zitadel/internal/eventstore"
|
||
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
||
|
"github.com/caos/zitadel/internal/repository/instance"
|
||
|
)
|
||
|
|
||
|
func TestCommandSide_AddInstanceDomain(t *testing.T) {
|
||
|
type fields struct {
|
||
|
eventstore *eventstore.Eventstore
|
||
|
}
|
||
|
type args struct {
|
||
|
ctx context.Context
|
||
|
domain string
|
||
|
}
|
||
|
type res struct {
|
||
|
want *domain.ObjectDetails
|
||
|
err func(error) bool
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
fields fields
|
||
|
args args
|
||
|
res res
|
||
|
}{
|
||
|
{
|
||
|
name: "invalid domain, error",
|
||
|
fields: fields{
|
||
|
eventstore: eventstoreExpect(
|
||
|
t,
|
||
|
),
|
||
|
},
|
||
|
args: args{
|
||
|
ctx: context.Background(),
|
||
|
domain: "",
|
||
|
},
|
||
|
res: res{
|
||
|
err: caos_errs.IsErrorInvalidArgument,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "domain already exists, precondition error",
|
||
|
fields: fields{
|
||
|
eventstore: eventstoreExpect(
|
||
|
t,
|
||
|
expectFilter(
|
||
|
eventFromEventPusher(
|
||
|
instance.NewDomainAddedEvent(context.Background(),
|
||
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
||
|
"domain.ch",
|
||
|
false,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
},
|
||
|
args: args{
|
||
|
ctx: context.Background(),
|
||
|
domain: "domain.ch",
|
||
|
},
|
||
|
res: res{
|
||
|
err: caos_errs.IsErrorAlreadyExists,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "domain add, ok",
|
||
|
fields: fields{
|
||
|
eventstore: eventstoreExpect(
|
||
|
t,
|
||
|
expectFilter(),
|
||
|
expectPush(
|
||
|
[]*repository.Event{
|
||
|
eventFromEventPusherWithInstanceID(
|
||
|
"INSTANCE",
|
||
|
instance.NewDomainAddedEvent(context.Background(),
|
||
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
||
|
"domain.ch",
|
||
|
false,
|
||
|
)),
|
||
|
},
|
||
|
uniqueConstraintsFromEventConstraintWithInstanceID("INSTANCE", instance.NewAddInstanceDomainUniqueConstraint("domain.ch")),
|
||
|
),
|
||
|
),
|
||
|
},
|
||
|
args: args{
|
||
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||
|
domain: "domain.ch",
|
||
|
},
|
||
|
res: res{
|
||
|
want: &domain.ObjectDetails{
|
||
|
ResourceOwner: "INSTANCE",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
r := &Commands{
|
||
|
eventstore: tt.fields.eventstore,
|
||
|
}
|
||
|
got, err := r.AddInstanceDomain(tt.args.ctx, tt.args.domain)
|
||
|
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_RemoveInstanceDomain(t *testing.T) {
|
||
|
type fields struct {
|
||
|
eventstore *eventstore.Eventstore
|
||
|
}
|
||
|
type args struct {
|
||
|
ctx context.Context
|
||
|
domain string
|
||
|
}
|
||
|
type res struct {
|
||
|
want *domain.ObjectDetails
|
||
|
err func(error) bool
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
fields fields
|
||
|
args args
|
||
|
res res
|
||
|
}{
|
||
|
{
|
||
|
name: "invalid domain, error",
|
||
|
fields: fields{
|
||
|
eventstore: eventstoreExpect(
|
||
|
t,
|
||
|
),
|
||
|
},
|
||
|
args: args{
|
||
|
ctx: context.Background(),
|
||
|
domain: "",
|
||
|
},
|
||
|
res: res{
|
||
|
err: caos_errs.IsErrorInvalidArgument,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "domain not exists, precondition error",
|
||
|
fields: fields{
|
||
|
eventstore: eventstoreExpect(
|
||
|
t,
|
||
|
expectFilter(),
|
||
|
),
|
||
|
},
|
||
|
args: args{
|
||
|
ctx: context.Background(),
|
||
|
domain: "domain.ch",
|
||
|
},
|
||
|
res: res{
|
||
|
err: caos_errs.IsNotFound,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "remove domain, ok",
|
||
|
fields: fields{
|
||
|
eventstore: eventstoreExpect(
|
||
|
t,
|
||
|
expectFilter(
|
||
|
eventFromEventPusherWithInstanceID(
|
||
|
"INSTANCE",
|
||
|
instance.NewDomainAddedEvent(context.Background(),
|
||
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
||
|
"domain.ch",
|
||
|
false,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
expectPush(
|
||
|
[]*repository.Event{
|
||
|
eventFromEventPusherWithInstanceID(
|
||
|
"INSTANCE",
|
||
|
instance.NewDomainRemovedEvent(context.Background(),
|
||
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
||
|
"domain.ch",
|
||
|
)),
|
||
|
},
|
||
|
uniqueConstraintsFromEventConstraintWithInstanceID("INSTANCE", instance.NewRemoveInstanceDomainUniqueConstraint("domain.ch")),
|
||
|
),
|
||
|
),
|
||
|
},
|
||
|
args: args{
|
||
|
ctx: authz.WithInstanceID(context.Background(), "INSTANCE"),
|
||
|
domain: "domain.ch",
|
||
|
},
|
||
|
res: res{
|
||
|
want: &domain.ObjectDetails{
|
||
|
ResourceOwner: "INSTANCE",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
name: "remove generated domain, precondition failed",
|
||
|
fields: fields{
|
||
|
eventstore: eventstoreExpect(
|
||
|
t,
|
||
|
expectFilter(
|
||
|
eventFromEventPusher(
|
||
|
instance.NewDomainAddedEvent(context.Background(),
|
||
|
&instance.NewAggregate("INSTANCE").Aggregate,
|
||
|
"domain.ch",
|
||
|
true,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
},
|
||
|
args: args{
|
||
|
ctx: context.Background(),
|
||
|
domain: "domain.ch",
|
||
|
},
|
||
|
res: res{
|
||
|
err: caos_errs.IsPreconditionFailed,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
r := &Commands{
|
||
|
eventstore: tt.fields.eventstore,
|
||
|
}
|
||
|
got, err := r.RemoveInstanceDomain(tt.args.ctx, tt.args.domain)
|
||
|
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)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|