Changeset 10526 for main


Ignore:
Timestamp:
22 Aug 2013, 21:21:52 (11 years ago)
Author:
Henrik Bettermann
Message:

Call core_user_get_users first and bring order into tests.

Location:
main/waeup.cas/trunk/waeup/cas
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.cas/trunk/waeup/cas/authenticators.py

    r10525 r10526  
    190190    name = 'kofa_moodle1'
    191191
    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):
    216193        try:
    217194            result = moodle.core_user_get_users([
     
    220197            faultstring = sys.exc_info()[1].faultString
    221198            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']
    223222        # Due to a lack of Moodle (Moodle requires an LDAP
    224223        # connection) the authentication method can't
     
    255254            if principal['type'] == 'student':
    256255                userdata = proxy.get_student_moodle_data(kofa_username)
    257                 return self._create_user(username, userdata, moodle)
     256                return self._update_user(username, userdata, moodle)
    258257            if principal['type'] == 'applicant':
    259258                userdata = proxy.get_applicant_moodle_data(kofa_username)
    260                 return self._create_user(username, userdata, moodle)
     259                return self._update_user(username, userdata, moodle)
    261260            return (False, 'User not eligible')
    262261        return (False, 'Invalid username or password')
  • main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py

    r10524 r10526  
    186186                    'type': 'applicant'}
    187187        if username in (
    188             'fault1', 'fault2', 'fault3', 'fault4', 'fault7') and password == 'biz':
     188            'fault1', 'fault2',
     189            'fault3', 'fault4', 'fault7') and password == 'biz':
    189190            return {'type': 'student'}
    190191        if username == 'fault5' and password == 'biz':
     
    231232        # fake Moodle core_user_create_users method.
    232233        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')
    236235        if arg[0]['username'] == 'school1-fault7':
    237236            raise xmlrpclib.Fault('faultCode', 'Email address already exists')
    238         return None
     237        return [{'username': 'any name', 'id': 37}]
    239238
    240239    def _core_user_get_users(self, arg):
    241240        # fake Moodle core_user_get_users method.
    242241        if arg[0]['value'] == 'SCHOOL1-fault2':
    243             raise xmlrpclib.Fault('faultCode', 'faultString')
     242            raise xmlrpclib.Fault('faultCode', 'core_user_get_users failed')
    244243        if arg[0]['value'] == 'SCHOOL1-fault3':
    245244            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': []}
    247248
    248249    def _core_user_update_users(self, arg):
    249250        # fake Moodle core_user_update_users method.
    250251        if arg[0]['id'] == 'SCHOOL1-fault3':
    251             raise xmlrpclib.Fault('faultCode', 'faultString')
     252            raise xmlrpclib.Fault('faultCode', 'core_user_update_users failed')
    252253        return None
    253254
     
    318319        result = proxy.core_user_create_users(
    319320            [{'username': 'any name'}])
    320         assert result == None
     321        assert result ==  [{'id': 37, 'username': 'any name'}]
    321322        self.assertRaises(
    322323            xmlrpclib.Fault, proxy.core_user_create_users,
     
    326327            [{'username': 'school1-fault7'}])
    327328        # 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'}]}
    331329        self.assertRaises(
    332330            xmlrpclib.Fault, proxy.core_user_get_users,
     
    334332        # test core_user_update_users
    335333        result = proxy.core_user_update_users(
    336             [{'id': 'any id'}])
     334            [{'id': 234}])
    337335        assert result == None
    338336        self.assertRaises(
     
    358356        result1a  = auth.check_credentials('SCHOOL1-pig', 'pog')
    359357        assert result1a == (True, '')
     358        # user does not exist
    360359        result2s = auth.check_credentials('SCHOOL1-foo', 'bar')
    361360        assert result2s == (False, 'Invalid username or password')
     361        # school does not exist
    362362        result3s = auth.check_credentials('SCHOOL2-bar', 'baz')
    363363        assert result3s == (False, 'Invalid username or password')
     364        # user is neither a student nor an applicant
    364365        result8 = auth.check_credentials('SCHOOL1-fault6', 'biz')
    365366        assert result8 == (False, 'User not eligible')
     367        # user has an existing email address
    366368        result9 = auth.check_credentials('SCHOOL1-fault7', 'biz')
    367369        assert result9 == (False,
     
    369371                    ' Email addresses can\'t be used twice in Moodle.')
    370372
    371         # exceptions are raised in the following cases
     373        # exception is catched if core_user_create_users fails
    372374        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
    374377        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
    376380        result6s = auth.check_credentials('SCHOOL1-fault3', 'biz')
    377         assert result6s == (False, 'faultString')
    378 
    379         # no exception if user exists
     381        assert result6s == (False, 'core_user_update_users failed')
     382
     383        # no exception is raised if user exists
    380384        result7s = auth.check_credentials('SCHOOL1-fault4', 'biz')
    381385        assert result7s == (True, '')
     
    383387        assert result7a == (True, '')
    384388
    385         # If marker is an empty sring school markers are neither
    386         # checked nor removed.
     389        # if marker is an empty string, school markers are neither
     390        # checked nor removed
    387391        BACKENDS3['inst1']['marker'] = ''
    388392        auth = KofaMoodleAuthenticator(auth_backends=str(BACKENDS3))
Note: See TracChangeset for help on using the changeset viewer.