-
Notifications
You must be signed in to change notification settings - Fork 143
/
template_versions_test.go
96 lines (77 loc) · 2.42 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
package mailgun_test
import (
"context"
"testing"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTemplateVersionsCRUD(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
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
}
}
}
require.NoError(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
require.NoError(t, mg.CreateTemplate(ctx, &tmpl))
version := mailgun.TemplateVersion{
Tag: Tag,
Comment: Comment,
Template: Template,
Active: true,
Engine: mailgun.TemplateEngineGo,
}
// Add a version version
require.NoError(t, mg.AddTemplateVersion(ctx, tmpl.Name, &version))
assert.Equal(t, Tag, version.Tag)
assert.Equal(t, Comment, version.Comment)
assert.Equal(t, mailgun.TemplateEngineGo, version.Engine)
// Ensure the version is in the list
require.True(t, findVersion(tmpl.Name, version.Tag))
// Update the Comment
version.Comment = UpdatedComment
version.Template = Template + "updated"
require.NoError(t, mg.UpdateTemplateVersion(ctx, tmpl.Name, &version))
// Ensure update took
updated, err := mg.GetTemplateVersion(ctx, tmpl.Name, version.Tag)
require.NoError(t, err)
assert.Equal(t, UpdatedComment, updated.Comment)
assert.Equal(t, Template+"updated", updated.Template)
// Add a new active Version
version2 := mailgun.TemplateVersion{
Tag: "v2",
Comment: Comment,
Template: Template,
Active: true,
Engine: mailgun.TemplateEngineGo,
}
require.NoError(t, mg.AddTemplateVersion(ctx, tmpl.Name, &version2))
// Ensure the version is in the list
require.True(t, findVersion(tmpl.Name, version2.Tag))
// Delete the first version
require.NoError(t, mg.DeleteTemplateVersion(ctx, tmpl.Name, version.Tag))
// Ensure version was deleted
require.False(t, findVersion(tmpl.Name, version.Tag))
// Delete the template
require.NoError(t, mg.DeleteTemplate(ctx, tmpl.Name))
}