Ignore:
Timestamp:
4 Sep 2011, 23:35:58 (13 years ago)
Author:
uli
Message:

Add support for assigning roles to student accounts and let password setter create unicode passwords.

File:
1 edited

Legend:

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

    r6668 r6680  
    2121##
    2222import unittest
     23from zope.authentication.interfaces import IAuthentication
    2324from zope.component import provideUtility, queryUtility, getGlobalSiteManager
    2425from zope.interface.verify import verifyClass, verifyObject
     
    2627from zope.password.interfaces import IPasswordManager
    2728from zope.pluggableauth import PluggableAuthentication
     29from zope.securitypolicy.role import Role
     30from zope.securitypolicy.interfaces import IRole, Allow
     31from waeup.sirp.authentication import get_principal_role_manager
    2832from waeup.sirp.interfaces import IAuthPluginUtility, IUserAccount
    2933from waeup.sirp.students.authentication import (
     
    6266    password = None
    6367
     68
     69class MinimalPAU(PluggableAuthentication):
     70    def getPrincipal(self, id):
     71        return 'faked principal'
     72
    6473class StudentAccountTests(unittest.TestCase):
    6574
     
    6776        self.fake_stud = FakeStudent()
    6877        self.account = StudentAccount(self.fake_stud)
     78
     79        # We provide a minimal PAU
     80        pau = MinimalPAU()
     81        provideUtility(pau, IAuthentication)
     82
     83        # We register a role
     84        test_role = Role('waeup.test.Role', 'Testing Role')
     85        provideUtility(test_role, IRole, name='waeup.test.Role')
     86
    6987        # We have to setup a password manager utility manually as we
    7088        # have no functional test. In functional tests this would
     
    7694
    7795    def tearDown(self):
    78         # Clear up the SSHA utility
    79         ssha_manager = queryUtility(
    80             IPasswordManager, name='SSHA', default=None)
    81         if ssha_manager is not None:
    82             gsm = getGlobalSiteManager()
    83             gsm.unregisterUtility(ssha_manager)
     96        self.account.roles = [] # make sure roles are reset
     97        gsm = getGlobalSiteManager()
     98        to_clean = []
     99        # Clear up utilities registered in setUp
     100        to_clean.append(
     101            (IPasswordManager, queryUtility(
     102                    IPasswordManager, name='SSHA', default=None)))
     103        to_clean.append(
     104            (IAuthentication, queryUtility(IAuthentication, default=None)))
     105        to_clean.append(
     106            (IRole, queryUtility(IRole, name='test.Role', default=None)))
     107        for iface, elem in to_clean:
     108            if elem is not None:
     109                gsm.unregisterUtility(elem, iface)
    84110        return
    85111
     
    95121        # we do not store plaintext passwords
    96122        self.assertTrue(self.fake_stud.password != 'secret')
     123        # passwords are stored as unicode
     124        self.assertTrue(isinstance(self.fake_stud.password, unicode))
    97125        return
    98126
     
    107135        self.assertEqual(result3, True)
    108136        return
     137
     138    def test_role_set(self):
     139        # make sure we can set roles for principals denoted by account
     140        prm = get_principal_role_manager()
     141        self.assertEqual(prm.getPrincipalsAndRoles(), [])
     142        self.account.roles = ['waeup.test.Role']
     143        self.assertEqual(
     144            prm.getPrincipalsAndRoles(),
     145            [('waeup.test.Role', 'test_stud', Allow)])
     146        return
     147
     148    def test_role_get(self):
     149        # make sure we can get roles set for an account
     150        self.assertEqual(self.account.roles, [])
     151        self.account.roles = ['waeup.test.Role',] # set a role
     152        self.assertEqual(self.account.roles, ['waeup.test.Role'])
     153        return
Note: See TracChangeset for help on using the changeset viewer.