Skip to content

Commit

Permalink
Add some testing
Browse files Browse the repository at this point in the history
Testing is limited by the lack of predictable response from upstream.

If you vote on this idea, you'll receive a notification when it's implemented: https://haveibeenpwned.uservoice.com/forums/275398-general/suggestions/48189713-implement-test-api-key-for-automated-domain-search
  • Loading branch information
freman committed Apr 10, 2024
1 parent a68273e commit 78e78f5
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions breach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,76 @@ func TestBreachAPI_BreachedAccount_WithoutTruncate(t *testing.T) {
}
}

// TestBreachAPI_SubscribedDomains tests the SubscribedDomains() method of the breaches API
func TestBreachAPI_SubscribedDomains(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())

domains, _, err := hc.BreachAPI.SubscribedDomains()
if err != nil {
t.Error(err)
}

if len(domains) < 1 {
t.Log("no subscribed domains found with provided api key")
t.SkipNow()
}

for i, domain := range domains {
t.Run(fmt.Sprintf("checking domain %d", i), func(t *testing.T) {
if domain.DomainName == "" {
t.Error("domain name is missing")
}

if domain.NextSubscriptionRenewal.Time().IsZero() {
t.Error("next subscription renewal is missing")
}
})
}
}

// TestBreachAPI_BreachedDomain tests the BreachedDomain() method of the breaches API
func TestBreachAPI_BreachedDomain(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
if apiKey == "" {
t.SkipNow()
}
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())

domains, _, err := hc.BreachAPI.SubscribedDomains()
if err != nil {
t.Error(err)
}

if len(domains) < 1 {
t.Log("no subscribed domains found with provided api key")
t.SkipNow()
}

for i, domain := range domains {
t.Run(fmt.Sprintf("checking domain %d", i), func(t *testing.T) {
breaches, _, err := hc.BreachAPI.BreachedDomain(domain.DomainName)
if err != nil {
t.Error(err)
}

if len(breaches) < 1 {
t.Logf("domain %s contains no breaches", domain.DomainName)
t.SkipNow()
}

for alias, list := range breaches {
if l := len(list); l == 0 {
t.Errorf("alias %s contains %d breaches, there should be at least 1", alias, l)
}
}
})
}
}

// TestAPIDate_UnmarshalJSON_Time tests the APIDate type JSON unmarshalling
func TestAPIDate_UnmarshalJSON_Time(t *testing.T) {
type testData struct {
Expand Down

0 comments on commit 78e78f5

Please sign in to comment.