/** * Checks a request for previous failures * * @param string $username * @param Request $request * * @return FailureManager * * @throws RequireCaptchaException In case a captcha unlock is required */ public function checkFailures($username, Request $request) { $failures = $this->repository->findLockedFailuresMatching($username, $request->getClientIp()); if (0 === count($failures)) { return; } if ($this->trials < count($failures) && $this->captcha->isSetup()) { $response = $this->captcha->bind($request); if ($response->isValid()) { foreach ($failures as $failure) { $failure->setLocked(false); } $this->em->flush(); } else { throw new RequireCaptchaException('Too much failure, require captcha'); } } return $this; }
/** @test */ public function bindShouldMapTheCorrectFields() { $challenge = 'challenger'; $userresponse = 'responser'; $ip = '192.168.17.24'; $httprequest = new Request(array(), array('recaptcha_challenge_field' => $challenge, 'recaptcha_response_field' => $userresponse), array(), array(), array(), array('REMOTE_ADDR' => $ip)); $catchParameters = null; $request = $this->getMock('Guzzle\\Http\\Message\\EntityEnclosingRequestInterface'); $request->expects($this->once())->method('addPostFields')->will($this->returnCallback(function ($parameters) use(&$catchParameters) { $catchParameters = $parameters; })); $response = $this->getmockBuilder('Guzzle\\Http\\Message\\Response')->disableOriginalConstructor()->getMock(); $responseData = "true"; $response->expects($this->once())->method('getBody')->with($this->equalTo(true))->will($this->returnValue($responseData)); $request->expects($this->once())->method('send')->will($this->returnValue($response)); $client = $this->getClientMock(); $client->expects($this->once())->method('post')->will($this->returnValue($request)); $recaptcha = new ReCaptcha($client, 'pub', 'private'); $answer = $recaptcha->bind($httprequest); $this->assertInternalType('array', $catchParameters); $this->assertArrayHasKey('challenge', $catchParameters); $this->assertArrayHasKey('response', $catchParameters); $this->assertEquals($challenge, $catchParameters['challenge']); $this->assertEquals($userresponse, $catchParameters['response']); $this->assertEquals($ip, $catchParameters['remoteip']); $this->assertInstanceOf('Neutron\\ReCaptcha\\Response', $answer); $this->assertTrue($answer->isValid()); }