2022-01-21 13:01:25 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2023-02-27 21:36:43 +00:00
|
|
|
"context"
|
2022-01-21 13:01:25 +00:00
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-02-27 21:36:43 +00:00
|
|
|
"reflect"
|
2022-01-21 13:01:25 +00:00
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-25 09:16:36 +00:00
|
|
|
sq "github.com/Masterminds/squirrel"
|
2022-03-23 08:02:39 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
errs "github.com/zitadel/zitadel/internal/errors"
|
2022-01-21 13:01:25 +00:00
|
|
|
)
|
|
|
|
|
2022-10-27 06:08:36 +00:00
|
|
|
var (
|
|
|
|
instanceQuery = `SELECT projections.instances.id,` +
|
|
|
|
` projections.instances.creation_date,` +
|
|
|
|
` projections.instances.change_date,` +
|
|
|
|
` projections.instances.sequence,` +
|
|
|
|
` projections.instances.default_org_id,` +
|
|
|
|
` projections.instances.iam_project_id,` +
|
|
|
|
` projections.instances.console_client_id,` +
|
|
|
|
` projections.instances.console_app_id,` +
|
|
|
|
` projections.instances.default_language` +
|
2023-02-27 21:36:43 +00:00
|
|
|
` FROM projections.instances` +
|
|
|
|
` AS OF SYSTEM TIME '-1 ms'`
|
2022-10-27 06:08:36 +00:00
|
|
|
instanceCols = []string{
|
|
|
|
"id",
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"sequence",
|
|
|
|
"default_org_id",
|
|
|
|
"iam_project_id",
|
|
|
|
"console_client_id",
|
|
|
|
"console_app_id",
|
|
|
|
"default_language",
|
|
|
|
}
|
|
|
|
instancesQuery = `SELECT f.count, f.id,` +
|
|
|
|
` projections.instances.creation_date,` +
|
|
|
|
` projections.instances.change_date,` +
|
|
|
|
` projections.instances.sequence,` +
|
|
|
|
` projections.instances.name,` +
|
|
|
|
` projections.instances.default_org_id,` +
|
|
|
|
` projections.instances.iam_project_id,` +
|
|
|
|
` projections.instances.console_client_id,` +
|
|
|
|
` projections.instances.console_app_id,` +
|
|
|
|
` projections.instances.default_language,` +
|
|
|
|
` projections.instance_domains.domain,` +
|
|
|
|
` projections.instance_domains.is_primary,` +
|
|
|
|
` projections.instance_domains.is_generated,` +
|
|
|
|
` projections.instance_domains.creation_date,` +
|
|
|
|
` projections.instance_domains.change_date, ` +
|
|
|
|
` projections.instance_domains.sequence` +
|
|
|
|
` FROM (SELECT projections.instances.id, COUNT(*) OVER () FROM projections.instances) AS f` +
|
2022-11-07 09:33:22 +00:00
|
|
|
` LEFT JOIN projections.instances ON f.id = projections.instances.id` +
|
2023-02-27 21:36:43 +00:00
|
|
|
` LEFT JOIN projections.instance_domains ON f.id = projections.instance_domains.instance_id` +
|
|
|
|
` AS OF SYSTEM TIME '-1 ms'`
|
2022-10-27 06:08:36 +00:00
|
|
|
instancesCols = []string{
|
|
|
|
"count",
|
|
|
|
"id",
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"sequence",
|
|
|
|
"name",
|
|
|
|
"default_org_id",
|
|
|
|
"iam_project_id",
|
|
|
|
"console_client_id",
|
|
|
|
"console_app_id",
|
|
|
|
"default_language",
|
|
|
|
"domain",
|
|
|
|
"is_primary",
|
|
|
|
"is_generated",
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"sequence",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func Test_InstancePrepares(t *testing.T) {
|
2022-01-21 13:01:25 +00:00
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2023-02-27 21:36:43 +00:00
|
|
|
name string
|
|
|
|
prepare interface{}
|
|
|
|
additionalArgs []reflect.Value
|
|
|
|
want want
|
|
|
|
object interface{}
|
2022-01-21 13:01:25 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-02-27 21:36:43 +00:00
|
|
|
name: "prepareInstanceQuery no result",
|
|
|
|
additionalArgs: []reflect.Value{reflect.ValueOf("")},
|
|
|
|
prepare: prepareInstanceQuery,
|
2022-01-21 13:01:25 +00:00
|
|
|
want: want{
|
2023-08-22 10:49:22 +00:00
|
|
|
sqlExpectations: mockQueriesScanErr(
|
2022-10-27 06:08:36 +00:00
|
|
|
regexp.QuoteMeta(instanceQuery),
|
2022-01-21 13:01:25 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !errs.IsNotFound(err) {
|
|
|
|
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
object: (*Instance)(nil),
|
2022-01-21 13:01:25 +00:00
|
|
|
},
|
|
|
|
{
|
2023-02-27 21:36:43 +00:00
|
|
|
name: "prepareInstanceQuery found",
|
|
|
|
additionalArgs: []reflect.Value{reflect.ValueOf("")},
|
|
|
|
prepare: prepareInstanceQuery,
|
2022-01-21 13:01:25 +00:00
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQuery(
|
2022-10-27 06:08:36 +00:00
|
|
|
regexp.QuoteMeta(instanceQuery),
|
|
|
|
instanceCols,
|
2022-01-21 13:01:25 +00:00
|
|
|
[]driver.Value{
|
|
|
|
"id",
|
|
|
|
testNow,
|
2022-04-21 10:37:39 +00:00
|
|
|
testNow,
|
2022-01-21 13:01:25 +00:00
|
|
|
uint64(20211108),
|
|
|
|
"global-org-id",
|
|
|
|
"project-id",
|
2022-03-29 09:53:19 +00:00
|
|
|
"client-id",
|
2022-04-14 12:19:18 +00:00
|
|
|
"app-id",
|
2022-02-16 15:49:17 +00:00
|
|
|
"en",
|
2022-01-21 13:01:25 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
object: &Instance{
|
2022-04-25 09:16:36 +00:00
|
|
|
ID: "id",
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
2022-06-03 12:30:39 +00:00
|
|
|
DefaultOrgID: "global-org-id",
|
2022-04-25 09:16:36 +00:00
|
|
|
IAMProjectID: "project-id",
|
|
|
|
ConsoleID: "client-id",
|
|
|
|
ConsoleAppID: "app-id",
|
|
|
|
DefaultLang: language.English,
|
2022-01-21 13:01:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-02-27 21:36:43 +00:00
|
|
|
name: "prepareInstanceQuery sql err",
|
|
|
|
additionalArgs: []reflect.Value{reflect.ValueOf("")},
|
|
|
|
prepare: prepareInstanceQuery,
|
2022-01-21 13:01:25 +00:00
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
2022-10-27 06:08:36 +00:00
|
|
|
regexp.QuoteMeta(instanceQuery),
|
2022-01-21 13:01:25 +00:00
|
|
|
sql.ErrConnDone,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !errors.Is(err, sql.ErrConnDone) {
|
|
|
|
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
2023-08-22 10:49:22 +00:00
|
|
|
object: (*Instance)(nil),
|
2022-01-21 13:01:25 +00:00
|
|
|
},
|
2022-06-08 11:46:24 +00:00
|
|
|
{
|
|
|
|
name: "prepareInstancesQuery no result",
|
2023-02-27 21:36:43 +00:00
|
|
|
prepare: func(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*Instances, error)) {
|
|
|
|
filter, query, scan := prepareInstancesQuery(ctx, db)
|
2022-06-08 11:46:24 +00:00
|
|
|
return query(filter), scan
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2022-10-27 06:08:36 +00:00
|
|
|
regexp.QuoteMeta(instancesQuery),
|
2022-06-08 11:46:24 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Instances{Instances: []*Instance{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareInstancesQuery one result",
|
2023-02-27 21:36:43 +00:00
|
|
|
prepare: func(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*Instances, error)) {
|
|
|
|
filter, query, scan := prepareInstancesQuery(ctx, db)
|
2022-06-08 11:46:24 +00:00
|
|
|
return query(filter), scan
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2022-10-27 06:08:36 +00:00
|
|
|
regexp.QuoteMeta(instancesQuery),
|
|
|
|
instancesCols,
|
2022-06-08 11:46:24 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
"1",
|
|
|
|
"id",
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
"test",
|
|
|
|
"global-org-id",
|
|
|
|
"project-id",
|
|
|
|
"client-id",
|
|
|
|
"app-id",
|
|
|
|
"en",
|
|
|
|
"test.zitadel.cloud",
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Instances{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Instances: []*Instance{
|
|
|
|
{
|
|
|
|
ID: "id",
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
|
|
|
Name: "test",
|
|
|
|
DefaultOrgID: "global-org-id",
|
|
|
|
IAMProjectID: "project-id",
|
|
|
|
ConsoleID: "client-id",
|
|
|
|
ConsoleAppID: "app-id",
|
|
|
|
DefaultLang: language.English,
|
|
|
|
Domains: []*InstanceDomain{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
|
|
|
InstanceID: "id",
|
|
|
|
Domain: "test.zitadel.cloud",
|
|
|
|
IsGenerated: true,
|
|
|
|
IsPrimary: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareInstancesQuery multiple results",
|
2023-02-27 21:36:43 +00:00
|
|
|
prepare: func(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*Instances, error)) {
|
|
|
|
filter, query, scan := prepareInstancesQuery(ctx, db)
|
2022-06-08 11:46:24 +00:00
|
|
|
return query(filter), scan
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
2022-10-27 06:08:36 +00:00
|
|
|
regexp.QuoteMeta(instancesQuery),
|
|
|
|
instancesCols,
|
2022-06-08 11:46:24 +00:00
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
2,
|
|
|
|
"id",
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
"test",
|
|
|
|
"global-org-id",
|
|
|
|
"project-id",
|
|
|
|
"client-id",
|
|
|
|
"app-id",
|
|
|
|
"en",
|
|
|
|
"test.zitadel.cloud",
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
2,
|
|
|
|
"id",
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
"test",
|
|
|
|
"global-org-id",
|
|
|
|
"project-id",
|
|
|
|
"client-id",
|
|
|
|
"app-id",
|
|
|
|
"en",
|
|
|
|
"zitadel.cloud",
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
2,
|
|
|
|
"id2",
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
"test2",
|
|
|
|
"global-org-id",
|
|
|
|
"project-id",
|
|
|
|
"client-id",
|
|
|
|
"app-id",
|
|
|
|
"en",
|
|
|
|
"test2.zitadel.cloud",
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211108),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Instances{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 2,
|
|
|
|
},
|
|
|
|
Instances: []*Instance{
|
|
|
|
{
|
|
|
|
ID: "id",
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
|
|
|
Name: "test",
|
|
|
|
DefaultOrgID: "global-org-id",
|
|
|
|
IAMProjectID: "project-id",
|
|
|
|
ConsoleID: "client-id",
|
|
|
|
ConsoleAppID: "app-id",
|
|
|
|
DefaultLang: language.English,
|
|
|
|
Domains: []*InstanceDomain{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
|
|
|
Domain: "test.zitadel.cloud",
|
|
|
|
InstanceID: "id",
|
|
|
|
IsGenerated: true,
|
|
|
|
IsPrimary: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
|
|
|
Domain: "zitadel.cloud",
|
|
|
|
InstanceID: "id",
|
|
|
|
IsGenerated: false,
|
|
|
|
IsPrimary: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
ID: "id2",
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
|
|
|
Name: "test2",
|
|
|
|
DefaultOrgID: "global-org-id",
|
|
|
|
IAMProjectID: "project-id",
|
|
|
|
ConsoleID: "client-id",
|
|
|
|
ConsoleAppID: "app-id",
|
|
|
|
DefaultLang: language.English,
|
|
|
|
Domains: []*InstanceDomain{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211108,
|
|
|
|
Domain: "test2.zitadel.cloud",
|
|
|
|
InstanceID: "id2",
|
|
|
|
IsGenerated: true,
|
|
|
|
IsPrimary: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareInstancesQuery sql err",
|
2023-02-27 21:36:43 +00:00
|
|
|
prepare: func(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*Instances, error)) {
|
|
|
|
filter, query, scan := prepareInstancesQuery(ctx, db)
|
2022-06-08 11:46:24 +00:00
|
|
|
return query(filter), scan
|
|
|
|
},
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
2022-10-27 06:08:36 +00:00
|
|
|
regexp.QuoteMeta(instancesQuery),
|
2022-06-08 11:46:24 +00:00
|
|
|
sql.ErrConnDone,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !errors.Is(err, sql.ErrConnDone) {
|
|
|
|
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
object: nil,
|
|
|
|
},
|
2022-01-21 13:01:25 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-27 21:36:43 +00:00
|
|
|
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, append(defaultPrepareArgs, tt.additionalArgs...)...)
|
2022-01-21 13:01:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|