@@ -7,10 +7,10 @@ import (
77 "math/rand"
88 "net/http"
99 "os"
10- "strings"
1110 "testing"
1211 "time"
1312
13+ "github.com/h2non/gock"
1414 "github.com/stretchr/testify/assert"
1515)
1616
@@ -24,86 +24,20 @@ func TestMain(m *testing.M) {
2424}
2525
2626func 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/5c1d8" ).Times (1 ).Reply (200 ).BodyString ("EAF2F254732680E8AC339B84F3266ECCBB5:1\r \n FC446EB88938834178CB9322C1EE273C2A7:2" )
30+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/ba189" ).Times (1 ).Reply (200 ).BodyString ("FD4CB34F0378BCB15D23F6FFD28F0775C9E:3\r \n FDF342FCD8C3611DAE4D76E8A992A3E4169:4" )
31+
32+ count , err := client .CheckPassword ("" , false )
2933 assert .ErrorIs (t , err , ErrEmptyPassword , "blank input should return ErrEmptyPassword" )
34+ assert .Equal (t , - 1 , count )
3035
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 )
3537 assert .NoError (t , err )
38+ assert .Equal (t , 1 , count )
3639
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 )
4141 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 )
10943}
0 commit comments