mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 15:49:35 +00:00
fixup! fixup! fixup! feat(db): Adding identity_providers table for relational database
This commit is contained in:
@@ -105,6 +105,6 @@ type IDProviderRepository interface {
|
||||
List(ctx context.Context, conditions ...database.Condition) ([]*IdentityProvider, error)
|
||||
|
||||
Create(ctx context.Context, idp *IdentityProvider) error
|
||||
Update(ctx context.Context, id IDPIdentifierCondition, changes ...database.Change) (int64, error)
|
||||
Update(ctx context.Context, id IDPIdentifierCondition, instnaceID string, orgID string, changes ...database.Change) (int64, error)
|
||||
Delete(ctx context.Context, id IDPIdentifierCondition) (int64, error)
|
||||
}
|
||||
|
@@ -61,8 +61,19 @@ const createIDProviderStmt = `INSERT INTO zitadel.identity_providers` +
|
||||
|
||||
func (i *idProvider) Create(ctx context.Context, idp *domain.IdentityProvider) error {
|
||||
builder := database.StatementBuilder{}
|
||||
builder.AppendArgs(idp.InstanceID, idp.OrgID, idp.ID, idp.State, idp.Name, idp.Type, idp.AllowCreation,
|
||||
idp.AllowAutoCreation, idp.AllowAutoUpdate, idp.AllowLinking, idp.StylingType, idp.Payload)
|
||||
builder.AppendArgs(
|
||||
idp.InstanceID,
|
||||
idp.OrgID,
|
||||
idp.ID,
|
||||
idp.State,
|
||||
idp.Name,
|
||||
idp.Type,
|
||||
idp.AllowCreation,
|
||||
idp.AllowAutoCreation,
|
||||
idp.AllowAutoUpdate,
|
||||
idp.AllowLinking,
|
||||
idp.StylingType,
|
||||
idp.Payload)
|
||||
builder.WriteString(createIDProviderStmt)
|
||||
|
||||
err := i.client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(&idp.CreatedAt, &idp.UpdatedAt)
|
||||
@@ -72,22 +83,24 @@ func (i *idProvider) Create(ctx context.Context, idp *domain.IdentityProvider) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *idProvider) Update(ctx context.Context, id domain.IDPIdentifierCondition, changes ...database.Change) (int64, error) {
|
||||
func (i *idProvider) Update(ctx context.Context, id domain.IDPIdentifierCondition, instnaceID string, orgID string, changes ...database.Change) (int64, error) {
|
||||
if changes == nil {
|
||||
return 0, errors.New("Update must contain a condition") // (otherwise ALL identity_providers will be updated)
|
||||
return 0, errors.New("Update must contain at least one change")
|
||||
}
|
||||
builder := database.StatementBuilder{}
|
||||
builder.WriteString(`UPDATE zitadel.identity_provider SET `)
|
||||
builder.WriteString(`UPDATE zitadel.identity_providers SET `)
|
||||
|
||||
// conditions := []database.Condition{i.IDCondition(id)}
|
||||
conditions := []database.Condition{id}
|
||||
conditions := []database.Condition{
|
||||
id,
|
||||
i.InstanceIDCondition(instnaceID),
|
||||
i.OrgIDCondition(orgID),
|
||||
}
|
||||
database.Changes(changes).Write(&builder)
|
||||
writeCondition(&builder, database.And(conditions...))
|
||||
|
||||
stmt := builder.String()
|
||||
|
||||
rowsAffected, err := i.client.Exec(ctx, stmt, builder.Args()...)
|
||||
return rowsAffected, err
|
||||
return i.client.Exec(ctx, stmt, builder.Args()...)
|
||||
}
|
||||
|
||||
func (i *idProvider) Delete(ctx context.Context, id domain.IDPIdentifierCondition) (int64, error) {
|
||||
@@ -230,7 +243,7 @@ func (i idProvider) SetAllowCreation(allow bool) database.Change {
|
||||
}
|
||||
|
||||
func (i idProvider) SetAllowAutoCreation(allow bool) database.Change {
|
||||
return database.NewChange(i.AllowAutoUpdateColumn(), allow)
|
||||
return database.NewChange(i.AllowAutoCreationColumn(), allow)
|
||||
}
|
||||
|
||||
func (i idProvider) SetAllowAutoUpdate(allow bool) database.Change {
|
||||
@@ -246,7 +259,7 @@ func (i idProvider) SetStylingType(stylingType int16) database.Change {
|
||||
}
|
||||
|
||||
func (i idProvider) SetPayload(payload string) database.Change {
|
||||
return database.NewChange(i.StylingTypeColumn(), payload)
|
||||
return database.NewChange(i.PayloadColumn(), payload)
|
||||
}
|
||||
|
||||
func scanIDProvider(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.IdentityProvider, error) {
|
||||
|
@@ -334,12 +334,783 @@ func TestCreateIDProvider(t *testing.T) {
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tt.idp.InstanceID, idp.InstanceID)
|
||||
assert.Equal(t, tt.idp.OrgID, idp.OrgID)
|
||||
assert.Equal(t, tt.idp.State, idp.State)
|
||||
assert.Equal(t, tt.idp.ID, idp.ID)
|
||||
assert.Equal(t, tt.idp.Name, idp.Name)
|
||||
assert.Equal(t, tt.idp.InstanceID, idp.InstanceID)
|
||||
assert.Equal(t, tt.idp.State, idp.State)
|
||||
assert.Equal(t, tt.idp.Type, idp.Type)
|
||||
assert.Equal(t, tt.idp.AllowCreation, idp.AllowCreation)
|
||||
assert.Equal(t, tt.idp.AllowAutoCreation, idp.AllowAutoCreation)
|
||||
assert.Equal(t, tt.idp.AllowAutoUpdate, idp.AllowAutoUpdate)
|
||||
assert.Equal(t, tt.idp.AllowLinking, idp.AllowLinking)
|
||||
assert.Equal(t, tt.idp.StylingType, idp.StylingType)
|
||||
assert.Equal(t, tt.idp.Payload, idp.Payload)
|
||||
assert.WithinRange(t, idp.CreatedAt, beforeCreate, afterCreate)
|
||||
assert.WithinRange(t, idp.UpdatedAt, beforeCreate, afterCreate)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateIDProvider(t *testing.T) {
|
||||
// create instance
|
||||
instanceId := gofakeit.Name()
|
||||
instance := domain.Instance{
|
||||
ID: instanceId,
|
||||
Name: gofakeit.Name(),
|
||||
DefaultOrgID: "defaultOrgId",
|
||||
IAMProjectID: "iamProject",
|
||||
ConsoleClientID: "consoleCLient",
|
||||
ConsoleAppID: "consoleApp",
|
||||
DefaultLanguage: "defaultLanguage",
|
||||
}
|
||||
instanceRepo := repository.InstanceRepository(pool)
|
||||
err := instanceRepo.Create(t.Context(), &instance)
|
||||
require.NoError(t, err)
|
||||
|
||||
// create org
|
||||
orgId := gofakeit.Name()
|
||||
org := domain.Organization{
|
||||
ID: orgId,
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
organizationRepo := repository.OrganizationRepository(pool)
|
||||
err = organizationRepo.Create(t.Context(), &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
idpRepo := repository.IDProviderRepository(pool)
|
||||
tests := []struct {
|
||||
name string
|
||||
testFunc func(ctx context.Context, t *testing.T) *domain.IdentityProvider
|
||||
update []database.Change
|
||||
rowsAffected int64
|
||||
}{
|
||||
{
|
||||
name: "happy path update name",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.Name = "new_name"
|
||||
return &idp
|
||||
},
|
||||
update: []database.Change{idpRepo.SetName("new_name")},
|
||||
rowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "happy path update state",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.State = domain.IDPStateInactive.String()
|
||||
return &idp
|
||||
},
|
||||
update: []database.Change{idpRepo.SetState(domain.IDPStateInactive)},
|
||||
rowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "happy path update AllowCreation",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.AllowCreation = false
|
||||
return &idp
|
||||
},
|
||||
update: []database.Change{idpRepo.SetAllowCreation(false)},
|
||||
rowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "happy path update AllowAutoCreation",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.AllowAutoCreation = false
|
||||
return &idp
|
||||
},
|
||||
update: []database.Change{idpRepo.SetAllowAutoCreation(false)},
|
||||
rowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "happy path update AllowLinking",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.AllowLinking = false
|
||||
return &idp
|
||||
},
|
||||
update: []database.Change{idpRepo.SetAllowLinking(false)},
|
||||
rowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "happy path update StylingType",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.StylingType = 2
|
||||
return &idp
|
||||
},
|
||||
update: []database.Change{idpRepo.SetStylingType(2)},
|
||||
rowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "happy path update Payload",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.Payload = `{"json": {}}`
|
||||
return &idp
|
||||
},
|
||||
// update: []database.Change{idpRepo.SetPayload("{{}}")},
|
||||
update: []database.Change{idpRepo.SetPayload(`{"json": {}}`)},
|
||||
rowsAffected: 1,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
organizationRepo := repository.OrganizationRepository(pool)
|
||||
idpRepo := repository.IDProviderRepository(pool)
|
||||
|
||||
createdIDP := tt.testFunc(ctx, t)
|
||||
|
||||
// update org
|
||||
beforeUpdate := time.Now()
|
||||
rowsAffected, err := idpRepo.Update(ctx,
|
||||
idpRepo.IDCondition(createdIDP.ID),
|
||||
createdIDP.InstanceID,
|
||||
createdIDP.OrgID,
|
||||
tt.update...,
|
||||
)
|
||||
afterUpdate := time.Now()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tt.rowsAffected, rowsAffected)
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// check organization values
|
||||
idp, err := idpRepo.Get(ctx,
|
||||
organizationRepo.IDCondition(createdIDP.ID),
|
||||
createdIDP.InstanceID,
|
||||
createdIDP.OrgID,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, createdIDP.InstanceID, idp.InstanceID)
|
||||
assert.Equal(t, createdIDP.OrgID, idp.OrgID)
|
||||
assert.Equal(t, createdIDP.State, idp.State)
|
||||
assert.Equal(t, createdIDP.ID, idp.ID)
|
||||
assert.Equal(t, createdIDP.Name, idp.Name)
|
||||
assert.Equal(t, createdIDP.Type, idp.Type)
|
||||
assert.Equal(t, createdIDP.AllowCreation, idp.AllowCreation)
|
||||
assert.Equal(t, createdIDP.AllowAutoCreation, idp.AllowAutoCreation)
|
||||
assert.Equal(t, createdIDP.AllowAutoUpdate, idp.AllowAutoUpdate)
|
||||
assert.Equal(t, createdIDP.AllowLinking, idp.AllowLinking)
|
||||
assert.Equal(t, createdIDP.StylingType, idp.StylingType)
|
||||
assert.Equal(t, createdIDP.Payload, idp.Payload)
|
||||
assert.WithinRange(t, idp.UpdatedAt, beforeUpdate, afterUpdate)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIDProvider(t *testing.T) {
|
||||
// create instance
|
||||
instanceId := gofakeit.Name()
|
||||
instance := domain.Instance{
|
||||
ID: instanceId,
|
||||
Name: gofakeit.Name(),
|
||||
DefaultOrgID: "defaultOrgId",
|
||||
IAMProjectID: "iamProject",
|
||||
ConsoleClientID: "consoleCLient",
|
||||
ConsoleAppID: "consoleApp",
|
||||
DefaultLanguage: "defaultLanguage",
|
||||
}
|
||||
instanceRepo := repository.InstanceRepository(pool)
|
||||
err := instanceRepo.Create(t.Context(), &instance)
|
||||
require.NoError(t, err)
|
||||
|
||||
// create org
|
||||
orgId := gofakeit.Name()
|
||||
org := domain.Organization{
|
||||
ID: orgId,
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
organizationRepo := repository.OrganizationRepository(pool)
|
||||
err = organizationRepo.Create(t.Context(), &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
// create organization
|
||||
// this org is created as an additional org which should NOT
|
||||
// be returned in the results of the tests
|
||||
preexistingOrg := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
err = organizationRepo.Create(t.Context(), &preexistingOrg)
|
||||
require.NoError(t, err)
|
||||
|
||||
idpRepo := repository.IDProviderRepository(pool)
|
||||
type test struct {
|
||||
name string
|
||||
testFunc func(ctx context.Context, t *testing.T) *domain.IdentityProvider
|
||||
idpIdentifierCondition domain.OrgIdentifierCondition
|
||||
err error
|
||||
}
|
||||
|
||||
tests := []test{
|
||||
func() test {
|
||||
id := gofakeit.Name()
|
||||
return test{
|
||||
name: "happy path get using id",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: id,
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
return &idp
|
||||
},
|
||||
idpIdentifierCondition: idpRepo.IDCondition(id),
|
||||
}
|
||||
}(),
|
||||
func() test {
|
||||
name := gofakeit.Name()
|
||||
return test{
|
||||
name: "happy path get using name",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: name,
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
return &idp
|
||||
},
|
||||
idpIdentifierCondition: idpRepo.NameCondition(name),
|
||||
}
|
||||
}(),
|
||||
{
|
||||
name: "get using non existent id",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
return &idp
|
||||
},
|
||||
idpIdentifierCondition: idpRepo.IDCondition("non-existent-id"),
|
||||
err: new(database.NoRowFoundError),
|
||||
},
|
||||
{
|
||||
name: "get using non existent name",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
return &idp
|
||||
},
|
||||
idpIdentifierCondition: idpRepo.NameCondition("non-existent-name"),
|
||||
err: new(database.NoRowFoundError),
|
||||
},
|
||||
////////
|
||||
func() test {
|
||||
id := gofakeit.Name()
|
||||
return test{
|
||||
name: "non existent orgID",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: id,
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: gofakeit.Name(),
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.OrgID = "non-existent-orgID"
|
||||
return &idp
|
||||
},
|
||||
idpIdentifierCondition: idpRepo.IDCondition(id),
|
||||
err: new(database.NoRowFoundError),
|
||||
}
|
||||
}(),
|
||||
func() test {
|
||||
name := gofakeit.Name()
|
||||
return test{
|
||||
name: "non existent instanceID",
|
||||
testFunc: func(ctx context.Context, t *testing.T) *domain.IdentityProvider {
|
||||
idp := domain.IdentityProvider{
|
||||
InstanceID: instanceId,
|
||||
OrgID: orgId,
|
||||
ID: gofakeit.Name(),
|
||||
State: domain.IDPStateActive.String(),
|
||||
Name: name,
|
||||
Type: domain.IDPTypeOIDC.String(),
|
||||
AllowCreation: true,
|
||||
AllowAutoCreation: true,
|
||||
AllowAutoUpdate: true,
|
||||
AllowLinking: true,
|
||||
StylingType: 1,
|
||||
Payload: "{}",
|
||||
}
|
||||
|
||||
err := idpRepo.Create(ctx, &idp)
|
||||
require.NoError(t, err)
|
||||
idp.InstanceID = "non-existent-instnaceID"
|
||||
return &idp
|
||||
},
|
||||
idpIdentifierCondition: idpRepo.NameCondition(name),
|
||||
err: new(database.NoRowFoundError),
|
||||
}
|
||||
}(),
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
var idp *domain.IdentityProvider
|
||||
if tt.testFunc != nil {
|
||||
idp = tt.testFunc(ctx, t)
|
||||
}
|
||||
|
||||
// get idp
|
||||
returnedIDP, err := idpRepo.Get(ctx,
|
||||
tt.idpIdentifierCondition,
|
||||
idp.InstanceID,
|
||||
idp.OrgID,
|
||||
)
|
||||
if err != nil {
|
||||
require.ErrorIs(t, tt.err, err)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, returnedIDP.ID, idp.ID)
|
||||
assert.Equal(t, returnedIDP.Name, idp.Name)
|
||||
assert.Equal(t, returnedIDP.InstanceID, idp.InstanceID)
|
||||
assert.Equal(t, returnedIDP.State, idp.State)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListIDProvider(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
pool, stop, err := newEmbeddedDB(ctx)
|
||||
require.NoError(t, err)
|
||||
defer stop()
|
||||
organizationRepo := repository.OrganizationRepository(pool)
|
||||
|
||||
// create instance
|
||||
instanceId := gofakeit.Name()
|
||||
instance := domain.Instance{
|
||||
ID: instanceId,
|
||||
Name: gofakeit.Name(),
|
||||
DefaultOrgID: "defaultOrgId",
|
||||
IAMProjectID: "iamProject",
|
||||
ConsoleClientID: "consoleCLient",
|
||||
ConsoleAppID: "consoleApp",
|
||||
DefaultLanguage: "defaultLanguage",
|
||||
}
|
||||
instanceRepo := repository.InstanceRepository(pool)
|
||||
err = instanceRepo.Create(ctx, &instance)
|
||||
require.NoError(t, err)
|
||||
|
||||
type test struct {
|
||||
name string
|
||||
testFunc func(ctx context.Context, t *testing.T) []*domain.Organization
|
||||
conditionClauses []database.Condition
|
||||
noOrganizationReturned bool
|
||||
}
|
||||
tests := []test{
|
||||
{
|
||||
name: "happy path single organization no filter",
|
||||
testFunc: func(ctx context.Context, t *testing.T) []*domain.Organization {
|
||||
noOfOrganizations := 1
|
||||
organizations := make([]*domain.Organization, noOfOrganizations)
|
||||
for i := range noOfOrganizations {
|
||||
|
||||
org := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
|
||||
// create organization
|
||||
err := organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
organizations[i] = &org
|
||||
}
|
||||
|
||||
return organizations
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "happy path multiple organization no filter",
|
||||
testFunc: func(ctx context.Context, t *testing.T) []*domain.Organization {
|
||||
noOfOrganizations := 5
|
||||
organizations := make([]*domain.Organization, noOfOrganizations)
|
||||
for i := range noOfOrganizations {
|
||||
|
||||
org := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
|
||||
// create organization
|
||||
err := organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
organizations[i] = &org
|
||||
}
|
||||
|
||||
return organizations
|
||||
},
|
||||
},
|
||||
func() test {
|
||||
organizationId := gofakeit.Name()
|
||||
return test{
|
||||
name: "organization filter on id",
|
||||
testFunc: func(ctx context.Context, t *testing.T) []*domain.Organization {
|
||||
// create organization
|
||||
// this org is created as an additional org which should NOT
|
||||
// be returned in the results of this test case
|
||||
org := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
err = organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
noOfOrganizations := 1
|
||||
organizations := make([]*domain.Organization, noOfOrganizations)
|
||||
for i := range noOfOrganizations {
|
||||
|
||||
org := domain.Organization{
|
||||
ID: organizationId,
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
|
||||
// create organization
|
||||
err := organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
organizations[i] = &org
|
||||
}
|
||||
|
||||
return organizations
|
||||
},
|
||||
conditionClauses: []database.Condition{organizationRepo.IDCondition(organizationId)},
|
||||
}
|
||||
}(),
|
||||
{
|
||||
name: "multiple organization filter on state",
|
||||
testFunc: func(ctx context.Context, t *testing.T) []*domain.Organization {
|
||||
// create organization
|
||||
// this org is created as an additional org which should NOT
|
||||
// be returned in the results of this test case
|
||||
org := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
err = organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
noOfOrganizations := 5
|
||||
organizations := make([]*domain.Organization, noOfOrganizations)
|
||||
for i := range noOfOrganizations {
|
||||
|
||||
org := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId,
|
||||
State: domain.OrgStateInactive.String(),
|
||||
}
|
||||
|
||||
// create organization
|
||||
err := organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
organizations[i] = &org
|
||||
}
|
||||
|
||||
return organizations
|
||||
},
|
||||
conditionClauses: []database.Condition{organizationRepo.StateCondition(domain.OrgStateInactive)},
|
||||
},
|
||||
func() test {
|
||||
instanceId_2 := gofakeit.Name()
|
||||
return test{
|
||||
name: "multiple organization filter on instance",
|
||||
testFunc: func(ctx context.Context, t *testing.T) []*domain.Organization {
|
||||
// create instance 1
|
||||
instanceId_1 := gofakeit.Name()
|
||||
instance := domain.Instance{
|
||||
ID: instanceId_1,
|
||||
Name: gofakeit.Name(),
|
||||
DefaultOrgID: "defaultOrgId",
|
||||
IAMProjectID: "iamProject",
|
||||
ConsoleClientID: "consoleCLient",
|
||||
ConsoleAppID: "consoleApp",
|
||||
DefaultLanguage: "defaultLanguage",
|
||||
}
|
||||
instanceRepo := repository.InstanceRepository(pool)
|
||||
err = instanceRepo.Create(ctx, &instance)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// create organization
|
||||
// this org is created as an additional org which should NOT
|
||||
// be returned in the results of this test case
|
||||
org := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId_1,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
err = organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
// create instance 2
|
||||
instance_2 := domain.Instance{
|
||||
ID: instanceId_2,
|
||||
Name: gofakeit.Name(),
|
||||
DefaultOrgID: "defaultOrgId",
|
||||
IAMProjectID: "iamProject",
|
||||
ConsoleClientID: "consoleCLient",
|
||||
ConsoleAppID: "consoleApp",
|
||||
DefaultLanguage: "defaultLanguage",
|
||||
}
|
||||
err = instanceRepo.Create(ctx, &instance_2)
|
||||
assert.Nil(t, err)
|
||||
|
||||
noOfOrganizations := 5
|
||||
organizations := make([]*domain.Organization, noOfOrganizations)
|
||||
for i := range noOfOrganizations {
|
||||
|
||||
org := domain.Organization{
|
||||
ID: gofakeit.Name(),
|
||||
Name: gofakeit.Name(),
|
||||
InstanceID: instanceId_2,
|
||||
State: domain.OrgStateActive.String(),
|
||||
}
|
||||
|
||||
// create organization
|
||||
err := organizationRepo.Create(ctx, &org)
|
||||
require.NoError(t, err)
|
||||
|
||||
organizations[i] = &org
|
||||
}
|
||||
|
||||
return organizations
|
||||
},
|
||||
conditionClauses: []database.Condition{organizationRepo.InstanceIDCondition(instanceId_2)},
|
||||
}
|
||||
}(),
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Cleanup(func() {
|
||||
_, err := pool.Exec(ctx, "DELETE FROM zitadel.organizations")
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
organizations := tt.testFunc(ctx, t)
|
||||
|
||||
// check organization values
|
||||
returnedOrgs, err := organizationRepo.List(ctx,
|
||||
tt.conditionClauses...,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
if tt.noOrganizationReturned {
|
||||
assert.Nil(t, returnedOrgs)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, len(organizations), len(returnedOrgs))
|
||||
for i, org := range organizations {
|
||||
assert.Equal(t, returnedOrgs[i].ID, org.ID)
|
||||
assert.Equal(t, returnedOrgs[i].Name, org.Name)
|
||||
assert.Equal(t, returnedOrgs[i].InstanceID, org.InstanceID)
|
||||
assert.Equal(t, returnedOrgs[i].State, org.State)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -519,11 +519,6 @@ func TestGetOrganization(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
if org.Name == "non existent org" {
|
||||
assert.Nil(t, returnedOrg)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, returnedOrg.ID, org.ID)
|
||||
assert.Equal(t, returnedOrg.Name, org.Name)
|
||||
assert.Equal(t, returnedOrg.InstanceID, org.InstanceID)
|
||||
|
Reference in New Issue
Block a user