-
Notifications
You must be signed in to change notification settings - Fork 14
/
recaptcha_test.go
134 lines (110 loc) · 3.04 KB
/
recaptcha_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package recaptcha
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
. "gopkg.in/check.v1"
)
func TestPackage(t *testing.T) { TestingT(t) }
type ReCaptchaSuite struct{}
var _ = Suite(&ReCaptchaSuite{})
type mockSuccessClient struct{}
func (*mockSuccessClient) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
resp = &http.Response{
Status: "200 OK",
StatusCode: 200,
}
resp.Body = ioutil.NopCloser(strings.NewReader(`
{
"success": true,
"challenge_ts": "2018-03-06T03:41:29+00:00",
"hostname": "test.com"
}
`))
return
}
type mockFailedClient struct{}
func (*mockFailedClient) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
resp = &http.Response{
Status: "200 OK",
StatusCode: 200,
}
resp.Body = ioutil.NopCloser(strings.NewReader(`
{
"success": false,
"challenge_ts": "2018-03-06T03:41:29+00:00",
"hostname": "test.com",
"error-codes": ["bad-request"]
}
`))
return
}
type mockInvalidClient struct{}
// bad json body
func (*mockInvalidClient) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
resp = &http.Response{
Status: "200 OK",
StatusCode: 200,
}
resp.Body = ioutil.NopCloser(strings.NewReader(` bogus json `))
return
}
type mockUnavailableClient struct{}
func (*mockUnavailableClient) Post(url string, contentType string, body io.Reader) (resp *http.Response, err error) {
resp = &http.Response{
Status: "Not Found",
StatusCode: 404,
}
resp.Body = ioutil.NopCloser(nil)
err = fmt.Errorf("Unable to connect to server")
return
}
func (s *ReCaptchaSuite) TestNewReCAPTCHA(c *C) {
captcha, err := NewReCAPTCHA("my secret")
c.Assert(err, IsNil)
c.Check(captcha.Secret, Equals, "my secret")
c.Check(captcha.ReCAPTCHALink, Equals, reCAPTCHALink)
captcha, err = NewReCAPTCHA("")
c.Assert(err, NotNil)
}
func (s *ReCaptchaSuite) TestVerifyWithClientIP(c *C) {
captcha := ReCAPTCHA{
Client: &mockSuccessClient{},
}
success, err := captcha.Verify("mycode", "127.0.0.1")
c.Assert(err, IsNil)
c.Check(success, Equals, true)
captcha.Client = &mockFailedClient{}
success, err = captcha.Verify("mycode", "127.0.0.1")
c.Assert(err, IsNil)
c.Check(success, Equals, false)
}
func (s *ReCaptchaSuite) TestVerifyWithoutClientIP(c *C) {
captcha := ReCAPTCHA{
Client: &mockSuccessClient{},
}
success, err := captcha.VerifyNoRemoteIP("mycode")
c.Assert(err, IsNil)
c.Check(success, Equals, true)
captcha.Client = &mockFailedClient{}
success, err = captcha.VerifyNoRemoteIP("mycode")
c.Assert(err, IsNil)
c.Check(success, Equals, false)
}
func (s *ReCaptchaSuite) TestConfirm(c *C) {
// check that an invalid json body errors
captcha := ReCAPTCHA{
Client: &mockInvalidClient{},
}
body := reCHAPTCHARequest{Secret: "", Response: ""}
success, err := captcha.confirm(body)
c.Assert(err, NotNil)
c.Check(success, Equals, false)
captcha.Client = &mockUnavailableClient{}
success, err = captcha.confirm(body)
c.Assert(err, NotNil)
c.Check(success, Equals, false)
}