mirror of
https://github.com/wneessen/go-hibp.git
synced 2024-11-22 12:50:50 +01:00
Merge pull request #18 from wneessen/test-coverage
Fixes and test coverage
This commit is contained in:
commit
a931f4aef3
3 changed files with 52 additions and 5 deletions
|
@ -213,8 +213,9 @@ func (d *ApiDate) UnmarshalJSON(s []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Time adds a Time() method to the ApiDate converted time.Time type
|
// Time adds a Time() method to the ApiDate converted time.Time type
|
||||||
func (d ApiDate) Time() time.Time {
|
func (d *ApiDate) Time() time.Time {
|
||||||
return time.Time(d)
|
dp := *d
|
||||||
|
return time.Time(dp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// setBreachOpts returns a map of default settings and overridden values from different BreachOption
|
// setBreachOpts returns a map of default settings and overridden values from different BreachOption
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
package hibp
|
package hibp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"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
|
// TestBreaches tests the Breaches() method of the breaches API
|
||||||
func TestBreaches(t *testing.T) {
|
func TestBreaches(t *testing.T) {
|
||||||
hc := New()
|
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
|
// ExampleBreachApi_Breaches_getAllBreaches is a code example to show how to fetch all breaches from the
|
||||||
// HIBP breaches API
|
// HIBP breaches API
|
||||||
func ExampleBreachApi_Breaches_getAllBreaches() {
|
func ExampleBreachApi_Breaches_getAllBreaches() {
|
||||||
|
|
|
@ -162,7 +162,7 @@ func ExamplePwnedPassApi_CheckPassword() {
|
||||||
if m != nil && m.Count != 0 {
|
if m != nil && m.Count != 0 {
|
||||||
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
||||||
m.Hash, m.Count)
|
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 {
|
if m != nil && m.Count != 0 {
|
||||||
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
||||||
m.Hash, m.Count)
|
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 {
|
if m != nil && m.Count != 0 {
|
||||||
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
||||||
m.Hash, m.Count)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue