Changeset 5463
- Timestamp:
- 22 Aug 2010, 14:23:24 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/jambtables/tests/test_authentication.py
r5461 r5463 22 22 import grok 23 23 import unittest 24 #import zope.session.tests.setUp 25 #import zope.session.tests.tearDown 26 from zope.component import provideAdapter 24 27 from zope.component.hooks import setSite 28 from zope.interface import verify 29 from zope.pluggableauth.interfaces import IAuthenticatedPrincipalFactory 30 from zope.publisher.browser import TestRequest 31 from zope.publisher.interfaces import IRequest 32 from zope.session.session import RAMSessionDataContainer 33 from zope.session.interfaces import ISession 25 34 from zope.site import LocalSiteManager 26 35 from zope.site.folder import Folder 27 36 from zope.site.testing import siteSetUp, siteTearDown 28 from waeup.sirp.jambtables.authentication import ApplicantsAuthenticatorPlugin 37 from zope.testing import cleanup 38 from waeup.sirp.jambtables.authentication import ( 39 ApplicantsAuthenticatorPlugin, WAeUPApplicantCredentialsPlugin, 40 ApplicantCredentials, AuthenticatedApplicantPrincipalFactory, 41 ApplicantPrincipalInfo, ApplicantPrincipal) 42 from waeup.sirp.jambtables.interfaces import( 43 IJAMBApplicantSessionCredentials,) 29 44 30 45 class FakeBatch(dict): … … 173 188 dict(accesscode='APP-54321', jambregno='JAMB3')) 174 189 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 196 session_data = { 197 'zope.pluggableauth.browserplugins': {} 198 } 199 200 class 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 214 class 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 281 class 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 296 class FakePluggableAuth(object): 297 prefix = 'foo' 298 299 class 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 177 339 def test_suite(): 178 340 suite = unittest.TestSuite() 179 341 for testcase in [ 180 AuthenticatorPluginTest, 342 AuthenticatorPluginTest, CredentialsPluginTest, 343 ApplicantCredentialsTest, PrincipalFactoryTest, 181 344 ]: 182 345 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
Note: See TracChangeset for help on using the changeset viewer.