zitadel/internal/query/idp_user_link_test.go
Silvan c9face4ea4
feat(queries): login policy idp links (#2767)
* fix(idp): set type in projection

* correct table

* user idp links

* refactor: user idp link query

* add not null constraint

* refactor: idp user links

* rename file

* fix(idp): correct resource owner

* refactor: rename test

* fix(query): implement idp login policy links

* unify naming of idp links

* test prepare

* fix(api): convert idp type

* rename migration
2021-12-08 14:49:19 +01:00

140 lines
3.0 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,` +
` 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",
"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,
},
},
),
},
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,
},
},
},
},
{
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,
},
},
),
},
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,
},
},
},
},
{
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)
})
}
}