-
Notifications
You must be signed in to change notification settings - Fork 145
/
template_test.go
88 lines (72 loc) · 1.93 KB
/
template_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
package mailgun_test
import (
"context"
"strings"
"testing"
"time"
"github.com/mailgun/errors"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTemplateCRUD(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
findTemplate := func(name string) bool {
it := mg.ListTemplates(nil)
var page []mailgun.Template
for it.Next(ctx, &page) {
for _, template := range page {
if template.Name == name {
return true
}
}
}
require.NoError(t, it.Err())
return false
}
const (
Name = "Mailgun-Go-TestTemplateCRUD"
Description = "Mailgun-Go Test Template Description"
UpdatedDesc = "Mailgun-Go Test Updated Description"
)
tmpl := mailgun.Template{
Name: Name,
Description: Description,
}
// Create a template
require.NoError(t, mg.CreateTemplate(ctx, &tmpl))
assert.Equal(t, strings.ToLower(Name), tmpl.Name)
assert.Equal(t, Description, tmpl.Description)
// Wait the template to show up
require.NoError(t, waitForTemplate(mg, tmpl.Name))
// Ensure the template is in the list
require.True(t, findTemplate(tmpl.Name))
// Update the description
tmpl.Description = UpdatedDesc
require.NoError(t, mg.UpdateTemplate(ctx, &tmpl))
// Ensure update took
updated, err := mg.GetTemplate(ctx, tmpl.Name)
require.NoError(t, err)
assert.Equal(t, UpdatedDesc, updated.Description)
// Delete the template
require.NoError(t, mg.DeleteTemplate(ctx, tmpl.Name))
}
func waitForTemplate(mg mailgun.Mailgun, id string) error {
ctx := context.Background()
var attempts int
for attempts <= 5 {
_, err := mg.GetTemplate(ctx, id)
if err != nil {
if mailgun.GetStatusFromErr(err) == 404 {
time.Sleep(time.Second * 2)
attempts += 1
continue
}
return err
}
return nil
}
return errors.Errorf("Waited to long for template '%s' to show up", id)
}