OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """Tests for the captcha module.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import mox |
| 11 |
| 12 from google.appengine.ext import testbed |
| 13 |
| 14 from framework import captcha |
| 15 |
| 16 |
| 17 class CaptchaTest(unittest.TestCase): |
| 18 |
| 19 def setUp(self): |
| 20 self.mox = mox.Mox() |
| 21 self.testbed = testbed.Testbed() |
| 22 self.testbed.activate() |
| 23 self.testbed.init_user_stub() |
| 24 self.testbed.init_memcache_stub() |
| 25 self.testbed.init_datastore_v3_stub() |
| 26 |
| 27 def tearDown(self): |
| 28 self.mox.UnsetStubs() |
| 29 self.mox.ResetAll() |
| 30 |
| 31 def testVerify_NoGuess(self): |
| 32 self.mox.StubOutWithMock(captcha, '_AskRecaptcha') |
| 33 # We are verifying that _AskRecaptcha is not called. |
| 34 self.mox.ReplayAll() |
| 35 self.assertEqual( |
| 36 (False, 'incorrect-captcha-sol'), |
| 37 captcha.Verify('1.2.3.4', '')) |
| 38 self.mox.VerifyAll() |
| 39 |
| 40 def testVerify_NullGuess(self): |
| 41 self.mox.StubOutWithMock(captcha, '_AskRecaptcha') |
| 42 # We are verifying that _AskRecaptcha is not called. |
| 43 self.mox.ReplayAll() |
| 44 self.assertEqual( |
| 45 (False, 'incorrect-captcha-sol'), |
| 46 captcha.Verify('1.2.3.4', None)) |
| 47 self.mox.VerifyAll() |
| 48 |
| 49 def testVerify_WrongGuess(self): |
| 50 self.mox.StubOutWithMock(captcha, '_AskRecaptcha') |
| 51 captcha._AskRecaptcha( |
| 52 '1.2.3.4', 'matching').AndReturn( |
| 53 {'success': False, 'error-codes': ['invalid-input-response']}) |
| 54 self.mox.ReplayAll() |
| 55 self.assertEqual( |
| 56 (False, ['invalid-input-response']), |
| 57 captcha.Verify('1.2.3.4', 'some challenge')) |
| 58 self.mox.VerifyAll() |
| 59 |
| 60 def testVerify_CorrectGuess(self): |
| 61 self.mox.StubOutWithMock(captcha, '_AskRecaptcha') |
| 62 captcha._AskRecaptcha( |
| 63 '1.2.3.4', 'matching').AndReturn({'success':True}) |
| 64 self.mox.ReplayAll() |
| 65 |
| 66 result = captcha.Verify('1.2.3.4', 'matching') |
| 67 |
| 68 self.mox.VerifyAll() |
| 69 self.assertEqual((True, ''), result) |
| 70 |
| 71 def testVerify_WrongGuess(self): |
| 72 self.mox.StubOutWithMock(captcha, '_AskRecaptcha') |
| 73 captcha._AskRecaptcha( |
| 74 '1.2.3.4', 'non-matching').AndReturn({'success': False}) |
| 75 self.mox.ReplayAll() |
| 76 |
| 77 result = captcha.Verify('1.2.3.4', 'non-matching') |
| 78 |
| 79 self.mox.VerifyAll() |
| 80 self.assertEqual((False, 'incorrect-captcha-sol'), result) |
| 81 |
| 82 |
| 83 if __name__ == '__main__': |
| 84 unittest.main() |
OLD | NEW |