2025-06-17 09:46:01 +02:00
|
|
|
package repository_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/brianvoe/gofakeit/v6"
|
2025-07-14 21:27:14 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2025-06-17 09:46:01 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateInstance(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
testFunc func(ctx context.Context, t *testing.T) *domain.Instance
|
|
|
|
instance domain.Instance
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "happy path",
|
|
|
|
instance: func() domain.Instance {
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
instance := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
return instance
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "create instance without name",
|
|
|
|
instance: func() domain.Instance {
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
// instanceName := gofakeit.Name()
|
|
|
|
instance := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: "",
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
return instance
|
|
|
|
}(),
|
2025-07-17 09:08:33 +02:00
|
|
|
err: new(database.CheckError),
|
2025-06-17 09:46:01 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "adding same instance twice",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) *domain.Instance {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
2025-07-14 21:27:14 +02:00
|
|
|
// change the name to make sure same only the id clashes
|
|
|
|
inst.Name = gofakeit.Name()
|
2025-06-17 09:46:01 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
return &inst
|
|
|
|
},
|
2025-07-17 09:08:33 +02:00
|
|
|
err: new(database.UniqueError),
|
2025-06-17 09:46:01 +02:00
|
|
|
},
|
2025-07-14 21:27:14 +02:00
|
|
|
func() struct {
|
|
|
|
name string
|
|
|
|
testFunc func(ctx context.Context, t *testing.T) *domain.Instance
|
|
|
|
instance domain.Instance
|
|
|
|
err error
|
|
|
|
} {
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
return struct {
|
|
|
|
name string
|
|
|
|
testFunc func(ctx context.Context, t *testing.T) *domain.Instance
|
|
|
|
instance domain.Instance
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
name: "adding instance with same name twice",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) *domain.Instance {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: gofakeit.Name(),
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// change the id
|
|
|
|
inst.ID = instanceId
|
|
|
|
return &inst
|
|
|
|
},
|
|
|
|
instance: domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
},
|
|
|
|
// two instances can have the sane name
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
}(),
|
2025-06-17 09:46:01 +02:00
|
|
|
{
|
|
|
|
name: "adding instance with no id",
|
|
|
|
instance: func() domain.Instance {
|
|
|
|
// instanceId := gofakeit.Name()
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
instance := domain.Instance{
|
|
|
|
// ID: instanceId,
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
return instance
|
|
|
|
}(),
|
2025-07-17 09:08:33 +02:00
|
|
|
err: new(database.CheckError),
|
2025-06-17 09:46:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var instance *domain.Instance
|
|
|
|
if tt.testFunc != nil {
|
|
|
|
instance = tt.testFunc(ctx, t)
|
|
|
|
} else {
|
|
|
|
instance = &tt.instance
|
|
|
|
}
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
beforeCreate := time.Now()
|
|
|
|
err := instanceRepo.Create(ctx, instance)
|
2025-07-17 00:54:21 +02:00
|
|
|
assert.ErrorIs(t, err, tt.err)
|
2025-06-17 09:46:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
afterCreate := time.Now()
|
|
|
|
|
|
|
|
// check instance values
|
|
|
|
instance, err = instanceRepo.Get(ctx,
|
2025-07-14 21:27:14 +02:00
|
|
|
instance.ID,
|
2025-06-17 09:46:01 +02:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, tt.instance.ID, instance.ID)
|
|
|
|
assert.Equal(t, tt.instance.Name, instance.Name)
|
|
|
|
assert.Equal(t, tt.instance.DefaultOrgID, instance.DefaultOrgID)
|
|
|
|
assert.Equal(t, tt.instance.IAMProjectID, instance.IAMProjectID)
|
|
|
|
assert.Equal(t, tt.instance.ConsoleClientID, instance.ConsoleClientID)
|
|
|
|
assert.Equal(t, tt.instance.ConsoleAppID, instance.ConsoleAppID)
|
|
|
|
assert.Equal(t, tt.instance.DefaultLanguage, instance.DefaultLanguage)
|
|
|
|
assert.WithinRange(t, instance.CreatedAt, beforeCreate, afterCreate)
|
|
|
|
assert.WithinRange(t, instance.UpdatedAt, beforeCreate, afterCreate)
|
2025-06-17 09:46:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateInstance(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
testFunc func(ctx context.Context, t *testing.T) *domain.Instance
|
|
|
|
rowsAffected int64
|
2025-07-14 21:27:14 +02:00
|
|
|
getErr error
|
2025-06-17 09:46:01 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "happy path",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) *domain.Instance {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return &inst
|
|
|
|
},
|
|
|
|
rowsAffected: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update deleted instance",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) *domain.Instance {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// delete instance
|
2025-07-14 21:27:14 +02:00
|
|
|
affectedRows, err := instanceRepo.Delete(ctx,
|
|
|
|
inst.ID,
|
2025-06-17 09:46:01 +02:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, int64(1), affectedRows)
|
2025-06-17 09:46:01 +02:00
|
|
|
|
|
|
|
return &inst
|
|
|
|
},
|
|
|
|
rowsAffected: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update non existent instance",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) *domain.Instance {
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
}
|
|
|
|
return &inst
|
|
|
|
},
|
|
|
|
rowsAffected: 0,
|
2025-07-17 09:08:33 +02:00
|
|
|
getErr: new(database.NoRowFoundError),
|
2025-06-17 09:46:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
|
|
|
|
instance := tt.testFunc(ctx, t)
|
|
|
|
|
|
|
|
beforeUpdate := time.Now()
|
|
|
|
// update name
|
|
|
|
newName := "new_" + instance.Name
|
|
|
|
rowsAffected, err := instanceRepo.Update(ctx,
|
2025-07-14 21:27:14 +02:00
|
|
|
instance.ID,
|
2025-06-17 09:46:01 +02:00
|
|
|
instanceRepo.SetName(newName),
|
|
|
|
)
|
|
|
|
afterUpdate := time.Now()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, tt.rowsAffected, rowsAffected)
|
2025-06-17 09:46:01 +02:00
|
|
|
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check instance values
|
|
|
|
instance, err = instanceRepo.Get(ctx,
|
2025-07-14 21:27:14 +02:00
|
|
|
instance.ID,
|
2025-06-17 09:46:01 +02:00
|
|
|
)
|
2025-07-14 21:27:14 +02:00
|
|
|
require.Equal(t, tt.getErr, err)
|
2025-06-17 09:46:01 +02:00
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, newName, instance.Name)
|
|
|
|
assert.WithinRange(t, instance.UpdatedAt, beforeUpdate, afterUpdate)
|
2025-06-17 09:46:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetInstance(t *testing.T) {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
type test struct {
|
2025-07-14 21:27:14 +02:00
|
|
|
name string
|
|
|
|
testFunc func(ctx context.Context, t *testing.T) *domain.Instance
|
|
|
|
err error
|
2025-06-17 09:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []test{
|
|
|
|
func() test {
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
return test{
|
|
|
|
name: "happy path get using id",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) *domain.Instance {
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return &inst
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
{
|
|
|
|
name: "get non existent instance",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) *domain.Instance {
|
2025-07-14 21:27:14 +02:00
|
|
|
inst := domain.Instance{
|
|
|
|
ID: "get non existent instance",
|
2025-06-17 09:46:01 +02:00
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
return &inst
|
2025-06-17 09:46:01 +02:00
|
|
|
},
|
2025-07-17 09:08:33 +02:00
|
|
|
err: new(database.NoRowFoundError),
|
2025-06-17 09:46:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
|
|
|
|
var instance *domain.Instance
|
|
|
|
if tt.testFunc != nil {
|
|
|
|
instance = tt.testFunc(ctx, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check instance values
|
|
|
|
returnedInstance, err := instanceRepo.Get(ctx,
|
2025-07-14 21:27:14 +02:00
|
|
|
instance.ID,
|
2025-06-17 09:46:01 +02:00
|
|
|
)
|
2025-07-14 21:27:14 +02:00
|
|
|
if tt.err != nil {
|
2025-07-17 00:54:21 +02:00
|
|
|
require.ErrorIs(t, err, tt.err)
|
2025-07-14 21:27:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if instance.ID == "get non existent instance" {
|
|
|
|
assert.Nil(t, returnedInstance)
|
2025-06-17 09:46:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, returnedInstance.ID, instance.ID)
|
|
|
|
assert.Equal(t, returnedInstance.Name, instance.Name)
|
|
|
|
assert.Equal(t, returnedInstance.DefaultOrgID, instance.DefaultOrgID)
|
|
|
|
assert.Equal(t, returnedInstance.IAMProjectID, instance.IAMProjectID)
|
|
|
|
assert.Equal(t, returnedInstance.ConsoleClientID, instance.ConsoleClientID)
|
|
|
|
assert.Equal(t, returnedInstance.ConsoleAppID, instance.ConsoleAppID)
|
|
|
|
assert.Equal(t, returnedInstance.DefaultLanguage, instance.DefaultLanguage)
|
2025-06-17 09:46:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListInstance(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
pool, stop, err := newEmbeddedDB(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
testFunc func(ctx context.Context, t *testing.T) []*domain.Instance
|
|
|
|
conditionClauses []database.Condition
|
|
|
|
noInstanceReturned bool
|
|
|
|
}
|
|
|
|
tests := []test{
|
|
|
|
{
|
|
|
|
name: "happy path single instance no filter",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) []*domain.Instance {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
noOfInstances := 1
|
|
|
|
instances := make([]*domain.Instance, noOfInstances)
|
|
|
|
for i := range noOfInstances {
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: gofakeit.Name(),
|
|
|
|
Name: gofakeit.Name(),
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
instances[i] = &inst
|
|
|
|
}
|
|
|
|
|
|
|
|
return instances
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "happy path multiple instance no filter",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) []*domain.Instance {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
noOfInstances := 5
|
|
|
|
instances := make([]*domain.Instance, noOfInstances)
|
|
|
|
for i := range noOfInstances {
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: gofakeit.Name(),
|
|
|
|
Name: gofakeit.Name(),
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
instances[i] = &inst
|
|
|
|
}
|
|
|
|
|
|
|
|
return instances
|
|
|
|
},
|
|
|
|
},
|
|
|
|
func() test {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
instanceId := gofakeit.Name()
|
|
|
|
return test{
|
|
|
|
name: "instance filter on id",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) []*domain.Instance {
|
|
|
|
noOfInstances := 1
|
|
|
|
instances := make([]*domain.Instance, noOfInstances)
|
|
|
|
for i := range noOfInstances {
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: gofakeit.Name(),
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
instances[i] = &inst
|
|
|
|
}
|
|
|
|
|
|
|
|
return instances
|
|
|
|
},
|
|
|
|
conditionClauses: []database.Condition{instanceRepo.IDCondition(instanceId)},
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
func() test {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
return test{
|
|
|
|
name: "multiple instance filter on name",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) []*domain.Instance {
|
|
|
|
noOfInstances := 5
|
|
|
|
instances := make([]*domain.Instance, noOfInstances)
|
|
|
|
for i := range noOfInstances {
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: gofakeit.Name(),
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
instances[i] = &inst
|
|
|
|
}
|
|
|
|
|
|
|
|
return instances
|
|
|
|
},
|
|
|
|
conditionClauses: []database.Condition{instanceRepo.NameCondition(database.TextOperationEqual, instanceName)},
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_, err := pool.Exec(ctx, "DELETE FROM zitadel.instances")
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
instances := tt.testFunc(ctx, t)
|
|
|
|
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
|
|
|
|
// check instance values
|
|
|
|
returnedInstances, err := instanceRepo.List(ctx,
|
|
|
|
tt.conditionClauses...,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if tt.noInstanceReturned {
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Nil(t, returnedInstances)
|
2025-06-17 09:46:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, len(instances), len(returnedInstances))
|
2025-06-17 09:46:01 +02:00
|
|
|
for i, instance := range instances {
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, returnedInstances[i].ID, instance.ID)
|
|
|
|
assert.Equal(t, returnedInstances[i].Name, instance.Name)
|
|
|
|
assert.Equal(t, returnedInstances[i].DefaultOrgID, instance.DefaultOrgID)
|
|
|
|
assert.Equal(t, returnedInstances[i].IAMProjectID, instance.IAMProjectID)
|
|
|
|
assert.Equal(t, returnedInstances[i].ConsoleClientID, instance.ConsoleClientID)
|
|
|
|
assert.Equal(t, returnedInstances[i].ConsoleAppID, instance.ConsoleAppID)
|
|
|
|
assert.Equal(t, returnedInstances[i].DefaultLanguage, instance.DefaultLanguage)
|
2025-06-17 09:46:01 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteInstance(t *testing.T) {
|
|
|
|
type test struct {
|
2025-07-14 21:27:14 +02:00
|
|
|
name string
|
|
|
|
testFunc func(ctx context.Context, t *testing.T)
|
|
|
|
instanceID string
|
|
|
|
noOfDeletedRows int64
|
2025-06-17 09:46:01 +02:00
|
|
|
}
|
|
|
|
tests := []test{
|
|
|
|
func() test {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
instanceId := gofakeit.Name()
|
2025-07-14 21:27:14 +02:00
|
|
|
var noOfInstances int64 = 1
|
2025-06-17 09:46:01 +02:00
|
|
|
return test{
|
|
|
|
name: "happy path delete single instance filter id",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) {
|
|
|
|
instances := make([]*domain.Instance, noOfInstances)
|
|
|
|
for i := range noOfInstances {
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: instanceId,
|
|
|
|
Name: gofakeit.Name(),
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
instances[i] = &inst
|
|
|
|
}
|
|
|
|
},
|
2025-07-14 21:27:14 +02:00
|
|
|
instanceID: instanceId,
|
|
|
|
noOfDeletedRows: noOfInstances,
|
2025-06-17 09:46:01 +02:00
|
|
|
}
|
|
|
|
}(),
|
|
|
|
func() test {
|
|
|
|
non_existent_instance_name := gofakeit.Name()
|
|
|
|
return test{
|
2025-07-14 21:27:14 +02:00
|
|
|
name: "delete non existent instance",
|
|
|
|
instanceID: non_existent_instance_name,
|
2025-06-17 09:46:01 +02:00
|
|
|
}
|
|
|
|
}(),
|
|
|
|
func() test {
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
instanceName := gofakeit.Name()
|
|
|
|
return test{
|
|
|
|
name: "deleted already deleted instance",
|
|
|
|
testFunc: func(ctx context.Context, t *testing.T) {
|
|
|
|
noOfInstances := 1
|
|
|
|
instances := make([]*domain.Instance, noOfInstances)
|
|
|
|
for i := range noOfInstances {
|
|
|
|
|
|
|
|
inst := domain.Instance{
|
|
|
|
ID: gofakeit.Name(),
|
|
|
|
Name: instanceName,
|
|
|
|
DefaultOrgID: "defaultOrgId",
|
|
|
|
IAMProjectID: "iamProject",
|
|
|
|
ConsoleClientID: "consoleCLient",
|
|
|
|
ConsoleAppID: "consoleApp",
|
|
|
|
DefaultLanguage: "defaultLanguage",
|
|
|
|
}
|
|
|
|
|
|
|
|
// create instance
|
|
|
|
err := instanceRepo.Create(ctx, &inst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
instances[i] = &inst
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete instance
|
2025-07-14 21:27:14 +02:00
|
|
|
affectedRows, err := instanceRepo.Delete(ctx,
|
|
|
|
instances[0].ID,
|
2025-06-17 09:46:01 +02:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, int64(1), affectedRows)
|
2025-06-17 09:46:01 +02:00
|
|
|
},
|
2025-07-14 21:27:14 +02:00
|
|
|
instanceID: instanceName,
|
|
|
|
// this test should return 0 affected rows as the instance was already deleted
|
|
|
|
noOfDeletedRows: 0,
|
2025-06-17 09:46:01 +02:00
|
|
|
}
|
|
|
|
}(),
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
instanceRepo := repository.InstanceRepository(pool)
|
|
|
|
|
|
|
|
if tt.testFunc != nil {
|
|
|
|
tt.testFunc(ctx, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete instance
|
2025-07-14 21:27:14 +02:00
|
|
|
noOfDeletedRows, err := instanceRepo.Delete(ctx,
|
|
|
|
tt.instanceID,
|
2025-06-17 09:46:01 +02:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Equal(t, noOfDeletedRows, tt.noOfDeletedRows)
|
2025-06-17 09:46:01 +02:00
|
|
|
|
|
|
|
// check instance was deleted
|
|
|
|
instance, err := instanceRepo.Get(ctx,
|
2025-07-14 21:27:14 +02:00
|
|
|
tt.instanceID,
|
2025-06-17 09:46:01 +02:00
|
|
|
)
|
2025-07-17 09:08:33 +02:00
|
|
|
require.ErrorIs(t, err, new(database.NoRowFoundError))
|
2025-07-14 21:27:14 +02:00
|
|
|
assert.Nil(t, instance)
|
2025-06-17 09:46:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|