2024-03-14 09:56:23 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2024-05-04 09:55:57 +00:00
|
|
|
exec "github.com/zitadel/zitadel/internal/repository/execution"
|
2024-03-14 09:56:23 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-05-04 09:55:57 +00:00
|
|
|
prepareExecutionsStmt = `SELECT projections.executions1.instance_id,` +
|
|
|
|
` projections.executions1.id,` +
|
2024-08-12 20:32:01 +00:00
|
|
|
` projections.executions1.creation_date,` +
|
2024-05-04 09:55:57 +00:00
|
|
|
` projections.executions1.change_date,` +
|
|
|
|
` execution_targets.targets,` +
|
2024-03-14 09:56:23 +00:00
|
|
|
` COUNT(*) OVER ()` +
|
2024-05-04 09:55:57 +00:00
|
|
|
` FROM projections.executions1` +
|
|
|
|
` JOIN (` +
|
|
|
|
`SELECT instance_id, execution_id, JSONB_AGG( JSON_OBJECT( 'position' : position, 'include' : include, 'target' : target_id ) ) as targets` +
|
|
|
|
` FROM projections.executions1_targets` +
|
|
|
|
` GROUP BY instance_id, execution_id` +
|
|
|
|
`)` +
|
|
|
|
` AS execution_targets` +
|
|
|
|
` ON execution_targets.instance_id = projections.executions1.instance_id` +
|
|
|
|
` AND execution_targets.execution_id = projections.executions1.id`
|
2024-03-14 09:56:23 +00:00
|
|
|
prepareExecutionsCols = []string{
|
2024-05-04 09:55:57 +00:00
|
|
|
"instance_id",
|
2024-03-14 09:56:23 +00:00
|
|
|
"id",
|
2024-08-12 20:32:01 +00:00
|
|
|
"creation_date",
|
2024-03-14 09:56:23 +00:00
|
|
|
"change_date",
|
|
|
|
"targets",
|
|
|
|
"count",
|
|
|
|
}
|
|
|
|
|
2024-05-04 09:55:57 +00:00
|
|
|
prepareExecutionStmt = `SELECT projections.executions1.instance_id,` +
|
|
|
|
` projections.executions1.id,` +
|
2024-08-12 20:32:01 +00:00
|
|
|
` projections.executions1.creation_date,` +
|
2024-05-04 09:55:57 +00:00
|
|
|
` projections.executions1.change_date,` +
|
|
|
|
` execution_targets.targets` +
|
|
|
|
` FROM projections.executions1` +
|
|
|
|
` JOIN (` +
|
|
|
|
`SELECT instance_id, execution_id, JSONB_AGG( JSON_OBJECT( 'position' : position, 'include' : include, 'target' : target_id ) ) as targets` +
|
|
|
|
` FROM projections.executions1_targets` +
|
|
|
|
` GROUP BY instance_id, execution_id` +
|
|
|
|
`)` +
|
|
|
|
` AS execution_targets` +
|
|
|
|
` ON execution_targets.instance_id = projections.executions1.instance_id` +
|
|
|
|
` AND execution_targets.execution_id = projections.executions1.id`
|
2024-03-14 09:56:23 +00:00
|
|
|
prepareExecutionCols = []string{
|
2024-05-04 09:55:57 +00:00
|
|
|
"instance_id",
|
2024-03-14 09:56:23 +00:00
|
|
|
"id",
|
2024-08-12 20:32:01 +00:00
|
|
|
"creation_date",
|
2024-03-14 09:56:23 +00:00
|
|
|
"change_date",
|
|
|
|
"targets",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_ExecutionPrepares(t *testing.T) {
|
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prepare interface{}
|
|
|
|
want want
|
|
|
|
object interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "prepareExecutionsQuery no result",
|
|
|
|
prepare: prepareExecutionsQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
regexp.QuoteMeta(prepareExecutionsStmt),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Executions{Executions: []*Execution{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareExecutionsQuery one result",
|
|
|
|
prepare: prepareExecutionsQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
regexp.QuoteMeta(prepareExecutionsStmt),
|
|
|
|
prepareExecutionsCols,
|
|
|
|
[][]driver.Value{
|
|
|
|
{
|
2024-05-04 09:55:57 +00:00
|
|
|
"ro",
|
2024-03-14 09:56:23 +00:00
|
|
|
"id",
|
|
|
|
testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
testNow,
|
2024-05-04 09:55:57 +00:00
|
|
|
[]byte(`[{"position" : 1, "target" : "target"}, {"position" : 2, "include" : "include"}]`),
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Executions{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Executions: []*Execution{
|
|
|
|
{
|
|
|
|
ObjectDetails: domain.ObjectDetails{
|
|
|
|
EventDate: testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
CreationDate: testNow,
|
2024-03-14 09:56:23 +00:00
|
|
|
ResourceOwner: "ro",
|
2024-08-12 20:32:01 +00:00
|
|
|
ID: "id",
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
2024-05-04 09:55:57 +00:00
|
|
|
Targets: []*exec.Target{
|
|
|
|
{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
|
|
|
|
{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
|
|
|
|
},
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareExecutionsQuery multiple result",
|
|
|
|
prepare: prepareExecutionsQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
regexp.QuoteMeta(prepareExecutionsStmt),
|
|
|
|
prepareExecutionsCols,
|
|
|
|
[][]driver.Value{
|
|
|
|
{
|
2024-05-04 09:55:57 +00:00
|
|
|
"ro",
|
2024-03-14 09:56:23 +00:00
|
|
|
"id-1",
|
|
|
|
testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
testNow,
|
2024-05-04 09:55:57 +00:00
|
|
|
[]byte(`[{"position" : 1, "target" : "target"}, {"position" : 2, "include" : "include"}]`),
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-04 09:55:57 +00:00
|
|
|
"ro",
|
2024-03-14 09:56:23 +00:00
|
|
|
"id-2",
|
|
|
|
testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
testNow,
|
2024-05-04 09:55:57 +00:00
|
|
|
[]byte(`[{"position" : 2, "target" : "target"}, {"position" : 1, "include" : "include"}]`),
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Executions{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 2,
|
|
|
|
},
|
|
|
|
Executions: []*Execution{
|
|
|
|
{
|
|
|
|
ObjectDetails: domain.ObjectDetails{
|
2024-08-12 20:32:01 +00:00
|
|
|
ID: "id-1",
|
2024-03-14 09:56:23 +00:00
|
|
|
EventDate: testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
CreationDate: testNow,
|
2024-03-14 09:56:23 +00:00
|
|
|
ResourceOwner: "ro",
|
|
|
|
},
|
2024-05-04 09:55:57 +00:00
|
|
|
Targets: []*exec.Target{
|
|
|
|
{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
|
|
|
|
{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
|
|
|
|
},
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectDetails: domain.ObjectDetails{
|
2024-08-12 20:32:01 +00:00
|
|
|
ID: "id-2",
|
2024-03-14 09:56:23 +00:00
|
|
|
EventDate: testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
CreationDate: testNow,
|
2024-03-14 09:56:23 +00:00
|
|
|
ResourceOwner: "ro",
|
|
|
|
},
|
2024-05-04 09:55:57 +00:00
|
|
|
Targets: []*exec.Target{
|
|
|
|
{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
|
|
|
|
{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
|
|
|
|
},
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareExecutionsQuery sql err",
|
|
|
|
prepare: prepareExecutionsQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
regexp.QuoteMeta(prepareExecutionsStmt),
|
|
|
|
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: (*Execution)(nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareExecutionQuery no result",
|
|
|
|
prepare: prepareExecutionQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueriesScanErr(
|
|
|
|
regexp.QuoteMeta(prepareExecutionStmt),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !zerrors.IsNotFound(err) {
|
|
|
|
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
object: (*Execution)(nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareExecutionQuery found",
|
|
|
|
prepare: prepareExecutionQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQuery(
|
|
|
|
regexp.QuoteMeta(prepareExecutionStmt),
|
|
|
|
prepareExecutionCols,
|
|
|
|
[]driver.Value{
|
2024-05-04 09:55:57 +00:00
|
|
|
"ro",
|
2024-03-14 09:56:23 +00:00
|
|
|
"id",
|
|
|
|
testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
testNow,
|
2024-05-04 09:55:57 +00:00
|
|
|
[]byte(`[{"position" : 1, "target" : "target"}, {"position" : 2, "include" : "include"}]`),
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Execution{
|
|
|
|
ObjectDetails: domain.ObjectDetails{
|
2024-08-12 20:32:01 +00:00
|
|
|
ID: "id",
|
2024-03-14 09:56:23 +00:00
|
|
|
EventDate: testNow,
|
2024-08-12 20:32:01 +00:00
|
|
|
CreationDate: testNow,
|
2024-03-14 09:56:23 +00:00
|
|
|
ResourceOwner: "ro",
|
|
|
|
},
|
2024-05-04 09:55:57 +00:00
|
|
|
Targets: []*exec.Target{
|
|
|
|
{Type: domain.ExecutionTargetTypeTarget, Target: "target"},
|
|
|
|
{Type: domain.ExecutionTargetTypeInclude, Target: "include"},
|
|
|
|
},
|
2024-03-14 09:56:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareExecutionQuery sql err",
|
|
|
|
prepare: prepareExecutionQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
regexp.QuoteMeta(prepareExecutionStmt),
|
|
|
|
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: (*Execution)(nil),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|