2022-02-21 12:22:20 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-06-13 06:34:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-02-21 12:22:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSMSProjection_reduces(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
event func(t *testing.T) eventstore.Event
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
reduce func(event eventstore.Event) (*handler.Statement, error)
|
|
|
|
want wantReduce
|
|
|
|
}{
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "instance.reduceSMSTwilioAdded",
|
2022-02-21 12:22:20 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMSConfigTwilioAddedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-21 12:22:20 +00:00
|
|
|
[]byte(`{
|
|
|
|
"id": "id",
|
|
|
|
"sid": "sid",
|
|
|
|
"token": {
|
|
|
|
"cryptoType": 0,
|
|
|
|
"algorithm": "RSA-265",
|
2022-06-13 06:34:11 +00:00
|
|
|
"keyId": "key-id",
|
|
|
|
"crypted": "Y3J5cHRlZA=="
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
|
|
|
"senderNumber": "sender-number"
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMSConfigTwilioAddedEventMapper),
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smsConfigProjection{}).reduceSMSConfigTwilioAdded,
|
2022-02-21 12:22:20 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-21 12:22:20 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-06-13 06:34:11 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.sms_configs (id, aggregate_id, creation_date, change_date, resource_owner, instance_id, state, sequence) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
2022-02-21 12:22:20 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"id",
|
|
|
|
"agg-id",
|
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-02-21 12:22:20 +00:00
|
|
|
domain.SMSConfigStateInactive,
|
|
|
|
uint64(15),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.sms_configs_twilio (sms_id, instance_id, sid, token, sender_number) VALUES ($1, $2, $3, $4, $5)",
|
2022-02-21 12:22:20 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-21 12:22:20 +00:00
|
|
|
"sid",
|
2022-06-13 06:34:11 +00:00
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "RSA-265",
|
|
|
|
KeyID: "key-id",
|
|
|
|
Crypted: []byte("crypted"),
|
|
|
|
},
|
2022-02-21 12:22:20 +00:00
|
|
|
"sender-number",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "instance.reduceSMSConfigTwilioChanged",
|
2022-02-21 12:22:20 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMSConfigTwilioChangedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-21 12:22:20 +00:00
|
|
|
[]byte(`{
|
|
|
|
"id": "id",
|
|
|
|
"sid": "sid",
|
|
|
|
"senderNumber": "sender-number"
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMSConfigTwilioChangedEventMapper),
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smsConfigProjection{}).reduceSMSConfigTwilioChanged,
|
2022-02-21 12:22:20 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-21 12:22:20 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "UPDATE projections.sms_configs_twilio SET (sid, sender_number) = ($1, $2) WHERE (sms_id = $3) AND (instance_id = $4)",
|
2022-02-21 12:22:20 +00:00
|
|
|
expectedArgs: []interface{}{
|
2022-06-13 06:34:11 +00:00
|
|
|
"sid",
|
|
|
|
"sender-number",
|
|
|
|
"id",
|
|
|
|
"instance-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expectedStmt: "UPDATE projections.sms_configs SET (change_date, sequence) = ($1, $2) WHERE (id = $3) AND (instance_id = $4)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"id",
|
|
|
|
"instance-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "instance.reduceSMSConfigTwilioTokenChanged",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(instance.SMSConfigTwilioTokenChangedEventType),
|
|
|
|
instance.AggregateType,
|
|
|
|
[]byte(`{
|
|
|
|
"id": "id",
|
|
|
|
"token": {
|
|
|
|
"cryptoType": 0,
|
|
|
|
"algorithm": "RSA-265",
|
|
|
|
"keyId": "key-id",
|
|
|
|
"crypted": "Y3J5cHRlZA=="
|
|
|
|
}
|
|
|
|
}`),
|
|
|
|
), instance.SMSConfigTwilioTokenChangedEventMapper),
|
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smsConfigProjection{}).reduceSMSConfigTwilioTokenChanged,
|
2022-06-13 06:34:11 +00:00
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-09-01 07:24:26 +00:00
|
|
|
expectedStmt: "UPDATE projections.sms_configs_twilio SET token = $1 WHERE (sms_id = $2) AND (instance_id = $3)",
|
2022-06-13 06:34:11 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
&crypto.CryptoValue{
|
|
|
|
CryptoType: crypto.TypeEncryption,
|
|
|
|
Algorithm: "RSA-265",
|
|
|
|
KeyID: "key-id",
|
|
|
|
Crypted: []byte("crypted"),
|
|
|
|
},
|
2022-02-21 12:22:20 +00:00
|
|
|
"id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "UPDATE projections.sms_configs SET (change_date, sequence) = ($1, $2) WHERE (id = $3) AND (instance_id = $4)",
|
2022-02-21 12:22:20 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "instance.reduceSMSConfigActivated",
|
2022-02-21 12:22:20 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMSConfigActivatedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-21 12:22:20 +00:00
|
|
|
[]byte(`{
|
|
|
|
"id": "id"
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMSConfigActivatedEventMapper),
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smsConfigProjection{}).reduceSMSConfigActivated,
|
2022-02-21 12:22:20 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-21 12:22:20 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "UPDATE projections.sms_configs SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
|
2022-02-21 12:22:20 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
domain.SMSConfigStateActive,
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "instance.reduceSMSConfigDeactivated",
|
2022-02-21 12:22:20 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMSConfigDeactivatedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-21 12:22:20 +00:00
|
|
|
[]byte(`{
|
|
|
|
"id": "id"
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMSConfigDeactivatedEventMapper),
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smsConfigProjection{}).reduceSMSConfigDeactivated,
|
2022-02-21 12:22:20 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-21 12:22:20 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "UPDATE projections.sms_configs SET (state, change_date, sequence) = ($1, $2, $3) WHERE (id = $4) AND (instance_id = $5)",
|
2022-02-21 12:22:20 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
domain.SMSConfigStateInactive,
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
"id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
name: "instance.reduceSMSConfigRemoved",
|
2022-02-21 12:22:20 +00:00
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMSConfigRemovedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-21 12:22:20 +00:00
|
|
|
[]byte(`{
|
|
|
|
"id": "id"
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMSConfigRemovedEventMapper),
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smsConfigProjection{}).reduceSMSConfigRemoved,
|
2022-02-21 12:22:20 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-21 12:22:20 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "DELETE FROM projections.sms_configs WHERE (id = $1) AND (instance_id = $2)",
|
2022-02-21 12:22:20 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-21 12:22:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
name: "instance.reduceInstanceRemoved",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
|
|
|
repository.EventType(instance.InstanceRemovedEventType),
|
|
|
|
instance.AggregateType,
|
2022-10-26 13:06:48 +00:00
|
|
|
nil,
|
2022-10-20 12:36:52 +00:00
|
|
|
), instance.InstanceRemovedEventMapper),
|
|
|
|
},
|
|
|
|
reduce: reduceInstanceRemovedHelper(SMSColumnInstanceID),
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
|
|
|
expectedStmt: "DELETE FROM projections.sms_configs WHERE (instance_id = $1)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-21 12:22:20 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
event := baseEvent(t)
|
|
|
|
got, err := tt.reduce(event)
|
|
|
|
if _, ok := err.(errors.InvalidArgument); !ok {
|
|
|
|
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
event = tt.args.event(t)
|
|
|
|
got, err = tt.reduce(event)
|
2022-10-20 12:36:52 +00:00
|
|
|
assertReduce(t, got, err, SMSConfigProjectionTable, tt.want)
|
2022-02-21 12:22:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|