zitadel/internal/query/project_member_test.go
Silvan c542cab4f8
feat(queries): user grants (#2838)
* 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(projections): add user grant projection

* fix(user_grant): handle state changes

* add state to migration

* fix(management): use query for user grant requests

* merge eventstore-naming into user-grant-projection

* feat(queries): user grants

* fix(migrations): version

* fix(api): user query for user grants

* fix(query): event mappers for usergrant aggregate

* fix(projection): correct aggregate for user grants

* fix(queries): user grant roles as list contains

* cleanup reducers

* fix avater_key to avatar_key

* tests

* cleanup

* cleanup

* add resourceowner query

* fix: user grant project name search query

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-01-14 09:45:50 +00:00

278 lines
6.5 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"github.com/lib/pq"
)
var (
projectMembersQuery = 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.avatar_key" +
", COUNT(*) OVER () " +
"FROM zitadel.projections.project_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")
projectMembersColumns = []string{
"creation_date",
"change_date",
"sequence",
"resource_owner",
"user_id",
"roles",
"login_name",
"email",
"first_name",
"last_name",
"display_name",
"name",
"avatar_key",
"count",
}
)
func Test_ProjectMemberPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareProjectMembersQuery no result",
prepare: prepareProjectMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectMembersQuery,
nil,
nil,
),
},
object: &Members{
Members: []*Member{},
},
},
{
name: "prepareProjectMembersQuery human found",
prepare: prepareProjectMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectMembersQuery,
projectMembersColumns,
[][]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: "prepareProjectMembersQuery machine found",
prepare: prepareProjectMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectMembersQuery,
projectMembersColumns,
[][]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: "prepareProjectMembersQuery multiple users",
prepare: prepareProjectMembersQuery,
want: want{
sqlExpectations: mockQueries(
projectMembersQuery,
projectMembersColumns,
[][]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: "prepareProjectMembersQuery sql err",
prepare: prepareProjectMembersQuery,
want: want{
sqlExpectations: mockQueryErr(
projectMembersQuery,
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)
})
}
}