zitadel/internal/query/idp_user_link_test.go
Livio Amstutz 56b916a2b0
feat: projections auto create their tables (#3324)
* begin init checks for projections

* first projection checks

* debug notification providers with query fixes

* more projections and first index

* more projections

* more projections

* finish projections

* fix tests (remove db name)

* create tables in setup

* fix logging / error handling

* add tenant to views

* rename tenant to instance_id

* add instance_id to all projections

* add instance_id to all queries

* correct instance_id on projections

* add instance_id to failed_events

* use separate context for instance

* implement features projection

* implement features projection

* remove unique constraint from setup when migration failed

* add error to failed setup event

* add instance_id to primary keys

* fix IAM projection

* remove old migrations folder

* fix keysFromYAML test
2022-03-23 09:02:39 +01:00

146 lines
3.1 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 projections.idp_user_links.idp_id,` +
` projections.idp_user_links.user_id,` +
` projections.idps.name,` +
` projections.idp_user_links.external_user_id,` +
` projections.idp_user_links.display_name,` +
` projections.idps.type,` +
` projections.idp_user_links.resource_owner,` +
` COUNT(*) OVER ()` +
` FROM projections.idp_user_links` +
` LEFT JOIN projections.idps ON projections.idp_user_links.idp_id = 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)
})
}
}