-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathnotification_setting_test.go
More file actions
362 lines (319 loc) · 11.6 KB
/
notification_setting_test.go
File metadata and controls
362 lines (319 loc) · 11.6 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
package dbt_cloud_test
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud/testutil"
)
// These tests pin down the contract between the dbt Cloud notification-settings
// endpoint and the Terraform provider's recovery behavior. The Read handler
// relies on HandleResourceNotFound recognizing a "resource-not-found"-prefixed
// error to drop a missing resource from state. If the API returns anything else
// — 500, 502, or any other 5xx — the provider must surface it instead of
// silently reconciling, otherwise users get either ghost resources in state or
// silent deletions of resources that still exist server-side.
//
// Background: a customer hit a 502 on DELETE followed by a 500 on the next
// Refresh because the backend was mapping a Django DoesNotExist to a generic
// 500 instead of 404. After the backend fix, GET on a missing setting returns
// 404 and Terraform reconciles cleanly. These tests lock that contract in.
const (
testAccountID int64 = 123
testNotificationID int64 = 456
testPrivateAPISegment = "/private/accounts/123/notification-settings"
)
// notificationSettingMockServer is a configurable test double for the
// /private/accounts/:id/notification-settings endpoint. Each method returns
// the configured status code (and body) on a single matching call. Default
// status is 200 with an empty success body, so unconfigured callers will still
// hit a deterministic path rather than panicking.
type notificationSettingMockServer struct {
t *testing.T
server *httptest.Server
getStatus int
getBody string
deleteStatus int
deleteBody string
postStatus int
postBody string
patchStatus int
patchBody string
lastRequestMethod string
lastRequestPath string
lastRequestBody []byte
}
func newNotificationSettingMockServer(t *testing.T) *notificationSettingMockServer {
m := ¬ificationSettingMockServer{
t: t,
getStatus: http.StatusOK,
deleteStatus: http.StatusNoContent,
postStatus: http.StatusCreated,
patchStatus: http.StatusOK,
}
m.server = httptest.NewServer(http.HandlerFunc(m.serve))
return m
}
func (m *notificationSettingMockServer) serve(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
m.lastRequestMethod = r.Method
m.lastRequestPath = r.URL.Path
m.lastRequestBody = body
if !strings.HasPrefix(r.URL.Path, testPrivateAPISegment) {
http.Error(w, "unexpected path "+r.URL.Path, http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
w.WriteHeader(m.getStatus)
_, _ = w.Write([]byte(m.getBody))
case http.MethodDelete:
w.WriteHeader(m.deleteStatus)
_, _ = w.Write([]byte(m.deleteBody))
case http.MethodPost:
w.WriteHeader(m.postStatus)
_, _ = w.Write([]byte(m.postBody))
case http.MethodPatch:
w.WriteHeader(m.patchStatus)
_, _ = w.Write([]byte(m.patchBody))
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
func (m *notificationSettingMockServer) close() { m.server.Close() }
func (m *notificationSettingMockServer) client() *dbt_cloud.Client {
return testutil.CreateTestClient(m.server.URL, testAccountID)
}
// not-found-style errors are the only ones HandleResourceNotFound recognizes.
// All other errors must propagate to Terraform.
func errorIsNotFound(err error) bool {
return err != nil && strings.HasPrefix(err.Error(), "resource-not-found")
}
func TestGetNotificationSetting_Success(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.getStatus = http.StatusOK
srv.getBody = `{
"status": {"code": 200, "is_success": true},
"data": {
"id": 456,
"account_id": 123,
"name": "weekly digest",
"description": "send teams pings",
"is_active": true,
"channels": [{"id": 1, "channel_type": "teams"}],
"rules": [{"id": 2, "trigger_on": "run_errored"}]
}
}`
got, err := srv.client().GetNotificationSetting(testNotificationID)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got == nil || got.ID == nil || *got.ID != testNotificationID {
t.Fatalf("expected ID %d, got %+v", testNotificationID, got)
}
if got.Name != "weekly digest" {
t.Errorf("expected name %q, got %q", "weekly digest", got.Name)
}
if len(got.Channels) != 1 || len(got.Rules) != 1 {
t.Errorf("expected one channel and one rule, got %d channels and %d rules", len(got.Channels), len(got.Rules))
}
}
// Regression test: post-backend-fix, a missing setting returns 404 and the
// provider must surface that as a "resource-not-found" error so the Read
// handler removes the resource from state via HandleResourceNotFound.
func TestGetNotificationSetting_404_IsRecognizedAsResourceNotFound(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.getStatus = http.StatusNotFound
srv.getBody = `{
"status": {
"code": 404,
"is_success": false,
"user_message": "Notification setting not found"
},
"data": null
}`
_, err := srv.client().GetNotificationSetting(testNotificationID)
if err == nil {
t.Fatal("expected an error, got nil")
}
if !errorIsNotFound(err) {
t.Fatalf("expected resource-not-found error, got %q", err.Error())
}
}
// Regression test: before the backend fix, a missing setting surfaced as a
// generic 500. The provider must NOT silently reconcile that — it should
// propagate the error so Terraform retries or the user investigates, rather
// than dropping a resource from state that may still exist server-side.
//
// This is the exact path that stranded Aramark: Refresh hit a 500 and the
// provider correctly refused to drop the resource. The test pins down that
// behavior so a future "just treat any 5xx as gone" change can't regress it.
func TestGetNotificationSetting_500_IsNotMaskedAsNotFound(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.getStatus = http.StatusInternalServerError
srv.getBody = `{
"status": {
"code": 500,
"is_success": false,
"user_message": "Internal server error getting notification setting"
}
}`
_, err := srv.client().GetNotificationSetting(testNotificationID)
if err == nil {
t.Fatal("expected an error, got nil")
}
if errorIsNotFound(err) {
t.Fatalf("500 must not be classified as resource-not-found, got %q", err.Error())
}
if !strings.Contains(err.Error(), "internal-server-error") {
t.Errorf("expected internal-server-error tag in error, got %q", err.Error())
}
}
// Regression test: matches the customer-reported failure mode — DELETE
// returned 502 from the gateway. The next Refresh re-fetched via GET, which
// the gateway also dropped with a 502 protocol error. That must propagate
// (not be silently reconciled) so Terraform retries.
func TestGetNotificationSetting_502_IsNotMaskedAsNotFound(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.getStatus = http.StatusBadGateway
srv.getBody = "upstream connect error or disconnect/reset before headers"
_, err := srv.client().GetNotificationSetting(testNotificationID)
if err == nil {
t.Fatal("expected an error, got nil")
}
if errorIsNotFound(err) {
t.Fatalf("502 must not be classified as resource-not-found, got %q", err.Error())
}
if !strings.Contains(err.Error(), "502") {
t.Errorf("expected the 502 status to appear in the error, got %q", err.Error())
}
}
func TestDeleteNotificationSetting_Success(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.deleteStatus = http.StatusNoContent
if err := srv.client().DeleteNotificationSetting(testNotificationID); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if srv.lastRequestMethod != http.MethodDelete {
t.Errorf("expected DELETE, got %s", srv.lastRequestMethod)
}
wantPath := fmt.Sprintf("%s/%d/", testPrivateAPISegment, testNotificationID)
if srv.lastRequestPath != wantPath {
t.Errorf("expected path %q, got %q", wantPath, srv.lastRequestPath)
}
}
// Regression test: after the backend fix, DELETE on a missing setting returns
// 404 (mapped from Django DoesNotExist). The provider currently surfaces this
// as an error from Delete — that's fine, since Terraform's destroy plan would
// not have been re-run if the resource was already gone. What matters is that
// it doesn't crash and the error string is recognizable.
func TestDeleteNotificationSetting_404(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.deleteStatus = http.StatusNotFound
srv.deleteBody = `{
"status": {"code": 404, "is_success": false, "user_message": "Notification setting not found"},
"data": null
}`
err := srv.client().DeleteNotificationSetting(testNotificationID)
if err == nil {
t.Fatal("expected an error, got nil")
}
if !errorIsNotFound(err) {
t.Fatalf("expected resource-not-found error, got %q", err.Error())
}
}
// Regression test for Aramark's original symptom: DELETE returns 502 from the
// gateway. The provider must surface the 502 so the user knows the destroy
// did not complete successfully — silently swallowing it would either leave
// orphaned server-side state or cause Terraform to think the resource is
// gone when it isn't.
func TestDeleteNotificationSetting_502_Surfaces(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.deleteStatus = http.StatusBadGateway
srv.deleteBody = "upstream connect error or disconnect/reset before headers"
err := srv.client().DeleteNotificationSetting(testNotificationID)
if err == nil {
t.Fatal("expected a 502 error, got nil")
}
if !strings.Contains(err.Error(), "502") {
t.Errorf("expected 502 in error, got %q", err.Error())
}
}
func TestCreateNotificationSetting_Success(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.postStatus = http.StatusCreated
srv.postBody = `{
"status": {"code": 201, "is_success": true},
"data": {
"id": 456,
"name": "alerts",
"is_active": true,
"channels": [],
"rules": []
}
}`
desc := "first"
created, err := srv.client().CreateNotificationSetting(dbt_cloud.NotificationSetting{
Name: "alerts",
Description: &desc,
IsActive: true,
AccountID: 999, // The client should strip this; the URL path carries the account.
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if created == nil || created.ID == nil || *created.ID != testNotificationID {
t.Fatalf("expected ID %d, got %+v", testNotificationID, created)
}
var sent dbt_cloud.NotificationSetting
if err := json.Unmarshal(srv.lastRequestBody, &sent); err != nil {
t.Fatalf("could not unmarshal recorded request body: %v", err)
}
if sent.AccountID != 0 {
t.Errorf("AccountID must be stripped from request body (URL carries it), got %d", sent.AccountID)
}
if sent.Name != "alerts" {
t.Errorf("expected name %q, got %q", "alerts", sent.Name)
}
}
func TestUpdateNotificationSetting_Success(t *testing.T) {
srv := newNotificationSettingMockServer(t)
defer srv.close()
srv.patchStatus = http.StatusOK
srv.patchBody = `{
"status": {"code": 200, "is_success": true},
"data": {
"id": 456,
"name": "renamed",
"is_active": false,
"channels": [],
"rules": []
}
}`
updated, err := srv.client().UpdateNotificationSetting(testNotificationID, dbt_cloud.NotificationSetting{
Name: "renamed",
IsActive: false,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if updated == nil || updated.Name != "renamed" {
t.Fatalf("expected updated name %q, got %+v", "renamed", updated)
}
if srv.lastRequestMethod != http.MethodPatch {
t.Errorf("expected PATCH, got %s", srv.lastRequestMethod)
}
}