Ignore:
Timestamp:
30 Jun 2012, 07:03:20 (12 years ago)
Author:
Henrik Bettermann
Message:

Let's use the PasswordMandate? also for change password requests (part1).

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

    r8853 r8857  
    5050        return _('Nothing to do.')
    5151
    52 class StudentPasswordMandate(Mandate):
     52class PasswordMandate(Mandate):
    5353    """This is a mandate which can set a student password.
    5454    """
    5555
    56     def _setStudentPassword(self):
    57         student_id = self.params.get('student_id', None)
     56    def _setPassword(self):
     57        user_id = self.params.get('user_id', None)
    5858        pwd = self.params.get('password', None)
    59         student = grok.getSite()['students'].get(student_id, None)
    60         if student and pwd:
    61             IUserAccount(student).setPassword(pwd)
     59        user_type = self.params.get('user_type', None)
     60        user = None
     61        if user_type == 'student':
     62            user = grok.getSite()['students'].get(user_id, None)
     63        elif user_type == 'officer':
     64            user = grok.getSite()['users'].get(user_id, None)
     65        if user and pwd:
     66            IUserAccount(user).setPassword(pwd)
    6267            return True
    6368        return False
     
    6772        if self.expires < datetime.utcnow():
    6873            msg = _('Mandate expired.')
    69         if self._setStudentPassword():
     74        if self._setPassword():
    7075            msg = _('Password has been successfully set. '
    7176                    'Proceed to the login page and enter your credentials.')
  • main/waeup.kofa/trunk/src/waeup/kofa/mandates/tests.py

    r8853 r8857  
    3131    IMandatesContainer, IMandate)
    3232from waeup.kofa.mandates.container import MandatesContainer
    33 from waeup.kofa.mandates.mandate import StudentPasswordMandate
     33from waeup.kofa.mandates.mandate import PasswordMandate
    3434from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
    3535
     
    5050        self.assertTrue(
    5151            verifyClass(
    52                 IMandate, StudentPasswordMandate)
     52                IMandate, PasswordMandate)
    5353            )
    5454        self.assertTrue(
    5555            verifyObject(
    56                 IMandate, StudentPasswordMandate())
     56                IMandate, PasswordMandate())
    5757            )
    5858        return
     
    8484        shutil.rmtree(self.dc_root)
    8585
    86     def test_set_password(self):
     86    def test_set_student_password(self):
    8787        student = createObject('waeup.Student')
    8888        self.app['students'].addStudent(student)
    8989        # Add and execute a mandate with missing parameters.
    90         mandate = StudentPasswordMandate()
     90        mandate = PasswordMandate()
    9191        self.app['mandates'].addMandate(mandate)
    9292        msg = mandate.execute()
    9393        self.assertEqual(msg, u'Wrong mandate parameters.')
    9494        # Add and execute an expired mandate.
    95         mandate = StudentPasswordMandate(days=0)
     95        mandate = PasswordMandate(days=0)
    9696        self.app['mandates'].addMandate(mandate)
    9797        msg = mandate.execute()
    9898        self.assertEqual(msg, u'Mandate expired.')
    9999        # Add and execute a perfect mandate
    100         mandate = StudentPasswordMandate()
    101         mandate.params['student_id'] = student.student_id
     100        mandate = PasswordMandate()
     101        mandate.params['user_id'] = student.student_id
    102102        mandate.params['password'] = 'mypwd1'
     103        mandate.params['user_type'] = 'student'
    103104        self.app['mandates'].addMandate(mandate)
    104105        msg = mandate.execute()
     
    110111        self.assertEqual(len(self.app['mandates'].keys()), 0)
    111112
     113    def test_set_officer_password(self):
     114        self.app['users'].addUser('bob', 'bobssecret')
     115        officer = self.app['users']['bob']
     116        mandate = PasswordMandate()
     117        mandate.params['user_id'] = 'bob'
     118        mandate.params['password'] = 'mypwd1'
     119        mandate.params['user_type'] = 'officer'
     120        self.app['mandates'].addMandate(mandate)
     121        msg = mandate.execute()
     122        # Password has been set.
     123        self.assertEqual(msg, 'Password has been successfully set. Proceed to '
     124            'the login page and enter your credentials.')
     125        self.assertTrue(IUserAccount(officer).checkPassword('mypwd1'))
     126
    112127    def test_remove_expired(self):
    113128        # mandate1 is an old mandate which just expired.
    114         mandate1 = StudentPasswordMandate(days=0)
     129        mandate1 = PasswordMandate(days=0)
    115130        self.app['mandates'].addMandate(mandate1)
    116131        # mandate2 is a new mandate with default time delta.
    117         mandate2 = StudentPasswordMandate(mandate_id='23456')
     132        mandate2 = PasswordMandate(mandate_id='23456')
    118133        self.app['mandates'].addMandate(mandate2)
    119134        self.assertEqual(len(self.app['mandates'].keys()), 2)
     
    126141        student = createObject('waeup.Student')
    127142        self.app['students'].addStudent(student)
    128         mandate = StudentPasswordMandate()
    129         mandate.params['student_id'] = student.student_id
     143        mandate = PasswordMandate()
     144        mandate.params['user_id'] = student.student_id
     145        mandate.params['user_type'] = 'student'
    130146        mandate.params['password'] = 'mypwd1'
    131147        self.app['mandates'].addMandate(mandate)
Note: See TracChangeset for help on using the changeset viewer.