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