-
Notifications
You must be signed in to change notification settings - Fork 146
/
tags_test.go
92 lines (75 loc) · 2.17 KB
/
tags_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
package mailgun_test
import (
"context"
"testing"
"time"
"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
"github.com/pkg/errors"
)
const (
fromUser = "=?utf-8?q?Katie_Brewer=2C_CFP=C2=AE?= <[email protected]>"
exampleSubject = "Mailgun-go Example Subject"
exampleText = "Testing some Mailgun awesomeness!"
)
func TestTags(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
msg := mg.NewMessage(fromUser, exampleSubject, exampleText, "[email protected]")
ensure.Nil(t, msg.AddTag("newsletter"))
ensure.Nil(t, msg.AddTag("homer"))
ensure.Nil(t, msg.AddTag("bart"))
ensure.NotNil(t, msg.AddTag("disco-steve"))
ensure.NotNil(t, msg.AddTag("newsletter"))
ctx := context.Background()
// Create an email with some tags attached
_, _, err := mg.Send(ctx, msg)
ensure.Nil(t, err)
// Wait for the tag to show up
ensure.Nil(t, waitForTag(mg, "newsletter"))
// Should return a list of available tags
it := mg.ListTags(nil)
var page []mailgun.Tag
for it.Next(ctx, &page) {
ensure.True(t, len(page) != 0)
}
ensure.Nil(t, it.Err())
// Should return a limited list of available tags
cursor := mg.ListTags(&mailgun.ListTagOptions{Limit: 1})
var tags []mailgun.Tag
for cursor.Next(ctx, &tags) {
ensure.DeepEqual(t, len(tags), 1)
}
ensure.Nil(t, cursor.Err())
err = mg.DeleteTag(ctx, "newsletter")
ensure.Nil(t, err)
tag, err := mg.GetTag(ctx, "homer")
ensure.Nil(t, err)
ensure.DeepEqual(t, tag.Value, "homer")
_, err = mg.GetTag(ctx, "i-dont-exist")
ensure.NotNil(t, err)
ensure.DeepEqual(t, mailgun.GetStatusFromErr(err), 404)
}
func waitForTag(mg mailgun.Mailgun, tag string) error {
ctx := context.Background()
var attempts int
for attempts <= 5 {
_, err := mg.GetTag(ctx, tag)
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 tag '%s' to show up", tag)
}
func TestDeleteTag(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
ensure.Nil(t, mg.DeleteTag(ctx, "newsletter"))
}