2022-02-21 12:22:20 +00:00
package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
2022-04-26 23:01:45 +00:00
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/domain"
2023-12-08 14:30:55 +00:00
"github.com/zitadel/zitadel/internal/zerrors"
2022-02-21 12:22:20 +00:00
)
var (
2024-09-06 13:11:36 +00:00
expectedSMSConfigQuery = regexp . QuoteMeta ( ` SELECT projections.sms_configs3.id, ` +
` projections.sms_configs3.aggregate_id, ` +
` projections.sms_configs3.creation_date, ` +
` projections.sms_configs3.change_date, ` +
` projections.sms_configs3.resource_owner, ` +
` projections.sms_configs3.state, ` +
` projections.sms_configs3.sequence, ` +
` projections.sms_configs3.description, ` +
2022-02-21 12:22:20 +00:00
// twilio config
2024-09-06 13:11:36 +00:00
` projections.sms_configs3_twilio.sms_id, ` +
` projections.sms_configs3_twilio.sid, ` +
` projections.sms_configs3_twilio.token, ` +
` projections.sms_configs3_twilio.sender_number, ` +
2024-09-26 07:14:33 +00:00
` projections.sms_configs3_twilio.verify_service_sid, ` +
2024-09-06 13:11:36 +00:00
// http config
` projections.sms_configs3_http.sms_id, ` +
` projections.sms_configs3_http.endpoint ` +
` FROM projections.sms_configs3 ` +
` LEFT JOIN projections.sms_configs3_twilio ON projections.sms_configs3.id = projections.sms_configs3_twilio.sms_id AND projections.sms_configs3.instance_id = projections.sms_configs3_twilio.instance_id ` +
` LEFT JOIN projections.sms_configs3_http ON projections.sms_configs3.id = projections.sms_configs3_http.sms_id AND projections.sms_configs3.instance_id = projections.sms_configs3_http.instance_id ` +
2023-02-27 21:36:43 +00:00
` AS OF SYSTEM TIME '-1 ms' ` )
2024-09-06 13:11:36 +00:00
expectedSMSConfigsQuery = regexp . QuoteMeta ( ` SELECT projections.sms_configs3.id, ` +
` projections.sms_configs3.aggregate_id, ` +
` projections.sms_configs3.creation_date, ` +
` projections.sms_configs3.change_date, ` +
` projections.sms_configs3.resource_owner, ` +
` projections.sms_configs3.state, ` +
` projections.sms_configs3.sequence, ` +
` projections.sms_configs3.description, ` +
2022-02-21 12:22:20 +00:00
// twilio config
2024-09-06 13:11:36 +00:00
` projections.sms_configs3_twilio.sms_id, ` +
` projections.sms_configs3_twilio.sid, ` +
` projections.sms_configs3_twilio.token, ` +
` projections.sms_configs3_twilio.sender_number, ` +
2024-09-26 07:14:33 +00:00
` projections.sms_configs3_twilio.verify_service_sid, ` +
2024-09-06 13:11:36 +00:00
// http config
` projections.sms_configs3_http.sms_id, ` +
` projections.sms_configs3_http.endpoint, ` +
2022-02-21 12:22:20 +00:00
` COUNT(*) OVER () ` +
2024-09-06 13:11:36 +00:00
` FROM projections.sms_configs3 ` +
` LEFT JOIN projections.sms_configs3_twilio ON projections.sms_configs3.id = projections.sms_configs3_twilio.sms_id AND projections.sms_configs3.instance_id = projections.sms_configs3_twilio.instance_id ` +
` LEFT JOIN projections.sms_configs3_http ON projections.sms_configs3.id = projections.sms_configs3_http.sms_id AND projections.sms_configs3.instance_id = projections.sms_configs3_http.instance_id ` +
2023-02-27 21:36:43 +00:00
` AS OF SYSTEM TIME '-1 ms' ` )
2022-02-21 12:22:20 +00:00
smsConfigCols = [ ] string {
"id" ,
"aggregate_id" ,
"creation_date" ,
"change_date" ,
"resource_owner" ,
"state" ,
"sequence" ,
2024-09-06 13:11:36 +00:00
"description" ,
2022-02-21 12:22:20 +00:00
// twilio config
"sms_id" ,
"sid" ,
"token" ,
"sender-number" ,
2024-09-26 07:14:33 +00:00
"verify_sid" ,
2024-09-06 13:11:36 +00:00
// http config
"sms_id" ,
"endpoint" ,
2022-02-21 12:22:20 +00:00
}
smsConfigsCols = append ( smsConfigCols , "count" )
)
2024-09-06 13:11:36 +00:00
func Test_SMSConfigsPrepare ( t * testing . T ) {
2022-02-21 12:22:20 +00:00
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := [ ] struct {
name string
prepare interface { }
want want
object interface { }
} {
{
name : "prepareSMSConfigsQuery no result" ,
prepare : prepareSMSConfigsQuery ,
want : want {
sqlExpectations : mockQueries (
expectedSMSConfigsQuery ,
nil ,
nil ,
) ,
} ,
object : & SMSConfigs { Configs : [ ] * SMSConfig { } } ,
} ,
{
name : "prepareSMSQuery twilio config" ,
prepare : prepareSMSConfigsQuery ,
want : want {
sqlExpectations : mockQueries (
expectedSMSConfigsQuery ,
smsConfigsCols ,
[ ] [ ] driver . Value {
{
"sms-id" ,
"agg-id" ,
testNow ,
testNow ,
"ro" ,
domain . SMSConfigStateInactive ,
uint64 ( 20211109 ) ,
2024-09-06 13:11:36 +00:00
"description" ,
2022-02-21 12:22:20 +00:00
// twilio config
"sms-id" ,
"sid" ,
& crypto . CryptoValue { } ,
"sender-number" ,
2024-09-26 07:14:33 +00:00
"" ,
2024-09-06 13:11:36 +00:00
// http config
nil ,
nil ,
2022-02-21 12:22:20 +00:00
} ,
} ,
) ,
} ,
object : & SMSConfigs {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Configs : [ ] * SMSConfig {
{
ID : "sms-id" ,
AggregateID : "agg-id" ,
CreationDate : testNow ,
ChangeDate : testNow ,
ResourceOwner : "ro" ,
State : domain . SMSConfigStateInactive ,
Sequence : 20211109 ,
2024-09-06 13:11:36 +00:00
Description : "description" ,
2022-02-21 12:22:20 +00:00
TwilioConfig : & Twilio {
2024-09-26 07:14:33 +00:00
SID : "sid" ,
Token : & crypto . CryptoValue { } ,
SenderNumber : "sender-number" ,
VerifyServiceSID : "" ,
2022-02-21 12:22:20 +00:00
} ,
} ,
} ,
} ,
} ,
{
2024-09-06 13:11:36 +00:00
name : "prepareSMSQuery http config" ,
2022-02-21 12:22:20 +00:00
prepare : prepareSMSConfigsQuery ,
want : want {
sqlExpectations : mockQueries (
expectedSMSConfigsQuery ,
smsConfigsCols ,
[ ] [ ] driver . Value {
{
"sms-id" ,
"agg-id" ,
testNow ,
testNow ,
"ro" ,
domain . SMSConfigStateInactive ,
uint64 ( 20211109 ) ,
2024-09-06 13:11:36 +00:00
"description" ,
// twilio config
nil ,
nil ,
nil ,
nil ,
2024-09-26 07:14:33 +00:00
nil ,
2024-09-06 13:11:36 +00:00
// http config
"sms-id" ,
"endpoint" ,
} ,
} ,
) ,
} ,
object : & SMSConfigs {
SearchResponse : SearchResponse {
Count : 1 ,
} ,
Configs : [ ] * SMSConfig {
{
ID : "sms-id" ,
AggregateID : "agg-id" ,
CreationDate : testNow ,
ChangeDate : testNow ,
ResourceOwner : "ro" ,
State : domain . SMSConfigStateInactive ,
Sequence : 20211109 ,
Description : "description" ,
HTTPConfig : & HTTP {
Endpoint : "endpoint" ,
} ,
} ,
} ,
} ,
} ,
{
name : "prepareSMSConfigsQuery multiple result" ,
prepare : prepareSMSConfigsQuery ,
want : want {
sqlExpectations : mockQueries (
expectedSMSConfigsQuery ,
smsConfigsCols ,
[ ] [ ] driver . Value {
{
"sms-id" ,
"agg-id" ,
testNow ,
testNow ,
"ro" ,
domain . SMSConfigStateActive ,
uint64 ( 20211109 ) ,
"description" ,
2022-02-21 12:22:20 +00:00
// twilio config
"sms-id" ,
"sid" ,
& crypto . CryptoValue { } ,
"sender-number" ,
2024-09-26 07:14:33 +00:00
"verify-service-sid" ,
2024-09-06 13:11:36 +00:00
// http config
nil ,
nil ,
2022-02-21 12:22:20 +00:00
} ,
{
"sms-id2" ,
"agg-id" ,
testNow ,
testNow ,
"ro" ,
domain . SMSConfigStateInactive ,
uint64 ( 20211109 ) ,
2024-09-06 13:11:36 +00:00
"description" ,
2022-02-21 12:22:20 +00:00
// twilio config
"sms-id2" ,
"sid2" ,
& crypto . CryptoValue { } ,
"sender-number2" ,
2024-09-26 07:14:33 +00:00
"verify-service-sid2" ,
2024-09-06 13:11:36 +00:00
// http config
nil ,
nil ,
} ,
{
"sms-id3" ,
"agg-id" ,
testNow ,
testNow ,
"ro" ,
domain . SMSConfigStateInactive ,
uint64 ( 20211109 ) ,
"description" ,
// twilio config
nil ,
nil ,
nil ,
nil ,
2024-09-26 07:14:33 +00:00
nil ,
2024-09-06 13:11:36 +00:00
// http config
"sms-id3" ,
"endpoint3" ,
2022-02-21 12:22:20 +00:00
} ,
} ,
) ,
} ,
object : & SMSConfigs {
SearchResponse : SearchResponse {
2024-09-06 13:11:36 +00:00
Count : 3 ,
2022-02-21 12:22:20 +00:00
} ,
Configs : [ ] * SMSConfig {
{
ID : "sms-id" ,
AggregateID : "agg-id" ,
CreationDate : testNow ,
ChangeDate : testNow ,
ResourceOwner : "ro" ,
2024-09-06 13:11:36 +00:00
State : domain . SMSConfigStateActive ,
2022-02-21 12:22:20 +00:00
Sequence : 20211109 ,
2024-09-06 13:11:36 +00:00
Description : "description" ,
2022-02-21 12:22:20 +00:00
TwilioConfig : & Twilio {
2024-09-26 07:14:33 +00:00
SID : "sid" ,
Token : & crypto . CryptoValue { } ,
SenderNumber : "sender-number" ,
VerifyServiceSID : "verify-service-sid" ,
2022-02-21 12:22:20 +00:00
} ,
} ,
{
ID : "sms-id2" ,
AggregateID : "agg-id" ,
CreationDate : testNow ,
ChangeDate : testNow ,
ResourceOwner : "ro" ,
State : domain . SMSConfigStateInactive ,
Sequence : 20211109 ,
2024-09-06 13:11:36 +00:00
Description : "description" ,
2022-02-21 12:22:20 +00:00
TwilioConfig : & Twilio {
2024-09-26 07:14:33 +00:00
SID : "sid2" ,
Token : & crypto . CryptoValue { } ,
SenderNumber : "sender-number2" ,
VerifyServiceSID : "verify-service-sid2" ,
2022-02-21 12:22:20 +00:00
} ,
} ,
2024-09-06 13:11:36 +00:00
{
ID : "sms-id3" ,
AggregateID : "agg-id" ,
CreationDate : testNow ,
ChangeDate : testNow ,
ResourceOwner : "ro" ,
State : domain . SMSConfigStateInactive ,
Sequence : 20211109 ,
Description : "description" ,
HTTPConfig : & HTTP {
Endpoint : "endpoint3" ,
} ,
} ,
2022-02-21 12:22:20 +00:00
} ,
} ,
} ,
{
name : "prepareSMSConfigsQuery sql err" ,
prepare : prepareSMSConfigsQuery ,
want : want {
sqlExpectations : mockQueryErr (
expectedSMSConfigsQuery ,
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
} ,
} ,
2023-08-22 10:49:22 +00:00
object : ( * SMSConfigs ) ( nil ) ,
2022-02-21 12:22:20 +00:00
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-02-27 21:36:43 +00:00
assertPrepare ( t , tt . prepare , tt . object , tt . want . sqlExpectations , tt . want . err , defaultPrepareArgs ... )
2022-02-21 12:22:20 +00:00
} )
}
}
func Test_SMSConfigPrepare ( t * testing . T ) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := [ ] struct {
name string
prepare interface { }
want want
object interface { }
} {
{
name : "prepareSMSConfigQuery no result" ,
prepare : prepareSMSConfigQuery ,
want : want {
2023-08-22 10:49:22 +00:00
sqlExpectations : mockQueriesScanErr (
2022-02-21 12:22:20 +00:00
expectedSMSConfigQuery ,
nil ,
nil ,
) ,
err : func ( err error ) ( error , bool ) {
2023-12-08 14:30:55 +00:00
if ! zerrors . IsNotFound ( err ) {
2022-02-21 12:22:20 +00:00
return fmt . Errorf ( "err should be zitadel.NotFoundError got: %w" , err ) , false
}
return nil , true
} ,
} ,
object : ( * SMSConfig ) ( nil ) ,
} ,
{
2024-09-06 13:11:36 +00:00
name : "prepareSMSConfigQuery, twilio, found" ,
2022-02-21 12:22:20 +00:00
prepare : prepareSMSConfigQuery ,
want : want {
sqlExpectations : mockQuery (
expectedSMSConfigQuery ,
smsConfigCols ,
[ ] driver . Value {
"sms-id" ,
"agg-id" ,
testNow ,
testNow ,
"ro" ,
2024-09-06 13:11:36 +00:00
domain . SMSConfigStateActive ,
2022-02-21 12:22:20 +00:00
uint64 ( 20211109 ) ,
2024-09-06 13:11:36 +00:00
"description" ,
2022-02-21 12:22:20 +00:00
// twilio config
"sms-id" ,
"sid" ,
& crypto . CryptoValue { } ,
"sender-number" ,
2024-09-26 07:14:33 +00:00
"verify-service-sid" ,
2024-09-06 13:11:36 +00:00
// http config
nil ,
nil ,
2022-02-21 12:22:20 +00:00
} ,
) ,
} ,
object : & SMSConfig {
ID : "sms-id" ,
AggregateID : "agg-id" ,
CreationDate : testNow ,
ChangeDate : testNow ,
ResourceOwner : "ro" ,
2024-09-06 13:11:36 +00:00
State : domain . SMSConfigStateActive ,
2022-02-21 12:22:20 +00:00
Sequence : 20211109 ,
2024-09-06 13:11:36 +00:00
Description : "description" ,
2022-02-21 12:22:20 +00:00
TwilioConfig : & Twilio {
2024-09-26 07:14:33 +00:00
SID : "sid" ,
SenderNumber : "sender-number" ,
Token : & crypto . CryptoValue { } ,
VerifyServiceSID : "verify-service-sid" ,
2022-02-21 12:22:20 +00:00
} ,
} ,
} ,
2024-09-06 13:11:36 +00:00
{
name : "prepareSMSConfigQuery, http, found" ,
prepare : prepareSMSConfigQuery ,
want : want {
sqlExpectations : mockQuery (
expectedSMSConfigQuery ,
smsConfigCols ,
[ ] driver . Value {
"sms-id" ,
"agg-id" ,
testNow ,
testNow ,
"ro" ,
domain . SMSConfigStateInactive ,
uint64 ( 20211109 ) ,
"description" ,
// twilio config
nil ,
nil ,
nil ,
nil ,
2024-09-26 07:14:33 +00:00
nil ,
2024-09-06 13:11:36 +00:00
// http config
"sms-id" ,
"endpoint" ,
} ,
) ,
} ,
object : & SMSConfig {
ID : "sms-id" ,
AggregateID : "agg-id" ,
CreationDate : testNow ,
ChangeDate : testNow ,
ResourceOwner : "ro" ,
State : domain . SMSConfigStateInactive ,
Sequence : 20211109 ,
Description : "description" ,
HTTPConfig : & HTTP {
Endpoint : "endpoint" ,
} ,
} ,
} ,
2022-02-21 12:22:20 +00:00
{
name : "prepareSMSConfigQuery sql err" ,
prepare : prepareSMSConfigQuery ,
want : want {
sqlExpectations : mockQueryErr (
expectedSMSConfigQuery ,
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
} ,
} ,
2023-08-22 10:49:22 +00:00
object : ( * SMSConfig ) ( nil ) ,
2022-02-21 12:22:20 +00:00
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-02-27 21:36:43 +00:00
assertPrepare ( t , tt . prepare , tt . object , tt . want . sqlExpectations , tt . want . err , defaultPrepareArgs ... )
2022-02-21 12:22:20 +00:00
} )
}
}