Changeset 10526
- Timestamp:
- 22 Aug 2013, 21:21:52 (11 years ago)
- Location:
- main/waeup.cas/trunk/waeup/cas
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.cas/trunk/waeup/cas/authenticators.py
r10525 r10526 190 190 name = 'kofa_moodle1' 191 191 192 def _create_user(self, username, userdata, moodle): 193 try: 194 # Usernames in Moodle must not contain uppercase 195 # letters even if extendedusernamechars is set True. 196 # Usernames in Moodle are case insensitive. 197 # In other words, Moodle changes usernames from 198 # uppercase to lowercase when you login. 199 result = moodle.core_user_create_users([ 200 {'username':username.lower(), 201 'password':'dummy', 202 'firstname':userdata['firstname'], 203 'lastname':userdata['lastname'], 204 'email':userdata['email']}]) 205 except xmlrpclib.Fault: 206 faultstring = sys.exc_info()[1].faultString 207 if 'Email address already exists' in faultstring: 208 return (False, 209 'Another Moodle user is using the same email address.' 210 ' Email addresses can\'t be used twice in Moodle.') 211 # If user exists just pass. Unfortunately the error message 212 # given by Moodle is not always the same. 213 if not ('Username already exists' in faultstring or 214 'Unknown error' in faultstring): 215 return (False, faultstring) 192 def _update_user(self, username, userdata, moodle): 216 193 try: 217 194 result = moodle.core_user_get_users([ … … 220 197 faultstring = sys.exc_info()[1].faultString 221 198 return (False, faultstring) 222 user_id = result['users'][0]['id'] 199 if result['users']: 200 user_id = result['users'][0]['id'] 201 else: 202 try: 203 # Usernames in Moodle must not contain uppercase 204 # letters even if extendedusernamechars is set True. 205 # Usernames in Moodle are case insensitive. 206 # In other words, Moodle changes the username from 207 # uppercase to lowercase when you login. 208 result = moodle.core_user_create_users([ 209 {'username':username.lower(), 210 'password':'dummy', 211 'firstname':userdata['firstname'], 212 'lastname':userdata['lastname'], 213 'email':userdata['email']}]) 214 except xmlrpclib.Fault: 215 faultstring = sys.exc_info()[1].faultString 216 if 'Email address already exists' in faultstring: 217 return (False, 218 'Another Moodle user is using the same email address.' 219 ' Email addresses can\'t be used twice in Moodle.') 220 return (False, faultstring) 221 user_id = result[0]['id'] 223 222 # Due to a lack of Moodle (Moodle requires an LDAP 224 223 # connection) the authentication method can't … … 255 254 if principal['type'] == 'student': 256 255 userdata = proxy.get_student_moodle_data(kofa_username) 257 return self._ create_user(username, userdata, moodle)256 return self._update_user(username, userdata, moodle) 258 257 if principal['type'] == 'applicant': 259 258 userdata = proxy.get_applicant_moodle_data(kofa_username) 260 return self._ create_user(username, userdata, moodle)259 return self._update_user(username, userdata, moodle) 261 260 return (False, 'User not eligible') 262 261 return (False, 'Invalid username or password') -
main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py
r10524 r10526 186 186 'type': 'applicant'} 187 187 if username in ( 188 'fault1', 'fault2', 'fault3', 'fault4', 'fault7') and password == 'biz': 188 'fault1', 'fault2', 189 'fault3', 'fault4', 'fault7') and password == 'biz': 189 190 return {'type': 'student'} 190 191 if username == 'fault5' and password == 'biz': … … 231 232 # fake Moodle core_user_create_users method. 232 233 if arg[0]['username'] == 'school1-fault1': 233 raise xmlrpclib.Fault('faultCode', 'faultString') 234 if arg[0]['username'] in ('school1-fault4', 'school1-fault5'): 235 raise xmlrpclib.Fault('faultCode', 'Username already exists') 234 raise xmlrpclib.Fault('faultCode', 'core_user_create_users failed') 236 235 if arg[0]['username'] == 'school1-fault7': 237 236 raise xmlrpclib.Fault('faultCode', 'Email address already exists') 238 return None237 return [{'username': 'any name', 'id': 37}] 239 238 240 239 def _core_user_get_users(self, arg): 241 240 # fake Moodle core_user_get_users method. 242 241 if arg[0]['value'] == 'SCHOOL1-fault2': 243 raise xmlrpclib.Fault('faultCode', ' faultString')242 raise xmlrpclib.Fault('faultCode', 'core_user_get_users failed') 244 243 if arg[0]['value'] == 'SCHOOL1-fault3': 245 244 return {'users':[{'id':'SCHOOL1-fault3'}]} 246 return {'users':[{'id':'any id'}]} 245 if arg[0]['value'] in ('SCHOOL1-fault4', 'SCHOOL1-fault5'): 246 return {'users':[{'id': 123}]} 247 return {'users': []} 247 248 248 249 def _core_user_update_users(self, arg): 249 250 # fake Moodle core_user_update_users method. 250 251 if arg[0]['id'] == 'SCHOOL1-fault3': 251 raise xmlrpclib.Fault('faultCode', ' faultString')252 raise xmlrpclib.Fault('faultCode', 'core_user_update_users failed') 252 253 return None 253 254 … … 318 319 result = proxy.core_user_create_users( 319 320 [{'username': 'any name'}]) 320 assert result == None321 assert result == [{'id': 37, 'username': 'any name'}] 321 322 self.assertRaises( 322 323 xmlrpclib.Fault, proxy.core_user_create_users, … … 326 327 [{'username': 'school1-fault7'}]) 327 328 # test core_user_get_users 328 result = proxy.core_user_get_users(329 [{'key': 'username', 'value': 'any name'}])330 assert result == {'users':[{'id':'any id'}]}331 329 self.assertRaises( 332 330 xmlrpclib.Fault, proxy.core_user_get_users, … … 334 332 # test core_user_update_users 335 333 result = proxy.core_user_update_users( 336 [{'id': 'any id'}])334 [{'id': 234}]) 337 335 assert result == None 338 336 self.assertRaises( … … 358 356 result1a = auth.check_credentials('SCHOOL1-pig', 'pog') 359 357 assert result1a == (True, '') 358 # user does not exist 360 359 result2s = auth.check_credentials('SCHOOL1-foo', 'bar') 361 360 assert result2s == (False, 'Invalid username or password') 361 # school does not exist 362 362 result3s = auth.check_credentials('SCHOOL2-bar', 'baz') 363 363 assert result3s == (False, 'Invalid username or password') 364 # user is neither a student nor an applicant 364 365 result8 = auth.check_credentials('SCHOOL1-fault6', 'biz') 365 366 assert result8 == (False, 'User not eligible') 367 # user has an existing email address 366 368 result9 = auth.check_credentials('SCHOOL1-fault7', 'biz') 367 369 assert result9 == (False, … … 369 371 ' Email addresses can\'t be used twice in Moodle.') 370 372 371 # exception s are raised in the following cases373 # exception is catched if core_user_create_users fails 372 374 result4s = auth.check_credentials('SCHOOL1-fault1', 'biz') 373 assert result4s == (False, 'faultString') 375 assert result4s == (False, 'core_user_create_users failed') 376 # exception is catched if core_user_get_users fails 374 377 result5s = auth.check_credentials('SCHOOL1-fault2', 'biz') 375 assert result5s == (False, 'faultString') 378 assert result5s == (False, 'core_user_get_users failed') 379 # exception is catched if core_user_update_users fails 376 380 result6s = auth.check_credentials('SCHOOL1-fault3', 'biz') 377 assert result6s == (False, ' faultString')378 379 # no exception i f user exists381 assert result6s == (False, 'core_user_update_users failed') 382 383 # no exception is raised if user exists 380 384 result7s = auth.check_credentials('SCHOOL1-fault4', 'biz') 381 385 assert result7s == (True, '') … … 383 387 assert result7a == (True, '') 384 388 385 # If marker is an empty sringschool markers are neither386 # checked nor removed .389 # if marker is an empty string, school markers are neither 390 # checked nor removed 387 391 BACKENDS3['inst1']['marker'] = '' 388 392 auth = KofaMoodleAuthenticator(auth_backends=str(BACKENDS3))
Note: See TracChangeset for help on using the changeset viewer.