mirror of
https://github.com/wneessen/go-hibp.git
synced 2024-11-23 05:10:50 +01:00
Winni Neessen
e42f8b3101
Fixes #22 - In case of a HTTP error the PastedAccount method is supposed to return the HTTP response, since this can hold valuable information about the reason why the request failed. Instead, it was returning `nil`. This PR fixes this behaviour. - Additionally, this PR introduces tests to catch such oversights - Finally a proper `error.New()` error has been introduces, to that `error.Is()` can be used on common error that are detected by the module
105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
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", "account-exists@hibp-integration-tests.com",
|
|
true, false,
|
|
},
|
|
{
|
|
"opt-out is not found in pastes", "opt-out-breach@hibp-integration-tests.com",
|
|
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("ö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")
|
|
}
|
|
}
|
|
|
|
// 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("account-exists@hibp-integration-tests.com")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for _, p := range pd {
|
|
fmt.Printf("Your account was part of the %q paste\n", p.Title)
|
|
}
|
|
}
|