def testVerify_NullGuess(self): self.mox.StubOutWithMock(captcha, '_AskRecaptcha') # We are verifying that _AskRecaptcha is not called. self.mox.ReplayAll() self.assertEqual((False, 'incorrect-captcha-sol'), captcha.Verify('1.2.3.4', None)) self.mox.VerifyAll()
def testVerify_WrongGuess(self): self.mox.StubOutWithMock(captcha, '_AskRecaptcha') captcha._AskRecaptcha('1.2.3.4', 'non-matching').AndReturn({'success': False}) self.mox.ReplayAll() result = captcha.Verify('1.2.3.4', 'non-matching') self.mox.VerifyAll() self.assertEqual((False, 'incorrect-captcha-sol'), result)
def testVerify_CorrectGuess(self): self.mox.StubOutWithMock(captcha, '_AskRecaptcha') captcha._AskRecaptcha('1.2.3.4', 'matching').AndReturn({'success': True}) self.mox.ReplayAll() result = captcha.Verify('1.2.3.4', 'matching') self.mox.VerifyAll() self.assertEqual((True, ''), result)
def testVerify_GotErrorCode(self): self.mox.StubOutWithMock(captcha, '_AskRecaptcha') captcha._AskRecaptcha('1.2.3.4', 'some challenge').AndReturn({ 'success': False, 'error-codes': ['invalid-input-response'] }) self.mox.ReplayAll() self.assertEqual((False, ['invalid-input-response']), captcha.Verify('1.2.3.4', 'some challenge')) self.mox.VerifyAll()
def CheckCaptcha(self, mr, post_data): """Check the provided CAPTCHA solution and add an error if it is wrong.""" if (mr.project and framework_bizobj.UserIsInProject( mr.project, mr.auth.effective_ids)): logging.info('Project member is exempt from CAPTCHA') return # Don't check a user's actions within their own projects. if not any( actionlimit.NeedCaptcha(mr.auth.user_pb, action_type) for action_type in self._CAPTCHA_ACTION_TYPES): logging.info('No CAPTCHA was required') return # no captcha was needed. remote_ip = mr.request.remote_addr captcha_response = post_data.get('g-recaptcha-response') correct, _msg = captcha.Verify(remote_ip, captcha_response) if correct: logging.info('CAPTCHA was solved') else: logging.info('BZzzz! Bad captcha solution.') mr.errors.captcha = 'Captcha check failed.'