forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_versions_test.go
98 lines (78 loc) · 2.39 KB
/
template_versions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mailgun_test
import (
"context"
"testing"
"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
)
func TestTemplateVersionsCRUD(t *testing.T) {
if reason := mailgun.SkipNetworkTest(); reason != "" {
t.Skip(reason)
}
mg, err := mailgun.NewMailgunFromEnv()
ensure.Nil(t, err)
ctx := context.Background()
findVersion := func(templateName, tag string) bool {
it := mg.ListTemplateVersions(templateName, nil)
var page []mailgun.TemplateVersion
for it.Next(ctx, &page) {
for _, v := range page {
if v.Tag == tag {
return true
}
}
}
ensure.Nil(t, it.Err())
return false
}
const (
Comment = "Mailgun-Go TestTemplateVersionsCRUD"
UpdatedComment = "Mailgun-Go Test Version Updated"
Template = "{{.Name}}"
Tag = "v1"
)
tmpl := mailgun.Template{
Name: randomString(10, "Mailgun-go-TestTemplateVersionsCRUD-"),
}
// Create a template
ensure.Nil(t, mg.CreateTemplate(ctx, &tmpl))
version := mailgun.TemplateVersion{
Tag: Tag,
Comment: Comment,
Template: Template,
Active: true,
Engine: mailgun.TemplateEngineGo,
}
// Add a version version
ensure.Nil(t, mg.AddTemplateVersion(ctx, tmpl.Name, &version))
ensure.DeepEqual(t, version.Tag, Tag)
ensure.DeepEqual(t, version.Comment, Comment)
ensure.DeepEqual(t, version.Engine, mailgun.TemplateEngineGo)
// Ensure the version is in the list
ensure.True(t, findVersion(tmpl.Name, version.Tag))
// Update the Comment
version.Comment = UpdatedComment
version.Template = Template + "updated"
ensure.Nil(t, mg.UpdateTemplateVersion(ctx, tmpl.Name, &version))
// Ensure update took
updated, err := mg.GetTemplateVersion(ctx, tmpl.Name, version.Tag)
ensure.DeepEqual(t, updated.Comment, UpdatedComment)
ensure.DeepEqual(t, updated.Template, Template+"updated")
// Add a new active Version
version2 := mailgun.TemplateVersion{
Tag: "v2",
Comment: Comment,
Template: Template,
Active: true,
Engine: mailgun.TemplateEngineGo,
}
ensure.Nil(t, mg.AddTemplateVersion(ctx, tmpl.Name, &version2))
// Ensure the version is in the list
ensure.True(t, findVersion(tmpl.Name, version2.Tag))
// Delete the first version
ensure.Nil(t, mg.DeleteTemplateVersion(ctx, tmpl.Name, version.Tag))
// Ensure version was deleted
ensure.False(t, findVersion(tmpl.Name, version.Tag))
// Delete the template
ensure.Nil(t, mg.DeleteTemplate(ctx, tmpl.Name))
}