zitadel/internal/query/idp_user_link_test.go
Silvan b8bec25129
fix: use query side for requests (#2818)
* 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

* refactor: remove unsued methods in repo-interfaces

* start

* fix(query): membership

* fix(auth): list my project orgs

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

* refactor(auth): MyProjectOrgs

* fix(queries): member and membership stmts

* fix user test

* fix(management): use query for project (-grant) members

* fix(admin): use query for member calls

* fix(api): add domain to org mapping

* remove old idp

* membership

* refactor: remove old files

* idp

* refactor: use query for idps and idp user links

* refactor(eventstore): rename EventPusher to Command, EventReader to Event, PushEvents to Push and FilterEvents to Filter

* gloabl org check for org roles

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-01-13 08:58:14 +01:00

146 lines
3.2 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"github.com/caos/zitadel/internal/domain"
)
var (
idpUserLinksQuery = regexp.QuoteMeta(`SELECT zitadel.projections.idp_user_links.idp_id,` +
` zitadel.projections.idp_user_links.user_id,` +
` zitadel.projections.idps.name,` +
` zitadel.projections.idp_user_links.external_user_id,` +
` zitadel.projections.idp_user_links.display_name,` +
` zitadel.projections.idps.type,` +
` zitadel.projections.idp_user_links.resource_owner,` +
` COUNT(*) OVER ()` +
` FROM zitadel.projections.idp_user_links` +
` LEFT JOIN zitadel.projections.idps ON zitadel.projections.idp_user_links.idp_id = zitadel.projections.idps.id`)
idpUserLinksCols = []string{
"idp_id",
"user_id",
"name",
"external_user_id",
"display_name",
"type",
"resource_owner",
"count",
}
)
func Test_IDPUserLinkPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareIDPsQuery found",
prepare: prepareIDPUserLinksQuery,
want: want{
sqlExpectations: mockQueries(
idpUserLinksQuery,
idpUserLinksCols,
[][]driver.Value{
{
"idp-id",
"user-id",
"idp-name",
"external-user-id",
"display-name",
domain.IDPConfigTypeJWT,
"ro",
},
},
),
},
object: &IDPUserLinks{
SearchResponse: SearchResponse{
Count: 1,
},
Links: []*IDPUserLink{
{
IDPID: "idp-id",
UserID: "user-id",
IDPName: "idp-name",
ProvidedUserID: "external-user-id",
ProvidedUsername: "display-name",
IDPType: domain.IDPConfigTypeJWT,
ResourceOwner: "ro",
},
},
},
},
{
name: "prepareIDPsQuery no idp",
prepare: prepareIDPUserLinksQuery,
want: want{
sqlExpectations: mockQueries(
idpUserLinksQuery,
idpUserLinksCols,
[][]driver.Value{
{
"idp-id",
"user-id",
nil,
"external-user-id",
"display-name",
nil,
"ro",
},
},
),
},
object: &IDPUserLinks{
SearchResponse: SearchResponse{
Count: 1,
},
Links: []*IDPUserLink{
{
IDPID: "idp-id",
UserID: "user-id",
IDPName: "",
ProvidedUserID: "external-user-id",
ProvidedUsername: "display-name",
IDPType: domain.IDPConfigTypeUnspecified,
ResourceOwner: "ro",
},
},
},
},
{
name: "prepareIDPsQuery sql err",
prepare: prepareIDPUserLinksQuery,
want: want{
sqlExpectations: mockQueryErr(
idpUserLinksQuery,
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)
})
}
}