2021-09-22 16:20:54 +02:00
|
|
|
package hibp
|
|
|
|
|
|
|
|
import (
|
2022-12-22 09:57:57 +01:00
|
|
|
"errors"
|
2022-05-08 12:02:58 +02:00
|
|
|
"fmt"
|
2021-09-22 16:20:54 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-12-22 09:57:57 +01:00
|
|
|
// TestPasteAPI_PastedAccount tests the PastedAccount() method of the pastes API
|
|
|
|
func TestPasteAPI_PastedAccount(t *testing.T) {
|
2021-09-22 16:20:54 +02:00
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
accountName string
|
2022-12-22 09:57:57 +01:00
|
|
|
isPasted bool
|
2022-05-07 15:59:20 +02:00
|
|
|
shouldFail bool
|
2021-09-22 16:20:54 +02:00
|
|
|
}{
|
2022-12-22 09:57:57 +01:00
|
|
|
{
|
|
|
|
"account-exists is found in pastes", "account-exists@hibp-integration-tests.com",
|
|
|
|
true, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"opt-out is not found in pastes", "opt-out-breach@hibp-integration-tests.com",
|
|
|
|
false, true,
|
|
|
|
},
|
2022-05-07 15:59:20 +02:00
|
|
|
{"empty account name", "", false, true},
|
2021-09-22 16:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
apiKey := os.Getenv("HIBP_API_KEY")
|
|
|
|
if apiKey == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2022-10-29 15:32:12 +02:00
|
|
|
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())
|
2021-09-22 16:20:54 +02:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
pasteDetails, _, err := hc.PasteAPI.PastedAccount(tc.accountName)
|
2022-05-07 15:59:20 +02:00
|
|
|
if err != nil && !tc.shouldFail {
|
2021-09-22 16:20:54 +02:00
|
|
|
t.Error(err)
|
2022-12-22 09:57:57 +01:00
|
|
|
return
|
2021-09-22 16:20:54 +02:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:57:57 +01:00
|
|
|
if pasteDetails == nil && tc.isPasted {
|
|
|
|
t.Errorf("paste for the account %q is expected, but returned 0 results.",
|
2021-09-22 16:20:54 +02:00
|
|
|
tc.accountName)
|
|
|
|
}
|
2022-12-22 09:57:57 +01:00
|
|
|
if pasteDetails != nil && !tc.isPasted {
|
|
|
|
t.Errorf("paste for the account %q is expected to be not found, but returned paste details.",
|
2021-09-22 16:20:54 +02:00
|
|
|
tc.accountName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 12:02:58 +02:00
|
|
|
|
2022-12-22 09:57:57 +01:00
|
|
|
// 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("öccount-exists@hibp-integration-tests.com")
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExamplePasteAPI_pastedAccount is a code example to show how to fetch a specific paste
|
2022-05-08 12:02:58 +02:00
|
|
|
// based on its name from the HIBP pastes API using the PastedAccount() method
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExamplePasteAPI_pastedAccount() {
|
2022-05-08 12:02:58 +02:00
|
|
|
apiKey := os.Getenv("HIBP_API_KEY")
|
|
|
|
if apiKey == "" {
|
|
|
|
panic("A API key is required for this API")
|
|
|
|
}
|
2022-10-29 15:32:12 +02:00
|
|
|
hc := New(WithAPIKey(apiKey))
|
|
|
|
pd, _, err := hc.PasteAPI.PastedAccount("account-exists@hibp-integration-tests.com")
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, p := range pd {
|
|
|
|
fmt.Printf("Your account was part of the %q paste\n", p.Title)
|
|
|
|
}
|
|
|
|
}
|