mirror of
https://github.com/wneessen/go-hibp.git
synced 2024-11-22 21:00:51 +01:00
Merge pull request #23 from wneessen/fix/22-should-return-httpresponse-for-paste
Fix missing HTTP response return in PastedAccount method.
This commit is contained in:
commit
ec5038a041
2 changed files with 66 additions and 18 deletions
22
paste.go
22
paste.go
|
@ -2,11 +2,15 @@ package hibp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrNoAccountID is returned if no account ID is given to the PastedAccount method
|
||||||
|
var ErrNoAccountID = errors.New("no account ID given")
|
||||||
|
|
||||||
// PasteAPI is a HIBP pastes API client
|
// PasteAPI is a HIBP pastes API client
|
||||||
type PasteAPI struct {
|
type PasteAPI struct {
|
||||||
hibp *Client // References back to the parent HIBP client
|
hibp *Client // References back to the parent HIBP client
|
||||||
|
@ -38,19 +42,19 @@ type Paste struct {
|
||||||
// PastedAccount returns a single breached site based on its name
|
// PastedAccount returns a single breached site based on its name
|
||||||
func (p *PasteAPI) PastedAccount(a string) ([]*Paste, *http.Response, error) {
|
func (p *PasteAPI) PastedAccount(a string) ([]*Paste, *http.Response, error) {
|
||||||
if a == "" {
|
if a == "" {
|
||||||
return nil, nil, fmt.Errorf("no account id given")
|
return nil, nil, ErrNoAccountID
|
||||||
}
|
}
|
||||||
|
|
||||||
apiURL := fmt.Sprintf("%s/pasteaccount/%s", BaseURL, a)
|
au := fmt.Sprintf("%s/pasteaccount/%s", BaseURL, a)
|
||||||
hb, hr, err := p.hibp.HTTPResBody(http.MethodGet, apiURL, nil)
|
hb, hr, err := p.hibp.HTTPResBody(http.MethodGet, au, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var pasteDetails []*Paste
|
|
||||||
if err := json.Unmarshal(hb, &pasteDetails); err != nil {
|
|
||||||
return nil, hr, err
|
return nil, hr, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return pasteDetails, hr, nil
|
var pd []*Paste
|
||||||
|
if err := json.Unmarshal(hb, &pd); err != nil {
|
||||||
|
return nil, hr, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return pd, hr, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,28 @@
|
||||||
package hibp
|
package hibp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestPasteAccount tests the BreachedAccount() method of the breaches API
|
// TestPasteAPI_PastedAccount tests the PastedAccount() method of the pastes API
|
||||||
func TestPasteAccount(t *testing.T) {
|
func TestPasteAPI_PastedAccount(t *testing.T) {
|
||||||
testTable := []struct {
|
testTable := []struct {
|
||||||
testName string
|
testName string
|
||||||
accountName string
|
accountName string
|
||||||
isBreached bool
|
isPasted bool
|
||||||
shouldFail bool
|
shouldFail bool
|
||||||
}{
|
}{
|
||||||
{"account-exists is breached once", "account-exists@hibp-integration-tests.com", true, false},
|
{
|
||||||
{"opt-out is not breached", "opt-out-breach@hibp-integration-tests.com", false, true},
|
"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},
|
{"empty account name", "", false, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,20 +36,57 @@ func TestPasteAccount(t *testing.T) {
|
||||||
pasteDetails, _, err := hc.PasteAPI.PastedAccount(tc.accountName)
|
pasteDetails, _, err := hc.PasteAPI.PastedAccount(tc.accountName)
|
||||||
if err != nil && !tc.shouldFail {
|
if err != nil && !tc.shouldFail {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if pasteDetails == nil && tc.isBreached {
|
if pasteDetails == nil && tc.isPasted {
|
||||||
t.Errorf("breach for the account %q is expected, but returned 0 results.",
|
t.Errorf("paste for the account %q is expected, but returned 0 results.",
|
||||||
tc.accountName)
|
tc.accountName)
|
||||||
}
|
}
|
||||||
if pasteDetails != nil && !tc.isBreached {
|
if pasteDetails != nil && !tc.isPasted {
|
||||||
t.Errorf("breach for the account %q is expected to be not breached, but returned breach details.",
|
t.Errorf("paste for the account %q is expected to be not found, but returned paste details.",
|
||||||
tc.accountName)
|
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
|
// 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
|
// based on its name from the HIBP pastes API using the PastedAccount() method
|
||||||
func ExamplePasteAPI_pastedAccount() {
|
func ExamplePasteAPI_pastedAccount() {
|
||||||
|
|
Loading…
Reference in a new issue