2022-02-16 15:49:17 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"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-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSMTPConfigProjection_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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reduceSMTPConfigChanged",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMTPConfigChangedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-16 15:49:17 +00:00
|
|
|
[]byte(`{
|
|
|
|
"tls": true,
|
|
|
|
"senderAddress": "sender",
|
|
|
|
"senderName": "name",
|
|
|
|
"host": "host",
|
|
|
|
"user": "user"
|
|
|
|
}`,
|
|
|
|
),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMTPConfigChangedEventMapper),
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smtpConfigProjection{}).reduceSMTPConfigChanged,
|
2022-02-16 15:49:17 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-16 15:49:17 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "UPDATE projections.smtp_configs SET (change_date, sequence, tls, sender_address, sender_name, host, username) = ($1, $2, $3, $4, $5, $6, $7) WHERE (aggregate_id = $8) AND (instance_id = $9)",
|
2022-02-16 15:49:17 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
true,
|
|
|
|
"sender",
|
|
|
|
"name",
|
|
|
|
"host",
|
|
|
|
"user",
|
|
|
|
"agg-id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceSMTPConfigAdded",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMTPConfigAddedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-16 15:49:17 +00:00
|
|
|
[]byte(`{
|
|
|
|
"tls": true,
|
|
|
|
"senderAddress": "sender",
|
|
|
|
"senderName": "name",
|
|
|
|
"host": "host",
|
|
|
|
"user": "user",
|
|
|
|
"password": {
|
|
|
|
"cryptoType": 0,
|
|
|
|
"algorithm": "RSA-265",
|
|
|
|
"keyId": "key-id"
|
|
|
|
}
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMTPConfigAddedEventMapper),
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smtpConfigProjection{}).reduceSMTPConfigAdded,
|
2022-02-16 15:49:17 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-16 15:49:17 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-03-23 08:02:39 +00:00
|
|
|
expectedStmt: "INSERT INTO projections.smtp_configs (aggregate_id, creation_date, change_date, resource_owner, instance_id, sequence, tls, sender_address, sender_name, host, username, password) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)",
|
2022-02-16 15:49:17 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
anyArg{},
|
|
|
|
anyArg{},
|
|
|
|
"ro-id",
|
2022-03-23 08:02:39 +00:00
|
|
|
"instance-id",
|
2022-02-16 15:49:17 +00:00
|
|
|
uint64(15),
|
|
|
|
true,
|
|
|
|
"sender",
|
|
|
|
"name",
|
|
|
|
"host",
|
|
|
|
"user",
|
|
|
|
anyArg{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reduceSMTPConfigPasswordChanged",
|
|
|
|
args: args{
|
|
|
|
event: getEvent(testEvent(
|
2022-03-24 16:21:34 +00:00
|
|
|
repository.EventType(instance.SMTPConfigPasswordChangedEventType),
|
|
|
|
instance.AggregateType,
|
2022-02-16 15:49:17 +00:00
|
|
|
[]byte(`{
|
|
|
|
"password": {
|
|
|
|
"cryptoType": 0,
|
|
|
|
"algorithm": "RSA-265",
|
|
|
|
"keyId": "key-id"
|
|
|
|
}
|
|
|
|
}`),
|
2022-03-24 16:21:34 +00:00
|
|
|
), instance.SMTPConfigPasswordChangedEventMapper),
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
2022-06-14 05:51:00 +00:00
|
|
|
reduce: (&smtpConfigProjection{}).reduceSMTPConfigPasswordChanged,
|
2022-02-16 15:49:17 +00:00
|
|
|
want: wantReduce{
|
2022-03-24 16:21:34 +00:00
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
2022-02-16 15:49:17 +00:00
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
2022-05-25 12:15:13 +00:00
|
|
|
expectedStmt: "UPDATE projections.smtp_configs SET (change_date, sequence, password) = ($1, $2, $3) WHERE (aggregate_id = $4) AND (instance_id = $5)",
|
2022-02-16 15:49:17 +00:00
|
|
|
expectedArgs: []interface{}{
|
|
|
|
anyArg{},
|
|
|
|
uint64(15),
|
|
|
|
anyArg{},
|
|
|
|
"agg-id",
|
2022-05-25 12:15:13 +00:00
|
|
|
"instance-id",
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
2022-11-10 10:59:33 +00:00
|
|
|
name: "instance reduceInstanceRemoved",
|
2022-10-20 12:36:52 +00:00
|
|
|
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(SMTPConfigColumnInstanceID),
|
|
|
|
want: wantReduce{
|
|
|
|
aggregateType: eventstore.AggregateType("instance"),
|
|
|
|
sequence: 15,
|
|
|
|
previousSequence: 10,
|
|
|
|
executer: &testExecuter{
|
|
|
|
executions: []execution{
|
|
|
|
{
|
|
|
|
expectedStmt: "DELETE FROM projections.smtp_configs WHERE (instance_id = $1)",
|
|
|
|
expectedArgs: []interface{}{
|
|
|
|
"agg-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-02-16 15:49:17 +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, SMTPConfigProjectionTable, tt.want)
|
2022-02-16 15:49:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|