Index: main/waeup.cas/trunk/waeup/cas/authenticators.py
===================================================================
--- main/waeup.cas/trunk/waeup/cas/authenticators.py	(revision 10525)
+++ main/waeup.cas/trunk/waeup/cas/authenticators.py	(revision 10526)
@@ -190,28 +190,5 @@
     name = 'kofa_moodle1'
 
-    def _create_user(self, username, userdata, moodle):
-        try:
-            # Usernames in Moodle must not contain uppercase
-            # letters even if extendedusernamechars is set True.
-            # Usernames in Moodle are case insensitive.
-            # In other words, Moodle changes usernames from
-            # uppercase to lowercase when you login.
-            result = moodle.core_user_create_users([
-                {'username':username.lower(),
-                 'password':'dummy',
-                 'firstname':userdata['firstname'],
-                 'lastname':userdata['lastname'],
-                 'email':userdata['email']}])
-        except xmlrpclib.Fault:
-            faultstring = sys.exc_info()[1].faultString
-            if 'Email address already exists' in faultstring:
-                return (False,
-                    'Another Moodle user is using the same email address.'
-                    ' Email addresses can\'t be used twice in Moodle.')
-            # If user exists just pass. Unfortunately the error message
-            # given by Moodle is not always the same.
-            if not ('Username already exists' in faultstring or
-                    'Unknown error' in faultstring):
-                return (False, faultstring)
+    def _update_user(self, username, userdata, moodle):
         try:
             result = moodle.core_user_get_users([
@@ -220,5 +197,27 @@
             faultstring = sys.exc_info()[1].faultString
             return (False, faultstring)
-        user_id = result['users'][0]['id']
+        if result['users']:
+            user_id = result['users'][0]['id']
+        else:
+            try:
+                # Usernames in Moodle must not contain uppercase
+                # letters even if extendedusernamechars is set True.
+                # Usernames in Moodle are case insensitive.
+                # In other words, Moodle changes the username from
+                # uppercase to lowercase when you login.
+                result = moodle.core_user_create_users([
+                    {'username':username.lower(),
+                     'password':'dummy',
+                     'firstname':userdata['firstname'],
+                     'lastname':userdata['lastname'],
+                     'email':userdata['email']}])
+            except xmlrpclib.Fault:
+                faultstring = sys.exc_info()[1].faultString
+                if 'Email address already exists' in faultstring:
+                    return (False,
+                        'Another Moodle user is using the same email address.'
+                        ' Email addresses can\'t be used twice in Moodle.')
+                return (False, faultstring)
+            user_id = result[0]['id']
         # Due to a lack of Moodle (Moodle requires an LDAP
         # connection) the authentication method can't
@@ -255,8 +254,8 @@
             if principal['type'] == 'student':
                 userdata = proxy.get_student_moodle_data(kofa_username)
-                return self._create_user(username, userdata, moodle)
+                return self._update_user(username, userdata, moodle)
             if principal['type'] == 'applicant':
                 userdata = proxy.get_applicant_moodle_data(kofa_username)
-                return self._create_user(username, userdata, moodle)
+                return self._update_user(username, userdata, moodle)
             return (False, 'User not eligible')
         return (False, 'Invalid username or password')
Index: main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py
===================================================================
--- main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py	(revision 10525)
+++ main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py	(revision 10526)
@@ -186,5 +186,6 @@
                     'type': 'applicant'}
         if username in (
-            'fault1', 'fault2', 'fault3', 'fault4', 'fault7') and password == 'biz':
+            'fault1', 'fault2',
+            'fault3', 'fault4', 'fault7') and password == 'biz':
             return {'type': 'student'}
         if username == 'fault5' and password == 'biz':
@@ -231,23 +232,23 @@
         # fake Moodle core_user_create_users method.
         if arg[0]['username'] == 'school1-fault1':
-            raise xmlrpclib.Fault('faultCode', 'faultString')
-        if arg[0]['username'] in ('school1-fault4', 'school1-fault5'):
-            raise xmlrpclib.Fault('faultCode', 'Username already exists')
+            raise xmlrpclib.Fault('faultCode', 'core_user_create_users failed')
         if arg[0]['username'] == 'school1-fault7':
             raise xmlrpclib.Fault('faultCode', 'Email address already exists')
