mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 19:14:23 +00:00
7d235e3eed
* feat: sms config * feat: twilio as sms provider * feat:sms projection * feat: sms queries * feat: sms queries test * feat: sms configs * feat: sms configs sql file * fix merge * fix: rename from to sendername * fix: proto comments * fix: token as crypto * fix: tests * fix: sms config sender name to sender number * fix: sms config sender name to sender number * Update email.go * Update channel.go * Update V1.111__settings.sql Co-authored-by: Livio Amstutz <livio.a@gmail.com>
327 lines
7.8 KiB
Go
327 lines
7.8 KiB
Go
package query
|
|
|
|
import (
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
"github.com/caos/zitadel/internal/domain"
|
|
errs "github.com/caos/zitadel/internal/errors"
|
|
)
|
|
|
|
var (
|
|
expectedSMSConfigQuery = regexp.QuoteMeta(`SELECT zitadel.projections.sms_configs.id,` +
|
|
` zitadel.projections.sms_configs.aggregate_id,` +
|
|
` zitadel.projections.sms_configs.creation_date,` +
|
|
` zitadel.projections.sms_configs.change_date,` +
|
|
` zitadel.projections.sms_configs.resource_owner,` +
|
|
` zitadel.projections.sms_configs.state,` +
|
|
` zitadel.projections.sms_configs.sequence,` +
|
|
|
|
// twilio config
|
|
` zitadel.projections.sms_configs_twilio.sms_id,` +
|
|
` zitadel.projections.sms_configs_twilio.sid,` +
|
|
` zitadel.projections.sms_configs_twilio.token,` +
|
|
` zitadel.projections.sms_configs_twilio.sender_number` +
|
|
` FROM zitadel.projections.sms_configs` +
|
|
` LEFT JOIN zitadel.projections.sms_configs_twilio ON zitadel.projections.sms_configs.id = zitadel.projections.sms_configs_twilio.sms_id`)
|
|
expectedSMSConfigsQuery = regexp.QuoteMeta(`SELECT zitadel.projections.sms_configs.id,` +
|
|
` zitadel.projections.sms_configs.aggregate_id,` +
|
|
` zitadel.projections.sms_configs.creation_date,` +
|
|
` zitadel.projections.sms_configs.change_date,` +
|
|
` zitadel.projections.sms_configs.resource_owner,` +
|
|
` zitadel.projections.sms_configs.state,` +
|
|
` zitadel.projections.sms_configs.sequence,` +
|
|
|
|
// twilio config
|
|
` zitadel.projections.sms_configs_twilio.sms_id,` +
|
|
` zitadel.projections.sms_configs_twilio.sid,` +
|
|
` zitadel.projections.sms_configs_twilio.token,` +
|
|
` zitadel.projections.sms_configs_twilio.sender_number,` +
|
|
` COUNT(*) OVER ()` +
|
|
` FROM zitadel.projections.sms_configs` +
|
|
` LEFT JOIN zitadel.projections.sms_configs_twilio ON zitadel.projections.sms_configs.id = zitadel.projections.sms_configs_twilio.sms_id`)
|
|
|
|
smsConfigCols = []string{
|
|
"id",
|
|
"aggregate_id",
|
|
"creation_date",
|
|
"change_date",
|
|
"resource_owner",
|
|
"state",
|
|
"sequence",
|
|
// twilio config
|
|
"sms_id",
|
|
"sid",
|
|
"token",
|
|
"sender-number",
|
|
}
|
|
smsConfigsCols = append(smsConfigCols, "count")
|
|
)
|
|
|
|
func Test_SMSConfigssPrepare(t *testing.T) {
|
|
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),
|
|
// twilio config
|
|
"sms-id",
|
|
"sid",
|
|
&crypto.CryptoValue{},
|
|
"sender-number",
|
|
},
|
|
},
|
|
),
|
|
},
|
|
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,
|
|
TwilioConfig: &Twilio{
|
|
SID: "sid",
|
|
Token: &crypto.CryptoValue{},
|
|
SenderNumber: "sender-number",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "prepareSMSConfigsQuery multiple result",
|
|
prepare: prepareSMSConfigsQuery,
|
|
want: want{
|
|
sqlExpectations: mockQueries(
|
|
expectedSMSConfigsQuery,
|
|
smsConfigsCols,
|
|
[][]driver.Value{
|
|
{
|
|
"sms-id",
|
|
"agg-id",
|
|
testNow,
|
|
testNow,
|
|
"ro",
|
|
domain.SMSConfigStateInactive,
|
|
uint64(20211109),
|
|
// twilio config
|
|
"sms-id",
|
|
"sid",
|
|
&crypto.CryptoValue{},
|
|
"sender-number",
|
|
},
|
|
{
|
|
"sms-id2",
|
|
"agg-id",
|
|
testNow,
|
|
testNow,
|
|
"ro",
|
|
domain.SMSConfigStateInactive,
|
|
uint64(20211109),
|
|
// twilio config
|
|
"sms-id2",
|
|
"sid2",
|
|
&crypto.CryptoValue{},
|
|
"sender-number2",
|
|
},
|
|
},
|
|
),
|
|
},
|
|
object: &SMSConfigs{
|
|
SearchResponse: SearchResponse{
|
|
Count: 2,
|
|
},
|
|
Configs: []*SMSConfig{
|
|
{
|
|
ID: "sms-id",
|
|
AggregateID: "agg-id",
|
|
CreationDate: testNow,
|
|
ChangeDate: testNow,
|
|
ResourceOwner: "ro",
|
|
State: domain.SMSConfigStateInactive,
|
|
Sequence: 20211109,
|
|
TwilioConfig: &Twilio{
|
|
SID: "sid",
|
|
Token: &crypto.CryptoValue{},
|
|
SenderNumber: "sender-number",
|
|
},
|
|
},
|
|
{
|
|
ID: "sms-id2",
|
|
AggregateID: "agg-id",
|
|
CreationDate: testNow,
|
|
ChangeDate: testNow,
|
|
ResourceOwner: "ro",
|
|
State: domain.SMSConfigStateInactive,
|
|
Sequence: 20211109,
|
|
TwilioConfig: &Twilio{
|
|
SID: "sid2",
|
|
Token: &crypto.CryptoValue{},
|
|
SenderNumber: "sender-number2",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
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
|
|
},
|
|
},
|
|
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)
|
|
})
|
|
}
|
|
}
|
|
|
|
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{
|
|
sqlExpectations: mockQueries(
|
|
expectedSMSConfigQuery,
|
|
nil,
|
|
nil,
|
|
),
|
|
err: func(err error) (error, bool) {
|
|
if !errs.IsNotFound(err) {
|
|
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
|
|
}
|
|
return nil, true
|
|
},
|
|
},
|
|
object: (*SMSConfig)(nil),
|
|
},
|
|
{
|
|
name: "prepareSMSConfigQuery found",
|
|
prepare: prepareSMSConfigQuery,
|
|
want: want{
|
|
sqlExpectations: mockQuery(
|
|
expectedSMSConfigQuery,
|
|
smsConfigCols,
|
|
[]driver.Value{
|
|
"sms-id",
|
|
"agg-id",
|
|
testNow,
|
|
testNow,
|
|
"ro",
|
|
domain.SMSConfigStateInactive,
|
|
uint64(20211109),
|
|
// twilio config
|
|
"sms-id",
|
|
"sid",
|
|
&crypto.CryptoValue{},
|
|
"sender-number",
|
|
},
|
|
),
|
|
},
|
|
object: &SMSConfig{
|
|
ID: "sms-id",
|
|
AggregateID: "agg-id",
|
|
CreationDate: testNow,
|
|
ChangeDate: testNow,
|
|
ResourceOwner: "ro",
|
|
State: domain.SMSConfigStateInactive,
|
|
Sequence: 20211109,
|
|
TwilioConfig: &Twilio{
|
|
SID: "sid",
|
|
SenderNumber: "sender-number",
|
|
Token: &crypto.CryptoValue{},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
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
|
|
},
|
|
},
|
|
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)
|
|
})
|
|
}
|
|
}
|