-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathintegration_test.go
More file actions
113 lines (90 loc) · 2.36 KB
/
integration_test.go
File metadata and controls
113 lines (90 loc) · 2.36 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//go:build integration
package mailgun_test
import (
"context"
"encoding/json"
"net/http"
"os"
"testing"
"time"
"github.com/mailgun/mailgun-go/v5"
"github.com/mailgun/mailgun-go/v5/mtypes"
"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)
}
domain := os.Getenv("MG_DOMAIN")
require.NotEmpty(t, domain)
opts := mtypes.MetricsRequest{
End: mtypes.RFC2822Time(time.Now().UTC()),
Duration: "30d",
Pagination: mtypes.MetricsPagination{
Limit: 10,
},
}
// filter by domain
opts.Filter.BoolGroupAnd = []mtypes.MetricsFilterPredicate{{
Attribute: "domain",
Comparator: "=",
LabeledValues: []mtypes.MetricsLabeledValue{{Label: domain, Value: domain}},
}}
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 mtypes.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)
}
domain := os.Getenv("MG_DOMAIN")
require.NotEmpty(t, domain)
const name = "permanent_fail"
ctx := context.Background()
urls := []string{"https://example.com/1", "https://example.com/2"}
err = mg.DeleteWebhook(ctx, domain, 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, domain, name)
}()
// Act
err = mg.CreateWebhook(ctx, domain, name, urls)
require.NoError(t, err)
time.Sleep(3 * time.Second)
// Assert
gotUrls, err := mg.GetWebhook(ctx, domain, name)
require.NoError(t, err)
t.Logf("Webhooks: %v", urls)
assert.ElementsMatch(t, urls, gotUrls)
}