-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaste_test.go
105 lines (97 loc) · 2.9 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
package hibp
import (
"errors"
"fmt"
"os"
"testing"
)
// TestPasteAPI_PastedAccount tests the PastedAccount() method of the pastes API
func TestPasteAPI_PastedAccount(t *testing.T) {
testTable := []struct {
testName string
accountName string
isPasted bool
shouldFail bool
}{
{
"account-exists is found in pastes", "[email protected]",
true, false,
},
{
"opt-out is not found in pastes", "[email protected]",
false, true,
},
{"empty account name", "", false, true},
}
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
pasteDetails, _, err := hc.PasteAPI.PastedAccount(tc.accountName)
if err != nil && !tc.shouldFail {
t.Error(err)
return
}
if pasteDetails == nil && tc.isPasted {
t.Errorf("paste for the account %q is expected, but returned 0 results.",
tc.accountName)
}
if pasteDetails != nil && !tc.isPasted {
t.Errorf("paste for the account %q is expected to be not found, but returned paste details.",
tc.accountName)
}
})
}
}
// TestPasteAPI_PastedAccount_WithFailedHTTP tests the PastedAccount() method of the pastes API with a failing HTTP request
func TestPasteAPI_PastedAccount_WithFailedHTTP(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())
_, res, err := hc.PasteAPI.PastedAccount("ö[email protected]")
if err == nil {
t.Errorf("HTTP request for paste should have failed but did not")
return
}
if res == nil {
t.Errorf("HTTP request for paste should have returned the HTTP response but did not")
}
}
// TestPasteAPI_PastedAccount_Errors tests the errors defined for the PastedAccount() method
func TestPasteAPI_PastedAccount_Errors(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())
// No account ID given
_, _, err := hc.PasteAPI.PastedAccount("")
if err == nil {
t.Errorf("HTTP request for paste should have failed but did not")
return
}
if !errors.Is(err, ErrNoAccountID) {
t.Errorf("error response for empty account ID should have been ErrNoAccountID but is not")
}
}
// 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)
}
}