-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatuspage_test.go
More file actions
380 lines (333 loc) · 12.2 KB
/
statuspage_test.go
File metadata and controls
380 lines (333 loc) · 12.2 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package flashduty
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
)
func TestStartStatusPageMigration(t *testing.T) {
var gotMethod, gotPath, gotAppKey, gotContentType string
var gotBody map[string]any
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
gotMethod = r.Method
gotPath = r.URL.Path
gotAppKey = r.URL.Query().Get("app_key")
gotContentType = r.Header.Get("Content-Type")
gotBody = decodeJSONBody(t, r)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{"job_id": "job-1"},
})
})
out, err := client.StartStatusPageMigration(context.Background(), &StartStatusPageMigrationInput{
SourceAPIKey: "atlassian-key",
SourcePageID: "page_123",
})
if err != nil {
t.Fatalf("StartStatusPageMigration() error = %v", err)
}
if gotMethod != http.MethodPost {
t.Errorf("method = %s, want POST", gotMethod)
}
if gotPath != "/status-page/migrate-structure" {
t.Errorf("path = %s, want /status-page/migrate-structure", gotPath)
}
if gotAppKey != "test-key" {
t.Errorf("app_key = %s, want test-key", gotAppKey)
}
if gotContentType != "application/json" {
t.Errorf("Content-Type = %s, want application/json", gotContentType)
}
if gotBody["api_key"] != "atlassian-key" {
t.Errorf("api_key = %v, want atlassian-key", gotBody["api_key"])
}
if gotBody["source_page_id"] != "page_123" {
t.Errorf("source_page_id = %v, want page_123", gotBody["source_page_id"])
}
if _, ok := gotBody["url_name"]; ok {
t.Errorf("url_name should be omitted when empty, got %v", gotBody["url_name"])
}
if out.JobID != "job-1" {
t.Errorf("JobID = %s, want job-1", out.JobID)
}
}
func TestStartStatusPageMigrationSendsURLName(t *testing.T) {
var gotBody map[string]any
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
gotBody = decodeJSONBody(t, r)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{"job_id": "job-url"},
})
})
out, err := client.StartStatusPageMigration(context.Background(), &StartStatusPageMigrationInput{
SourceAPIKey: "atlassian-key",
SourcePageID: "page_123",
URLName: "desired-page",
})
if err != nil {
t.Fatalf("StartStatusPageMigration() error = %v", err)
}
if gotBody["url_name"] != "desired-page" {
t.Errorf("url_name = %v, want desired-page", gotBody["url_name"])
}
if out.JobID != "job-url" {
t.Errorf("JobID = %s, want job-url", out.JobID)
}
}
func TestStartStatusPageEmailSubscriberMigration(t *testing.T) {
var gotMethod, gotPath string
var gotBody map[string]any
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
gotMethod = r.Method
gotPath = r.URL.Path
gotBody = decodeJSONBody(t, r)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{"job_id": "job-2"},
})
})
out, err := client.StartStatusPageEmailSubscriberMigration(context.Background(), &StartStatusPageEmailSubscriberMigrationInput{
SourceAPIKey: "atlassian-key",
SourcePageID: "page_123",
TargetPageID: 1024,
})
if err != nil {
t.Fatalf("StartStatusPageEmailSubscriberMigration() error = %v", err)
}
if gotMethod != http.MethodPost {
t.Errorf("method = %s, want POST", gotMethod)
}
if gotPath != "/status-page/migrate-email-subscribers" {
t.Errorf("path = %s, want /status-page/migrate-email-subscribers", gotPath)
}
if gotBody["api_key"] != "atlassian-key" {
t.Errorf("api_key = %v, want atlassian-key", gotBody["api_key"])
}
if gotBody["source_page_id"] != "page_123" {
t.Errorf("source_page_id = %v, want page_123", gotBody["source_page_id"])
}
// JSON decoding of map[string]any produces float64 for numbers.
if got, ok := gotBody["target_page_id"].(float64); !ok || int64(got) != 1024 {
t.Errorf("target_page_id = %#v, want 1024", gotBody["target_page_id"])
}
if out.JobID != "job-2" {
t.Errorf("JobID = %s, want job-2", out.JobID)
}
}
func TestGetStatusPageMigrationStatus(t *testing.T) {
var gotMethod, gotPath, gotJobID string
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
gotMethod = r.Method
gotPath = r.URL.Path
gotJobID = r.URL.Query().Get("job_id")
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"job_id": "job-3",
"source_page_id": "src-1",
"target_page_id": 2048,
"phase": "history",
"status": "running",
"progress": map[string]any{
"total_steps": 5,
"completed_steps": 3,
"components_imported": 10,
"sections_imported": 2,
"incidents_imported": 4,
"maintenances_imported": 1,
"subscribers_imported": 50,
"subscribers_skipped": 3,
"templates_imported": 2,
"warnings": []string{"missing field X"},
},
"created_at": 1713225600,
"updated_at": 1713225700,
},
})
})
out, err := client.GetStatusPageMigrationStatus(context.Background(), "job-3")
if err != nil {
t.Fatalf("GetStatusPageMigrationStatus() error = %v", err)
}
if gotMethod != http.MethodGet {
t.Errorf("method = %s, want GET", gotMethod)
}
if gotPath != "/status-page/migration/status" {
t.Errorf("path = %s, want /status-page/migration/status", gotPath)
}
if gotJobID != "job-3" {
t.Errorf("job_id query = %s, want job-3", gotJobID)
}
if out.JobID != "job-3" {
t.Errorf("JobID = %s, want job-3", out.JobID)
}
if out.SourcePageID != "src-1" {
t.Errorf("SourcePageID = %s, want src-1", out.SourcePageID)
}
if out.TargetPageID != 2048 {
t.Errorf("TargetPageID = %d, want 2048", out.TargetPageID)
}
if out.Phase != "history" {
t.Errorf("Phase = %s, want history", out.Phase)
}
if out.Status != "running" {
t.Errorf("Status = %s, want running", out.Status)
}
if out.Progress.CompletedSteps != 3 || out.Progress.TotalSteps != 5 {
t.Errorf("Progress steps = %d/%d, want 3/5", out.Progress.CompletedSteps, out.Progress.TotalSteps)
}
if out.Progress.SubscribersImported != 50 {
t.Errorf("Progress.SubscribersImported = %d, want 50", out.Progress.SubscribersImported)
}
if len(out.Progress.Warnings) != 1 || out.Progress.Warnings[0] != "missing field X" {
t.Errorf("Progress.Warnings = %v, want [missing field X]", out.Progress.Warnings)
}
if out.CreatedAt != 1713225600 || out.UpdatedAt != 1713225700 {
t.Errorf("timestamps = (%d, %d)", out.CreatedAt, out.UpdatedAt)
}
}
func TestGetStatusPageMigrationStatusRejectsEmptyJobID(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("server should not be hit for empty jobID; got %s %s", r.Method, r.URL.Path)
})
if _, err := client.GetStatusPageMigrationStatus(context.Background(), ""); err == nil {
t.Fatalf("expected error for empty jobID, got nil")
}
}
func TestCancelStatusPageMigration(t *testing.T) {
var gotMethod, gotPath string
var gotBody map[string]any
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
gotMethod = r.Method
gotPath = r.URL.Path
gotBody = decodeJSONBody(t, r)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{}})
})
if err := client.CancelStatusPageMigration(context.Background(), "job-4"); err != nil {
t.Fatalf("CancelStatusPageMigration() error = %v", err)
}
if gotMethod != http.MethodPost {
t.Errorf("method = %s, want POST", gotMethod)
}
if gotPath != "/status-page/migration/cancel" {
t.Errorf("path = %s, want /status-page/migration/cancel", gotPath)
}
if gotBody["job_id"] != "job-4" {
t.Errorf("job_id = %v, want job-4", gotBody["job_id"])
}
}
func TestCancelStatusPageMigrationRejectsEmptyJobID(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("server should not be hit for empty jobID; got %s %s", r.Method, r.URL.Path)
})
if err := client.CancelStatusPageMigration(context.Background(), ""); err == nil {
t.Fatalf("expected error for empty jobID, got nil")
}
}
func TestStatusPageMigrationReturnsDutyError(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{
"code": "invalid_api_key",
"message": "source provider API key is invalid",
},
})
})
_, err := client.StartStatusPageMigration(context.Background(), &StartStatusPageMigrationInput{
SourceAPIKey: "bad",
SourcePageID: "page_123",
})
if err == nil {
t.Fatalf("expected error, got nil")
}
dutyErr, ok := err.(*DutyError)
if !ok {
t.Fatalf("error type = %T, want *DutyError (err: %v)", err, err)
}
if dutyErr.Code != "invalid_api_key" {
t.Errorf("Code = %s, want invalid_api_key", dutyErr.Code)
}
if dutyErr.Message != "source provider API key is invalid" {
t.Errorf("Message = %s, want source provider API key is invalid", dutyErr.Message)
}
}
func TestStatusPageMigrationWrapsHTTPError(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"error":{"code":"internal","message":"boom"}}`))
})
_, err := client.GetStatusPageMigrationStatus(context.Background(), "job-5")
if err == nil {
t.Fatalf("expected error, got nil")
}
// handleAPIError wraps the HTTP status into the error message.
if !strings.Contains(err.Error(), "500") {
t.Errorf("error = %v; want it to mention HTTP 500", err)
}
}
func TestStartStatusPageMigrationErrorsOnMissingData(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{}`))
})
_, err := client.StartStatusPageMigration(context.Background(), &StartStatusPageMigrationInput{
SourceAPIKey: "k",
SourcePageID: "p",
})
if err == nil || !strings.Contains(err.Error(), "missing data") {
t.Fatalf("expected missing-data error, got %v", err)
}
}
func TestGetStatusPageMigrationStatusErrorsOnMissingData(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{}`))
})
_, err := client.GetStatusPageMigrationStatus(context.Background(), "job-6")
if err == nil || !strings.Contains(err.Error(), "missing data") {
t.Fatalf("expected missing-data error, got %v", err)
}
}
func TestCancelStatusPageMigrationReturnsDutyError(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{"code": "not_found", "message": "job not found"},
})
})
err := client.CancelStatusPageMigration(context.Background(), "missing-job")
if err == nil {
t.Fatalf("expected error, got nil")
}
if _, ok := err.(*DutyError); !ok {
t.Errorf("error type = %T, want *DutyError", err)
}
}
func TestStartStatusPageMigrationRejectsNilInput(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("server should not be hit for nil input; got %s %s", r.Method, r.URL.Path)
})
if _, err := client.StartStatusPageMigration(context.Background(), nil); err == nil {
t.Fatalf("expected error for nil input, got nil")
}
if _, err := client.StartStatusPageEmailSubscriberMigration(context.Background(), nil); err == nil {
t.Fatalf("expected error for nil input, got nil")
}
}
func TestCancelStatusPageMigrationWrapsHTTPError(t *testing.T) {
client := newSDKExtensionsTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
_, _ = w.Write([]byte(`{"error":{"code":"forbidden","message":"nope"}}`))
})
err := client.CancelStatusPageMigration(context.Background(), "job-7")
if err == nil {
t.Fatalf("expected error, got nil")
}
if !strings.Contains(err.Error(), "403") {
t.Errorf("error = %v; want it to mention HTTP 403", err)
}
}