Michael Waeger f2a32871a7
feat: e-mail templates (#1158)
* View definition added

* Get templates and texts from the database.

* Fill in texts in templates

* Fill in texts in templates

* Client API added

* Weekly backup

* Weekly backup

* Daily backup

* Weekly backup

* Tests added

* Corrections from merge branch

* Fixes from pull request review
2021-01-18 14:17:22 +01:00

135 lines
3.4 KiB
Go

package model
import (
"encoding/json"
"testing"
es_models "github.com/caos/zitadel/internal/eventstore/models"
)
func TestAppendAddMailTextEvent(t *testing.T) {
type args struct {
iam *IAM
mailText *MailText
event *es_models.Event
}
tests := []struct {
name string
args args
result *IAM
}{
{
name: "append add mailText event",
args: args{
iam: &IAM{},
mailText: &MailText{
MailTextType: "PasswordReset",
Language: "DE"},
event: &es_models.Event{},
},
result: &IAM{DefaultMailTexts: []*MailText{&MailText{
MailTextType: "PasswordReset",
Language: "DE"}}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.mailText != nil {
data, _ := json.Marshal(tt.args.mailText)
tt.args.event.Data = data
}
tt.args.iam.appendAddMailTextEvent(tt.args.event)
if len(tt.args.iam.DefaultMailTexts) != 1 {
t.Errorf("got wrong result should have one mailText actual: %v ", len(tt.args.iam.DefaultMailTexts))
}
if tt.args.iam.DefaultMailTexts[0] == tt.result.DefaultMailTexts[0] {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.DefaultMailTexts[0], tt.args.iam.DefaultMailTexts[0])
}
})
}
}
func TestAppendChangeMailTextEvent(t *testing.T) {
type args struct {
iam *IAM
mailText *MailText
event *es_models.Event
}
tests := []struct {
name string
args args
result *IAM
}{
{
name: "append change mailText event",
args: args{
iam: &IAM{DefaultMailTexts: []*MailText{&MailText{
MailTextType: "PasswordReset",
Language: "DE"}}},
mailText: &MailText{
MailTextType: "ChangedPasswordReset",
Language: "DE"},
event: &es_models.Event{},
},
result: &IAM{DefaultMailTexts: []*MailText{&MailText{
MailTextType: "PasswordReset",
Language: "ChangedDE"}}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.mailText != nil {
data, _ := json.Marshal(tt.args.mailText)
tt.args.event.Data = data
}
tt.args.iam.appendChangeMailTextEvent(tt.args.event)
if len(tt.args.iam.DefaultMailTexts) != 1 {
t.Errorf("got wrong result should have one mailText actual: %v ", len(tt.args.iam.DefaultMailTexts))
}
if tt.args.iam.DefaultMailTexts[0] == tt.result.DefaultMailTexts[0] {
t.Errorf("got wrong result: expected: %v, actual: %v ", tt.result.DefaultMailTexts[0], tt.args.iam.DefaultMailTexts[0])
}
})
}
}
func TestAppendRemoveMailTextEvent(t *testing.T) {
type args struct {
iam *IAM
mailText *MailText
event *es_models.Event
}
tests := []struct {
name string
args args
result *IAM
}{
{
name: "append remove mailText event",
args: args{
iam: &IAM{DefaultMailTexts: []*MailText{&MailText{
MailTextType: "PasswordReset",
Language: "DE",
Subject: "Subject"}}},
mailText: &MailText{
MailTextType: "PasswordReset",
Language: "DE"},
event: &es_models.Event{},
},
result: &IAM{DefaultMailTexts: []*MailText{}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.mailText != nil {
data, _ := json.Marshal(tt.args.mailText)
tt.args.event.Data = data
}
tt.args.iam.appendRemoveMailTextEvent(tt.args.event)
if len(tt.args.iam.DefaultMailTexts) != 0 {
t.Errorf("got wrong result should have no mailText actual: %v ", len(tt.args.iam.DefaultMailTexts))
}
})
}
}