Skip to content

Commit b7efe3a

Browse files
committed
fix: address failing tests and linter errors
1 parent 74f8c6c commit b7efe3a

3 files changed

Lines changed: 58 additions & 44 deletions

File tree

forwardemail/aliases_test.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package forwardemail
55

66
import (
7+
"errors"
78
"fmt"
89
"net/http"
910
"net/http/httptest"
@@ -282,7 +283,7 @@ func TestClient_CreateAlias(t *testing.T) {
282283
"labels": [
283284
"catch-all"
284285
],
285-
Description: "main email",
286+
"description": "main email",
286287
"is_enabled": true,
287288
"has_recipient_verification": true,
288289
"recipients": [
@@ -381,7 +382,7 @@ func TestClient_UpdateAlias(t *testing.T) {
381382
"catch-all",
382383
"friends"
383384
],
384-
Description: "main email",
385+
"description": "main email",
385386
"is_enabled": true,
386387
"has_recipient_verification": true,
387388
"recipients": [
@@ -447,10 +448,10 @@ func TestClient_DeleteAlias(t *testing.T) {
447448
}
448449

449450
tests := []struct {
450-
name string
451-
req request
452-
res response
453-
want error
451+
name string
452+
req request
453+
res response
454+
wantError bool
454455
}{
455456
{
456457
name: "ok",
@@ -464,15 +465,15 @@ func TestClient_DeleteAlias(t *testing.T) {
464465
code: http.StatusInternalServerError,
465466
body: "oh no",
466467
},
467-
want: fmt.Errorf("failed to complete request: oh no"), //nolint:all
468+
wantError: true,
468469
},
469470
}
470471

471472
for _, tt := range tests {
472473
t.Run(tt.name, func(t *testing.T) {
473474
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
474475
w.WriteHeader(tt.res.code)
475-
fmt.Fprint(w, tt.res.body)
476+
_, _ = w.Write([]byte(tt.res.body))
476477
}))
477478
defer svr.Close()
478479

@@ -482,8 +483,15 @@ func TestClient_DeleteAlias(t *testing.T) {
482483
})
483484

484485
got := c.DeleteAlias(tt.req.domain, tt.req.alias)
485-
if diff := cmp.Diff(tt.want, got, cmp.Comparer(equateErrorMessage)); diff != "" {
486-
t.Fatalf("values are not the same %s", diff)
486+
if tt.wantError {
487+
if got == nil {
488+
t.Fatal("expected error, got nil")
489+
}
490+
if !errors.Is(got, ErrRequestFailure) {
491+
t.Fatalf("expected error to wrap ErrRequestFailure, got %v", got)
492+
}
493+
} else if got != nil {
494+
t.Fatalf("expected no error, got %v", got)
487495
}
488496
})
489497
}

forwardemail/client_test.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@
44
package forwardemail
55

66
import (
7+
"errors"
78
"net/http"
89
"testing"
10+
"time"
911

1012
"github.com/google/go-cmp/cmp"
1113
)
1214

1315
func TestNewClient(t *testing.T) {
1416
tests := []struct {
15-
name string
16-
options ClientOptions
17-
want *Client
17+
name string
18+
options ClientOptions
19+
want *Client
20+
wantError error
1821
}{
1922
{
20-
name: "empty options",
21-
options: ClientOptions{},
22-
want: &Client{
23-
APIURL: "https://api.forwardemail.net",
24-
HTTPClient: &http.Client{},
25-
},
23+
name: "empty options returns error",
24+
options: ClientOptions{},
25+
want: nil,
26+
wantError: ErrMissingAPIKey,
2627
},
2728
{
2829
name: "with api key",
@@ -32,18 +33,16 @@ func TestNewClient(t *testing.T) {
3233
want: &Client{
3334
APIKey: "4e4d6c332b6fe62a63afe56171fd3725",
3435
APIURL: "https://api.forwardemail.net",
35-
HTTPClient: &http.Client{},
36+
HTTPClient: &http.Client{Timeout: 30 * time.Second},
3637
},
3738
},
3839
{
39-
name: "with api url",
40+
name: "with api url but no api key returns error",
4041
options: ClientOptions{
4142
APIURL: "https://google.com",
4243
},
43-
want: &Client{
44-
APIURL: "https://google.com",
45-
HTTPClient: &http.Client{},
46-
},
44+
want: nil,
45+
wantError: ErrMissingAPIKey,
4746
},
4847
{
4948
name: "with everything at once",
@@ -54,14 +53,23 @@ func TestNewClient(t *testing.T) {
5453
want: &Client{
5554
APIKey: "4e4d6c332b6fe62a63afe56171fd3725",
5655
APIURL: "https://google.com",
57-
HTTPClient: &http.Client{},
56+
HTTPClient: &http.Client{Timeout: 30 * time.Second},
5857
},
5958
},
6059
}
6160

6261
for _, tt := range tests {
6362
t.Run(tt.name, func(t *testing.T) {
64-
got, _ := NewClient(tt.options)
63+
got, err := NewClient(tt.options)
64+
if tt.wantError != nil {
65+
if !errors.Is(err, tt.wantError) {
66+
t.Fatalf("expected error %v, got %v", tt.wantError, err)
67+
}
68+
return
69+
}
70+
if err != nil {
71+
t.Fatalf("unexpected error: %v", err)
72+
}
6573
if diff := cmp.Diff(tt.want, got); diff != "" {
6674
t.Fatalf("values are not the same %s", diff)
6775
}

forwardemail/domains_test.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,10 @@ func TestClient_DeleteDomain(t *testing.T) {
382382
}
383383

384384
tests := []struct {
385-
name string
386-
domain string
387-
resp response
388-
want error
385+
name string
386+
domain string
387+
resp response
388+
wantError bool
389389
}{
390390
{
391391
name: "ok",
@@ -399,15 +399,15 @@ func TestClient_DeleteDomain(t *testing.T) {
399399
code: http.StatusInternalServerError,
400400
body: "oh no",
401401
},
402-
want: fmt.Errorf("failed to complete request: oh no"), //nolint:all
402+
wantError: true,
403403
},
404404
}
405405

406406
for _, tt := range tests {
407407
t.Run(tt.name, func(t *testing.T) {
408408
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
409409
w.WriteHeader(tt.resp.code)
410-
fmt.Fprint(w, tt.resp.body)
410+
_, _ = w.Write([]byte(tt.resp.body))
411411
}))
412412
defer svr.Close()
413413

@@ -417,22 +417,20 @@ func TestClient_DeleteDomain(t *testing.T) {
417417
})
418418

419419
got := c.DeleteDomain(tt.domain)
420-
if diff := cmp.Diff(tt.want, got, cmp.Comparer(equateErrorMessage)); diff != "" {
421-
t.Fatalf("values are not the same %s", diff)
420+
if tt.wantError {
421+
if got == nil {
422+
t.Fatal("expected error, got nil")
423+
}
424+
if !errors.Is(got, ErrRequestFailure) {
425+
t.Fatalf("expected error to wrap ErrRequestFailure, got %v", got)
426+
}
427+
} else if got != nil {
428+
t.Fatalf("expected no error, got %v", got)
422429
}
423430
})
424431
}
425432
}
426433

427-
// I took this black magic from here (thanks Joe):
428-
// https://github.com/google/go-cmp/issues/24#issuecomment-317635190
429-
func equateErrorMessage(a, b error) bool {
430-
if a == nil || b == nil {
431-
return errors.Is(a, b)
432-
}
433-
return a.Error() == b.Error()
434-
}
435-
436434
func pointBool(b bool) *bool {
437435
return &b
438436
}

0 commit comments

Comments
 (0)