-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleRecaptchaV2Test.php
More file actions
152 lines (131 loc) · 5.59 KB
/
GoogleRecaptchaV2Test.php
File metadata and controls
152 lines (131 loc) · 5.59 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php declare(strict_types=1);
namespace Tests\Csrf;
use PHPUnit\Framework\TestCase;
/**
* 测试 php-csrf\Csrf\GoogleRecaptchaV2
*
* @author fdipzone
*/
final class GoogleRecaptchaV2Test extends TestCase
{
/**
* @covers \Csrf\GoogleRecaptchaV2::__construct
*/
public function testConstruct()
{
$secret = 'abc123';
$config = new \Csrf\Config\GoogleRecaptchaV2Config($secret);
$google_recaptcha_v2 = new \Csrf\GoogleRecaptchaV2($config);
$this->assertEquals('Csrf\GoogleRecaptchaV2', get_class($google_recaptcha_v2));
$this->assertInstanceOf(\Csrf\ICsrf::class, $google_recaptcha_v2);
}
/**
* @covers \Csrf\GoogleRecaptchaV2::__construct
*/
public function testConstructException()
{
$this->expectException(\Csrf\Exception\TokenException::class);
$this->expectExceptionMessage('config secret is empty');
$secret = 'abc123';
$config = new \Csrf\Config\GoogleRecaptchaV2Config($secret);
// 将 config 的 secret 更新为空
\Tests\Utils\PHPUnitExtension::setVariable($config, 'secret', '');
new \Csrf\GoogleRecaptchaV2($config);
}
/**
* @covers \Csrf\GoogleRecaptchaV2::generate
*/
public function testGenerate()
{
$secret = 'abc123';
$config = new \Csrf\Config\GoogleRecaptchaV2Config($secret);
$google_recaptcha_v2 = new \Csrf\GoogleRecaptchaV2($config);
$token = $google_recaptcha_v2->generate('login');
$this->assertEquals('', $token);
}
/**
* @covers \Csrf\GoogleRecaptchaV2::verify
*/
public function testVerify()
{
$secret = 'abc123';
$config = new \Csrf\Config\GoogleRecaptchaV2Config($secret);
$mock_recaptcha_response = new \ReCaptcha\Response(true);
// mock google recaptcha v2
$mock_google_recaptcha_v2 = $this->getMockBuilder('\Csrf\GoogleRecaptchaV2')
->setConstructorArgs([$config])
->setMethods(['googleRecaptchaVerify'])
->getMock();
$mock_google_recaptcha_v2->expects($this->any())
->method('googleRecaptchaVerify')
->willReturn($mock_recaptcha_response);
$action = 'login';
$remote_ip = '192.168.1.1';
$token = 'google recaptcha v2 token';
$response = $mock_google_recaptcha_v2->verify($token, $action, $remote_ip);
$this->assertTrue($response->success());
$this->assertSame(0, count($response->errors()));
}
/**
* @covers \Csrf\GoogleRecaptchaV2::verify
*/
public function testVerifyFail()
{
$secret = 'abc123';
$config = new \Csrf\Config\GoogleRecaptchaV2Config($secret);
$mock_recaptcha_response = new \ReCaptcha\Response(false, ['error']);
// mock google recaptcha v2
$mock_google_recaptcha_v2 = $this->getMockBuilder('\Csrf\GoogleRecaptchaV2')
->setConstructorArgs([$config])
->setMethods(['googleRecaptchaVerify'])
->getMock();
$mock_google_recaptcha_v2->expects($this->any())
->method('googleRecaptchaVerify')
->willReturn($mock_recaptcha_response);
$action = 'login';
$remote_ip = '192.168.1.1';
$token = 'google recaptcha v2 token';
$response = $mock_google_recaptcha_v2->verify($token, $action, $remote_ip);
$this->assertFalse($response->success());
$this->assertSame(1, count($response->errors()));
$this->assertEquals('error', $response->errors()[0]);
}
/**
* @covers \Csrf\GoogleRecaptchaV2::verify
*/
public function testVerifyException()
{
$this->expectException(\Csrf\Exception\TokenException::class);
$this->expectExceptionMessage('google recaptcha v2 call fail');
$secret = 'abc123';
$config = new \Csrf\Config\GoogleRecaptchaV2Config($secret);
// mock google recaptcha v2
$mock_google_recaptcha_v2 = $this->getMockBuilder('\Csrf\GoogleRecaptchaV2')
->setConstructorArgs([$config])
->setMethods(['googleRecaptchaVerify'])
->getMock();
$mock_google_recaptcha_v2->expects($this->any())
->method('googleRecaptchaVerify')
->willThrowException(new \Exception('test exception'));
$action = 'login';
$remote_ip = '192.168.1.1';
$token = 'google recaptcha v2 token';
$mock_google_recaptcha_v2->verify($token, $action, $remote_ip);
}
/**
* @covers \Csrf\GoogleRecaptchaV2::googleRecaptchaVerify
*/
public function testGoogleRecaptchaVerify()
{
$secret = 'abc123';
$config = new \Csrf\Config\GoogleRecaptchaV2Config($secret);
$google_recaptcha_v2 = new \Csrf\GoogleRecaptchaV2($config);
$action = 'login';
$remote_ip = '192.168.1.1';
$token = '';
$recaptcha_response = \Tests\Utils\PHPUnitExtension::callMethod($google_recaptcha_v2, 'googleRecaptchaVerify', [$token, $action, $remote_ip]);
$this->assertFalse($recaptcha_response->isSuccess());
$this->assertSame(1, count($recaptcha_response->getErrorCodes()));
$this->assertEquals(\ReCaptcha\Recaptcha::E_MISSING_INPUT_RESPONSE, $recaptcha_response->getErrorCodes()[0]);
}
}