-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpaste_test.go
136 lines (130 loc) Β· 4.91 KB
/
paste_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
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
// SPDX-FileCopyrightText: Winni Neessen <[email protected]> et al
//
// SPDX-License-Identifier: MIT
package hibp
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
const (
// ServerResponsePastesAccountExists represents the filename of the test data indicating an existing
// account for pastes.
ServerResponsePastesAccountExists = "testdata/pastes-account-exist.txt"
// ServerResponsePastesAccountExistsBroken represents the filename of the test data for a broken response
// indicating an account exists.
ServerResponsePastesAccountExistsBroken = "testdata/pastes-account-exist-broken.txt"
)
func TestPasteAPI_PastedAccount(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
t.Run("account-exists is found in pastes", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponsePastesAccountExists))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
pastes, _, err := hc.PasteAPI.PastedAccount("[email protected]")
if err != nil {
t.Errorf("failed to get pasted account details: %s", err)
}
if len(pastes) != 1 {
t.Fatalf("expected %d paste(s), got %d", 1, len(pastes))
}
paste := pastes[0]
if !paste.Present() {
t.Error("paste is expected to be present, but is not")
}
if !strings.EqualFold(paste.Title, "nmd") {
t.Errorf("paste title is expected to be 'nmd', but is %q", paste.Title)
}
})
t.Run("opt-out is not found in pastes", func(t *testing.T) {
server := httptest.NewServer(newTestFailureHandler(t, http.StatusNotFound))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
pastes, _, err := hc.PasteAPI.PastedAccount("[email protected]")
if err != nil {
t.Errorf("failed to get pasted account details: %s", err)
}
if len(pastes) != 0 {
t.Error("expected no pastes to be returned, but got some")
}
})
t.Run("pasted account fails on broken JSON", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponsePastesAccountExistsBroken))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.PasteAPI.PastedAccount("[email protected]")
if err == nil {
t.Error("expected pasted account request to fail on broken JSON")
}
})
t.Run("pasted account with empty account id fails", func(t *testing.T) {
server := httptest.NewServer(newTestFileHandler(t, ServerResponsePastesAccountExists))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)))
_, _, err := hc.PasteAPI.PastedAccount("")
if err == nil {
t.Error("expected pasted account request to fail with empty account id")
}
if !errors.Is(err, ErrNoAccountID) {
t.Errorf("expected error to be %q, got %q", ErrNoAccountID, err)
}
})
t.Run("pasted account 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)))
_, _, err := hc.PasteAPI.PastedAccount("[email protected]")
if err == nil {
t.Error("expected pasted account request to fail on HTTP error")
}
})
t.Run("pasted account with retry after rate limit succeeds", func(t *testing.T) {
run := 0
server := httptest.NewServer(newTestRetryHandler(t, &run, true))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithRateLimitSleep(), WithLogger(newTestLogger(t)))
_, _, err := hc.PasteAPI.PastedAccount("[email protected]")
if err != nil {
t.Errorf("failed to get pasted account details: %s", err)
}
})
t.Run("pasted account with retry after rate limit fails", func(t *testing.T) {
run := 0
server := httptest.NewServer(newTestRetryHandler(t, &run, true))
defer server.Close()
hc := New(WithHTTPClient(newTestClient(t, server.URL)), WithLogger(newTestLogger(t)))
_, hr, err := hc.PasteAPI.PastedAccount("[email protected]")
if err == nil {
t.Error("expected pasted account 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)
}
})
}
// ExamplePasteAPI_pastedAccount is a code example to show how to fetch a specific paste
// based on its name from the HIBP pastes API using the PastedAccount() method
func ExamplePasteAPI_pastedAccount() {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
panic("A API key is required for this API")
}
hc := New(WithAPIKey(apiKey))
pd, _, err := hc.PasteAPI.PastedAccount("[email protected]")
if err != nil {
panic(err)
}
for _, p := range pd {
fmt.Printf("Your account was part of the %q paste\n", p.Title)
}
}