2022-02-16 16:49:17 +01:00
package projection
import (
"testing"
2024-04-11 09:16:10 +02:00
"github.com/zitadel/zitadel/internal/domain"
2022-04-27 01:01:45 +02:00
"github.com/zitadel/zitadel/internal/eventstore"
2023-10-19 12:19:10 +02:00
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
2022-04-27 01:01:45 +02:00
"github.com/zitadel/zitadel/internal/repository/instance"
2023-12-08 16:30:55 +02:00
"github.com/zitadel/zitadel/internal/zerrors"
2022-02-16 16:49:17 +01: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 {
2023-10-19 12:19:10 +02:00
event : getEvent (
testEvent (
instance . SMTPConfigChangedEventType ,
instance . AggregateType ,
[ ] byte ( ` {
2024-04-11 09:16:10 +02:00
"description" : "test" ,
2022-02-16 16:49:17 +01:00
"tls" : true ,
"senderAddress" : "sender" ,
"senderName" : "name" ,
2023-08-29 09:08:24 +02:00
"replyToAddress" : "reply-to" ,
2022-02-16 16:49:17 +01:00
"host" : "host" ,
2024-04-11 09:16:10 +02:00
"user" : "user" ,
"id" : "44444" ,
"resource_owner" : "ro-id" ,
"instance_id" : "instance-id"
2022-02-16 16:49:17 +01:00
} ` ,
2023-10-19 12:19:10 +02:00
) ,
) , instance . SMTPConfigChangedEventMapper ) ,
2022-02-16 16:49:17 +01:00
} ,
2022-06-14 07:51:00 +02:00
reduce : ( & smtpConfigProjection { } ) . reduceSMTPConfigChanged ,
2022-02-16 16:49:17 +01:00
want : wantReduce {
2023-10-19 12:19:10 +02:00
aggregateType : eventstore . AggregateType ( "instance" ) ,
sequence : 15 ,
2022-02-16 16:49:17 +01:00
executer : & testExecuter {
executions : [ ] execution {
{
2024-04-11 09:16:10 +02:00
expectedStmt : "UPDATE projections.smtp_configs2 SET (change_date, sequence, tls, sender_address, sender_name, reply_to_address, host, username, description) = ($1, $2, $3, $4, $5, $6, $7, $8, $9) WHERE (id = $10) AND (resource_owner = $11) AND (instance_id = $12)" ,
2022-02-16 16:49:17 +01:00
expectedArgs : [ ] interface { } {
anyArg { } ,
uint64 ( 15 ) ,
true ,
"sender" ,
"name" ,
2023-08-29 09:08:24 +02:00
"reply-to" ,
2022-02-16 16:49:17 +01:00
"host" ,
"user" ,
2024-04-11 09:16:10 +02:00
"test" ,
"44444" ,
"ro-id" ,
2022-05-25 14:15:13 +02:00
"instance-id" ,
2022-02-16 16:49:17 +01:00
} ,
} ,
} ,
} ,
} ,
} ,
{
name : "reduceSMTPConfigAdded" ,
args : args {
2023-10-19 12:19:10 +02:00
event : getEvent (
testEvent (
instance . SMTPConfigAddedEventType ,
instance . AggregateType ,
[ ] byte ( ` {
2022-02-16 16:49:17 +01:00
"tls" : true ,
2024-04-11 09:16:10 +02:00
"id" : "id" ,
"description" : "test" ,
2022-02-16 16:49:17 +01:00
"senderAddress" : "sender" ,
"senderName" : "name" ,
2023-08-29 09:08:24 +02:00
"replyToAddress" : "reply-to" ,
2022-02-16 16:49:17 +01:00
"host" : "host" ,
"user" : "user" ,
"password" : {
"cryptoType" : 0 ,
"algorithm" : "RSA-265" ,
"keyId" : "key-id"
}
} ` ) ,
2023-10-19 12:19:10 +02:00
) , instance . SMTPConfigAddedEventMapper ) ,
2022-02-16 16:49:17 +01:00
} ,
2022-06-14 07:51:00 +02:00
reduce : ( & smtpConfigProjection { } ) . reduceSMTPConfigAdded ,
2022-02-16 16:49:17 +01:00
want : wantReduce {
2023-10-19 12:19:10 +02:00
aggregateType : eventstore . AggregateType ( "instance" ) ,
sequence : 15 ,
2022-02-16 16:49:17 +01:00
executer : & testExecuter {
executions : [ ] execution {
{
2024-04-11 09:16:10 +02:00
expectedStmt : "INSERT INTO projections.smtp_configs2 (creation_date, change_date, resource_owner, instance_id, sequence, id, tls, sender_address, sender_name, reply_to_address, host, username, password, state, description) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)" ,
2022-02-16 16:49:17 +01:00
expectedArgs : [ ] interface { } {
anyArg { } ,
anyArg { } ,
"ro-id" ,
2022-03-23 09:02:39 +01:00
"instance-id" ,
2022-02-16 16:49:17 +01:00
uint64 ( 15 ) ,
2024-04-11 09:16:10 +02:00
"id" ,
2022-02-16 16:49:17 +01:00
true ,
"sender" ,
"name" ,
2023-08-29 09:08:24 +02:00
"reply-to" ,
2022-02-16 16:49:17 +01:00
"host" ,
"user" ,
anyArg { } ,
2024-04-11 09:16:10 +02:00
domain . SMTPConfigState ( 3 ) ,
"test" ,
} ,
} ,
} ,
} ,
} ,
} ,
{
name : "reduceSMTPConfigActivated" ,
args : args {
event : getEvent ( testEvent (
instance . SMTPConfigActivatedEventType ,
instance . AggregateType ,
[ ] byte ( ` {
"id" : "config-id"
} ` ) ,
) , instance . SMTPConfigActivatedEventMapper ) ,
} ,
reduce : ( & smtpConfigProjection { } ) . reduceSMTPConfigActivated ,
want : wantReduce {
aggregateType : eventstore . AggregateType ( "instance" ) ,
sequence : 15 ,
executer : & testExecuter {
executions : [ ] execution {
{
expectedStmt : "UPDATE projections.smtp_configs2 SET (change_date, sequence, state) = ($1, $2, $3) WHERE (id = $4) AND (resource_owner = $5) AND (instance_id = $6)" ,
expectedArgs : [ ] interface { } {
anyArg { } ,
uint64 ( 15 ) ,
domain . SMTPConfigStateActive ,
"config-id" ,
"ro-id" ,
"instance-id" ,
} ,
} ,
} ,
} ,
} ,
} ,
{
name : "reduceSMTPConfigDeactivated" ,
args : args {
event : getEvent ( testEvent (
instance . SMTPConfigDeactivatedEventType ,
instance . AggregateType ,
[ ] byte ( ` {
"id" : "config-id"
} ` ) ,
) , instance . SMTPConfigDeactivatedEventMapper ) ,
} ,
reduce : ( & smtpConfigProjection { } ) . reduceSMTPConfigDeactivated ,
want : wantReduce {
aggregateType : eventstore . AggregateType ( "instance" ) ,
sequence : 15 ,
executer : & testExecuter {
executions : [ ] execution {
{
expectedStmt : "UPDATE projections.smtp_configs2 SET (change_date, sequence, state) = ($1, $2, $3) WHERE (id = $4) AND (resource_owner = $5) AND (instance_id = $6)" ,
expectedArgs : [ ] interface { } {
anyArg { } ,
uint64 ( 15 ) ,
domain . SMTPConfigStateInactive ,
"config-id" ,
"ro-id" ,
"instance-id" ,
2022-02-16 16:49:17 +01:00
} ,
} ,
} ,
} ,
} ,
} ,
{
name : "reduceSMTPConfigPasswordChanged" ,
args : args {
2023-10-19 12:19:10 +02:00
event : getEvent (
testEvent (
instance . SMTPConfigPasswordChangedEventType ,
instance . AggregateType ,
[ ] byte ( ` {
2024-04-11 09:16:10 +02:00
"id" : "config-id" ,
2022-02-16 16:49:17 +01:00
"password" : {
"cryptoType" : 0 ,
"algorithm" : "RSA-265" ,
"keyId" : "key-id"
}
} ` ) ,
2023-10-19 12:19:10 +02:00
) , instance . SMTPConfigPasswordChangedEventMapper ) ,
2022-02-16 16:49:17 +01:00
} ,
2022-06-14 07:51:00 +02:00
reduce : ( & smtpConfigProjection { } ) . reduceSMTPConfigPasswordChanged ,
2022-02-16 16:49:17 +01:00
want : wantReduce {
2023-10-19 12:19:10 +02:00
aggregateType : eventstore . AggregateType ( "instance" ) ,
sequence : 15 ,
2022-02-16 16:49:17 +01:00
executer : & testExecuter {
executions : [ ] execution {
{
2024-04-11 09:16:10 +02:00
expectedStmt : "UPDATE projections.smtp_configs2 SET (change_date, sequence, password) = ($1, $2, $3) WHERE (id = $4) AND (resource_owner = $5) AND (instance_id = $6)" ,
2022-02-16 16:49:17 +01:00
expectedArgs : [ ] interface { } {
anyArg { } ,
uint64 ( 15 ) ,
anyArg { } ,
2024-04-11 09:16:10 +02:00
"config-id" ,
"ro-id" ,
2022-05-25 14:15:13 +02:00
"instance-id" ,
2022-02-16 16:49:17 +01:00
} ,
} ,
} ,
} ,
} ,
} ,
2023-08-18 14:22:57 +02:00
{
name : "reduceSMTPConfigRemoved" ,
args : args {
event : getEvent ( testEvent (
2023-10-19 12:19:10 +02:00
instance . SMTPConfigRemovedEventType ,
2023-08-18 14:22:57 +02:00
instance . AggregateType ,
2024-04-11 09:16:10 +02:00
[ ] byte ( ` { "id": "config-id"} ` ) ,
2023-08-18 14:22:57 +02:00
) , instance . SMTPConfigRemovedEventMapper ) ,
} ,
reduce : ( & smtpConfigProjection { } ) . reduceSMTPConfigRemoved ,
want : wantReduce {
2023-10-19 12:19:10 +02:00
aggregateType : eventstore . AggregateType ( "instance" ) ,
sequence : 15 ,
2023-08-18 14:22:57 +02:00
executer : & testExecuter {
executions : [ ] execution {
{
2024-04-11 09:16:10 +02:00
expectedStmt : "DELETE FROM projections.smtp_configs2 WHERE (id = $1) AND (resource_owner = $2) AND (instance_id = $3)" ,
2023-08-18 14:22:57 +02:00
expectedArgs : [ ] interface { } {
2024-04-11 09:16:10 +02:00
"config-id" ,
"ro-id" ,
2023-08-18 14:22:57 +02:00
"instance-id" ,
} ,
} ,
} ,
} ,
} ,
} ,
2022-10-20 13:36:52 +01:00
{
2022-11-10 11:59:33 +01:00
name : "instance reduceInstanceRemoved" ,
2022-10-20 13:36:52 +01:00
args : args {
2023-10-19 12:19:10 +02:00
event : getEvent (
testEvent (
instance . InstanceRemovedEventType ,
instance . AggregateType ,
nil ,
) , instance . InstanceRemovedEventMapper ) ,
2022-10-20 13:36:52 +01:00
} ,
reduce : reduceInstanceRemovedHelper ( SMTPConfigColumnInstanceID ) ,
want : wantReduce {
2023-10-19 12:19:10 +02:00
aggregateType : eventstore . AggregateType ( "instance" ) ,
sequence : 15 ,
2022-10-20 13:36:52 +01:00
executer : & testExecuter {
executions : [ ] execution {
{
2024-04-11 09:16:10 +02:00
expectedStmt : "DELETE FROM projections.smtp_configs2 WHERE (instance_id = $1)" ,
2022-10-20 13:36:52 +01:00
expectedArgs : [ ] interface { } {
"agg-id" ,
} ,
} ,
} ,
} ,
} ,
} ,
2022-02-16 16:49:17 +01:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
event := baseEvent ( t )
got , err := tt . reduce ( event )
2023-12-08 16:30:55 +02:00
if ok := zerrors . IsErrorInvalidArgument ( err ) ; ! ok {
2022-02-16 16:49:17 +01:00
t . Errorf ( "no wrong event mapping: %v, got: %v" , err , got )
}
event = tt . args . event ( t )
got , err = tt . reduce ( event )
2022-10-20 13:36:52 +01:00
assertReduce ( t , got , err , SMTPConfigProjectionTable , tt . want )
2022-02-16 16:49:17 +01:00
} )
}
}