2021-09-21 18:21:23 +02:00
|
|
|
package hibp
|
|
|
|
|
|
|
|
import (
|
2022-10-01 16:06:56 +02:00
|
|
|
"encoding/json"
|
2022-12-22 11:55:06 +01:00
|
|
|
"errors"
|
2021-09-22 13:59:22 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-09-21 18:21:23 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-10-01 16:06:56 +02:00
|
|
|
const (
|
|
|
|
validDateJSON = `{"date": "2022-10-01"}`
|
2022-10-01 16:10:25 +02:00
|
|
|
validNullDateJSON = `{"date": "null"}`
|
2022-10-01 16:06:56 +02:00
|
|
|
invalidJSON = `{"date": '2022-10-01'}`
|
|
|
|
invalidDateJSON = `{"date": "202299-10-01"}`
|
|
|
|
)
|
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_Breaches tests the Breaches() method of the breaches API
|
|
|
|
func TestBreachAPI_Breaches(t *testing.T) {
|
2021-09-21 19:46:48 +02:00
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
breachList, _, err := hc.BreachAPI.Breaches()
|
2021-09-21 18:21:23 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-09-21 19:56:05 +02:00
|
|
|
if breachList != nil && len(breachList) <= 0 {
|
2021-09-21 19:46:48 +02:00
|
|
|
t.Error("breaches list returned 0 results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_Breaches_WithNIL tests the Breaches() method of the breaches API with a nil option
|
|
|
|
func TestBreachAPI_Breaches_WithNIL(t *testing.T) {
|
2021-09-22 13:59:22 +02:00
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
breachList, _, err := hc.BreachAPI.Breaches(nil)
|
2021-09-22 13:59:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2022-12-22 11:55:06 +01:00
|
|
|
return
|
2021-09-22 13:59:22 +02:00
|
|
|
}
|
|
|
|
if breachList != nil && len(breachList) <= 0 {
|
|
|
|
t.Error("breaches list returned 0 results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_Breaches_WithDomain tests the Breaches() method of the breaches API for a specific domain
|
|
|
|
func TestBreachAPI_Breaches_WithDomain(t *testing.T) {
|
2021-09-21 19:46:48 +02:00
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
domain string
|
|
|
|
isBreached bool
|
|
|
|
}{
|
|
|
|
{"adobe.com is breached", "adobe.com", true},
|
|
|
|
{"example.com is not breached", "example.com", false},
|
|
|
|
}
|
|
|
|
|
2022-05-07 14:55:33 +02:00
|
|
|
hc := New(WithRateLimitSleep())
|
2021-09-21 19:46:48 +02:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
breachList, _, err := hc.BreachAPI.Breaches(WithDomain(tc.domain))
|
2021-09-21 19:46:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2022-12-22 11:55:06 +01:00
|
|
|
return
|
2021-09-21 19:46:48 +02:00
|
|
|
}
|
2021-09-21 19:56:05 +02:00
|
|
|
|
|
|
|
if breachList == nil && tc.isBreached {
|
|
|
|
t.Errorf("domain %s is expected to be breached, but returned 0 results.",
|
|
|
|
tc.domain)
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:46:48 +02:00
|
|
|
breachLen := len(breachList)
|
|
|
|
if tc.isBreached && breachLen <= 0 {
|
|
|
|
t.Errorf("domain %s is expected to be breached, but returned 0 results.",
|
|
|
|
tc.domain)
|
|
|
|
}
|
|
|
|
if !tc.isBreached && breachLen != 0 {
|
|
|
|
t.Errorf("domain %s is expected to be not breached, but returned %d results.",
|
|
|
|
tc.domain, breachLen)
|
|
|
|
}
|
|
|
|
})
|
2021-09-21 18:21:23 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-22 09:46:18 +02:00
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_Breaches_WithoutUnverified tests the Breaches() method of the breaches API with the unverified parameter
|
|
|
|
func TestBreachAPI_Breaches_WithoutUnverified(t *testing.T) {
|
2021-09-22 09:46:18 +02:00
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
domain string
|
|
|
|
isBreached bool
|
|
|
|
isVerified bool
|
|
|
|
}{
|
|
|
|
{"adobe.com is breached and verified", "adobe.com", true, true},
|
|
|
|
{"parapa.mail.ru is breached and verified", "parapa.mail.ru", true, true},
|
|
|
|
{"xiaomi.cn is breached but not verified", "xiaomi.cn", true, false},
|
|
|
|
}
|
|
|
|
|
2022-05-07 14:55:33 +02:00
|
|
|
hc := New(WithRateLimitSleep())
|
2021-09-22 09:46:18 +02:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
breachList, _, err := hc.BreachAPI.Breaches(WithDomain(tc.domain), WithoutUnverified())
|
2021-09-22 09:46:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2022-12-22 11:55:06 +01:00
|
|
|
return
|
2021-09-22 09:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if breachList == nil && tc.isVerified && tc.isBreached {
|
|
|
|
t.Errorf("domain %s is expected to be breached, but returned 0 results.",
|
|
|
|
tc.domain)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_BreachByName tests the BreachByName() method of the breaches API for a specific domain
|
|
|
|
func TestBreachAPI_BreachByName(t *testing.T) {
|
2021-09-22 09:46:18 +02:00
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
breachName string
|
|
|
|
isBreached bool
|
|
|
|
shouldFail bool
|
|
|
|
}{
|
|
|
|
{"Adobe is a known breach", "Adobe", true, false},
|
|
|
|
{"Example is not a known breach", "Example", false, true},
|
|
|
|
}
|
|
|
|
|
2022-05-07 14:55:33 +02:00
|
|
|
hc := New(WithRateLimitSleep())
|
2021-09-22 09:46:18 +02:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
breachDetails, _, err := hc.BreachAPI.BreachByName(tc.breachName)
|
2021-09-22 09:46:18 +02:00
|
|
|
if err != nil && !tc.shouldFail {
|
|
|
|
t.Error(err)
|
2022-12-22 11:55:06 +01:00
|
|
|
return
|
2021-09-22 09:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if breachDetails == nil && tc.isBreached {
|
|
|
|
t.Errorf("breach with the name %q is expected to be breached, but returned 0 results.",
|
|
|
|
tc.breachName)
|
|
|
|
}
|
|
|
|
if breachDetails != nil && !tc.isBreached {
|
|
|
|
t.Errorf("breach with the name %q is expected to be not breached, but returned breach details.",
|
|
|
|
tc.breachName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 13:59:22 +02:00
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_BreachByName_FailedHTTP tests the BreachByName() method with a failing HTTP request
|
|
|
|
func TestBreachAPI_BreachByName_FailedHTTP(t *testing.T) {
|
|
|
|
hc := New(WithRateLimitSleep())
|
|
|
|
_, res, err := hc.BreachAPI.BreachByName("fäiled_invalid")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("HTTP request was supposed to fail but didn't")
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
t.Errorf("expected HTTP response but got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBreachAPI_BreachByName_Errors tests the errors for the BreachByName() method
|
|
|
|
func TestBreachAPI_BreachByName_Errors(t *testing.T) {
|
|
|
|
hc := New(WithRateLimitSleep())
|
|
|
|
_, _, err := hc.BreachAPI.BreachByName("")
|
|
|
|
if !errors.Is(err, ErrNoName) {
|
|
|
|
t.Errorf("expected to receive ErrNoName error but didn't")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBreachAPI_DataClasses tests the DataClasses() method of the breaches API
|
|
|
|
func TestBreachAPI_DataClasses(t *testing.T) {
|
2021-09-22 13:59:22 +02:00
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
classList, _, err := hc.BreachAPI.DataClasses()
|
2021-09-22 13:59:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2022-12-22 11:55:06 +01:00
|
|
|
return
|
2021-09-22 13:59:22 +02:00
|
|
|
}
|
|
|
|
if classList != nil && len(classList) <= 0 {
|
|
|
|
t.Error("breaches list returned 0 results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_BreachedAccount tests the BreachedAccount() method of the breaches API
|
|
|
|
func TestBreachAPI_BreachedAccount(t *testing.T) {
|
2021-09-22 13:59:22 +02:00
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
accountName string
|
|
|
|
isBreached bool
|
|
|
|
moreThanOneBreach bool
|
|
|
|
}{
|
2022-10-29 15:32:12 +02:00
|
|
|
{
|
|
|
|
"account-exists is breached once", "account-exists", true,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple-breaches is breached multiple times", "multiple-breaches",
|
|
|
|
true, true,
|
|
|
|
},
|
2021-09-22 13:59:22 +02:00
|
|
|
{"opt-out is not breached", "opt-out", false, false},
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:16:02 +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 13:59:22 +02:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
breachDetails, _, err := hc.BreachAPI.BreachedAccount(
|
2021-09-22 13:59:22 +02:00
|
|
|
fmt.Sprintf("%s@hibp-integration-tests.com", tc.accountName))
|
|
|
|
if err != nil && tc.isBreached {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if breachDetails == nil && tc.isBreached {
|
|
|
|
t.Errorf("breach for the account %q is expected, but returned 0 results.",
|
|
|
|
tc.accountName)
|
|
|
|
}
|
|
|
|
if breachDetails != nil && !tc.isBreached {
|
|
|
|
t.Errorf("breach for the account %q is expected to be not breached, but returned breach details.",
|
|
|
|
tc.accountName)
|
|
|
|
}
|
|
|
|
if breachDetails != nil && tc.moreThanOneBreach && len(breachDetails) <= 1 {
|
|
|
|
t.Errorf("breach for the account %q is expected to be breached multiple, but returned %d breaches.",
|
|
|
|
tc.accountName, len(breachDetails))
|
|
|
|
}
|
|
|
|
if breachDetails != nil && !tc.moreThanOneBreach && len(breachDetails) > 1 {
|
|
|
|
t.Errorf("breach for the account %q is expected to be breached once, but returned %d breaches.",
|
|
|
|
tc.accountName, len(breachDetails))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 15:00:29 +02:00
|
|
|
|
2022-12-22 11:55:06 +01:00
|
|
|
// TestBreachAPI_BreachedAccount_FailedHTTP tests the BreachedAccount() method of the breaches API with a failing
|
|
|
|
// HTTP request
|
|
|
|
func TestBreachAPI_BreachedAccount_FailedHTTP(t *testing.T) {
|
|
|
|
apiKey := os.Getenv("HIBP_API_KEY")
|
|
|
|
if apiKey == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())
|
|
|
|
_, res, err := hc.BreachAPI.BreachedAccount("bröken@invalid_domain.tld")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("HTTP request was supposed to fail, but didn't")
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
t.Errorf("expected HTTP response but got nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBreachAPI_BreachedAccount_Errors tests the errors for the BreachedAccount() method
|
|
|
|
func TestBreachAPI_BreachedAccount_Errors(t *testing.T) {
|
|
|
|
hc := New(WithRateLimitSleep())
|
|
|
|
_, _, err := hc.BreachAPI.BreachedAccount("")
|
|
|
|
if !errors.Is(err, ErrNoAccountID) {
|
|
|
|
t.Errorf("expected to receive ErrNoAccountID error but didn't")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBreachAPI_BreachedAccount_WithoutTruncate tests the BreachedAccount() method of the breaches API with the
|
2021-09-22 15:00:29 +02:00
|
|
|
// truncateResponse option set to false
|
2022-12-22 11:55:06 +01:00
|
|
|
func TestBreachAPI_BreachedAccount_WithoutTruncate(t *testing.T) {
|
2021-09-22 15:00:29 +02:00
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
accountName string
|
|
|
|
breachName string
|
|
|
|
breachDomain string
|
|
|
|
shouldFail bool
|
|
|
|
}{
|
2022-10-29 15:32:12 +02:00
|
|
|
{
|
2022-12-22 11:55:06 +01:00
|
|
|
"account-exists is breached once", "account-exists@hibp-integration-tests.com",
|
|
|
|
"Adobe", "adobe.com", false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple-breaches is breached multiple times", "multiple-breaches@hibp-integration-tests.com",
|
|
|
|
"Adobe", "adobe.com", false,
|
2022-10-29 15:32:12 +02:00
|
|
|
},
|
|
|
|
{
|
2022-12-22 11:55:06 +01:00
|
|
|
"opt-out is not breached", "opt-out@hibp-integration-tests.com", "",
|
|
|
|
"", true,
|
2022-10-29 15:32:12 +02:00
|
|
|
},
|
2022-05-07 15:59:20 +02:00
|
|
|
{"empty string should fail", "", "", "", true},
|
2021-09-22 15:00:29 +02:00
|
|
|
}
|
|
|
|
|
2021-09-22 15:16:02 +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 15:00:29 +02:00
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
breachDetails, _, err := hc.BreachAPI.BreachedAccount(tc.accountName, WithoutTruncate())
|
2021-09-22 15:00:29 +02:00
|
|
|
if err != nil && !tc.shouldFail {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2021-09-22 15:10:10 +02:00
|
|
|
if len(breachDetails) == 0 && !tc.shouldFail {
|
|
|
|
t.Errorf("breach details for account %q are expected but none were returned", tc.accountName)
|
|
|
|
return
|
|
|
|
}
|
2021-09-22 15:00:29 +02:00
|
|
|
|
2021-09-22 15:10:10 +02:00
|
|
|
if len(breachDetails) > 0 {
|
|
|
|
b := breachDetails[0]
|
2021-09-22 15:00:29 +02:00
|
|
|
if tc.breachName != b.Name {
|
|
|
|
t.Errorf("breach name for the account %q does not match. expected: %q, got: %q",
|
|
|
|
tc.accountName, tc.breachName, b.Name)
|
|
|
|
}
|
|
|
|
if tc.breachDomain != b.Domain {
|
|
|
|
t.Errorf("breach domain for the account %q does not match. expected: %q, got: %q",
|
|
|
|
tc.accountName, tc.breachDomain, b.Domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 12:02:58 +02:00
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// TestAPIDate_UnmarshalJSON_Time tests the APIDate type JSON unmarshalling
|
|
|
|
func TestAPIDate_UnmarshalJSON_Time(t *testing.T) {
|
2022-10-01 16:06:56 +02:00
|
|
|
type testData struct {
|
2022-10-29 15:32:12 +02:00
|
|
|
Date *APIDate `json:"date"`
|
2022-10-01 16:06:56 +02:00
|
|
|
}
|
|
|
|
tt := []struct {
|
|
|
|
n string
|
|
|
|
j []byte
|
|
|
|
d string
|
|
|
|
nil bool
|
|
|
|
sf bool
|
|
|
|
}{
|
|
|
|
{"valid Date JSON", []byte(validDateJSON), "2022-10-01", false, false},
|
|
|
|
{"valid Null Date JSON", []byte(validNullDateJSON), "", true, false},
|
|
|
|
{"invalid JSON", []byte(invalidJSON), "", true, true},
|
|
|
|
{"invalid Date", []byte(invalidDateJSON), "", true, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.n, func(t *testing.T) {
|
|
|
|
var td testData
|
|
|
|
if err := json.Unmarshal(tc.j, &td); err != nil && !tc.sf {
|
|
|
|
t.Errorf("failed to unmarshal test JSON: %s", err)
|
|
|
|
}
|
|
|
|
if td.Date == nil && !tc.nil {
|
2022-10-29 15:32:12 +02:00
|
|
|
t.Errorf("unmarshal on APIDate type failed. Expected data but got nil")
|
2022-10-01 16:06:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !tc.nil {
|
|
|
|
tdd := td.Date.Time().Format("2006-01-02")
|
|
|
|
if tdd != tc.d && !tc.sf {
|
2022-10-29 15:32:12 +02:00
|
|
|
t.Errorf(`unmarshal of APIDate type failed. Expected: %q, got %q"`, tc.d, tdd)
|
2022-10-01 16:06:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExampleBreachAPI_Breaches_getAllBreaches is a code example to show how to fetch all breaches from the
|
2022-05-08 12:02:58 +02:00
|
|
|
// HIBP breaches API
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExampleBreachAPI_Breaches_getAllBreaches() {
|
2022-05-08 12:02:58 +02:00
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
bl, _, err := hc.BreachAPI.Breaches()
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-08 12:09:39 +02:00
|
|
|
if len(bl) != 0 {
|
2022-05-08 12:02:58 +02:00
|
|
|
for _, b := range bl {
|
|
|
|
fmt.Printf("Found breach:\n\tName: %s\n\tDomain: %s\n\tBreach date: %s\n\n",
|
|
|
|
b.Name, b.Domain, b.BreachDate.Time().Format("Mon, 2. January 2006"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExampleBreachAPI_Breaches_getAllBreachesNoUnverified is a code example to show how to fetch all breaches from the
|
2022-05-08 12:02:58 +02:00
|
|
|
// HIBP breaches API but ignoring unverified breaches
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExampleBreachAPI_Breaches_getAllBreachesNoUnverified() {
|
2022-05-08 12:02:58 +02:00
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
bl, _, err := hc.BreachAPI.Breaches()
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-08 12:09:39 +02:00
|
|
|
if len(bl) != 0 {
|
2022-05-08 12:02:58 +02:00
|
|
|
fmt.Printf("Found %d breaches total.\n", len(bl))
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
bl, _, err = hc.BreachAPI.Breaches(WithoutUnverified())
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-05-08 12:09:39 +02:00
|
|
|
if len(bl) != 0 {
|
2022-05-08 12:02:58 +02:00
|
|
|
fmt.Printf("Found %d verified breaches total.\n", len(bl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExampleBreachAPI_BreachByName is a code example to show how to fetch a specific breach
|
2022-05-08 12:02:58 +02:00
|
|
|
// from the HIBP breaches API using the BreachByName method
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExampleBreachAPI_BreachByName() {
|
2022-05-08 12:02:58 +02:00
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
bd, _, err := hc.BreachAPI.BreachByName("Adobe")
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if bd != nil {
|
|
|
|
fmt.Println("Details of the 'Adobe' breach:")
|
|
|
|
fmt.Printf("\tDomain: %s\n", bd.Domain)
|
|
|
|
fmt.Printf("\tBreach date: %s\n", bd.BreachDate.Time().Format("2006-01-02"))
|
|
|
|
fmt.Printf("\tAdded to HIBP: %s\n", bd.AddedDate.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExampleBreachAPI_BreachedAccount is a code example to show how to fetch a list of breaches
|
2022-05-08 12:02:58 +02:00
|
|
|
// for a specific site/account from the HIBP breaches API using the BreachedAccount method
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExampleBreachAPI_BreachedAccount() {
|
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))
|
|
|
|
bd, _, err := hc.BreachAPI.BreachedAccount("multiple-breaches@hibp-integration-tests.com")
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, b := range bd {
|
|
|
|
fmt.Printf("Your account was part of the %q breach\n", b.Name)
|
|
|
|
}
|
|
|
|
}
|