zitadel/internal/query/action_test.go

351 lines
9.4 KiB
Go
Raw Normal View History

fix(queries): actions prepare funcs (#2689) * chore(queries): test suite for prepare stmt funcs * test(queries): prepare project funcs * refactor: add comments * test: simlify expected sql, added possibility to add args to expected queries * test(queries): prepare funcs in org * chore(backend): correct modules * test(queries): org domain prepare funcs * test: correct name * refactor: file name * refactor: add table to login policy columns * chore(prepare_test): only add row to result if columns * test(queries): login policy prepare funcs * chore: add comments for configs * test(queries): prepare idp funcs * fix(queries): add table to password complexity policy cols * test(queries): password complexity policy prepare funcs * fix(queries): add table to password age policy cols * test(queries): password age policy prepare func * fix(queries): set cols on lockout policy * test(queries): lockout policy prepare funs * fix(queries): set table on privacy policy cols * test(queries): privacy policy prepare funcs * fix(queries): set table on org iam policy cols * fix(queries): correct table in org iam policy cols * test(queries): org iam policy prepare funcs * test(queries): prepare project grant funcs * refactor(queries): prepareProjectRoleQuery as func * test(queries): prepare project role funcs * test(queries): project grant check for nulls in joins * fix(queries): allow null values in project grant * refactor(queries): make toQuery private * test(queries): action prepare funcs * refactor: rename prepareFlowQuery to prepareFlowsQuery * test: generic count only if count in cols * refactor: remove param in prepareFlowQuery * fix(queries): correct left joins in action flows * test(queries): action flow prepare funcs
2021-11-16 14:04:22 +01:00
package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"time"
"github.com/caos/zitadel/internal/domain"
errs "github.com/caos/zitadel/internal/errors"
)
func Test_ActionPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareActionsQuery no result",
prepare: prepareActionsQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT zitadel.projections.actions.id,`+
` zitadel.projections.actions.creation_date,`+
` zitadel.projections.actions.change_date,`+
` zitadel.projections.actions.resource_owner,`+
` zitadel.projections.actions.sequence,`+
` zitadel.projections.actions.action_state,`+
` zitadel.projections.actions.name,`+
` zitadel.projections.actions.script,`+
` zitadel.projections.actions.timeout,`+
` zitadel.projections.actions.allowed_to_fail,`+
` COUNT(*) OVER ()`+
` FROM zitadel.projections.actions`),
nil,
nil,
),
},
object: &Actions{Actions: []*Action{}},
},
{
name: "prepareActionsQuery one result",
prepare: prepareActionsQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT zitadel.projections.actions.id,`+
` zitadel.projections.actions.creation_date,`+
` zitadel.projections.actions.change_date,`+
` zitadel.projections.actions.resource_owner,`+
` zitadel.projections.actions.sequence,`+
` zitadel.projections.actions.action_state,`+
` zitadel.projections.actions.name,`+
` zitadel.projections.actions.script,`+
` zitadel.projections.actions.timeout,`+
` zitadel.projections.actions.allowed_to_fail,`+
` COUNT(*) OVER ()`+
` FROM zitadel.projections.actions`),
[]string{
"id",
"creation_date",
"change_date",
"resource_owner",
"sequence",
"action_state",
"name",
"script",
"timeout",
"allowed_to_fail",
"count",
},
[][]driver.Value{
{
"id",
testNow,
testNow,
"ro",
uint64(20211109),
domain.ActionStateActive,
"action-name",
"script",
1 * time.Second,
true,
},
},
),
},
object: &Actions{
SearchResponse: SearchResponse{
Count: 1,
},
Actions: []*Action{
{
ID: "id",
CreationDate: testNow,
ChangeDate: testNow,
ResourceOwner: "ro",
State: domain.ActionStateActive,
Sequence: 20211109,
Name: "action-name",
Script: "script",
Timeout: 1 * time.Second,
AllowedToFail: true,
},
},
},
},
{
name: "prepareActionsQuery multiple result",
prepare: prepareActionsQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT zitadel.projections.actions.id,`+
` zitadel.projections.actions.creation_date,`+
` zitadel.projections.actions.change_date,`+
` zitadel.projections.actions.resource_owner,`+
` zitadel.projections.actions.sequence,`+
` zitadel.projections.actions.action_state,`+
` zitadel.projections.actions.name,`+
` zitadel.projections.actions.script,`+
` zitadel.projections.actions.timeout,`+
` zitadel.projections.actions.allowed_to_fail,`+
` COUNT(*) OVER ()`+
` FROM zitadel.projections.actions`),
[]string{
"id",
"creation_date",
"change_date",
"resource_owner",
"sequence",
"action_state",
"name",
"script",
"timeout",
"allowed_to_fail",
"count",
},
[][]driver.Value{
{
"id-1",
testNow,
testNow,
"ro",
uint64(20211109),
domain.ActionStateActive,
"action-name-1",
"script",
1 * time.Second,
true,
},
{
"id-2",
testNow,
testNow,
"ro",
uint64(20211109),
domain.ActionStateActive,
"action-name-2",
"script",
1 * time.Second,
true,
},
},
),
},
object: &Actions{
SearchResponse: SearchResponse{
Count: 2,
},
Actions: []*Action{
{
ID: "id-1",
CreationDate: testNow,
ChangeDate: testNow,
ResourceOwner: "ro",
State: domain.ActionStateActive,
Sequence: 20211109,
Name: "action-name-1",
Script: "script",
Timeout: 1 * time.Second,
AllowedToFail: true,
},
{
ID: "id-2",
CreationDate: testNow,
ChangeDate: testNow,
ResourceOwner: "ro",
State: domain.ActionStateActive,
Sequence: 20211109,
Name: "action-name-2",
Script: "script",
Timeout: 1 * time.Second,
AllowedToFail: true,
},
},
},
},
{
name: "prepareActionsQuery sql err",
prepare: prepareActionsQuery,
want: want{
sqlExpectations: mockQueryErr(
regexp.QuoteMeta(`SELECT zitadel.projections.actions.id,`+
` zitadel.projections.actions.creation_date,`+
` zitadel.projections.actions.change_date,`+
` zitadel.projections.actions.resource_owner,`+
` zitadel.projections.actions.sequence,`+
` zitadel.projections.actions.action_state,`+
` zitadel.projections.actions.name,`+
` zitadel.projections.actions.script,`+
` zitadel.projections.actions.timeout,`+
` zitadel.projections.actions.allowed_to_fail,`+
` COUNT(*) OVER ()`+
` FROM zitadel.projections.actions`),
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,
},
{
name: "prepareActionQuery no result",
prepare: prepareActionQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT zitadel.projections.actions.id,`+
` zitadel.projections.actions.creation_date,`+
` zitadel.projections.actions.change_date,`+
` zitadel.projections.actions.resource_owner,`+
` zitadel.projections.actions.sequence,`+
` zitadel.projections.actions.action_state,`+
` zitadel.projections.actions.name,`+
` zitadel.projections.actions.script,`+
` zitadel.projections.actions.timeout,`+
` zitadel.projections.actions.allowed_to_fail`+
` FROM zitadel.projections.actions`),
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
},
},
object: (*Action)(nil),
},
{
name: "prepareActionQuery found",
prepare: prepareActionQuery,
want: want{
sqlExpectations: mockQuery(
regexp.QuoteMeta(`SELECT zitadel.projections.actions.id,`+
` zitadel.projections.actions.creation_date,`+
` zitadel.projections.actions.change_date,`+
` zitadel.projections.actions.resource_owner,`+
` zitadel.projections.actions.sequence,`+
` zitadel.projections.actions.action_state,`+
` zitadel.projections.actions.name,`+
` zitadel.projections.actions.script,`+
` zitadel.projections.actions.timeout,`+
` zitadel.projections.actions.allowed_to_fail`+
` FROM zitadel.projections.actions`),
[]string{
"id",
"creation_date",
"change_date",
"resource_owner",
"sequence",
"action_state",
"name",
"script",
"timeout",
"allowed_to_fail",
},
[]driver.Value{
"id",
testNow,
testNow,
"ro",
uint64(20211109),
domain.ActionStateActive,
"action-name",
"script",
1 * time.Second,
true,
},
),
},
object: &Action{
ID: "id",
CreationDate: testNow,
ChangeDate: testNow,
ResourceOwner: "ro",
State: domain.ActionStateActive,
Sequence: 20211109,
Name: "action-name",
Script: "script",
Timeout: 1 * time.Second,
AllowedToFail: true,
},
},
{
name: "prepareActionQuery sql err",
prepare: prepareActionQuery,
want: want{
sqlExpectations: mockQueryErr(
regexp.QuoteMeta(`SELECT zitadel.projections.actions.id,`+
` zitadel.projections.actions.creation_date,`+
` zitadel.projections.actions.change_date,`+
` zitadel.projections.actions.resource_owner,`+
` zitadel.projections.actions.sequence,`+
` zitadel.projections.actions.action_state,`+
` zitadel.projections.actions.name,`+
` zitadel.projections.actions.script,`+
` zitadel.projections.actions.timeout,`+
` zitadel.projections.actions.allowed_to_fail`+
` FROM zitadel.projections.actions`),
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,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err)
})
}
}