ReCaptchaTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. *
  5. * BSD 3-Clause License
  6. * @copyright (c) 2019, Google Inc.
  7. * @link https://www.google.com/recaptcha
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. Neither the name of the copyright holder nor the names of its
  20. * contributors may be used to endorse or promote products derived from
  21. * this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. namespace ReCaptcha;
  35. use PHPUnit\Framework\TestCase;
  36. class ReCaptchaTest extends TestCase
  37. {
  38. /**
  39. * @expectedException \RuntimeException
  40. * @dataProvider invalidSecretProvider
  41. */
  42. public function testExceptionThrownOnInvalidSecret($invalid)
  43. {
  44. $rc = new ReCaptcha($invalid);
  45. }
  46. public function invalidSecretProvider()
  47. {
  48. return array(
  49. array(''),
  50. array(null),
  51. array(0),
  52. array(new \stdClass()),
  53. array(array()),
  54. );
  55. }
  56. public function testVerifyReturnsErrorOnMissingResponse()
  57. {
  58. $rc = new ReCaptcha('secret');
  59. $response = $rc->verify('');
  60. $this->assertFalse($response->isSuccess());
  61. $this->assertEquals(array(Recaptcha::E_MISSING_INPUT_RESPONSE), $response->getErrorCodes());
  62. }
  63. private function getMockRequestMethod($responseJson)
  64. {
  65. $method = $this->getMockBuilder(\ReCaptcha\RequestMethod::class)
  66. ->disableOriginalConstructor()
  67. ->setMethods(array('submit'))
  68. ->getMock();
  69. $method->expects($this->any())
  70. ->method('submit')
  71. ->with($this->callback(function ($params) {
  72. return true;
  73. }))
  74. ->will($this->returnValue($responseJson));
  75. return $method;
  76. }
  77. public function testVerifyReturnsResponse()
  78. {
  79. $method = $this->getMockRequestMethod('{"success": true}');
  80. $rc = new ReCaptcha('secret', $method);
  81. $response = $rc->verify('response');
  82. $this->assertTrue($response->isSuccess());
  83. }
  84. public function testVerifyReturnsInitialResponseWithoutAdditionalChecks()
  85. {
  86. $method = $this->getMockRequestMethod('{"success": true}');
  87. $rc = new ReCaptcha('secret', $method);
  88. $initialResponse = $rc->verify('response');
  89. $this->assertEquals($initialResponse, $rc->verify('response'));
  90. }
  91. public function testVerifyHostnameMatch()
  92. {
  93. $method = $this->getMockRequestMethod('{"success": true, "hostname": "host.name"}');
  94. $rc = new ReCaptcha('secret', $method);
  95. $response = $rc->setExpectedHostname('host.name')->verify('response');
  96. $this->assertTrue($response->isSuccess());
  97. }
  98. public function testVerifyHostnameMisMatch()
  99. {
  100. $method = $this->getMockRequestMethod('{"success": true, "hostname": "host.NOTname"}');
  101. $rc = new ReCaptcha('secret', $method);
  102. $response = $rc->setExpectedHostname('host.name')->verify('response');
  103. $this->assertFalse($response->isSuccess());
  104. $this->assertEquals(array(ReCaptcha::E_HOSTNAME_MISMATCH), $response->getErrorCodes());
  105. }
  106. public function testVerifyApkPackageNameMatch()
  107. {
  108. $method = $this->getMockRequestMethod('{"success": true, "apk_package_name": "apk.name"}');
  109. $rc = new ReCaptcha('secret', $method);
  110. $response = $rc->setExpectedApkPackageName('apk.name')->verify('response');
  111. $this->assertTrue($response->isSuccess());
  112. }
  113. public function testVerifyApkPackageNameMisMatch()
  114. {
  115. $method = $this->getMockRequestMethod('{"success": true, "apk_package_name": "apk.NOTname"}');
  116. $rc = new ReCaptcha('secret', $method);
  117. $response = $rc->setExpectedApkPackageName('apk.name')->verify('response');
  118. $this->assertFalse($response->isSuccess());
  119. $this->assertEquals(array(ReCaptcha::E_APK_PACKAGE_NAME_MISMATCH), $response->getErrorCodes());
  120. }
  121. public function testVerifyActionMatch()
  122. {
  123. $method = $this->getMockRequestMethod('{"success": true, "action": "action/name"}');
  124. $rc = new ReCaptcha('secret', $method);
  125. $response = $rc->setExpectedAction('action/name')->verify('response');
  126. $this->assertTrue($response->isSuccess());
  127. }
  128. public function testVerifyActionMisMatch()
  129. {
  130. $method = $this->getMockRequestMethod('{"success": true, "action": "action/NOTname"}');
  131. $rc = new ReCaptcha('secret', $method);
  132. $response = $rc->setExpectedAction('action/name')->verify('response');
  133. $this->assertFalse($response->isSuccess());
  134. $this->assertEquals(array(ReCaptcha::E_ACTION_MISMATCH), $response->getErrorCodes());
  135. }
  136. public function testVerifyAboveThreshold()
  137. {
  138. $method = $this->getMockRequestMethod('{"success": true, "score": "0.9"}');
  139. $rc = new ReCaptcha('secret', $method);
  140. $response = $rc->setScoreThreshold('0.5')->verify('response');
  141. $this->assertTrue($response->isSuccess());
  142. }
  143. public function testVerifyBelowThreshold()
  144. {
  145. $method = $this->getMockRequestMethod('{"success": true, "score": "0.1"}');
  146. $rc = new ReCaptcha('secret', $method);
  147. $response = $rc->setScoreThreshold('0.5')->verify('response');
  148. $this->assertFalse($response->isSuccess());
  149. $this->assertEquals(array(ReCaptcha::E_SCORE_THRESHOLD_NOT_MET), $response->getErrorCodes());
  150. }
  151. public function testVerifyWithinTimeout()
  152. {
  153. // Responses come back like 2018-07-31T13:48:41Z
  154. $challengeTs = date('Y-M-d\TH:i:s\Z', time());
  155. $method = $this->getMockRequestMethod('{"success": true, "challenge_ts": "'.$challengeTs.'"}');
  156. $rc = new ReCaptcha('secret', $method);
  157. $response = $rc->setChallengeTimeout('1000')->verify('response');
  158. $this->assertTrue($response->isSuccess());
  159. }
  160. public function testVerifyOverTimeout()
  161. {
  162. // Responses come back like 2018-07-31T13:48:41Z
  163. $challengeTs = date('Y-M-d\TH:i:s\Z', time() - 600);
  164. $method = $this->getMockRequestMethod('{"success": true, "challenge_ts": "'.$challengeTs.'"}');
  165. $rc = new ReCaptcha('secret', $method);
  166. $response = $rc->setChallengeTimeout('60')->verify('response');
  167. $this->assertFalse($response->isSuccess());
  168. $this->assertEquals(array(ReCaptcha::E_CHALLENGE_TIMEOUT), $response->getErrorCodes());
  169. }
  170. public function testVerifyMergesErrors()
  171. {
  172. $method = $this->getMockRequestMethod('{"success": false, "error-codes": ["initial-error"], "score": "0.1"}');
  173. $rc = new ReCaptcha('secret', $method);
  174. $response = $rc->setScoreThreshold('0.5')->verify('response');
  175. $this->assertFalse($response->isSuccess());
  176. $this->assertEquals(array('initial-error', ReCaptcha::E_SCORE_THRESHOLD_NOT_MET), $response->getErrorCodes());
  177. }
  178. }