2021-12-14 08:19:02 +01:00
package query
import (
2023-02-27 22:36:43 +01:00
"context"
2021-12-14 08:19:02 +01:00
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
2022-11-30 17:01:17 +01:00
sq "github.com/Masterminds/squirrel"
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 (
2023-10-19 12:19:10 +02:00
"SELECT members.user_id" +
", members.roles" +
", members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.org_id" +
", members.id" +
", members.project_id" +
", members.grant_id" +
2023-11-20 16:21:08 +01:00
", projections.project_grants4.granted_org_id" +
", projections.projects4.name" +
2023-09-07 06:54:51 +02:00
", projections.orgs1.name" +
2023-10-19 12:19:10 +02:00
", projections.instances.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" +
2023-11-20 16:21:08 +01:00
" FROM projections.org_members4 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" +
2023-11-20 16:21:08 +01:00
" FROM projections.instance_members4 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" +
2023-11-20 16:21:08 +01:00
" FROM projections.project_members4 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" +
2023-11-20 16:21:08 +01:00
" FROM projections.project_grant_members4 AS members" +
2023-10-19 12:19:10 +02:00
") AS members" +
2023-11-20 16:21:08 +01:00
" LEFT JOIN projections.projects4 ON members.project_id = projections.projects4.id AND members.instance_id = projections.projects4.instance_id" +
2023-10-19 12:19:10 +02:00
" LEFT JOIN projections.orgs1 ON members.org_id = projections.orgs1.id AND members.instance_id = projections.orgs1.instance_id" +
2023-11-20 16:21:08 +01:00
" LEFT JOIN projections.project_grants4 ON members.grant_id = projections.project_grants4.grant_id AND members.instance_id = projections.project_grants4.instance_id" +
2023-10-19 12:19:10 +02:00
" LEFT JOIN projections.instances ON members.instance_id = projections.instances.id" +
2023-02-27 22:36:43 +01:00
` AS OF SYSTEM TIME '-1 ms' ` )
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
2023-10-19 12:19:10 +02:00
"name" , // instance 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" ,
2023-11-20 16:21:08 +01:00
prepare : prepareMembershipWrapper ( ) ,
2021-12-14 08:19:02 +01:00
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
nil ,
nil ,
) ,
} ,
object : & Memberships { Memberships : [ ] * Membership { } } ,
} ,
{
2021-12-16 14:25:38 +01:00
name : "prepareMembershipsQuery one org member" ,
2023-11-20 16:21:08 +01:00
prepare : prepareMembershipWrapper ( ) ,
2021-12-14 08:19:02 +01:00
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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" ,
2023-10-19 12:19:10 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "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" ,
2023-11-20 16:21:08 +01:00
prepare : prepareMembershipWrapper ( ) ,
2021-12-14 08:19:02 +01:00
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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 ,
2023-10-19 12:19:10 +02:00
"instance" ,
2021-12-14 08:19:02 +01:00
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2023-10-19 12:19:10 +02:00
IAM : & IAMMembership { IAMID : "iam-id" , Name : "instance" } ,
2021-12-14 08:19:02 +01:00
} ,
} ,
} ,
} ,
{
2021-12-16 14:25:38 +01:00
name : "prepareMembershipsQuery one project member" ,
2023-11-20 16:21:08 +01:00
prepare : prepareMembershipWrapper ( ) ,
2021-12-14 08:19:02 +01:00
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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 ,
2023-10-19 12:19:10 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "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" ,
2023-11-20 16:21:08 +01:00
prepare : prepareMembershipWrapper ( ) ,
2021-12-14 08:19:02 +01:00
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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 ,
2023-10-19 12:19:10 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "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" ,
2023-11-20 16:21:08 +01:00
prepare : prepareMembershipWrapper ( ) ,
2021-12-14 08:19:02 +01:00
want : want {
sqlExpectations : mockQueries (
membershipsStmt ,
membershipCols ,
[ ] [ ] driver . Value {
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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" ,
2023-10-19 12:19:10 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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 ,
2023-10-19 12:19:10 +02:00
"instance" ,
2021-12-14 08:19:02 +01:00
} ,
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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 ,
2023-10-19 12:19:10 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
{
"user-id" ,
2023-10-19 12:19:10 +02:00
database . TextArray [ string ] { "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 ,
2023-10-19 12:19:10 +02:00
nil ,
2021-12-14 08:19:02 +01:00
} ,
} ,
) ,
} ,
object : & Memberships {
SearchResponse : SearchResponse {
Count : 4 ,
} ,
Memberships : [ ] * Membership {
{
UserID : "user-id" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "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" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "role1" , "role2" } ,
2021-12-14 08:19:02 +01:00
CreationDate : testNow ,
ChangeDate : testNow ,
Sequence : 20211202 ,
ResourceOwner : "ro" ,
2023-10-19 12:19:10 +02:00
IAM : & IAMMembership { IAMID : "iam-id" , Name : "instance" } ,
2021-12-14 08:19:02 +01:00
} ,
{
UserID : "user-id" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "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" ,
2023-10-19 12:19:10 +02:00
Roles : database . TextArray [ string ] { "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" ,
2023-11-20 16:21:08 +01:00
prepare : prepareMembershipWrapper ( ) ,
2021-12-14 08:19:02 +01:00
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
} ,
} ,
2023-08-22 12:49:22 +02:00
object : ( * Memberships ) ( nil ) ,
2021-12-14 08:19:02 +01:00
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-02-27 22:36:43 +01:00
assertPrepare ( t , tt . prepare , tt . object , tt . want . sqlExpectations , tt . want . err , defaultPrepareArgs ... )
2021-12-14 08:19:02 +01:00
} )
}
}
2022-11-30 17:01:17 +01:00
2023-11-20 16:21:08 +01:00
func prepareMembershipWrapper ( ) func ( ctx context . Context , db prepareDatabase ) ( sq . SelectBuilder , func ( * sql . Rows ) ( * Memberships , error ) ) {
2023-02-27 22:36:43 +01:00
return func ( ctx context . Context , db prepareDatabase ) ( sq . SelectBuilder , func ( * sql . Rows ) ( * Memberships , error ) ) {
2023-11-20 16:21:08 +01:00
builder , _ , fun := prepareMembershipsQuery ( ctx , db , & MembershipSearchQuery { } )
2022-11-30 17:01:17 +01:00
return builder , fun
}
}