From 540e745c7fe2e1399cc7838cd0d836b904747ec3 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 1 Oct 2022 15:04:06 +0200 Subject: [PATCH 1/4] Fixed output results in PwnedPassApi tests --- password_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/password_test.go b/password_test.go index 65dd035..a4ad5b9 100644 --- a/password_test.go +++ b/password_test.go @@ -162,7 +162,7 @@ func ExamplePwnedPassApi_CheckPassword() { if m != nil && m.Count != 0 { fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", m.Hash, m.Count) - // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86453 times in the pwned passwords DB + // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86495 times in the pwned passwords DB } } @@ -177,7 +177,7 @@ func ExamplePwnedPassApi_CheckPassword_withPadding() { if m != nil && m.Count != 0 { fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", m.Hash, m.Count) - // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86453 times in the pwned passwords DB + // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86495 times in the pwned passwords DB } } @@ -193,6 +193,6 @@ func ExamplePwnedPassApi_CheckSHA1() { if m != nil && m.Count != 0 { fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n", m.Hash, m.Count) - // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86453 times in the pwned passwords DB + // Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86495 times in the pwned passwords DB } } From a8623f1162ad36521b2600cd98fb593079504438 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 1 Oct 2022 16:05:50 +0200 Subject: [PATCH 2/4] Fixed mixed pointer/copy assignment in ApiDate type --- breach.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/breach.go b/breach.go index 4ae83f4..cecfb76 100644 --- a/breach.go +++ b/breach.go @@ -213,8 +213,9 @@ func (d *ApiDate) UnmarshalJSON(s []byte) error { } // Time adds a Time() method to the ApiDate converted time.Time type -func (d ApiDate) Time() time.Time { - return time.Time(d) +func (d *ApiDate) Time() time.Time { + dp := *d + return time.Time(dp) } // setBreachOpts returns a map of default settings and overridden values from different BreachOption From 2a92929c2024c72a904bdc84eac1fabeadd4bcab Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 1 Oct 2022 16:06:56 +0200 Subject: [PATCH 3/4] Better test coverage for breaches API --- breach_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/breach_test.go b/breach_test.go index 8e02fdd..602cd84 100644 --- a/breach_test.go +++ b/breach_test.go @@ -1,11 +1,19 @@ package hibp import ( + "encoding/json" "fmt" "os" "testing" ) +const ( + validDateJSON = `{"date": "2022-10-01"}` + validNullDateJSON = `{"date": "null""}` + invalidJSON = `{"date": '2022-10-01'}` + invalidDateJSON = `{"date": "202299-10-01"}` +) + // TestBreaches tests the Breaches() method of the breaches API func TestBreaches(t *testing.T) { hc := New() @@ -238,6 +246,44 @@ func TestBreachedAccountWithoutTruncate(t *testing.T) { } } +// TestApiDate_UnmarshalJSON_Time tests the ApiDate type JSON unmarshalling +func TestApiDate_UnmarshalJSON_Time(t *testing.T) { + type testData struct { + Date *ApiDate `json:"date"` + } + 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 { + t.Errorf("unmarshal on ApiDate type failed. Expected data but got nil") + return + } + if !tc.nil { + tdd := td.Date.Time().Format("2006-01-02") + if tdd != tc.d && !tc.sf { + t.Errorf(`unmarshal of ApiDate type failed. Expected: %q, got %q"`, tc.d, tdd) + } + } + }) + } +} + // ExampleBreachApi_Breaches_getAllBreaches is a code example to show how to fetch all breaches from the // HIBP breaches API func ExampleBreachApi_Breaches_getAllBreaches() { From f1d9046155ead2439f814dd3ae4830cad87180ca Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 1 Oct 2022 16:10:25 +0200 Subject: [PATCH 4/4] Fixed failing test --- breach_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breach_test.go b/breach_test.go index 602cd84..554e02b 100644 --- a/breach_test.go +++ b/breach_test.go @@ -9,7 +9,7 @@ import ( const ( validDateJSON = `{"date": "2022-10-01"}` - validNullDateJSON = `{"date": "null""}` + validNullDateJSON = `{"date": "null"}` invalidJSON = `{"date": '2022-10-01'}` invalidDateJSON = `{"date": "202299-10-01"}` )