zitadel/internal/query/project_member_test.go
Silvan 77b4fc5487
feat(database): support for postgres (#3998)
* beginning with postgres statements

* try pgx

* use pgx

* database

* init works for postgres

* arrays working

* init for cockroach

* init

* start tests

* tests

* TESTS

* ch

* ch

* chore: use go 1.18

* read stmts

* fix typo

* tests

* connection string

* add missing error handler

* cleanup

* start all apis

* go mod tidy

* old update

* switch back to minute

* on conflict

* replace string slice with `database.StringArray` in db models

* fix tests and start

* update go version in dockerfile

* setup go

* clean up

* remove notification migration

* update

* docs: add deploy guide for postgres

* fix: revert sonyflake

* use `database.StringArray` for daos

* use `database.StringArray` every where

* new tables

* index naming,
metadata primary key,
project grant role key type

* docs(postgres): change to beta

* chore: correct compose

* fix(defaults): add empty postgres config

* refactor: remove unused code

* docs: add postgres to self hosted

* fix broken link

* so?

* change title

* add mdx to link

* fix stmt

* update goreleaser in test-code

* docs: improve postgres example

* update more projections

* fix: add beta log for postgres

* revert index name change

* prerelease

* fix: add sequence to v1 "reduce paniced"

* log if nil

* add logging

* fix: log output

* fix(import): check if org exists and user

* refactor: imports

* fix(user): ignore malformed events

* refactor: method naming

* fix: test

* refactor: correct errors.Is call

* ci: don't build dev binaries on main

* fix(go releaser): update version to 1.11.0

* fix(user): projection should not break

* fix(user): handle error properly

* docs: correct config example

* Update .releaserc.js

* Update .releaserc.js

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00

278 lines
6.5 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"github.com/zitadel/zitadel/internal/database"
)
var (
projectMembersQuery = regexp.QuoteMeta("SELECT" +
" members.creation_date" +
", members.change_date" +
", members.sequence" +
", members.resource_owner" +
", members.user_id" +
", members.roles" +
", projections.login_names.login_name" +
", projections.users3_humans.email" +
", projections.users3_humans.first_name" +
", projections.users3_humans.last_name" +
", projections.users3_humans.display_name" +
", projections.users3_machines.name" +
", projections.users3_humans.avatar_key" +
", COUNT(*) OVER () " +
"FROM projections.project_members2 AS members " +
"LEFT JOIN projections.users3_humans " +
"ON members.user_id = projections.users3_humans.user_id " +
"LEFT JOIN projections.users3_machines " +
"ON members.user_id = projections.users3_machines.user_id " +
"LEFT JOIN projections.login_names " +
"ON members.user_id = projections.login_names.user_id " +
"WHERE 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",
database.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: database.StringArray{"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",
database.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: database.StringArray{"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",
database.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",
database.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: database.StringArray{"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: database.StringArray{"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)
})
}
}