Skip to content

Commit 9235442

Browse files
authored
Remove external API calls in TestPassword (go-gitea#30716)
The test had a dependency on `https://api.pwnedpasswords.com` which caused many failures on CI recently: ``` --- FAIL: TestPassword (2.37s) pwn_test.go:41: Get "https://api.pwnedpasswords.com/range/e6b6a": context deadline exceeded (Client.Timeout exceeded while awaiting headers) FAIL coverage: 82.9% of statements ```
1 parent cb9e1a3 commit 9235442

File tree

3 files changed

+31
-76
lines changed

3 files changed

+31
-76
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ require (
5959
github.com/google/uuid v1.6.0
6060
github.com/gorilla/feeds v1.1.2
6161
github.com/gorilla/sessions v1.2.2
62+
github.com/h2non/gock v1.2.0
6263
github.com/hashicorp/go-version v1.6.0
6364
github.com/hashicorp/golang-lru/v2 v2.0.7
6465
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

+6
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

+23-76
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
package pwn
55

66
import (
7-
"math/rand/v2"
87
"net/http"
9-
"strings"
108
"testing"
119
"time"
1210

11+
"github.com/h2non/gock"
1312
"github.com/stretchr/testify/assert"
1413
)
1514

@@ -18,86 +17,34 @@ var client = New(WithHTTP(&http.Client{
1817
}))
1918

2019
func TestPassword(t *testing.T) {
21-
// Check input error
22-
_, err := client.CheckPassword("", false)
20+
defer gock.Off()
21+
22+
count, err := client.CheckPassword("", false)
2323
assert.ErrorIs(t, err, ErrEmptyPassword, "blank input should return ErrEmptyPassword")
24+
assert.Equal(t, -1, count)
2425

25-
// Should fail
26-
fail := "password1234"
27-
count, err := client.CheckPassword(fail, false)
28-
assert.NotEmpty(t, count, "%s should fail as a password", fail)
26+
gock.New("https://api.pwnedpasswords.com").Get("/range/5c1d8").Times(1).Reply(200).BodyString("EAF2F254732680E8AC339B84F3266ECCBB5:1\r\nFC446EB88938834178CB9322C1EE273C2A7:2")
27+
count, err = client.CheckPassword("pwned", false)
2928
assert.NoError(t, err)
29+
assert.Equal(t, 1, count)
3030

31-
// Should fail (with padding)
32-
failPad := "administrator"
33-
count, err = client.CheckPassword(failPad, true)
34-
assert.NotEmpty(t, count, "%s should fail as a password", failPad)
31+
gock.New("https://api.pwnedpasswords.com").Get("/range/ba189").Times(1).Reply(200).BodyString("FD4CB34F0378BCB15D23F6FFD28F0775C9E:3\r\nFDF342FCD8C3611DAE4D76E8A992A3E4169:4")
32+
count, err = client.CheckPassword("notpwned", false)
3533
assert.NoError(t, err)
34+
assert.Equal(t, 0, count)
3635

37-
// Checking for a "good" password isn't going to be perfect, but we can give it a good try
38-
// with hopefully minimal error. Try five times?
39-
assert.Condition(t, func() bool {
40-
for i := 0; i <= 5; i++ {
41-
count, err = client.CheckPassword(testPassword(), false)
42-
assert.NoError(t, err)
43-
if count == 0 {
44-
return true
45-
}
46-
}
47-
return false
48-
}, "no generated passwords passed. there is a chance this is a fluke")
49-
50-
// Again, but with padded responses
51-
assert.Condition(t, func() bool {
52-
for i := 0; i <= 5; i++ {
53-
count, err = client.CheckPassword(testPassword(), true)
54-
assert.NoError(t, err)
55-
if count == 0 {
56-
return true
57-
}
58-
}
59-
return false
60-
}, "no generated passwords passed. there is a chance this is a fluke")
61-
}
62-
63-
// Credit to https://golangbyexample.com/generate-random-password-golang/
64-
// DO NOT USE THIS FOR AN ACTUAL PASSWORD GENERATOR
65-
var (
66-
lowerCharSet = "abcdedfghijklmnopqrst"
67-
upperCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
68-
specialCharSet = "!@#$%&*"
69-
numberSet = "0123456789"
70-
allCharSet = lowerCharSet + upperCharSet + specialCharSet + numberSet
71-
)
72-
73-
func testPassword() string {
74-
var password strings.Builder
75-
76-
// Set special character
77-
for i := 0; i < 5; i++ {
78-
random := rand.IntN(len(specialCharSet))
79-
password.WriteString(string(specialCharSet[random]))
80-
}
81-
82-
// Set numeric
83-
for i := 0; i < 5; i++ {
84-
random := rand.IntN(len(numberSet))
85-
password.WriteString(string(numberSet[random]))
86-
}
36+
gock.New("https://api.pwnedpasswords.com").Get("/range/a1733").Times(1).Reply(200).BodyString("C4CE0F1F0062B27B9E2F41AF0C08218017C:1\r\nFC446EB88938834178CB9322C1EE273C2A7:2\r\nFE81480327C992FE62065A827429DD1318B:0")
37+
count, err = client.CheckPassword("paddedpwned", true)
38+
assert.NoError(t, err)
39+
assert.Equal(t, 1, count)
8740

88-
// Set uppercase
89-
for i := 0; i < 5; i++ {
90-
random := rand.IntN(len(upperCharSet))
91-
password.WriteString(string(upperCharSet[random]))
92-
}
41+
gock.New("https://api.pwnedpasswords.com").Get("/range/5617b").Times(1).Reply(200).BodyString("FD4CB34F0378BCB15D23F6FFD28F0775C9E:3\r\nFDF342FCD8C3611DAE4D76E8A992A3E4169:4\r\nFE81480327C992FE62065A827429DD1318B:0")
42+
count, err = client.CheckPassword("paddednotpwned", true)
43+
assert.NoError(t, err)
44+
assert.Equal(t, 0, count)
9345

94-
for i := 0; i < 5; i++ {
95-
random := rand.IntN(len(allCharSet))
96-
password.WriteString(string(allCharSet[random]))
97-
}
98-
inRune := []rune(password.String())
99-
rand.Shuffle(len(inRune), func(i, j int) {
100-
inRune[i], inRune[j] = inRune[j], inRune[i]
101-
})
102-
return string(inRune)
46+
gock.New("https://api.pwnedpasswords.com").Get("/range/79082").Times(1).Reply(200).BodyString("FDF342FCD8C3611DAE4D76E8A992A3E4169:4\r\nFE81480327C992FE62065A827429DD1318B:0\r\nAFEF386F56EB0B4BE314E07696E5E6E6536:0")
47+
count, err = client.CheckPassword("paddednotpwnedzero", true)
48+
assert.NoError(t, err)
49+
assert.Equal(t, 0, count)
10350
}

0 commit comments

Comments
 (0)