-
Notifications
You must be signed in to change notification settings - Fork 9
/
audiences_test.go
135 lines (111 loc) · 3.27 KB
/
audiences_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package resend
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateAudience(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/audiences", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
var ret interface{}
ret = `
{
"object": "audience",
"id": "78261eea-8f8b-4381-83c6-79fa7120f1c",
"name": "Registered Users"
}`
fmt.Fprint(w, ret)
})
req := &CreateAudienceRequest{
Name: "New Audience",
}
resp, err := client.Audiences.Create(req)
if err != nil {
t.Errorf("Audiences.Create returned error: %v", err)
}
assert.Equal(t, resp.Id, "78261eea-8f8b-4381-83c6-79fa7120f1c")
assert.Equal(t, resp.Object, "audience")
assert.Equal(t, resp.Name, "Registered Users")
}
func TestListAudiences(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/audiences", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusOK)
ret := `
{
"object": "list",
"data": [
{
"id": "d91cd9bd-1176-453e-8fc1-35364d380206",
"name": "Registered Users",
"created_at": "2023-04-26T20:21:26.347412+00:00"
}
]
}`
fmt.Fprint(w, ret)
})
audiences, err := client.Audiences.List()
if err != nil {
t.Errorf("Audiences.List returned error: %v", err)
}
assert.Equal(t, len(audiences.Data), 1)
assert.Equal(t, audiences.Object, "list")
assert.Equal(t, audiences.Data[0].Id, "d91cd9bd-1176-453e-8fc1-35364d380206")
assert.Equal(t, audiences.Data[0].Name, "Registered Users")
assert.Equal(t, audiences.Data[0].CreatedAt, "2023-04-26T20:21:26.347412+00:00")
}
func TestRemoveAudience(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/audiences/b6d24b8e-af0b-4c3c-be0c-359bbd97381e", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
w.WriteHeader(http.StatusOK)
var ret interface{}
ret = `
{
"object": "audience",
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"deleted": true
}`
fmt.Fprint(w, ret)
})
deleted, err := client.Audiences.Remove("b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
if err != nil {
t.Errorf("Audiences.Remove returned error: %v", err)
}
assert.True(t, deleted.Deleted)
assert.Equal(t, deleted.Id, "b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
assert.Equal(t, deleted.Object, "audience")
}
func TestGetAudience(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/audiences/d91cd9bd-1176-453e-8fc1-35364d380206", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
ret := `
{
"object": "audience",
"id": "d91cd9bd-1176-453e-8fc1-35364d380206",
"name": "Registered Users",
"created_at": "2023-10-06T22:59:55.977Z"
}`
fmt.Fprint(w, ret)
})
audience, err := client.Audiences.Get("d91cd9bd-1176-453e-8fc1-35364d380206")
if err != nil {
t.Errorf("Audience.Get returned error: %v", err)
}
assert.Equal(t, audience.Id, "d91cd9bd-1176-453e-8fc1-35364d380206")
assert.Equal(t, audience.Object, "audience")
assert.Equal(t, audience.Name, "Registered Users")
assert.Equal(t, audience.CreatedAt, "2023-10-06T22:59:55.977Z")
}