-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubscription_test.go
103 lines (97 loc) · 3.73 KB
/
subscription_test.go
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
// SPDX-FileCopyrightText: Winni Neessen <wn@neessen.dev> et al
//
// SPDX-License-Identifier: MIT
package hibp
import (
"errors"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
const (
// ServerResponseSubscriptionStatus specifies the file path for the subscription status test data.
ServerResponseSubscriptionStatus = "testdata/subscription-status.txt"
// ServerResponseSubscriptionStatusBroken specifies the file path for the broken subscription
// status test data.
ServerResponseSubscriptionStatusBroken = "testdata/subscription-status-broken.txt"
)
func TestSubscriptionAPI_Status(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
t.Run("subscription status is returned successfully", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseSubscriptionStatus))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
status, _, err := hc.SubscriptionAPI.Status()
if err != nil {
t.Errorf("failed to get subscription status: %s", err)
}
if !status.Present() {
t.Error("subscription status is expected to be present")
}
if !strings.EqualFold(status.SubscriptionName, "Pwned 1") {
t.Errorf("subscription description is expected to be 'Pwned 1', but is %q", status.Description)
}
})
t.Run("subscription status fails on HTTP error", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusInternalServerError))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.SubscriptionAPI.Status()
if err == nil {
t.Error("expected subscription status request to fail on HTTP error")
}
})
t.Run("subscription status fails on broken JSON", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseSubscriptionStatusBroken))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithAPIKey(apiKey))
_, _, err := hc.SubscriptionAPI.Status()
if err == nil {
t.Error("expected subscription status request to fail on broken JSON")
}
})
t.Run("subscription status with retry after rate limit succeeds", func(t *testing.T) {
run := 0
server := httptest.NewServer(newTestRetryHandler(t, &run, false))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithRateLimitSleep(), WithLogger(newTestLogger(t)),
WithAPIKey(apiKey))
_, _, err := hc.SubscriptionAPI.Status()
if err != nil {
t.Errorf("failed to get subscription status: %s", err)
}
})
t.Run("subscription status with retry after rate limit fails", func(t *testing.T) {
run := 0
server := httptest.NewServer(newTestRetryHandler(t, &run, false))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithLogger(newTestLogger(t)), WithAPIKey(apiKey))
_, hr, err := hc.SubscriptionAPI.Status()
if err == nil {
t.Error("expected subscription status request to fail on HTTP error")
}
if hr == nil {
t.Fatal("expected HTTP response to be returned")
}
if hr.StatusCode != http.StatusTooManyRequests {
t.Errorf("expected HTTP status code to be %d, got %d", http.StatusTooManyRequests, hr.StatusCode)
}
})
t.Run("subscription status fails if no API key is provided", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponseSubscriptionStatus))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.SubscriptionAPI.Status()
if err == nil {
t.Error("expected subscription status request to fail if no API key is provided")
}
if !errors.Is(err, ErrMethodRequiresAPIKey) {
t.Errorf("expected error to be %q, got %q", ErrMethodRequiresAPIKey, err)
}
})
}