@@ -7,10 +7,10 @@ import (
7
7
"math/rand"
8
8
"net/http"
9
9
"os"
10
- "strings"
11
10
"testing"
12
11
"time"
13
12
13
+ "github.com/h2non/gock"
14
14
"github.com/stretchr/testify/assert"
15
15
)
16
16
@@ -24,86 +24,20 @@ func TestMain(m *testing.M) {
24
24
}
25
25
26
26
func TestPassword (t * testing.T ) {
27
- // Check input error
28
- _ , err := client .CheckPassword ("" , false )
27
+ defer gock .Off ()
28
+
29
+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/ba189" ).Times (1 ).Reply (200 ).BodyString ("EAF2F254732680E8AC339B84F3266ECCBB5:10\n 00AF95075E990658DE18AC06B46E9E581C3:1" )
30
+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/5c1d8" ).Times (1 ).Reply (200 ).BodyString ("FE36C085E615BB5E9F30E5EA9322C498AD4:1\F C446EB88938834178CB9322C1EE273C2A7:1" )
31
+
32
+ count , err := client .CheckPassword ("" , false )
29
33
assert .ErrorIs (t , err , ErrEmptyPassword , "blank input should return ErrEmptyPassword" )
34
+ assert .Equal (t , - 1 , count )
30
35
31
- // Should fail
32
- fail := "password1234"
33
- count , err := client .CheckPassword (fail , false )
34
- assert .NotEmpty (t , count , "%s should fail as a password" , fail )
36
+ count , err = client .CheckPassword ("pwned" , true )
35
37
assert .NoError (t , err )
38
+ assert .Equal (t , 10 , count )
36
39
37
- // Should fail (with padding)
38
- failPad := "administrator"
39
- count , err = client .CheckPassword (failPad , true )
40
- assert .NotEmpty (t , count , "%s should fail as a password" , failPad )
40
+ count , err = client .CheckPassword ("notpwned" , false )
41
41
assert .NoError (t , err )
42
-
43
- // Checking for a "good" password isn't going to be perfect, but we can give it a good try
44
- // with hopefully minimal error. Try five times?
45
- assert .Condition (t , func () bool {
46
- for i := 0 ; i <= 5 ; i ++ {
47
- count , err = client .CheckPassword (testPassword (), false )
48
- assert .NoError (t , err )
49
- if count == 0 {
50
- return true
51
- }
52
- }
53
- return false
54
- }, "no generated passwords passed. there is a chance this is a fluke" )
55
-
56
- // Again, but with padded responses
57
- assert .Condition (t , func () bool {
58
- for i := 0 ; i <= 5 ; i ++ {
59
- count , err = client .CheckPassword (testPassword (), true )
60
- assert .NoError (t , err )
61
- if count == 0 {
62
- return true
63
- }
64
- }
65
- return false
66
- }, "no generated passwords passed. there is a chance this is a fluke" )
67
- }
68
-
69
- // Credit to https://golangbyexample.com/generate-random-password-golang/
70
- // DO NOT USE THIS FOR AN ACTUAL PASSWORD GENERATOR
71
- var (
72
- lowerCharSet = "abcdedfghijklmnopqrst"
73
- upperCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
74
- specialCharSet = "!@#$%&*"
75
- numberSet = "0123456789"
76
- allCharSet = lowerCharSet + upperCharSet + specialCharSet + numberSet
77
- )
78
-
79
- func testPassword () string {
80
- var password strings.Builder
81
-
82
- // Set special character
83
- for i := 0 ; i < 5 ; i ++ {
84
- random := rand .Intn (len (specialCharSet ))
85
- password .WriteString (string (specialCharSet [random ]))
86
- }
87
-
88
- // Set numeric
89
- for i := 0 ; i < 5 ; i ++ {
90
- random := rand .Intn (len (numberSet ))
91
- password .WriteString (string (numberSet [random ]))
92
- }
93
-
94
- // Set uppercase
95
- for i := 0 ; i < 5 ; i ++ {
96
- random := rand .Intn (len (upperCharSet ))
97
- password .WriteString (string (upperCharSet [random ]))
98
- }
99
-
100
- for i := 0 ; i < 5 ; i ++ {
101
- random := rand .Intn (len (allCharSet ))
102
- password .WriteString (string (allCharSet [random ]))
103
- }
104
- inRune := []rune (password .String ())
105
- rand .Shuffle (len (inRune ), func (i , j int ) {
106
- inRune [i ], inRune [j ] = inRune [j ], inRune [i ]
107
- })
108
- return string (inRune )
42
+ assert .Equal (t , 0 , count )
109
43
}
0 commit comments