Commits

iElectric  committed 1f681ea

small ident fixes, add simple nosetests

  • Participants
  • Parent commits 8a712bd

Comments (0)

Files changed (4)

File repoze/who/plugins/captcha.py

 from zope.interface import implements
 from repoze.who.interfaces import IAuthenticator
 
-class RecaptchaAuthenticatorPlugin(object):
+class RecaptchaPlugin(object):
 
     implements(IAuthenticator)
 
     def __init__(self, private_key, form_handler):
         self.private_key = private_key
-	self.handler = form_handler
+        self.handler = form_handler
 
     def authenticate(self, environ, identity):
 
-    	log = environ['repoze.who.logger']
+        log = environ['repoze.who.logger']
 
-	# check if validation is needed
-	if self.handler:
-	    if environ['PATH_INFO'] not in self.handler.split():
-	    	log.debug('no recapcha validation needed.')
-	    	return None
+        # check if validation is needed
+        if self.handler:
+            if environ['PATH_INFO'] not in self.handler.split():
+                log.debug('no recapcha validation needed.')
+                return None
 
 
         form = parse_formvars(environ)
     if private_key is None:
         raise ValueError('private_key must be provided for recaptcha API.')
 
-    return RecaptchaAuthenticatorPlugin(private_key, form_handler)
+    return RecaptchaPlugin(private_key, form_handler)

File repoze/who/plugins/test_captcha.py

+from ConfigParser import ConfigParser
+from repoze.who.tests import Base, DummyIdentifier, encode_multipart_formdata
+
+PRIVATE_KEY = 'whocares'
+
+class DummyLogger(object):
+    def debug(self, msg):
+        return msg
+
+class TestFormPlugin(Base):
+    def _getTargetClass(self):
+        from repoze.who.plugins.captcha import RecaptchaPlugin
+        return RecaptchaPlugin
+
+    def _makeOne(self, private_key='read_some_config', form_handler=None):
+        plugin = self._getTargetClass()(private_key, form_handler)
+        return plugin
+
+    def _makeFormEnviron(self, login='chris', password='password', pathinfo='/process'):
+        from StringIO import StringIO
+        fields = []
+        if login:
+            fields.append(('login', login))
+        if password:
+            fields.append(('password', password))
+        content_type, body = encode_multipart_formdata(fields)
+        credentials = {'login':'chris', 'password':'password'}
+        identifier = DummyIdentifier(credentials)
+
+        extra = {'wsgi.input':StringIO(body),
+                 'wsgi.url_scheme': 'http',
+                 'SERVER_NAME': 'localhost',
+                 'SERVER_PORT': '8080',
+                 'CONTENT_TYPE':content_type,
+                 'CONTENT_LENGTH':len(body),
+                 'REQUEST_METHOD':'POST',
+                 'repoze.who.plugins': {'cookie':identifier},
+                 'repoze.who.logger': DummyLogger(),
+                 'PATH_INFO': pathinfo,
+                 'QUERY_STRING':'',
+                 'REMOTE_ADDR':'127.0.0.1',
+                 }
+        environ = self._makeEnviron(extra)
+        return environ
+
+    def test_implements(self):
+        from zope.interface.verify import verifyClass
+        from repoze.who.interfaces import IAuthenticator
+        klass = self._getTargetClass()
+        verifyClass(IAuthenticator, klass)
+
+    def test_nohandler_fail(self):
+
+        plugin = self._makeOne(PRIVATE_KEY)
+        environ = self._makeFormEnviron()
+        plugin.authenticate(environ, environ['repoze.who.plugins']['cookie'])
+
+        app = environ['repoze.who.application']
+        self.assertEqual(environ['repoze.who.error'], 'incorrect-captcha-sol')
+        self.assertEqual(app.code, 401)
+
+    # it's an irony to test against successive match, right? Maybe add selenium test.
+
+    def test_handler_pass(self):
+
+        plugin = self._makeOne(PRIVATE_KEY, '/')
+        environ = self._makeFormEnviron()
+        plugin.authenticate(environ, environ['repoze.who.plugins']['cookie'])
+
+        self.assertEqual(environ.get('repoze.who.error'), None)
+        self.assertEqual(environ.get('repoze.who.application'), None)
+
+    def test_handler_fail(self):
+
+        plugin = self._makeOne(PRIVATE_KEY, '/process')
+        environ = self._makeFormEnviron()
+        plugin.authenticate(environ, environ['repoze.who.plugins']['cookie'])
+
+        app = environ['repoze.who.application']
+        self.assertEqual(environ['repoze.who.error'], 'incorrect-captcha-sol')
+        self.assertEqual(app.code, 401)
+
 tag_build = dev
 tag_date = true
 
+[nosetests]
+verbose=True
+verbosity=3
+detailed-errors=True
+pdb=True
+pdb-failures=True
+
 
 version = '0.2'
 
-desc = """**repoze.who.recaptcha** implements server side of the recaptcha API. This ``IAuthenticator`` plugin examines environment for recaptcha form values and if given submits to recaptcha main server for validation. If the validation succeeds, no action is taken. Otherwise error is passed to ``environ['repoze.who.error']`` and ``HTTPUnauthorzied(401)`` is triggered.::
+desc = """**repoze.who.recaptcha** implements server side of the recaptcha API. This ``IAuthenticator`` plugin examines environment for recaptcha form values and if given submits to recaptcha main server for validation. If the validation succeeds, no action is taken. Otherwise error is passed to ``environ['repoze.who.error']`` and ``HTTPUnauthorzied(401)`` is triggered.
+
+.. _www.bitbucket.org: http://www.bitbucket.org/iElectric/repozewhorecaptcha/
+
+Public Mercurial repo is avaliable at www.bitbucket.org_ ::
 
     # sample .ini configuration
 
       packages=find_packages(exclude=['ez_setup']),
       include_package_data=True,
       zip_safe=False,
+      test_suite='nose.collector',
+      tests_require=['nose', 'repoze.who'],
       namespace_packages = ['repoze', 'repoze.who', 'repoze.who.plugins'],
       install_requires=[
       	'setuptools',