[Checkins] SVN: zc.openid/trunk/src/zc/openid/ Began unit tests for the package.
Shane Hathaway
shane at hathawaymix.org
Wed Feb 25 18:15:28 EST 2009
Log message for revision 97277:
Began unit tests for the package.
The unit tests are already much larger than the implementation code.
I wonder if python-openid provides some better way to write unit tests.
Changed:
U zc.openid/trunk/src/zc/openid/authentication.py
A zc.openid/trunk/src/zc/openid/tests.py
A zc.openid/trunk/src/zc/openid/unittests.txt
-=-
Modified: zc.openid/trunk/src/zc/openid/authentication.py
===================================================================
--- zc.openid/trunk/src/zc/openid/authentication.py 2009-02-25 21:40:45 UTC (rev 97276)
+++ zc.openid/trunk/src/zc/openid/authentication.py 2009-02-25 23:15:28 UTC (rev 97277)
@@ -53,6 +53,7 @@
implements(IOpenIDConsumer)
single_provider = None
+ _consumer_class = Consumer
def authenticate(self, request):
"""Identify a principal for a request.
@@ -74,7 +75,7 @@
session[CONSUMER] = consumer_session_data
else:
consumer_session_data._p_changed = True
- return Consumer(consumer_session_data, store)
+ return self._consumer_class(consumer_session_data, store)
def _get_view_url(self, request, name):
site = getSite()
Added: zc.openid/trunk/src/zc/openid/tests.py
===================================================================
--- zc.openid/trunk/src/zc/openid/tests.py (rev 0)
+++ zc.openid/trunk/src/zc/openid/tests.py 2009-02-25 23:15:28 UTC (rev 97277)
@@ -0,0 +1,110 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+import time
+import unittest
+import sys
+
+from zope.component import provideAdapter
+from zope.component import provideUtility
+from zope.publisher.interfaces import IRequest
+from zope.session.http import CookieClientIdManager
+from zope.session.interfaces import IClientIdManager
+from zope.session.interfaces import ISessionDataContainer
+from zope.session.session import ClientId
+from zope.session.session import RAMSessionDataContainer
+from zope.session.session import Session
+from zope.testing import doctest
+from zope.testing.cleanup import cleanUp
+from zope.traversing.browser.interfaces import IAbsoluteURL
+from zope.traversing.browser.absoluteurl import AbsoluteURL
+
+from openid.association import Association
+from openid.consumer.consumer import AuthRequest
+from openid.consumer.consumer import SuccessResponse
+from openid.consumer.discover import OpenIDServiceEndpoint
+
+
+def configure():
+ """Set up global configuration for the unit tests.
+
+ This includes session support that does not require ZODB.
+ """
+ provideUtility(CookieClientIdManager(), IClientIdManager)
+ provideUtility(RAMSessionDataContainer(), ISessionDataContainer)
+ provideAdapter(Session)
+ provideAdapter(ClientId)
+ provideAdapter(
+ AbsoluteURL, adapts=(None, IRequest), provides=IAbsoluteURL)
+
+
+xrds_template = """\
+<?xml version="1.0" encoding="UTF-8"?>
+<xrds:XRDS
+ xmlns:xrds="xri://$xrds"
+ xmlns="xri://$xrd*($v*2.0)">
+ <XRD>
+ <Service priority="0">
+ <Type>http://specs.openid.net/auth/2.0/signon</Type>
+ <Type>http://openid.net/signon/1.0</Type>
+ <URI>http://example.com/openidserver</URI>
+ <LocalID>%s</LocalID>
+ </Service>
+ </XRD>
+</xrds:XRDS>
+"""
+
+
+class MockConsumer(object):
+ """Mock implementation of openid.consumer.consumer.Consumer for testing.
+ """
+
+ def __init__(self, session, store):
+ self.session = session
+ self.store = store
+ self.association = Association(
+ # some of this data was sampled from a live example session.
+ handle=u'{HMAC-SHA1}{49a5be49}{co7U9w==}',
+ secret='0' * 20,
+ issued=1234567890,
+ lifetime=sys.maxint,
+ assoc_type=u'HMAC-SHA1'
+ )
+
+ def begin(self, url):
+ endpoints = OpenIDServiceEndpoint.fromXRDS(url, xrds_template % url)
+ endpoint = endpoints[0]
+ return AuthRequest(endpoints[0], self.association)
+
+ def complete(self, form_data, app_url):
+ return SuccessResponse()
+
+
+def setUp(doctest=None):
+ cleanUp()
+
+def tearDown(doctest=None):
+ cleanUp()
+
+
+def test_suite():
+ return unittest.TestSuite([
+ doctest.DocFileSuite(
+ 'unittests.txt',
+ setUp=setUp, tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
+ ])
+
+if __name__ == '__main__':
+ unittest.main()
Added: zc.openid/trunk/src/zc/openid/unittests.txt
===================================================================
--- zc.openid/trunk/src/zc/openid/unittests.txt (rev 0)
+++ zc.openid/trunk/src/zc/openid/unittests.txt 2009-02-25 23:15:28 UTC (rev 97277)
@@ -0,0 +1,63 @@
+
+Unit tests of OpenIDConsumer
+----------------------------
+
+Create an OpenIDConsumer instance and link it to a MockConsumer.
+
+ >>> from zc.openid.authentication import OpenIDConsumer
+ >>> from zc.openid.tests import MockConsumer
+ >>> auth = OpenIDConsumer()
+ >>> auth._consumer_class = MockConsumer
+
+Set up session support and some foundational stuff.
+
+ >>> from zc.openid.tests import configure
+ >>> configure()
+ >>> from pprint import pprint
+
+Make a TestRequest and try to authenticate it. Authentication should
+fail, since no one has logged in yet.
+
+ >>> from zope.publisher.browser import TestRequest
+ >>> request = TestRequest()
+ >>> auth.authenticate(request) is None
+ True
+
+Start the login process with no identity URL. Users should be
+redirected to a page where they can enter their identity.
+
+ >>> auth.login(request)
+ >>> request.response.getStatus()
+ 302
+ >>> request.response.getHeader('Location')
+ 'http://127.0.0.1/@@openid/choose_identity'
+
+After the user submits the form, start the login process again, now
+with an identity URL. The browser should be redirected to
+http://example.com/openidserver for login.
+
+ >>> auth.login(request, "http://example.com/id/alice")
+ >>> request.response.getStatus()
+ 302
+ >>> url = request.response.getHeader('Location')
+ >>> len(url)
+ 390
+ >>> u, query = url.split('?', 1)
+ >>> u
+ 'http://example.com/openidserver'
+ >>> from cgi import parse_qs
+ >>> pprint(parse_qs(query))
+ {'openid.assoc_handle': ['{HMAC-SHA1}{49a5be49}{co7U9w==}'],
+ 'openid.claimed_id': ['http://example.com/id/alice'],
+ 'openid.identity': ['http://example.com/id/alice'],
+ 'openid.mode': ['checkid_setup'],
+ 'openid.ns': ['http://specs.openid.net/auth/2.0'],
+ 'openid.realm': ['http://127.0.0.1'],
+ 'openid.return_to': ['http://127.0.0.1/@@openid/complete']}
+
+The identity provider should redirect the user back to the
+@@openid/complete view, which calls auth.complete().
+
+ >>> request = TestRequest()
+ >>> #auth.complete(request)
+
More information about the Checkins
mailing list