Changeset 10512 for main/waeup.cas/trunk


Ignore:
Timestamp:
20 Aug 2013, 12:25:19 (11 years ago)
Author:
Henrik Bettermann
Message:

Increase test coverage.

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

Legend:

Unmodified
Added
Removed
  • main/waeup.cas/trunk/README.rst

    r10421 r10512  
    3737You can generate coverage reports by::
    3838
    39   (py27)$ py.test --cov
     39  (py27)$ py.test --cov .
    4040
    4141This will dump reports on commandline. For HTML reports you can try::
    4242
    43   (py27)$ py.test --cov --cov-report=html
     43  (py27)$ py.test --cov . --cov-report=html
    4444
    4545Please make sure, that coverage stays at 100%.
  • main/waeup.cas/trunk/waeup/cas/tests/test_authenticators.py

    r10511 r10512  
    8888        url='http://localhost:6666/',
    8989        marker='^SCHOOL1-',
    90         moodle_url = 'http://localhost/moodle',
     90        moodle_url = 'http://localhost:7777/',
    9191        )
    9292    )
     
    166166        self.register_function(
    167167            self._check_credentials, 'check_student_credentials')
     168        self.register_function(
     169            self._get_moodle_data, 'get_moodle_data')
    168170
    169171    def _check_credentials(self, username, password):
     
    183185        return None
    184186
    185 
    186 class KofaAuthenticatorIntegrationTests(unittest.TestCase):
     187    def _get_moodle_data(self, username):
     188        # fake waeup.kofa get_moodle_data method.
     189        if username == 'bird':
     190            return dict(email='aa@aa.aa',
     191                        firstname='Charles',
     192                        lastname='Parker',
     193                        )
     194        if username == 'pig':
     195            return dict(email='bb@bb.bb',
     196                        firstname='Ray',
     197                        lastname='Charles',
     198                        )
     199        return None
     200
     201class FakeMoodleServer(SimpleXMLRPCServer):
     202    # A fake Moodle server that provides only XMLRPC methods
     203
     204    allow_reuse_address = True
     205
     206    def __init__(self, *args, **kw):
     207        kw.update(allow_none=True)
     208        SimpleXMLRPCServer.__init__(self, *args, **kw)
     209        self.register_function(
     210            self._core_user_create_users, 'core_user_create_users')
     211        self.register_function(
     212            self._core_user_get_users, 'core_user_get_users')
     213        self.register_function(
     214            self._core_user_update_users, 'core_user_update_users')
     215
     216    def _core_user_create_users(self, list_of_dicts):
     217        # fake Moodle core_user_create_users method.
     218        return None
     219
     220    def _core_user_get_users(self, list_of_dicts):
     221        # fake Moodle core_user_get_users method.
     222        return {'users':[{'id':'any id'}]}
     223
     224    def _core_user_update_users(self, list_of_dicts):
     225        # fake Moodle core_user_update_users method.
     226        return None
     227
     228class KofaAuthenticatorsIntegrationTests(unittest.TestCase):
    187229    # A test case where a fake Kofa server is started before tests (and
    188230    # shut down afterwards).
    189231
    190     server = None
    191     th = None
     232    kofa_server = None
     233    kofa_th = None
     234    moodle_server = None
     235    moodle_th = None
    192236
    193237    @classmethod
    194238    def setUpClass(cls):
    195239        # set up a fake kofa server
    196         cls.server = FakeKofaServer(('localhost', 6666))
    197         cls.th = threading.Thread(target=cls.server.serve_forever)
    198         cls.th.daemon = True
    199         cls.th.start()
     240        cls.kofa_server = FakeKofaServer(('localhost', 6666))
     241        cls.kofa_th = threading.Thread(target=cls.kofa_server.serve_forever)
     242        cls.kofa_th.daemon = True
     243        cls.kofa_th.start()
     244        # set up a fake moodle server
     245        cls.moodle_server = FakeMoodleServer(('localhost', 7777))
     246        cls.moodle_th = threading.Thread(target=cls.moodle_server.serve_forever)
     247        cls.moodle_th.daemon = True
     248        cls.moodle_th.start()
    200249
    201250    @classmethod
    202251    def tearDownClass(cls):
    203252        # tear down fake kofa server
    204         cls.server.shutdown()
    205         cls.server.server_close()
     253        cls.kofa_server.shutdown()
     254        cls.kofa_server.server_close()
     255        # tear down fake moodle server
     256        cls.moodle_server.shutdown()
     257        cls.moodle_server.server_close()
    206258
    207259    def test_fake_kofa_works(self):
     
    220272            'id': 'pig',
    221273            'type': 'applicant'}
     274        result = proxy.get_moodle_data('pig')
     275        assert result == {
     276            'lastname': 'Charles',
     277            'email': 'bb@bb.bb',
     278            'firstname': 'Ray',
     279            }
    222280        return
    223281
    224     def test_check_credentials(self):
     282    def test_moodle_works(self):
     283        # make sure the local fake moodle works
     284        proxy = xmlrpclib.ServerProxy("http://localhost:7777", allow_none=True)
     285        result = proxy.core_user_create_users([])
     286        assert result == None
     287        result = proxy.core_user_get_users([])
     288        assert result == {'users':[{'id':'any id'}]}
     289        result = proxy.core_user_update_users([])
     290        assert result == None
     291        return
     292
     293    def test_check_credentials_1(self):
    225294        # we get real responses when querying Kofa instances
    226295        auth = KofaAuthenticator(auth_backends=str(BACKENDS2))
     
    232301        assert result3 == (False, 'Invalid username or password.')
    233302
     303    def test_check_credentials_2(self):
     304        # we get real responses when querying Kofa instances
     305        auth = KofaMoodleAuthenticator(auth_backends=str(BACKENDS3))
     306        result1 = auth.check_credentials('SCHOOL1-bird', 'bebop')
     307        assert result1 == (True, '')
     308        result2 = auth.check_credentials('SCHOOL1-foo', 'bar')
     309        assert result2 == (False, 'Invalid username or password.')
     310        result3 = auth.check_credentials('SCHOOL2-bar', 'baz')
     311        assert result3 == (False, 'Invalid username or password.')
Note: See TracChangeset for help on using the changeset viewer.