2021-12-14 08:19:02 +01:00
package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
2022-08-31 09:52:43 +02:00
"github.com/zitadel/zitadel/internal/database"
2021-12-14 08:19:02 +01:00
)
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 11:53:19 +02:00
", memberships.id" +
2021-12-14 08:19:02 +01:00
", memberships.project_id" +
", memberships.grant_id" +
2022-08-31 09:52:43 +02:00
", projections.project_grants2.granted_org_id" +
", projections.projects2.name" +
2022-03-23 09:02:39 +01:00
", projections.orgs.name" +
2021-12-14 08:19:02 +01:00
", COUNT(*) OVER ()" +
" FROM (" +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
2022-03-29 11:53:19 +02:00
", members.instance_id" +
2021-12-14 08:19:02 +01:00
", members.org_id" +
2022-08-31 09:52:43 +02:00
", NULL::TEXT AS id" +
", NULL::TEXT AS project_id" +
", NULL::TEXT AS grant_id" +
" FROM projections.org_members2 AS members" +
2021-12-14 08:19:02 +01:00
" UNION ALL " +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
2022-03-24 17:21:34 +01:00
", members.instance_id" +
2022-08-31 09:52:43 +02:00
", NULL::TEXT AS org_id" +
2022-03-29 11:53:19 +02:00
", members.id" +
2022-08-31 09:52:43 +02:00
", NULL::TEXT AS project_id" +
", NULL::TEXT AS grant_id" +
" FROM projections.instance_members2 AS members" +
2021-12-14 08:19:02 +01:00
" UNION ALL " +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
2022-03-29 11:53:19 +02:00
", members.instance_id" +
2022-08-31 09:52:43 +02:00
", NULL::TEXT AS org_id" +
", NULL::TEXT AS id" +
2021-12-14 08:19:02 +01:00
", members.project_id" +
2022-08-31 09:52:43 +02:00
", NULL::TEXT AS grant_id" +
" FROM projections.project_members2 AS members" +
2021-12-14 08:19:02 +01:00
" UNION ALL " +
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
2022-03-29 11:53:19 +02:00
", members.instance_id" +
2022-08-31 09:52:43 +02:00
", NULL::TEXT AS org_id" +
", NULL::TEXT AS id" +
2021-12-14 08:19:02 +01:00
", members.project_id" +
", members.grant_id" +
2022-08-31 09:52:43 +02:00
" FROM projections.project_grant_members2 AS members" +
2021-12-14 08:19:02 +01:00
") AS memberships" +
2022-10-27 08:08:36 +02:00
" LEFT JOIN projections.projects2 ON memberships.project_id = projections.projects2.id AND memberships.instance_id = projections.projects2.instance_id" +
" LEFT JOIN projections.orgs ON memberships.org_id = projections.orgs.id AND memberships.instance_id = projections.orgs.instance_id" +
" LEFT JOIN projections.project_grants2 ON memberships.grant_id = projections.project_grants2.grant_id AND memberships.instance_id = projections.project_grants2.instance_id" )
2021-12-14 08:19:02 +01:00
membershipCols = [ ] string {
"user_id" ,
"roles" ,
"creation_date" ,
"change_date" ,
"sequence" ,
"resource_owner" ,
"org_id" ,
2022-03-24 17:21:34 +01:00
"instance_id" ,
2021-12-14 08:19:02 +01:00
"project_id" ,
"grant_id" ,
2022-07-27 09:55:44 +02:00
"granted_org_id" ,
2021-12-16 14:25:38 +01:00
"name" , //project name
"name" , //org name
2021-12-14 08:19:02 +01: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 14:25:38 +01:00
name : "prepareMembershipsQuery one org member" ,
2021-12-14 08:19:02 +01:00
prepare : prepareMembershipsQuery ,
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
"org-id" ,
nil ,
nil ,
nil ,
nil ,
2022-07-27 09:55:44 +02:00
nil ,
2021-12-16 14:25:38 +01:00
"org-name" ,
2021-12-14 08:19:02 +01:00
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2021-12-16 14:25:38 +01:00
Org : & OrgMembership { OrgID : "org-id" , Name : "org-name" } ,
2021-12-14 08:19:02 +01:00
} ,
} ,
} ,
} ,
{
2022-03-24 17:21:34 +01:00
name : "prepareMembershipsQuery one instance member" ,
2021-12-14 08:19:02 +01:00
prepare : prepareMembershipsQuery ,
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
nil ,
"iam-id" ,
nil ,
nil ,
nil ,
nil ,
2022-07-27 09:55:44 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2021-12-16 14:25:38 +01:00
IAM : & IAMMembership { IAMID : "iam-id" , Name : "iam-id" } ,
2021-12-14 08:19:02 +01:00
} ,
} ,
} ,
} ,
{
2021-12-16 14:25:38 +01:00
name : "prepareMembershipsQuery one project member" ,
2021-12-14 08:19:02 +01:00
prepare : prepareMembershipsQuery ,
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
nil ,
nil ,
"project-id" ,
nil ,
2022-07-27 09:55:44 +02:00
nil ,
2021-12-16 14:25:38 +01:00
"project-name" ,
2021-12-14 08:19:02 +01:00
nil ,
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2021-12-16 14:25:38 +01:00
Project : & ProjectMembership { ProjectID : "project-id" , Name : "project-name" } ,
2021-12-14 08:19:02 +01:00
} ,
} ,
} ,
} ,
{
2021-12-16 14:25:38 +01:00
name : "prepareMembershipsQuery one project grant member" ,
2021-12-14 08:19:02 +01:00
prepare : prepareMembershipsQuery ,
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
nil ,
nil ,
"project-id" ,
"grant-id" ,
2022-07-27 09:55:44 +02:00
"granted-org-id" ,
2021-12-16 14:25:38 +01:00
"project-name" ,
2021-12-14 08:19:02 +01:00
nil ,
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
ProjectGrant : & ProjectGrantMembership {
2022-07-27 09:55:44 +02:00
GrantID : "grant-id" ,
ProjectID : "project-id" ,
ProjectName : "project-name" ,
GrantedOrgID : "granted-org-id" ,
2021-12-14 08:19:02 +01:00
} ,
} ,
} ,
} ,
} ,
{
name : "prepareMembershipsQuery one for each member type" ,
prepare : prepareMembershipsQuery ,
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
"org-id" ,
nil ,
nil ,
nil ,
nil ,
2022-07-27 09:55:44 +02:00
nil ,
2021-12-16 14:25:38 +01:00
"org-name" ,
2021-12-14 08:19:02 +01:00
} ,
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
nil ,
"iam-id" ,
nil ,
nil ,
2021-12-16 14:25:38 +01:00
nil ,
2021-12-14 08:19:02 +01:00
nil ,
2022-07-27 09:55:44 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
nil ,
nil ,
"project-id" ,
nil ,
2022-07-27 09:55:44 +02:00
nil ,
2021-12-16 14:25:38 +01:00
"project-name" ,
2021-12-14 08:19:02 +01:00
nil ,
} ,
{
"user-id" ,
2022-08-31 09:52:43 +02:00
database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
testNow ,
testNow ,
uint64 ( 20211202 ) ,
"ro" ,
nil ,
nil ,
"project-id" ,
"grant-id" ,
2022-07-27 09:55:44 +02:00
"granted-org-id" ,
2021-12-16 14:25:38 +01:00
"project-name" ,
2021-12-14 08:19:02 +01:00
nil ,
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 4 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2021-12-16 14:25:38 +01:00
Org : & OrgMembership { OrgID : "org-id" , Name : "org-name" } ,
2021-12-14 08:19:02 +01:00
} ,
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2021-12-16 14:25:38 +01:00
IAM : & IAMMembership { IAMID : "iam-id" , Name : "iam-id" } ,
2021-12-14 08:19:02 +01:00
} ,
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2021-12-16 14:25:38 +01:00
Project : & ProjectMembership { ProjectID : "project-id" , Name : "project-name" } ,
2021-12-14 08:19:02 +01:00
} ,
{
UserID : "user-id" ,
2022-08-31 09:52:43 +02:00
Roles : database . StringArray { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
ProjectGrant : & ProjectGrantMembership {
2022-07-27 09:55:44 +02:00
ProjectID : "project-id" ,
GrantID : "grant-id" ,
ProjectName : "project-name" ,
GrantedOrgID : "granted-org-id" ,
2021-12-14 08:19:02 +01: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 )
} )
}
}