zitadel/internal/query/iam_member_test.go
Silvan d2ea9a1b8c
feat: member queries (#2796)
* refactor(domain): add user type

* fix(projections): start with login names

* fix(login_policy): correct handling of user domain claimed event

* fix(projections): add members

* refactor: simplify member projections

* add migration for members

* add metadata to member projections

* refactor: login name projection

* fix: set correct suffixes on login name projections

* test(projections): login name reduces

* fix: correct cols in reduce member

* test(projections): org, iam, project members

* member additional cols and conds as opt,
add project grant members

* fix(migration): members

* fix(migration): correct database name

* migration version

* migs

* better naming for member cond and col

* split project and project grant members

* prepare member columns

* feat(queries): membership query

* test(queries): membership prepare

* fix(queries): multiple projections for latest sequence

* fix(api): use query for membership queries in auth and management

* feat: org member queries

* fix(api): use query for iam member calls

* fix(queries): org members

* fix(queries): project members

* fix(queries): project grant members

* fix(query): member queries and user avatar column

* member cols

* fix(queries): membership stmt

* fix user test

* fix user test

* fix(membership): correct display name

* fix(projection): additional member manipulation events

* additional member tests

* fix(projections): additional events of idp links

* fix: use query for memberships (#2797)

* fix(api): use query for memberships

* remove comment

* handle err

* refactor(projections): idp user link user aggregate type

* fix(projections): handle old user events

* fix(api): add asset prefix

* no image for iam members
2021-12-16 13:25:38 +00:00

278 lines
6.4 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"github.com/lib/pq"
)
var (
iamMembersQuery = regexp.QuoteMeta("SELECT" +
" members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.user_id" +
", members.roles" +
", zitadel.projections.login_names.login_name" +
", zitadel.projections.users_humans.email" +
", zitadel.projections.users_humans.first_name" +
", zitadel.projections.users_humans.last_name" +
", zitadel.projections.users_humans.display_name" +
", zitadel.projections.users_machines.name" +
", zitadel.projections.users_humans.avater_key" +
", COUNT(*) OVER () " +
"FROM zitadel.projections.iam_members as members " +
"LEFT JOIN zitadel.projections.users_humans " +
"ON members.user_id = zitadel.projections.users_humans.user_id " +
"LEFT JOIN zitadel.projections.users_machines " +
"ON members.user_id = zitadel.projections.users_machines.user_id " +
"LEFT JOIN zitadel.projections.login_names " +
"ON members.user_id = zitadel.projections.login_names.user_id " +
"WHERE zitadel.projections.login_names.is_primary = $1")
iamMembersColumns = []string{
"creation_date",
"change_date",
"sequence",
"resource_owner",
"user_id",
"roles",
"login_name",
"email",
"first_name",
"last_name",
"display_name",
"name",
"avater_key",
"count",
}
)
func Test_IAMMemberPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareIAMMembersQuery no result",
prepare: prepareIAMMembersQuery,
want: want{
sqlExpectations: mockQueries(
iamMembersQuery,
nil,
nil,
),
},
object: &Members{
Members: []*Member{},
},
},
{
name: "prepareIAMMembersQuery human found",
prepare: prepareIAMMembersQuery,
want: want{
sqlExpectations: mockQueries(
iamMembersQuery,
iamMembersColumns,
[][]driver.Value{
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id",
pq.StringArray{"role-1", "role-2"},
"gigi@caos-ag.zitadel.ch",
"gigi@caos.ch",
"first-name",
"last-name",
"display name",
nil,
nil,
},
},
),
},
object: &Members{
SearchResponse: SearchResponse{
Count: 1,
},
Members: []*Member{
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id",
Roles: []string{"role-1", "role-2"},
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
Email: "gigi@caos.ch",
FirstName: "first-name",
LastName: "last-name",
DisplayName: "display name",
AvatarURL: "",
},
},
},
},
{
name: "prepareIAMMembersQuery machine found",
prepare: prepareIAMMembersQuery,
want: want{
sqlExpectations: mockQueries(
iamMembersQuery,
iamMembersColumns,
[][]driver.Value{
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id",
pq.StringArray{"role-1", "role-2"},
"machine@caos-ag.zitadel.ch",
nil,
nil,
nil,
nil,
"machine-name",
nil,
},
},
),
},
object: &Members{
SearchResponse: SearchResponse{
Count: 1,
},
Members: []*Member{
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id",
Roles: []string{"role-1", "role-2"},
PreferredLoginName: "machine@caos-ag.zitadel.ch",
Email: "",
FirstName: "",
LastName: "",
DisplayName: "machine-name",
AvatarURL: "",
},
},
},
},
{
name: "prepareIAMMembersQuery multiple users",
prepare: prepareIAMMembersQuery,
want: want{
sqlExpectations: mockQueries(
iamMembersQuery,
iamMembersColumns,
[][]driver.Value{
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id-1",
pq.StringArray{"role-1", "role-2"},
"gigi@caos-ag.zitadel.ch",
"gigi@caos.ch",
"first-name",
"last-name",
"display name",
nil,
nil,
},
{
testNow,
testNow,
uint64(20211206),
"ro",
"user-id-2",
pq.StringArray{"role-1", "role-2"},
"machine@caos-ag.zitadel.ch",
nil,
nil,
nil,
nil,
"machine-name",
nil,
},
},
),
},
object: &Members{
SearchResponse: SearchResponse{
Count: 2,
},
Members: []*Member{
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id-1",
Roles: []string{"role-1", "role-2"},
PreferredLoginName: "gigi@caos-ag.zitadel.ch",
Email: "gigi@caos.ch",
FirstName: "first-name",
LastName: "last-name",
DisplayName: "display name",
AvatarURL: "",
},
{
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211206,
ResourceOwner: "ro",
UserID: "user-id-2",
Roles: []string{"role-1", "role-2"},
PreferredLoginName: "machine@caos-ag.zitadel.ch",
Email: "",
FirstName: "",
LastName: "",
DisplayName: "machine-name",
AvatarURL: "",
},
},
},
},
{
name: "prepareIAMMembersQuery sql err",
prepare: prepareIAMMembersQuery,
want: want{
sqlExpectations: mockQueryErr(
iamMembersQuery,
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)
})
}
}