zitadel/internal/query/user_membership_test.go
Livio Amstutz 958362e6c9
feat: handle instance from context (#3382)
* commander

* commander

* selber!

* move to packages

* fix(errors): implement Is interface

* test: command

* test: commands

* add init steps

* setup tenant

* add default step yaml

* possibility to set password

* merge v2 into v2-commander

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: search query builder can filter events in memory

* fix: filters for add member

* fix(setup): add `ExternalSecure` to config

* chore: name iam to instance

* fix: matching

* remove unsued func

* base url

* base url

* test(command): filter funcs

* test: commands

* fix: rename orgiampolicy to domain policy

* start from init

* commands

* config

* fix indexes and add constraints

* fixes

* fix: merge conflicts

* fix: protos

* fix: md files

* setup

* add deprecated org iam policy again

* typo

* fix search query

* fix filter

* Apply suggestions from code review

* remove custom org from org setup

* add todos for verification

* change apps creation

* simplify package structure

* fix error

* move preparation helper for tests

* fix unique constraints

* fix config mapping in setup

* fix error handling in encryption_keys.go

* fix projection config

* fix query from old views to projection

* fix setup of mgmt api

* set iam project and fix instance projection

* fix tokens view

* fix steps.yaml and defaults.yaml

* fix projections

* change instance context to interface

* instance interceptors and additional events in setup

* cleanup

* tests for interceptors

* fix label policy

* add todo

* single api endpoint in environment.json

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-29 11:53:19 +02:00

435 lines
9.6 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"github.com/lib/pq"
)
var (
membershipsStmt = regexp.QuoteMeta(
"SELECT memberships.user_id" +
", memberships.roles" +
", memberships.creation_date" +
", memberships.change_date" +
", memberships.sequence" +
", memberships.resource_owner" +
", memberships.org_id" +
", memberships.id" +
", memberships.project_id" +
", memberships.grant_id" +
", projections.projects.name" +
", projections.orgs.name" +
", COUNT(*) OVER ()" +
" FROM (" +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.instance_id" +
", members.org_id" +
", NULL::STRING AS id" +
", NULL::STRING AS project_id" +
", NULL::STRING AS grant_id" +
" FROM projections.org_members as members" +
" UNION ALL " +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.instance_id" +
", NULL::STRING AS org_id" +
", members.id" +
", NULL::STRING AS project_id" +
", NULL::STRING AS grant_id" +
" FROM projections.instance_members as members" +
" UNION ALL " +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.instance_id" +
", NULL::STRING AS org_id" +
", NULL::STRING AS id" +
", members.project_id" +
", NULL::STRING AS grant_id" +
" FROM projections.project_members as members" +
" UNION ALL " +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.instance_id" +
", NULL::STRING AS org_id" +
", NULL::STRING AS id" +
", members.project_id" +
", members.grant_id" +
" FROM projections.project_grant_members as members" +
") AS memberships" +
" LEFT JOIN projections.projects ON memberships.project_id = projections.projects.id" +
" LEFT JOIN projections.orgs ON memberships.org_id = projections.orgs.id")
membershipCols = []string{
"user_id",
"roles",
"creation_date",
"change_date",
"sequence",
"resource_owner",
"org_id",
"instance_id",
"project_id",
"grant_id",
"name", //project name
"name", //org name
"count",
}
)
func Test_MembershipPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareMembershipsQuery no result",
prepare: prepareMembershipsQuery,
want: want{
sqlExpectations: mockQueries(
membershipsStmt,
nil,
nil,
),
},
object: &Memberships{Memberships: []*Membership{}},
},
{
name: "prepareMembershipsQuery one org member",
prepare: prepareMembershipsQuery,
want: want{
sqlExpectations: mockQueries(
membershipsStmt,
membershipCols,
[][]driver.Value{
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
"org-id",
nil,
nil,
nil,
nil,
"org-name",
},
},
),
},
object: &Memberships{
SearchResponse: SearchResponse{
Count: 1,
},
Memberships: []*Membership{
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
Org: &OrgMembership{OrgID: "org-id", Name: "org-name"},
},
},
},
},
{
name: "prepareMembershipsQuery one instance member",
prepare: prepareMembershipsQuery,
want: want{
sqlExpectations: mockQueries(
membershipsStmt,
membershipCols,
[][]driver.Value{
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
nil,
"iam-id",
nil,
nil,
nil,
nil,
},
},
),
},
object: &Memberships{
SearchResponse: SearchResponse{
Count: 1,
},
Memberships: []*Membership{
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
IAM: &IAMMembership{IAMID: "iam-id", Name: "iam-id"},
},
},
},
},
{
name: "prepareMembershipsQuery one project member",
prepare: prepareMembershipsQuery,
want: want{
sqlExpectations: mockQueries(
membershipsStmt,
membershipCols,
[][]driver.Value{
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
nil,
nil,
"project-id",
nil,
"project-name",
nil,
},
},
),
},
object: &Memberships{
SearchResponse: SearchResponse{
Count: 1,
},
Memberships: []*Membership{
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
Project: &ProjectMembership{ProjectID: "project-id", Name: "project-name"},
},
},
},
},
{
name: "prepareMembershipsQuery one project grant member",
prepare: prepareMembershipsQuery,
want: want{
sqlExpectations: mockQueries(
membershipsStmt,
membershipCols,
[][]driver.Value{
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
nil,
nil,
"project-id",
"grant-id",
"project-name",
nil,
},
},
),
},
object: &Memberships{
SearchResponse: SearchResponse{
Count: 1,
},
Memberships: []*Membership{
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
ProjectGrant: &ProjectGrantMembership{
GrantID: "grant-id",
ProjectID: "project-id",
ProjectName: "project-name",
},
},
},
},
},
{
name: "prepareMembershipsQuery one for each member type",
prepare: prepareMembershipsQuery,
want: want{
sqlExpectations: mockQueries(
membershipsStmt,
membershipCols,
[][]driver.Value{
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
"org-id",
nil,
nil,
nil,
nil,
"org-name",
},
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
nil,
"iam-id",
nil,
nil,
nil,
nil,
},
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
nil,
nil,
"project-id",
nil,
"project-name",
nil,
},
{
"user-id",
pq.StringArray{"role1", "role2"},
testNow,
testNow,
uint64(20211202),
"ro",
nil,
nil,
"project-id",
"grant-id",
"project-name",
nil,
},
},
),
},
object: &Memberships{
SearchResponse: SearchResponse{
Count: 4,
},
Memberships: []*Membership{
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
Org: &OrgMembership{OrgID: "org-id", Name: "org-name"},
},
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
IAM: &IAMMembership{IAMID: "iam-id", Name: "iam-id"},
},
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
Project: &ProjectMembership{ProjectID: "project-id", Name: "project-name"},
},
{
UserID: "user-id",
Roles: []string{"role1", "role2"},
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211202,
ResourceOwner: "ro",
ProjectGrant: &ProjectGrantMembership{
ProjectID: "project-id",
GrantID: "grant-id",
ProjectName: "project-name",
},
},
},
},
},
{
name: "prepareMembershipsQuery sql err",
prepare: prepareMembershipsQuery,
want: want{
sqlExpectations: mockQueryErr(
membershipsStmt,
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)
})
}
}