Ignore:
Timestamp:
22 Aug 2010, 14:23:24 (14 years ago)
Author:
uli
Message:

Update tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/jambtables/tests/test_authentication.py

    r5461 r5463  
    2222import grok
    2323import unittest
     24#import zope.session.tests.setUp
     25#import zope.session.tests.tearDown
     26from zope.component import provideAdapter
    2427from zope.component.hooks import setSite
     28from zope.interface import verify
     29from zope.pluggableauth.interfaces import  IAuthenticatedPrincipalFactory
     30from zope.publisher.browser import TestRequest
     31from zope.publisher.interfaces import IRequest
     32from zope.session.session import RAMSessionDataContainer
     33from zope.session.interfaces import ISession
    2534from zope.site import LocalSiteManager
    2635from zope.site.folder import Folder
    2736from zope.site.testing import siteSetUp, siteTearDown
    28 from waeup.sirp.jambtables.authentication import ApplicantsAuthenticatorPlugin
     37from zope.testing import cleanup
     38from waeup.sirp.jambtables.authentication import (
     39    ApplicantsAuthenticatorPlugin, WAeUPApplicantCredentialsPlugin,
     40    ApplicantCredentials, AuthenticatedApplicantPrincipalFactory,
     41    ApplicantPrincipalInfo, ApplicantPrincipal)
     42from waeup.sirp.jambtables.interfaces import(
     43    IJAMBApplicantSessionCredentials,)
    2944
    3045class FakeBatch(dict):
     
    173188            dict(accesscode='APP-54321', jambregno='JAMB3'))
    174189        assert result is not None
    175         return
    176 
     190
     191        # check the `principalInfo` method of authenticator
     192        # plugin. This is only here to satisfy the coverage report.
     193        assert self.plugin.principalInfo('not-an-id') is None
     194        return
     195
     196session_data = {
     197    'zope.pluggableauth.browserplugins': {}
     198    }
     199
     200class FakeSession(dict):
     201    def __init__(self, request):
     202        pass
     203
     204    def get(self, key, default=None):
     205        return self.__getitem__(key, default)
     206   
     207    def __getitem__(self, key, default=None):
     208        return session_data.get(key, default)
     209
     210    def __setitem__(self, key, value):
     211        session_data[key] = value
     212        return
     213
     214class CredentialsPluginTest(unittest.TestCase):
     215
     216    def setUp(self):
     217        self.request = TestRequest()
     218        provideAdapter(FakeSession, (IRequest,), ISession)
     219        self.plugin = WAeUPApplicantCredentialsPlugin()
     220        self.full_request = TestRequest()
     221        session_data['zope.pluggableauth.browserplugins'] = {}
     222        return
     223
     224    def tearDown(self):
     225        cleanup.tearDown()
     226        return
     227
     228    def filled_request(self, form_dict):
     229        request = TestRequest()
     230        for key, value in form_dict.items():
     231            request.form[key] = value
     232        return request
     233
     234    def test_extractCredentials_invalid(self):
     235        result = self.plugin.extractCredentials('not-a-request')
     236        assert result is None
     237        return
     238
     239    def test_extractCredentials_empty(self):
     240        result = self.plugin.extractCredentials(self.request)
     241        assert result is None
     242        return
     243
     244    def test_extractCredentials_full_set(self):
     245        request = self.filled_request({
     246                'form.prefix': 'APP',
     247                'form.ac_series': '1',
     248                'form.ac_number': '1234567890',
     249                'form.jamb_reg_no': 'JAMB_NUMBER',
     250                })
     251        result = self.plugin.extractCredentials(request)
     252        self.assertEqual(result, {'jambregno': 'JAMB_NUMBER',
     253                                  'accesscode': 'APP-1-1234567890'})
     254        return
     255
     256    def test_extractCredentials_accesscode_only(self):
     257        request = self.filled_request({
     258                'form.prefix': 'APP',
     259                'form.ac_series': '1',
     260                'form.ac_number': '1234567890',
     261                })
     262        result = self.plugin.extractCredentials(request)
     263        self.assertEqual(result, {'accesscode': 'APP-1-1234567890'})
     264        return
     265
     266    def test_extractCredentials_from_empty_session(self):
     267        session_data['zope.pluggableauth.browserplugins']['credentials'] = None
     268        result = self.plugin.extractCredentials(self.request)
     269        assert result is None
     270        return
     271
     272    def test_extractCredentials_from_nonempty_session(self):
     273        credentials = ApplicantCredentials('APP-1-12345')
     274        session_data['zope.pluggableauth.browserplugins'][
     275            'credentials'] = credentials
     276        result = self.plugin.extractCredentials(self.request)
     277        self.assertEqual(result, {'accesscode': 'APP-1-12345'})
     278        return
     279
     280
     281class ApplicantCredentialsTest(unittest.TestCase):
     282
     283    def setUp(self):
     284        self.credentials = ApplicantCredentials('SOME_ACCESSCODE')
     285        return
     286
     287    def tearDown(self):
     288        return
     289
     290    def test_methods(self):
     291        self.assertEqual(self.credentials.getAccessCode(), 'SOME_ACCESSCODE')
     292        assert self.credentials.getLogin() is None
     293        assert self.credentials.getPassword() is None
     294        return
     295
     296class FakePluggableAuth(object):
     297    prefix = 'foo'
     298
     299class PrincipalFactoryTest(unittest.TestCase):
     300
     301    def setUp(self):
     302        self.info = ApplicantPrincipalInfo('APP-1-1234567890')
     303        return
     304
     305    def tearDown(self):
     306        pass
     307
     308    def test_principalFactory_interface(self):
     309        verify.verifyClass(IAuthenticatedPrincipalFactory,
     310                           AuthenticatedApplicantPrincipalFactory
     311                           )
     312        return
     313
     314    def test_principalFactory_create(self):
     315        factory = AuthenticatedApplicantPrincipalFactory(self.info, None)
     316
     317        assert factory.info is self.info
     318        assert factory.request is None
     319        return
     320
     321    def test_principalFactory_call_w_prefix(self):
     322        factory = AuthenticatedApplicantPrincipalFactory(self.info, None)
     323        principal = factory(FakePluggableAuth())
     324
     325        assert isinstance(principal, ApplicantPrincipal)
     326        self.assertEqual(principal.__repr__(),
     327                         "ApplicantPrincipal('foo.APP-1-1234567890')")
     328        self.assertEqual(principal.id, 'foo.APP-1-1234567890')
     329        return
     330
     331    def test_principalFactory_call_wo_prefix(self):
     332        factory = AuthenticatedApplicantPrincipalFactory(self.info, None)
     333        fake_auth = FakePluggableAuth()
     334        fake_auth.prefix = None
     335        principal = factory(fake_auth)
     336        self.assertEqual(principal.id, 'APP-1-1234567890')
     337        return
     338       
    177339def test_suite():
    178340    suite = unittest.TestSuite()
    179341    for testcase in [
    180         AuthenticatorPluginTest,
     342        AuthenticatorPluginTest, CredentialsPluginTest,
     343        ApplicantCredentialsTest, PrincipalFactoryTest,
    181344        ]:
    182345        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
Note: See TracChangeset for help on using the changeset viewer.