2021-12-07 08:33:52 +01:00
package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
2022-04-27 01:01:45 +02:00
"github.com/zitadel/zitadel/internal/domain"
2021-12-07 08:33:52 +01:00
)
var (
2022-11-30 17:01:17 +01:00
loginPolicyIDPLinksQuery = regexp . QuoteMeta ( ` SELECT projections.idp_login_policy_links4.idp_id, ` +
2023-02-28 21:20:58 +01:00
` projections.idp_templates2.name, ` +
` projections.idp_templates2.type, ` +
` projections.idp_templates2.owner_type, ` +
2021-12-07 08:33:52 +01:00
` COUNT(*) OVER () ` +
2022-11-30 17:01:17 +01:00
` FROM projections.idp_login_policy_links4 ` +
2023-02-28 21:20:58 +01:00
` LEFT JOIN projections.idp_templates2 ON projections.idp_login_policy_links4.idp_id = projections.idp_templates2.id AND projections.idp_login_policy_links4.instance_id = projections.idp_templates2.instance_id ` +
2023-02-27 22:36:43 +01:00
` AS OF SYSTEM TIME '-1 ms' ` )
2021-12-08 14:49:19 +01:00
loginPolicyIDPLinksCols = [ ] string {
2021-12-07 08:33:52 +01:00
"idp_id" ,
"name" ,
"type" ,
2023-02-28 21:20:58 +01:00
"owner_type" ,
2021-12-07 08:33:52 +01:00
"count" ,
}
)
2021-12-08 14:49:19 +01:00
func Test_IDPLoginPolicyLinkPrepares ( t * testing . T ) {
2021-12-07 08:33:52 +01:00
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := [ ] struct {
name string
prepare interface { }
want want
object interface { }
} {
{
name : "prepareIDPsQuery found" ,
2021-12-08 14:49:19 +01:00
prepare : prepareIDPLoginPolicyLinksQuery ,
2021-12-07 08:33:52 +01:00
want : want {
sqlExpectations : mockQueries (
2021-12-08 14:49:19 +01:00
loginPolicyIDPLinksQuery ,
loginPolicyIDPLinksCols ,
2021-12-07 08:33:52 +01:00
[ ] [ ] driver . Value {
{
"idp-id" ,
"idp-name" ,
2023-02-28 21:20:58 +01:00
domain . IDPTypeJWT ,
domain . IdentityProviderTypeSystem ,
2021-12-07 08:33:52 +01:00
} ,
} ,
) ,
} ,
2021-12-08 14:49:19 +01:00
object : & IDPLoginPolicyLinks {
2021-12-07 08:33:52 +01:00
SearchResponse : SearchResponse {
Count : 1 ,
} ,
2021-12-08 14:49:19 +01:00
Links : [ ] * IDPLoginPolicyLink {
2021-12-07 08:33:52 +01:00
{
2023-02-28 21:20:58 +01:00
IDPID : "idp-id" ,
IDPName : "idp-name" ,
IDPType : domain . IDPTypeJWT ,
OwnerType : domain . IdentityProviderTypeSystem ,
2021-12-07 08:33:52 +01:00
} ,
} ,
} ,
} ,
{
name : "prepareIDPsQuery no idp" ,
2021-12-08 14:49:19 +01:00
prepare : prepareIDPLoginPolicyLinksQuery ,
2021-12-07 08:33:52 +01:00
want : want {
sqlExpectations : mockQueries (
2021-12-08 14:49:19 +01:00
loginPolicyIDPLinksQuery ,
loginPolicyIDPLinksCols ,
2021-12-07 08:33:52 +01:00
[ ] [ ] driver . Value {
{
"idp-id" ,
nil ,
nil ,
2023-02-28 21:20:58 +01:00
nil ,
2021-12-07 08:33:52 +01:00
} ,
} ,
) ,
} ,
2021-12-08 14:49:19 +01:00
object : & IDPLoginPolicyLinks {
2021-12-07 08:33:52 +01:00
SearchResponse : SearchResponse {
Count : 1 ,
} ,
2021-12-08 14:49:19 +01:00
Links : [ ] * IDPLoginPolicyLink {
2021-12-07 08:33:52 +01:00
{
2021-12-08 14:49:19 +01:00
IDPID : "idp-id" ,
IDPName : "" ,
2023-02-28 21:20:58 +01:00
IDPType : domain . IDPTypeUnspecified ,
2021-12-07 08:33:52 +01:00
} ,
} ,
} ,
} ,
{
name : "prepareIDPsQuery sql err" ,
2021-12-08 14:49:19 +01:00
prepare : prepareIDPLoginPolicyLinksQuery ,
2021-12-07 08:33:52 +01:00
want : want {
sqlExpectations : mockQueryErr (
2021-12-08 14:49:19 +01:00
loginPolicyIDPLinksQuery ,
2021-12-07 08:33:52 +01: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 ) {
2023-02-27 22:36:43 +01:00
assertPrepare ( t , tt . prepare , tt . object , tt . want . sqlExpectations , tt . want . err , defaultPrepareArgs ... )
2021-12-07 08:33:52 +01:00
} )
}
}