-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhibp_test.go
88 lines (68 loc) · 2.13 KB
/
hibp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package hibp
import (
"fmt"
"net/http"
"testing"
"time"
. "github.com/onsi/gomega"
)
// TestClientCustomHTTPClient will test using a custom HTTP client.
func TestClientCustomHTTPClient(t *testing.T) {
// Register the test.
RegisterTestingT(t)
client := NewClient()
client.SetHTTPClient(&http.Client{
Timeout: 3 * time.Second,
})
compromised, err := client.Compromised("p@ssword")
if err != nil {
t.Fatalf("Unexpected error running client.Pwned.Compromised(): %s", err)
}
if !compromised {
t.Fatalf("Expected compromised hash (p@ssword) to be true but got: %v", compromised)
}
}
// TestCompromisedHash will test a compromised value against the HIBP API.
func TestCompromisedHash(t *testing.T) {
// Register the test.
RegisterTestingT(t)
client := NewClient()
compromised, err := client.Compromised("p@ssword")
if err != nil {
t.Fatalf("Unexpected error running client.Pwned.Compromised(): %s", err)
}
if !compromised {
t.Fatalf("Expected compromised hash (p@ssword) to be true but got: %v", compromised)
}
}
// TestNonCompromisedHash will test a non-compromised value against the HIBP API.
func TestNonCompromisedHash(t *testing.T) {
// Register the test.
RegisterTestingT(t)
client := NewClient()
// Check if input is compromised.
value := fmt.Sprintf("SHOULD_NOT_BE_COMPROMISED_%s", time.Now().Format("2006-01-02 15:04:05"))
compromised, err := client.Compromised(value)
if err != nil {
t.Fatalf("Unexpected error running client.Pwned.Compromised(): %s", err)
}
if compromised {
t.Fatalf("Expected non-compromised hash to be false but got: %v", compromised)
}
}
func TestEmptyCompromisedHash(t *testing.T) {
// Register the test.
RegisterTestingT(t)
client := NewClient()
// Check if input is compromised.
compromised, err := client.Compromised("")
if err == nil {
t.Fatal("Expected error when checking empty value, but got: nil")
}
if err.Error() != "value for compromised check cannot be empty" {
t.Fatalf("Expected err to read 'value for compromised check cannot be empty' but got: '%v'", err)
}
if compromised {
t.Fatalf("Expected empty compromised hash to be false but got: %v", compromised)
}
}