-        return None
+        return [{'username': 'any name', 'id': 37}]
 
     def _core_user_get_users(self, arg):
         # fake Moodle core_user_get_users method.
         if arg[0]['value'] == 'SCHOOL1-fault2':
-            raise xmlrpclib.Fault('faultCode', 'faultString')
+            raise xmlrpclib.Fault('faultCode', 'core_user_get_users failed')
         if arg[0]['value'] == 'SCHOOL1-fault3':
             return {'users':[{'id':'SCHOOL1-fault3'}]}
-        return {'users':[{'id':'any id'}]}
+        if arg[0]['value'] in ('SCHOOL1-fault4', 'SCHOOL1-fault5'):
+            return {'users':[{'id': 123}]}
+        return {'users': []}
 
     def _core_user_update_users(self, arg):
         # fake Moodle core_user_update_users method.
         if arg[0]['id'] == 'SCHOOL1-fault3':
-            raise xmlrpclib.Fault('faultCode', 'faultString')
+            raise xmlrpclib.Fault('faultCode', 'core_user_update_users failed')
         return None
 
@@ -318,5 +319,5 @@
         result = proxy.core_user_create_users(
             [{'username': 'any name'}])
-        assert result == None
+        assert result ==  [{'id': 37, 'username': 'any name'}]
         self.assertRaises(
             xmlrpclib.Fault, proxy.core_user_create_users,
@@ -326,7 +327,4 @@
             [{'username': 'school1-fault7'}])
         # test core_user_get_users
-        result = proxy.core_user_get_users(
-            [{'key': 'username', 'value': 'any name'}])
-        assert result == {'users':[{'id':'any id'}]}
         self.assertRaises(
             xmlrpclib.Fault, proxy.core_user_get_users,
@@ -334,5 +332,5 @@
         # test core_user_update_users
         result = proxy.core_user_update_users(
-            [{'id': 'any id'}])
+            [{'id': 234}])
         assert result == None
         self.assertRaises(
@@ -358,10 +356,14 @@
         result1a  = auth.check_credentials('SCHOOL1-pig', 'pog')
         assert result1a == (True, '')
+        # user does not exist
         result2s = auth.check_credentials('SCHOOL1-foo', 'bar')
         assert result2s == (False, 'Invalid username or password')
+        # school does not exist
         result3s = auth.check_credentials('SCHOOL2-bar', 'baz')
         assert result3s == (False, 'Invalid username or password')
+        # user is neither a student nor an applicant
         result8 = auth.check_credentials('SCHOOL1-fault6', 'biz')
         assert result8 == (False, 'User not eligible')
+        # user has an existing email address
         result9 = auth.check_credentials('SCHOOL1-fault7', 'biz')
         assert result9 == (False,
@@ -369,13 +371,15 @@
                     ' Email addresses can\'t be used twice in Moodle.')
 
-        # exceptions are raised in the following cases
+        # exception is catched if core_user_create_users fails
         result4s = auth.check_credentials('SCHOOL1-fault1', 'biz')
-        assert result4s == (False, 'faultString')
+        assert result4s == (False, 'core_user_create_users failed')
+        # exception is catched if core_user_get_users fails
         result5s = auth.check_credentials('SCHOOL1-fault2', 'biz')
-        assert result5s == (False, 'faultString')
+        assert result5s == (False, 'core_user_get_users failed')
+        # exception is catched if core_user_update_users fails
         result6s = auth.check_credentials('SCHOOL1-fault3', 'biz')
-        assert result6s == (False, 'faultString')
-
-        # no exception if user exists
+        assert result6s == (False, 'core_user_update_users failed')
+
+        # no exception is raised if user exists
         result7s = auth.check_credentials('SCHOOL1-fault4', 'biz')
         assert result7s == (True, '')
@@ -383,6 +387,6 @@
         assert result7a == (True, '')
 
-        # If marker is an empty sring school markers are neither
-        # checked nor removed.
+        # if marker is an empty string, school markers are neither
+        # checked nor removed
         BACKENDS3['inst1']['marker'] = ''
         auth = KofaMoodleAuthenticator(auth_backends=str(BACKENDS3))
