Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing HTTP response return in PastedAccount method. #23

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package hibp

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"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
type PasteAPI struct {
hibp *Client // References back to the parent HIBP client
Expand Down Expand Up @@ -38,19 +42,19 @@ type Paste struct {
// PastedAccount returns a single breached site based on its name
func (p *PasteAPI) PastedAccount(a string) ([]*Paste, *http.Response, error) {
if a == "" {
return nil, nil, fmt.Errorf("no account id given")
return nil, nil, ErrNoAccountID
}

apiURL := fmt.Sprintf("%s/pasteaccount/%s", BaseURL, a)
hb, hr, err := p.hibp.HTTPResBody(http.MethodGet, apiURL, nil)
au := fmt.Sprintf("%s/pasteaccount/%s", BaseURL, a)
hb, hr, err := p.hibp.HTTPResBody(http.MethodGet, au, nil)
if err != nil {
return nil, nil, err
return nil, hr, err
}

var pasteDetails []*Paste
if err := json.Unmarshal(hb, &pasteDetails); err != nil {
var pd []*Paste
if err := json.Unmarshal(hb, &pd); err != nil {
return nil, hr, err
}

return pasteDetails, hr, nil
return pd, hr, nil
}
62 changes: 53 additions & 9 deletions paste_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package hibp

import (
"errors"
"fmt"
"os"
"testing"
)

// TestPasteAccount tests the BreachedAccount() method of the breaches API
func TestPasteAccount(t *testing.T) {
// TestPasteAPI_PastedAccount tests the PastedAccount() method of the pastes API
func TestPasteAPI_PastedAccount(t *testing.T) {
testTable := []struct {
testName string
accountName string
isBreached bool
isPasted bool
shouldFail bool
}{
{"account-exists is breached once", "[email protected]", true, false},
{"opt-out is not breached", "[email protected]", false, true},
{
"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},
}

Expand All @@ -29,20 +36,57 @@ func TestPasteAccount(t *testing.T) {
pasteDetails, _, err := hc.PasteAPI.PastedAccount(tc.accountName)
if err != nil && !tc.shouldFail {
t.Error(err)
return
}

if pasteDetails == nil && tc.isBreached {
t.Errorf("breach for the account %q is expected, but returned 0 results.",
if pasteDetails == nil && tc.isPasted {
t.Errorf("paste for the account %q is expected, but returned 0 results.",
tc.accountName)
}
if pasteDetails != nil && !tc.isBreached {
t.Errorf("breach for the account %q is expected to be not breached, but returned breach details.",
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() {
Expand Down