-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcampaign_test.go
More file actions
156 lines (141 loc) · 3.83 KB
/
campaign_test.go
File metadata and controls
156 lines (141 loc) · 3.83 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package plunk
import (
"context"
"encoding/json"
"io"
"net/http"
"testing"
)
func TestCreateCampaign(t *testing.T) {
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %s, want POST", r.Method)
}
if r.URL.Path != "/campaigns" {
t.Errorf("path = %s, want /campaigns", r.URL.Path)
}
body, _ := io.ReadAll(r.Body)
var req map[string]any
json.Unmarshal(body, &req)
if req["name"] != "Launch" {
t.Errorf("name = %v, want Launch", req["name"])
}
if req["audienceType"] != "ALL" {
t.Errorf("audienceType = %v, want ALL", req["audienceType"])
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"success": true,
"data": map[string]any{
"id": "camp1",
"name": "Launch",
"subject": "We're live!",
"type": "ALL",
"status": "DRAFT",
"scheduledAt": nil,
},
})
})
resp, err := c.CreateCampaign(context.Background(), &CreateCampaignRequest{
Name: "Launch",
Subject: "We're live!",
Body: "<h1>Hello</h1>",
From: "hello@acme.com",
AudienceType: AudienceAll,
})
if err != nil {
t.Fatal(err)
}
if resp.ID != "camp1" {
t.Errorf("id = %s, want camp1", resp.ID)
}
if resp.Status != CampaignDraft {
t.Errorf("status = %s, want DRAFT", resp.Status)
}
}
func TestListCampaigns(t *testing.T) {
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %s, want GET", r.Method)
}
if r.URL.Path != "/campaigns" {
t.Errorf("path = %s, want /campaigns", r.URL.Path)
}
if got := r.URL.Query().Get("status"); got != "SENT" {
t.Errorf("status = %s, want SENT", got)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"campaigns": []any{
map[string]any{
"id": "camp1",
"name": "Launch",
"subject": "We're live!",
"type": "ALL",
"status": "SENT",
"scheduledAt": nil,
},
},
"total": 1,
"page": 1,
"pageSize": 20,
"totalPages": 1,
})
})
resp, err := c.ListCampaigns(context.Background(), &ListCampaignsRequest{
Status: CampaignSent,
})
if err != nil {
t.Fatal(err)
}
if len(resp.Campaigns) != 1 {
t.Fatalf("len(campaigns) = %d, want 1", len(resp.Campaigns))
}
if resp.Campaigns[0].Status != CampaignSent {
t.Errorf("status = %s, want SENT", resp.Campaigns[0].Status)
}
if resp.Total != 1 {
t.Errorf("total = %d, want 1", resp.Total)
}
}
func TestSendCampaign(t *testing.T) {
t.Run("immediate", func(t *testing.T) {
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %s, want POST", r.Method)
}
if r.URL.Path != "/campaigns/camp1/send" {
t.Errorf("path = %s, want /campaigns/camp1/send", r.URL.Path)
}
body, _ := io.ReadAll(r.Body)
var req map[string]any
json.Unmarshal(body, &req)
if req["scheduledFor"] != nil {
t.Errorf("scheduledFor = %v, want nil", req["scheduledFor"])
}
w.WriteHeader(http.StatusOK)
})
err := c.SendCampaign(context.Background(), "camp1", &SendCampaignRequest{})
if err != nil {
t.Fatal(err)
}
})
t.Run("scheduled", func(t *testing.T) {
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var req map[string]any
json.Unmarshal(body, &req)
if req["scheduledFor"] != "2025-06-01T10:00:00Z" {
t.Errorf("scheduledFor = %v, want 2025-06-01T10:00:00Z", req["scheduledFor"])
}
w.WriteHeader(http.StatusOK)
})
scheduled := "2025-06-01T10:00:00Z"
err := c.SendCampaign(context.Background(), "camp1", &SendCampaignRequest{
ScheduledFor: &scheduled,
})
if err != nil {
t.Fatal(err)
}
})
}