Ignore:
Timestamp:
25 Sep 2019, 11:34:29 (5 years ago)
Author:
Henrik Bettermann
Message:

Add ParentsPasswordMandate?.

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

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/mandates/mandate.py

    r13990 r15607  
    8282        return msg, redirect_path
    8383
     84class ParentsPasswordMandate(Mandate):
     85
     86    def _setPassword(self):
     87        student = self.params.get('student', None)
     88        pwd = self.params.get('password', None)
     89        if not None in (student, pwd):
     90            try:
     91                student.setParentsPassword(pwd)
     92                return True
     93            except:
     94                return False
     95        return False
     96
     97    def execute(self):
     98        msg = _('Wrong mandate parameters.')
     99        redirect_path = ''
     100        if self.expires < datetime.utcnow():
     101            msg = _('Mandate expired.')
     102        elif self._setPassword():
     103            msg = _('Parents password has been successfully set. '
     104                    'Login with your parents password.')
     105            grok.getSite().logger.info(
     106                'ParentsPasswordMandate used: %s '
     107                % self.params['student'].student_id)
     108            redirect_path = '/login'
     109        del self.__parent__[self.mandate_id]
     110        return msg, redirect_path
     111
    84112class RefereeReportMandate(Mandate):
    85113    """This is a mandate which can unlock a `RefereeReportAddFormPage`.
  • main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py

    r15287 r15607  
    3232    IMandatesContainer, IMandate)
    3333from waeup.kofa.mandates.container import MandatesContainer
    34 from waeup.kofa.mandates.mandate import PasswordMandate, RefereeReportMandate
     34from waeup.kofa.mandates.mandate import (
     35    PasswordMandate, RefereeReportMandate, ParentsPasswordMandate)
    3536from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
    3637from waeup.kofa.tests.test_authentication import SECRET
     
    5758            verifyObject(
    5859                IMandate, PasswordMandate())
     60            )
     61        self.assertTrue(
     62            verifyClass(
     63                IMandate, RefereeReportMandate)
     64            )
     65        self.assertTrue(
     66            verifyObject(
     67                IMandate, RefereeReportMandate())
     68            )
     69        self.assertTrue(
     70            verifyClass(
     71                IMandate, ParentsPasswordMandate)
     72            )
     73        self.assertTrue(
     74            verifyObject(
     75                IMandate, ParentsPasswordMandate())
    5976            )
    6077        return
     
    246263        # Both mandates still exist
    247264        self.assertEqual(len(self.app['mandates'].keys()), 2)
     265
     266    def test_set_parents_password(self):
     267        student = createObject('waeup.Student')
     268        # Add and execute a mandate with missing parameters.
     269        mandate = ParentsPasswordMandate()
     270        self.app['mandates'].addMandate(mandate)
     271        (msg, redirect_path) = mandate.execute()
     272        self.assertEqual(redirect_path, '')
     273        self.assertEqual(msg, u'Wrong mandate parameters.')
     274        # Add and execute an expired mandate.
     275        mandate = PasswordMandate(days=0)
     276        mandate.params['student'] = student
     277        mandate.params['password'] = 'mypwd1'
     278        self.app['mandates'].addMandate(mandate)
     279        (msg, redirect_path) = mandate.execute()
     280        self.assertEqual(msg, u'Mandate expired.')
     281        self.assertEqual(redirect_path, '')
     282        # Password has not been set
     283        self.assertEqual(student.getParentsPassword(), None)
     284        # Add and execute a perfect mandate
     285        mandate = ParentsPasswordMandate()
     286        mandate.params['student'] = student
     287        mandate.params['password'] = 'mypwd1'
     288        self.app['mandates'].addMandate(mandate)
     289        (msg, redirect_path) = mandate.execute()
     290        # Parents password has been set and must be used for login.
     291        self.assertEqual(msg,
     292            'Parents password has been successfully set. '
     293            'Login with your parents password.')
     294        self.assertEqual(redirect_path, '/login')
     295        self.assertTrue(IUserAccount(student).checkPassword('mypwd1'))
     296        # All mandates have been removed.
     297        self.assertEqual(len(self.app['mandates'].keys()), 0)
     298        logfile = os.path.join(
     299            self.app['datacenter'].storage, 'logs', 'main.log')
     300        logcontent = open(logfile).read()
     301        self.assertTrue('system - ParentsPasswordMandate used: K1000000'
     302                        in logcontent)
Note: See TracChangeset for help on using the changeset viewer.