Ignore:
Timestamp:
14 Oct 2012, 21:02:31 (12 years ago)
Author:
Henrik Bettermann
Message:

Dedicated officers should be able to login as student with a temporary password set by the system. This is the first part of its implementation.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_authentication.py

    r8983 r9334  
    1717##
    1818import unittest
     19from datetime import datetime, timedelta
    1920from zope.authentication.interfaces import IAuthentication
    2021from zope.component import provideUtility, queryUtility, getGlobalSiteManager
     
    6667    phone = None
    6768    suspended = False
     69    temp_password_minutes = 10
     70
     71    def setTempPassword(self, user, password):
     72        passwordmanager = queryUtility(IPasswordManager, 'SSHA')
     73        self.temp_password = {}
     74        self.temp_password[
     75            'password'] = passwordmanager.encodePassword(password)
     76        self.temp_password['user'] = user
     77        self.temp_password['timestamp'] = datetime.utcnow()
     78
     79    def getTempPassword(self):
     80        temp_password_dict = getattr(self, 'temp_password', None)
     81        if temp_password_dict is not None:
     82            delta = timedelta(minutes=self.temp_password_minutes)
     83            now = datetime.utcnow()
     84            if now < temp_password_dict.get('timestamp') + delta:
     85                return temp_password_dict.get('password')
     86            else:
     87                # Unset temporary password if expired
     88                self.temp_password = None
     89        return None
    6890
    6991
     
    138160        return
    139161
     162    def test_check_temp_password(self):
     163        # make sure that, if a temp password is set,
     164        # this password is used for authentication
     165        self.account.setPassword('secret')
     166        self.fake_stud.setTempPassword(user='beate', password='temp_secret')
     167        result1 = self.account.checkPassword('secret')
     168        result2 = self.account.checkPassword(None)
     169        result3 = self.account.checkPassword('nonsense')
     170        result4 = self.account.checkPassword('temp_secret')
     171        self.assertEqual(result1, False)
     172        self.assertEqual(result2, False)
     173        self.assertEqual(result3, False)
     174        self.assertEqual(result4, True)
     175        # if the temp password is expired, the original password
     176        # is used again
     177        delta = timedelta(minutes=11)
     178        self.fake_stud.temp_password['timestamp'] = datetime.utcnow() - delta
     179        result5 = self.account.checkPassword('temp_secret')
     180        result6 = self.account.checkPassword('secret')
     181        self.assertEqual(result5, False)
     182        self.assertEqual(result6, True)
     183        return
     184
    140185    def test_check_unset_password(self):
    141186        # empty and unset passwords do not match anything
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py

    r9332 r9334  
    11831183        return
    11841184
    1185     def test_student_access(self):
     1185    def test_student_login(self):
    11861186        # Student cant login if their password is not set
    11871187        self.student.password = None
     
    11951195        IUserAccount(
    11961196            self.app['students'][self.student_id]).setPassword('spwd')
    1197         IWorkflowInfo(self.student).fireTransition('admit')
    11981197        # Students can't login if their account is suspended/deactivated
    11991198        self.student.suspended = True
     
    12021201        self.browser.getControl(name="form.password").value = 'spwd'
    12031202        self.browser.getControl("Login").click()
    1204         self.assertTrue(
    1205             'Your account has been deactivated.' in self.browser.contents)
     1203        self.assertMatches(
     1204            '...Your account has been deactivated...', self.browser.contents)
    12061205        self.student.suspended = False
     1206        # Students can't login if a temporary password has been set and
     1207        # is not expired
     1208        self.app['students'][self.student_id].setTempPassword(
     1209            'anybody', 'temp_spwd')
     1210        self.browser.open(self.login_path)
     1211        self.browser.getControl(name="form.login").value = self.student_id
     1212        self.browser.getControl(name="form.password").value = 'spwd'
    12071213        self.browser.getControl("Login").click()
    1208         self.assertTrue(
    1209             'You logged in.' in self.browser.contents)
     1214        self.assertMatches(
     1215            '...Your account has been temporarily deactivated...',
     1216            self.browser.contents)
     1217        # The student can login with the temporary password
     1218        self.browser.open(self.login_path)
     1219        self.browser.getControl(name="form.login").value = self.student_id
     1220        self.browser.getControl(name="form.password").value = 'temp_spwd'
     1221        self.browser.getControl("Login").click()
     1222        self.assertMatches(
     1223            '...You logged in...', self.browser.contents)
     1224        # Student can view the base data
     1225        self.browser.open(self.student_path)
     1226        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     1227        self.assertEqual(self.browser.url, self.student_path)
     1228        # When the password expires ...
     1229        delta = timedelta(minutes=11)
     1230        self.app['students'][self.student_id].temp_password[
     1231            'timestamp'] = datetime.utcnow() - delta
     1232        self.app['students'][self.student_id]._p_changed = True
     1233        # ... the student will be automatically logged out
     1234        self.assertRaises(
     1235            Unauthorized, self.browser.open, self.student_path)
     1236        # Then the student can login with the original password
     1237        self.browser.open(self.login_path)
     1238        self.browser.getControl(name="form.login").value = self.student_id
     1239        self.browser.getControl(name="form.password").value = 'spwd'
     1240        self.browser.getControl("Login").click()
     1241        self.assertMatches(
     1242            '...You logged in...', self.browser.contents)
     1243
     1244    def test_student_access(self):
     1245        # Student cant login if their password is not set
     1246        IWorkflowInfo(self.student).fireTransition('admit')
     1247        self.browser.open(self.login_path)
     1248        self.browser.getControl(name="form.login").value = self.student_id
     1249        self.browser.getControl(name="form.password").value = 'spwd'
     1250        self.browser.getControl("Login").click()
     1251        self.assertMatches(
     1252            '...You logged in...', self.browser.contents)
    12101253        # Admitted student can upload a passport picture
    12111254        self.browser.open(self.student_path + '/change_portrait')
Note: See TracChangeset for help on using the changeset viewer.