mirror of
https://github.com/zitadel/zitadel.git
synced 2025-11-16 03:34:48 +00:00
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
This commit is contained in:
@@ -3996,3 +3996,327 @@ func TestRemovePasswordLockoutPolicy(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddMailTemplate(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
type args struct {
|
||||
es *OrgEventstore
|
||||
ctx context.Context
|
||||
template *iam_model.MailTemplate
|
||||
}
|
||||
type res struct {
|
||||
result *iam_model.MailTemplate
|
||||
wantErr bool
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "add label template, ok",
|
||||
args: args{
|
||||
es: GetMockChangesOrgOK(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
template: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
Template: []byte("<!doctype html>"),
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
result: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
Template: []byte("<!doctype html>"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid template",
|
||||
args: args{
|
||||
es: GetMockChangesOrgOK(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
template: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsPreconditionFailed,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "existing org not found",
|
||||
args: args{
|
||||
es: GetMockChangesOrgNoEvents(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
template: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsNotFound,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := tt.args.es.AddMailTemplate(tt.args.ctx, tt.args.template)
|
||||
if (tt.res.wantErr && !tt.res.errFunc(err)) || (err != nil && !tt.res.wantErr) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
return
|
||||
}
|
||||
if tt.res.wantErr && tt.res.errFunc(err) {
|
||||
return
|
||||
}
|
||||
if string(result.Template) != string(tt.res.result.Template) {
|
||||
t.Errorf("got wrong result Template: expected: %v, actual: %v ", tt.res.result.Template, result.Template)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChangeMailTemplate(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
type args struct {
|
||||
es *OrgEventstore
|
||||
ctx context.Context
|
||||
template *iam_model.MailTemplate
|
||||
}
|
||||
type res struct {
|
||||
result *iam_model.MailTemplate
|
||||
wantErr bool
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "add mail template, ok",
|
||||
args: args{
|
||||
es: GetMockChangesOrgWithMailTemplate(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
template: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
Template: []byte("<!doctype html>"),
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
result: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
Template: []byte("<!doctype html>"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid template",
|
||||
args: args{
|
||||
es: GetMockChangesOrgOK(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
template: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsPreconditionFailed,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "existing iam not found",
|
||||
args: args{
|
||||
es: GetMockChangesOrgNoEvents(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
template: &iam_model.MailTemplate{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsNotFound,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := tt.args.es.ChangeMailTemplate(tt.args.ctx, tt.args.template)
|
||||
if (tt.res.wantErr && !tt.res.errFunc(err)) || (err != nil && !tt.res.wantErr) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
return
|
||||
}
|
||||
if tt.res.wantErr && tt.res.errFunc(err) {
|
||||
return
|
||||
}
|
||||
if string(result.Template) != string(tt.res.result.Template) {
|
||||
t.Errorf("got wrong result Template: expected: %v, actual: %v ", tt.res.result.Template, result.Template)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddMailText(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
type args struct {
|
||||
es *OrgEventstore
|
||||
ctx context.Context
|
||||
mailtext *iam_model.MailText
|
||||
}
|
||||
type res struct {
|
||||
result *iam_model.MailText
|
||||
wantErr bool
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "add label mailtext, ok",
|
||||
args: args{
|
||||
es: GetMockChangesOrgOK(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
mailtext: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
MailTextType: "Type",
|
||||
Language: "DE",
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
result: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
MailTextType: "Type",
|
||||
Language: "DE",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid mailtext",
|
||||
args: args{
|
||||
es: GetMockChangesOrgOK(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
mailtext: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsPreconditionFailed,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "existing org not found",
|
||||
args: args{
|
||||
es: GetMockChangesOrgNoEvents(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
mailtext: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsNotFound,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := tt.args.es.AddMailText(tt.args.ctx, tt.args.mailtext)
|
||||
if (tt.res.wantErr && !tt.res.errFunc(err)) || (err != nil && !tt.res.wantErr) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
return
|
||||
}
|
||||
if tt.res.wantErr && tt.res.errFunc(err) {
|
||||
return
|
||||
}
|
||||
if result.MailTextType != tt.res.result.MailTextType {
|
||||
t.Errorf("got wrong result MailTextType: expected: %v, actual: %v ", tt.res.result.MailTextType, result.MailTextType)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChangeMailText(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
type args struct {
|
||||
es *OrgEventstore
|
||||
ctx context.Context
|
||||
mailtext *iam_model.MailText
|
||||
}
|
||||
type res struct {
|
||||
result *iam_model.MailText
|
||||
wantErr bool
|
||||
errFunc func(err error) bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
res res
|
||||
}{
|
||||
{
|
||||
name: "add label mailtext, ok",
|
||||
args: args{
|
||||
es: GetMockChangesOrgWithMailText(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
mailtext: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
MailTextType: "Type",
|
||||
Language: "DE",
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
result: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
MailTextType: "Type",
|
||||
Language: "DE",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid mailtext",
|
||||
args: args{
|
||||
es: GetMockChangesOrgOK(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
mailtext: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsPreconditionFailed,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "existing iam not found",
|
||||
args: args{
|
||||
es: GetMockChangesOrgNoEvents(ctrl),
|
||||
ctx: authz.NewMockContext("orgID", "userID"),
|
||||
mailtext: &iam_model.MailText{
|
||||
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID", Sequence: 0},
|
||||
},
|
||||
},
|
||||
res: res{
|
||||
wantErr: true,
|
||||
errFunc: caos_errs.IsNotFound,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := tt.args.es.ChangeMailText(tt.args.ctx, tt.args.mailtext)
|
||||
if (tt.res.wantErr && !tt.res.errFunc(err)) || (err != nil && !tt.res.wantErr) {
|
||||
t.Errorf("got wrong err: %v ", err)
|
||||
return
|
||||
}
|
||||
if tt.res.wantErr && tt.res.errFunc(err) {
|
||||
return
|
||||
}
|
||||
if string(result.MailTextType) != string(tt.res.result.MailTextType) {
|
||||
t.Errorf("got wrong result MailTextType: expected: %v, actual: %v ", tt.res.result.MailTextType, result.MailTextType)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user