-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathintegration_test.go
99 lines (78 loc) · 1.95 KB
/
integration_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
99
//go:build integration
package mailgun_test
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIntegrationMailgunImpl_ListMetrics(t *testing.T) {
mg, err := mailgun.NewMailgunFromEnv()
if err != nil {
require.NoError(t, err)
}
opts := mailgun.MetricsOptions{
End: mailgun.RFC2822Time(time.Now().UTC()),
Duration: "30d",
Pagination: mailgun.MetricsPagination{
Limit: 10,
},
}
iter, err := mg.ListMetrics(opts)
require.NoError(t, err)
// create context to list all pages
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
for i := 0; i < 2; i++ {
var resp mailgun.MetricsResponse
more := iter.Next(ctx, &resp)
if iter.Err() != nil {
require.NoError(t, err)
}
t.Logf("Page %d: Start: %s; End: %s; Pagination: %+v\n",
i+1, resp.Start, resp.End, resp.Pagination)
assert.GreaterOrEqual(t, len(resp.Items), 1)
for _, item := range resp.Items {
b, _ := json.Marshal(item)
t.Logf("%s\n", b)
}
if !more {
t.Log("no more pages")
break
}
}
}
func TestIntegrationWebhooksCRUD(t *testing.T) {
// Arrange
mg, err := mailgun.NewMailgunFromEnv()
if err != nil {
require.NoError(t, err)
}
const name = "permanent_fail"
ctx := context.Background()
urls := []string{"https://example.com/1", "https://example.com/2"}
err = mg.DeleteWebhook(ctx, name)
if err != nil {
// 200 or 404 is expected
status := mailgun.GetStatusFromErr(err)
require.Equal(t, http.StatusNotFound, status, err)
}
time.Sleep(3 * time.Second)
defer func() {
// Cleanup
_ = mg.DeleteWebhook(ctx, name)
}()
// Act
err = mg.CreateWebhook(ctx, name, urls)
require.NoError(t, err)
time.Sleep(3 * time.Second)
// Assert
gotUrls, err := mg.GetWebhook(ctx, name)
require.NoError(t, err)
t.Logf("Webhooks: %v", urls)
assert.ElementsMatch(t, urls, gotUrls)
}