2021-12-16 13:25:38 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2023-04-11 15:56:51 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2021-12-16 13:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
orgMembersQuery = regexp.QuoteMeta("SELECT" +
|
|
|
|
" members.creation_date" +
|
|
|
|
", members.change_date" +
|
|
|
|
", members.sequence" +
|
|
|
|
", members.resource_owner" +
|
|
|
|
", members.user_id" +
|
|
|
|
", members.roles" +
|
2022-11-30 16:01:17 +00:00
|
|
|
", projections.login_names2.login_name" +
|
2023-02-08 08:06:34 +00:00
|
|
|
", projections.users8_humans.email" +
|
|
|
|
", projections.users8_humans.first_name" +
|
|
|
|
", projections.users8_humans.last_name" +
|
|
|
|
", projections.users8_humans.display_name" +
|
|
|
|
", projections.users8_machines.name" +
|
|
|
|
", projections.users8_humans.avatar_key" +
|
2023-04-11 15:56:51 +00:00
|
|
|
", projections.users8.type" +
|
2021-12-16 13:25:38 +00:00
|
|
|
", COUNT(*) OVER () " +
|
2022-11-30 16:01:17 +00:00
|
|
|
"FROM projections.org_members3 AS members " +
|
2023-02-08 08:06:34 +00:00
|
|
|
"LEFT JOIN projections.users8_humans " +
|
|
|
|
"ON members.user_id = projections.users8_humans.user_id " +
|
|
|
|
"AND members.instance_id = projections.users8_humans.instance_id " +
|
|
|
|
"LEFT JOIN projections.users8_machines " +
|
|
|
|
"ON members.user_id = projections.users8_machines.user_id " +
|
|
|
|
"AND members.instance_id = projections.users8_machines.instance_id " +
|
2023-04-11 15:56:51 +00:00
|
|
|
"LEFT JOIN projections.users8 " +
|
|
|
|
"ON members.user_id = projections.users8.id " +
|
|
|
|
"AND members.instance_id = projections.users8.instance_id " +
|
2022-11-30 16:01:17 +00:00
|
|
|
"LEFT JOIN projections.login_names2 " +
|
|
|
|
"ON members.user_id = projections.login_names2.user_id " +
|
|
|
|
"AND members.instance_id = projections.login_names2.instance_id " +
|
2023-02-27 21:36:43 +00:00
|
|
|
"AS OF SYSTEM TIME '-1 ms' " +
|
2022-11-30 16:01:17 +00:00
|
|
|
"WHERE projections.login_names2.is_primary = $1")
|
2021-12-16 13:25:38 +00:00
|
|
|
orgMembersColumns = []string{
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"sequence",
|
|
|
|
"resource_owner",
|
|
|
|
"user_id",
|
|
|
|
"roles",
|
|
|
|
"login_name",
|
|
|
|
"email",
|
|
|
|
"first_name",
|
|
|
|
"last_name",
|
|
|
|
"display_name",
|
|
|
|
"name",
|
2022-01-14 09:45:50 +00:00
|
|
|
"avatar_key",
|
2023-04-11 15:56:51 +00:00
|
|
|
"type",
|
2021-12-16 13:25:38 +00:00
|
|
|
"count",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_OrgMemberPrepares(t *testing.T) {
|
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prepare interface{}
|
|
|
|
want want
|
|
|
|
object interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "prepareOrgMembersQuery no result",
|
|
|
|
prepare: prepareOrgMembersQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
orgMembersQuery,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
Members: []*Member{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMembersQuery human found",
|
|
|
|
prepare: prepareOrgMembersQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
orgMembersQuery,
|
|
|
|
orgMembersColumns,
|
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"gigi@caos-ag.zitadel.ch",
|
|
|
|
"gigi@caos.ch",
|
|
|
|
"first-name",
|
|
|
|
"last-name",
|
|
|
|
"display name",
|
|
|
|
nil,
|
|
|
|
nil,
|
2023-04-11 15:56:51 +00:00
|
|
|
domain.UserTypeHuman,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Members: []*Member{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
|
|
|
|
Email: "gigi@caos.ch",
|
|
|
|
FirstName: "first-name",
|
|
|
|
LastName: "last-name",
|
|
|
|
DisplayName: "display name",
|
|
|
|
AvatarURL: "",
|
2023-04-11 15:56:51 +00:00
|
|
|
UserType: domain.UserTypeHuman,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMembersQuery machine found",
|
|
|
|
prepare: prepareOrgMembersQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
orgMembersQuery,
|
|
|
|
orgMembersColumns,
|
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"machine@caos-ag.zitadel.ch",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"machine-name",
|
|
|
|
nil,
|
2023-04-11 15:56:51 +00:00
|
|
|
domain.UserTypeMachine,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Members: []*Member{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "machine@caos-ag.zitadel.ch",
|
|
|
|
Email: "",
|
|
|
|
FirstName: "",
|
|
|
|
LastName: "",
|
|
|
|
DisplayName: "machine-name",
|
|
|
|
AvatarURL: "",
|
2023-04-11 15:56:51 +00:00
|
|
|
UserType: domain.UserTypeMachine,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMembersQuery multiple users",
|
|
|
|
prepare: prepareOrgMembersQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
orgMembersQuery,
|
|
|
|
orgMembersColumns,
|
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id-1",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"gigi@caos-ag.zitadel.ch",
|
|
|
|
"gigi@caos.ch",
|
|
|
|
"first-name",
|
|
|
|
"last-name",
|
|
|
|
"display name",
|
|
|
|
nil,
|
|
|
|
nil,
|
2023-04-11 15:56:51 +00:00
|
|
|
domain.UserTypeHuman,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
uint64(20211206),
|
|
|
|
"ro",
|
|
|
|
"user-id-2",
|
2022-08-31 07:52:43 +00:00
|
|
|
database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
"machine@caos-ag.zitadel.ch",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"machine-name",
|
|
|
|
nil,
|
2023-04-11 15:56:51 +00:00
|
|
|
domain.UserTypeMachine,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &Members{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 2,
|
|
|
|
},
|
|
|
|
Members: []*Member{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id-1",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
|
|
|
|
Email: "gigi@caos.ch",
|
|
|
|
FirstName: "first-name",
|
|
|
|
LastName: "last-name",
|
|
|
|
DisplayName: "display name",
|
|
|
|
AvatarURL: "",
|
2023-04-11 15:56:51 +00:00
|
|
|
UserType: domain.UserTypeHuman,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211206,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
UserID: "user-id-2",
|
2022-08-31 07:52:43 +00:00
|
|
|
Roles: database.StringArray{"role-1", "role-2"},
|
2021-12-16 13:25:38 +00:00
|
|
|
PreferredLoginName: "machine@caos-ag.zitadel.ch",
|
|
|
|
Email: "",
|
|
|
|
FirstName: "",
|
|
|
|
LastName: "",
|
|
|
|
DisplayName: "machine-name",
|
|
|
|
AvatarURL: "",
|
2023-04-11 15:56:51 +00:00
|
|
|
UserType: domain.UserTypeMachine,
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMembersQuery sql err",
|
|
|
|
prepare: prepareOrgMembersQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
orgMembersQuery,
|
|
|
|
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:02 +00:00
|
|
|
object: (*OrgMembership)(nil),
|
2021-12-16 13:25:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-27 21:36:43 +00:00
|
|
|
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
|
2021-12-16 13:25:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|