2021-09-22 16:20:54 +02:00
|
|
|
package hibp
|
|
|
|
|
|
|
|
import (
|
2022-05-08 12:02:58 +02:00
|
|
|
"fmt"
|
2021-09-22 16:20:54 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestPasteAccount tests the BreachedAccount() method of the breaches API
|
|
|
|
func TestPasteAccount(t *testing.T) {
|
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
accountName string
|
|
|
|
isBreached bool
|
2022-05-07 15:59:20 +02:00
|
|
|
shouldFail bool
|
2021-09-22 16:20:54 +02:00
|
|
|
}{
|
2022-05-07 15:59:20 +02:00
|
|
|
{"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},
|
|
|
|
{"empty account name", "", false, true},
|
2021-09-22 16:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
apiKey := os.Getenv("HIBP_API_KEY")
|
|
|
|
if apiKey == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2022-05-07 14:55:33 +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-05-07 15:59:20 +02:00
|
|
|
pasteDetails, _, err := hc.PasteApi.PastedAccount(tc.accountName)
|
|
|
|
if err != nil && !tc.shouldFail {
|
2021-09-22 16:20:54 +02:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pasteDetails == nil && tc.isBreached {
|
|
|
|
t.Errorf("breach 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.",
|
|
|
|
tc.accountName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 12:02:58 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|