Skip to content

Commit 5312ccd

Browse files
committed
[e2e] add cleanup
1 parent bb457cd commit 5312ccd

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

test/e2e/webhooks_test.go

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ func TestWebhooks_Post(t *testing.T) {
205205
t.Fatal(err)
206206
}
207207

208+
t.Cleanup(func() {
209+
_, err := authorizedClient.R().Delete("webhooks/" + result.ID)
210+
if err != nil {
211+
t.Fatal(err)
212+
}
213+
})
214+
208215
// Verify response structure
209216
if result.ID == "" {
210217
t.Error("webhook ID is empty")
@@ -241,6 +248,13 @@ func TestWebhooks_Post(t *testing.T) {
241248
t.Fatal(err)
242249
}
243250

251+
t.Cleanup(func() {
252+
_, err := authorizedClient.R().Delete("webhooks/" + result.ID)
253+
if err != nil {
254+
t.Fatal(err)
255+
}
256+
})
257+
244258
// Verify response structure
245259
if result.ID == "" {
246260
t.Error("webhook ID is empty")
@@ -279,6 +293,13 @@ func TestWebhooks_Post(t *testing.T) {
279293
t.Fatal(err)
280294
}
281295

296+
t.Cleanup(func() {
297+
_, err := authorizedClient.R().Delete("webhooks/" + result.ID)
298+
if err != nil {
299+
t.Fatal(err)
300+
}
301+
})
302+
282303
// Verify response structure
283304
if result.Event != "sms:data-received" {
284305
t.Errorf("expected event 'sms:data-received', got '%s'", result.Event)
@@ -481,18 +502,17 @@ func TestWebhooks_Delete(t *testing.T) {
481502

482503
cases := []struct {
483504
name string
484-
setup func()
505+
setup func() string
485506
expectedStatusCode int
486-
request func() *resty.Request
507+
request func(id string) *resty.Request
487508
validate func(t *testing.T, response *resty.Response)
488509
}{
489510
{
490511
name: "Remove webhook by ID",
491-
setup: func() {
492-
// Create a webhook to delete
512+
setup: func() string {
513+
// Create a webhook to delete (server will generate ID)
493514
resp, err := authorizedClient.R().
494515
SetBody(webhook{
495-
ID: "delete-test",
496516
URL: "https://example.com/delete-test",
497517
Event: "sms:failed",
498518
}).Post("webhooks")
@@ -505,15 +525,11 @@ func TestWebhooks_Delete(t *testing.T) {
505525
t.Fatal(err)
506526
}
507527

508-
// Store webhook ID for deletion test
509-
t.Cleanup(func() {
510-
// Clean up any remaining webhooks
511-
authorizedClient.R().Delete("webhooks/" + created.ID)
512-
})
528+
return created.ID
513529
},
514530
expectedStatusCode: 204,
515-
request: func() *resty.Request {
516-
return authorizedClient.R().SetPathParam("id", "delete-test")
531+
request: func(id string) *resty.Request {
532+
return authorizedClient.R().SetPathParam("id", id)
517533
},
518534
validate: func(t *testing.T, response *resty.Response) {
519535
if len(response.Body()) != 0 {
@@ -523,11 +539,12 @@ func TestWebhooks_Delete(t *testing.T) {
523539
},
524540
{
525541
name: "Remove non-existent webhook",
526-
setup: func() {
542+
setup: func() string {
527543
// No setup needed
544+
return ""
528545
},
529546
expectedStatusCode: 204,
530-
request: func() *resty.Request {
547+
request: func(id string) *resty.Request {
531548
return authorizedClient.R().SetPathParam("id", "non-existent-id")
532549
},
533550
validate: func(t *testing.T, response *resty.Response) {
@@ -538,11 +555,12 @@ func TestWebhooks_Delete(t *testing.T) {
538555
},
539556
{
540557
name: "Missing authentication",
541-
setup: func() {
558+
setup: func() string {
542559
// No setup needed
560+
return ""
543561
},
544562
expectedStatusCode: 401,
545-
request: func() *resty.Request {
563+
request: func(id string) *resty.Request {
546564
return publicUserClient.R().SetPathParam("id", "test-id")
547565
},
548566
validate: func(t *testing.T, response *resty.Response) {
@@ -558,11 +576,12 @@ func TestWebhooks_Delete(t *testing.T) {
558576
},
559577
{
560578
name: "Invalid credentials",
561-
setup: func() {
579+
setup: func() string {
562580
// No setup needed
581+
return ""
563582
},
564583
expectedStatusCode: 401,
565-
request: func() *resty.Request {
584+
request: func(id string) *resty.Request {
566585
return publicUserClient.R().SetBasicAuth("invalid", "credentials").SetPathParam("id", "test-id")
567586
},
568587
validate: func(t *testing.T, response *resty.Response) {
@@ -580,9 +599,15 @@ func TestWebhooks_Delete(t *testing.T) {
580599

581600
for _, c := range cases {
582601
t.Run(c.name, func(t *testing.T) {
583-
c.setup()
602+
webhookID := c.setup()
603+
// Clean up the webhook if one was created
604+
if webhookID != "" {
605+
t.Cleanup(func() {
606+
authorizedClient.R().Delete("webhooks/" + webhookID)
607+
})
608+
}
584609

585-
res, err := c.request().Delete("webhooks/{id}")
610+
res, err := c.request(webhookID).Delete("webhooks/{id}")
586611
if err != nil {
587612
t.Fatal(err)
588613
}
@@ -594,6 +619,7 @@ func TestWebhooks_Delete(t *testing.T) {
594619
if c.validate != nil {
595620
c.validate(t, res)
596621
}
622+
597623
})
598624
}
599625
}

0 commit comments

Comments
 (0)