Skip to content

Commit 807bdd6

Browse files
committed
Remove external api call in TestPassword
1 parent 1e749b8 commit 807bdd6

File tree

3 files changed

+20
-78
lines changed

3 files changed

+20
-78
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ require (
5858
github.com/google/uuid v1.6.0
5959
github.com/gorilla/feeds v1.1.2
6060
github.com/gorilla/sessions v1.2.2
61+
github.com/h2non/gock v1.2.0
6162
github.com/hashicorp/go-version v1.6.0
6263
github.com/hashicorp/golang-lru/v2 v2.0.7
6364
github.com/huandu/xstrings v1.4.0
@@ -209,6 +210,7 @@ require (
209210
github.com/gorilla/handlers v1.5.2 // indirect
210211
github.com/gorilla/mux v1.8.1 // indirect
211212
github.com/gorilla/securecookie v1.1.2 // indirect
213+
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
212214
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
213215
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
214216
github.com/hashicorp/hcl v1.0.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pw
430430
github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
431431
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
432432
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
433+
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
434+
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
435+
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
436+
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
433437
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
434438
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
435439
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
@@ -591,6 +595,8 @@ github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM=
591595
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
592596
github.com/msteinert/pam v1.2.0 h1:mYfjlvN2KYs2Pb9G6nb/1f/nPfAttT/Jee5Sq9r3bGE=
593597
github.com/msteinert/pam v1.2.0/go.mod h1:d2n0DCUK8rGecChV3JzvmsDjOY4R7AYbsNxAT+ftQl0=
598+
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
599+
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
594600
github.com/niklasfasching/go-org v1.7.0 h1:vyMdcMWWTe/XmANk19F4k8XGBYg0GQ/gJGMimOjGMek=
595601
github.com/niklasfasching/go-org v1.7.0/go.mod h1:WuVm4d45oePiE0eX25GqTDQIt/qPW1T9DGkRscqLW5o=
596602
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=

modules/auth/password/pwn/pwn_test.go

Lines changed: 12 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2626
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/5c1d8").Times(1).Reply(200).BodyString("EAF2F254732680E8AC339B84F3266ECCBB5:1\r\nFC446EB88938834178CB9322C1EE273C2A7:2")
30+
gock.New("https://api.pwnedpasswords.com").Get("/range/ba189").Times(1).Reply(200).BodyString("FD4CB34F0378BCB15D23F6FFD28F0775C9E:3\r\nFDF342FCD8C3611DAE4D76E8A992A3E4169: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

Comments
 (0)