## $Id: test_captcha.py 15012 2018-05-19 21:47:54Z uli $
##
## Copyright (C) 2011 Uli Fouquet
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import grok
from zope.component import getAdapter, getUtility
from zope.component.hooks import setSite, clearSite
from zope.interface import verify
from zope.publisher.browser import TestRequest
from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
from waeup.kofa.browser.captcha import (
CaptchaResponse, CaptchaRequest, NullCaptcha, StaticCaptcha, ReCaptcha,
CaptchaManager)
from waeup.kofa.browser.interfaces import (
ICaptchaRequest, ICaptchaResponse, ICaptcha, ICaptchaConfig,
ICaptchaManager)
class CaptchaResponseTests(FunctionalTestCase):
layer = FunctionalLayer
def test_ifaces(self):
# make sure we implement the promised interfaces
obj = CaptchaResponse(True)
verify.verifyClass(ICaptchaResponse, CaptchaResponse)
verify.verifyObject(ICaptchaResponse, obj)
return
def test_init_vals(self):
# make sure the initial values are stored
resp = CaptchaResponse(False, 'some-msg')
self.assertEqual(resp.is_valid, False)
self.assertEqual(resp.error_code, 'some-msg')
return
class CaptchaRequestTests(FunctionalTestCase):
layer = FunctionalLayer
def test_ifaces(self):
# make sure we implement the promised interfces
obj = CaptchaRequest(None)
verify.verifyClass(ICaptchaRequest, CaptchaRequest)
verify.verifyObject(ICaptchaRequest, obj)
return
def test_init_vals(self):
# make sure initial values are stored
req = CaptchaRequest('my-solution', 'my-challenge')
self.assertEqual(req.solution, 'my-solution')
self.assertEqual(req.challenge, 'my-challenge')
return
class CaptchaTestBase(object):
def test_ifaces(self):
# make sure we implement the promised interfaces
obj = self.factory()
verify.verifyClass(ICaptcha, self.factory)
verify.verifyObject(ICaptcha, obj)
return
def test_utility(self):
# the null captcha is also registered as a utility for ICaptcha
captcha = getUtility(ICaptcha, name=self.name)
self.assertTrue(isinstance(captcha, self.factory))
return
class NullCaptchaTests(FunctionalTestCase, CaptchaTestBase):
layer = FunctionalLayer
factory = NullCaptcha
name = 'No captcha'
def test_verify(self):
# null captchas accept any input request
captcha = self.factory()
result1 = captcha.verify(TestRequest())
result2 = captcha.verify(
TestRequest(form={'solution': 'a', 'challenge': 'b'}))
self.assertEqual(result1.is_valid, True)
self.assertEqual(result2.is_valid, True)
return
def test_display(self):
# null captchas do not generate additional HTML code
captcha = self.factory()
result = captcha.display()
self.assertEqual(result, u'')
return
class StaticCaptchaTests(FunctionalTestCase, CaptchaTestBase):
layer = FunctionalLayer
factory = StaticCaptcha
name = 'Testing captcha'
def test_verify(self):
# id captchas accept any solution that matches challenge and
# is not empty or None
captcha = self.factory()
request1 = TestRequest(form={'solution': 'my-sol',
'challenge': 'my-challenge'})
request2 = TestRequest()
request3 = TestRequest(form={'solution': '', 'challenge': ''})
request4 = TestRequest(form={'solution': 'my-sol',
'challenge': 'my-sol'})
result1 = captcha.verify(request1)
result2 = captcha.verify(request2)
result3 = captcha.verify(request3)
result4 = captcha.verify(request4)
self.assertEqual(result1.is_valid, False)
self.assertEqual(result2.is_valid, False)
self.assertEqual(result3.is_valid, False)
self.assertEqual(result4.is_valid, True)
return
def test_display(self):
# id captchas provide a simple HTML input
captcha = self.factory()
result = captcha.display()
self.assertMatches(
'
Type: ...
'
'
',
result)
return
class ReCaptchaTests(FunctionalTestCase, CaptchaTestBase):
layer = FunctionalLayer
factory = ReCaptcha
name = 'ReCaptcha'
def DISABLEDtest_verify(self):
# recaptcha verification cannot be tested easily. As the
# solution and other environment parameters is only known to
# the remote server, we cannot guess on server side what the
# solution is. Further more this test contacts a remote
# server so that we might want to disable this test by
# default.
captcha = self.factory()
request1 = TestRequest(
form={'recaptcha_challenge_field': 'my-challenge',
'recaptcha_response_field': 'my-solution'})
result1 = captcha.verify(request1)
self.assertEqual(result1.is_valid, False)
self.assertEqual(result1.error_code, 'invalid-request-cookie')
return
def test_display(self):
# recaptchas provide the pieces to trigger the remote API
captcha = self.factory()
result = captcha.display()
self.assertMatches(
''
'