2021-12-14 07:19:02 +00:00
|
|
|
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" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", memberships.id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", memberships.project_id" +
|
|
|
|
", memberships.grant_id" +
|
2022-03-23 08:02:39 +00:00
|
|
|
", projections.projects.name" +
|
|
|
|
", projections.orgs.name" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", COUNT(*) OVER ()" +
|
|
|
|
" FROM (" +
|
|
|
|
"SELECT members.user_id" +
|
|
|
|
", members.roles" +
|
|
|
|
", members.creation_date" +
|
|
|
|
", members.change_date" +
|
|
|
|
", members.sequence" +
|
|
|
|
", members.resource_owner" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", members.instance_id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", members.org_id" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", NULL::STRING AS id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", NULL::STRING AS project_id" +
|
|
|
|
", NULL::STRING AS grant_id" +
|
2022-03-23 08:02:39 +00:00
|
|
|
" FROM projections.org_members as members" +
|
2021-12-14 07:19:02 +00:00
|
|
|
" UNION ALL " +
|
|
|
|
"SELECT members.user_id" +
|
|
|
|
", members.roles" +
|
|
|
|
", members.creation_date" +
|
|
|
|
", members.change_date" +
|
|
|
|
", members.sequence" +
|
|
|
|
", members.resource_owner" +
|
2022-03-24 16:21:34 +00:00
|
|
|
", members.instance_id" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", NULL::STRING AS org_id" +
|
|
|
|
", members.id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", NULL::STRING AS project_id" +
|
|
|
|
", NULL::STRING AS grant_id" +
|
2022-03-24 16:21:34 +00:00
|
|
|
" FROM projections.instance_members as members" +
|
2021-12-14 07:19:02 +00:00
|
|
|
" UNION ALL " +
|
|
|
|
"SELECT members.user_id" +
|
|
|
|
", members.roles" +
|
|
|
|
", members.creation_date" +
|
|
|
|
", members.change_date" +
|
|
|
|
", members.sequence" +
|
|
|
|
", members.resource_owner" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", members.instance_id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", NULL::STRING AS org_id" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", NULL::STRING AS id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", members.project_id" +
|
|
|
|
", NULL::STRING AS grant_id" +
|
2022-03-23 08:02:39 +00:00
|
|
|
" FROM projections.project_members as members" +
|
2021-12-14 07:19:02 +00:00
|
|
|
" UNION ALL " +
|
|
|
|
"SELECT members.user_id" +
|
|
|
|
", members.roles" +
|
|
|
|
", members.creation_date" +
|
|
|
|
", members.change_date" +
|
|
|
|
", members.sequence" +
|
|
|
|
", members.resource_owner" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", members.instance_id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", NULL::STRING AS org_id" +
|
2022-03-29 09:53:19 +00:00
|
|
|
", NULL::STRING AS id" +
|
2021-12-14 07:19:02 +00:00
|
|
|
", members.project_id" +
|
|
|
|
", members.grant_id" +
|
2022-03-23 08:02:39 +00:00
|
|
|
" FROM projections.project_grant_members as members" +
|
2021-12-14 07:19:02 +00:00
|
|
|
") AS memberships" +
|
2022-03-23 08:02:39 +00:00
|
|
|
" LEFT JOIN projections.projects ON memberships.project_id = projections.projects.id" +
|
|
|
|
" LEFT JOIN projections.orgs ON memberships.org_id = projections.orgs.id")
|
2021-12-14 07:19:02 +00:00
|
|
|
membershipCols = []string{
|
|
|
|
"user_id",
|
|
|
|
"roles",
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"sequence",
|
|
|
|
"resource_owner",
|
|
|
|
"org_id",
|
2022-03-24 16:21:34 +00:00
|
|
|
"instance_id",
|
2021-12-14 07:19:02 +00:00
|
|
|
"project_id",
|
|
|
|
"grant_id",
|
2021-12-16 13:25:38 +00:00
|
|
|
"name", //project name
|
|
|
|
"name", //org name
|
2021-12-14 07:19:02 +00:00
|
|
|
"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{}},
|
|
|
|
},
|
|
|
|
{
|
2021-12-16 13:25:38 +00:00
|
|
|
name: "prepareMembershipsQuery one org member",
|
2021-12-14 07:19:02 +00:00
|
|
|
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,
|
2021-12-16 13:25:38 +00:00
|
|
|
"org-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Memberships{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Memberships: []*Membership{
|
|
|
|
{
|
|
|
|
UserID: "user-id",
|
|
|
|
Roles: []string{"role1", "role2"},
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211202,
|
|
|
|
ResourceOwner: "ro",
|
2021-12-16 13:25:38 +00:00
|
|
|
Org: &OrgMembership{OrgID: "org-id", Name: "org-name"},
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "prepareMembershipsQuery one instance member",
|
2021-12-14 07:19:02 +00:00
|
|
|
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",
|
2021-12-16 13:25:38 +00:00
|
|
|
IAM: &IAMMembership{IAMID: "iam-id", Name: "iam-id"},
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-16 13:25:38 +00:00
|
|
|
name: "prepareMembershipsQuery one project member",
|
2021-12-14 07:19:02 +00:00
|
|
|
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,
|
2021-12-16 13:25:38 +00:00
|
|
|
"project-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Memberships{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Memberships: []*Membership{
|
|
|
|
{
|
|
|
|
UserID: "user-id",
|
|
|
|
Roles: []string{"role1", "role2"},
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211202,
|
|
|
|
ResourceOwner: "ro",
|
2021-12-16 13:25:38 +00:00
|
|
|
Project: &ProjectMembership{ProjectID: "project-id", Name: "project-name"},
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-16 13:25:38 +00:00
|
|
|
name: "prepareMembershipsQuery one project grant member",
|
2021-12-14 07:19:02 +00:00
|
|
|
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",
|
2021-12-16 13:25:38 +00:00
|
|
|
"project-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
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{
|
2021-12-16 13:25:38 +00:00
|
|
|
GrantID: "grant-id",
|
|
|
|
ProjectID: "project-id",
|
|
|
|
ProjectName: "project-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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,
|
2021-12-16 13:25:38 +00:00
|
|
|
"org-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"user-id",
|
|
|
|
pq.StringArray{"role1", "role2"},
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211202),
|
|
|
|
"ro",
|
|
|
|
nil,
|
|
|
|
"iam-id",
|
|
|
|
nil,
|
|
|
|
nil,
|
2021-12-16 13:25:38 +00:00
|
|
|
nil,
|
2021-12-14 07:19:02 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"user-id",
|
|
|
|
pq.StringArray{"role1", "role2"},
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211202),
|
|
|
|
"ro",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"project-id",
|
|
|
|
nil,
|
2021-12-16 13:25:38 +00:00
|
|
|
"project-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"user-id",
|
|
|
|
pq.StringArray{"role1", "role2"},
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211202),
|
|
|
|
"ro",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"project-id",
|
|
|
|
"grant-id",
|
2021-12-16 13:25:38 +00:00
|
|
|
"project-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Memberships{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 4,
|
|
|
|
},
|
|
|
|
Memberships: []*Membership{
|
|
|
|
{
|
|
|
|
UserID: "user-id",
|
|
|
|
Roles: []string{"role1", "role2"},
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211202,
|
|
|
|
ResourceOwner: "ro",
|
2021-12-16 13:25:38 +00:00
|
|
|
Org: &OrgMembership{OrgID: "org-id", Name: "org-name"},
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
UserID: "user-id",
|
|
|
|
Roles: []string{"role1", "role2"},
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211202,
|
|
|
|
ResourceOwner: "ro",
|
2021-12-16 13:25:38 +00:00
|
|
|
IAM: &IAMMembership{IAMID: "iam-id", Name: "iam-id"},
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
UserID: "user-id",
|
|
|
|
Roles: []string{"role1", "role2"},
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211202,
|
|
|
|
ResourceOwner: "ro",
|
2021-12-16 13:25:38 +00:00
|
|
|
Project: &ProjectMembership{ProjectID: "project-id", Name: "project-name"},
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
UserID: "user-id",
|
|
|
|
Roles: []string{"role1", "role2"},
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211202,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
ProjectGrant: &ProjectGrantMembership{
|
2021-12-16 13:25:38 +00:00
|
|
|
ProjectID: "project-id",
|
|
|
|
GrantID: "grant-id",
|
|
|
|
ProjectName: "project-name",
|
2021-12-14 07:19:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|