Ignore:
Timestamp:
20 Oct 2016, 21:13:17 (8 years ago)
Author:
Henrik Bettermann
Message:

Count mandates on configuration page and provide 'Purge' button.

Location:
main/waeup.ikoba/trunk/src/waeup/ikoba/mandates
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/mandates/container.py

    r11954 r14221  
    3131    grok.provides(IMandatesContainer)
    3232
     33    @property
     34    def count(self):
     35        """Count active and expired mandates.
     36        """
     37        now = datetime.utcnow()
     38        total = len(self)
     39        expired = len([i for i in self.values() if i.expires < now])
     40        active = total - expired
     41        return active, expired, total
     42
    3343    def addMandate(self, mandate):
    3444        if not IMandate.providedBy(mandate):
     
    4252        """
    4353        num_deleted = 0
    44         for mandate in self.keys():
     54        keys = list(self.keys())
     55        for mandate in keys:
    4556            if self[mandate].expires < datetime.utcnow():
    4657                del self[mandate]
    4758                num_deleted += 1
     59        grok.getSite().logger.info('%s mandates purged' % num_deleted)
    4860        return num_deleted
    4961
  • main/waeup.ikoba/trunk/src/waeup/ikoba/mandates/interfaces.py

    r11949 r14221  
    2525    """
    2626
     27    count = Attribute('Count active and expired mandates.')
     28
    2729    def addMandate(mandate):
    2830        """Add mandate.
  • main/waeup.ikoba/trunk/src/waeup/ikoba/mandates/mandate.py

    r11949 r14221  
    6767    def execute(self):
    6868        msg = _('Wrong mandate parameters.')
     69        redirect_path = ''
    6970        if self.expires < datetime.utcnow():
    7071            msg = _('Mandate expired.')
    71         if self._setPassword():
     72        elif self._setPassword():
    7273            msg = _('Password has been successfully set. '
    7374                    'Login with your new password.')
     
    7576            grok.getSite().logger.info(
    7677                'PasswordMandate used: %s ' % username)
     78            redirect_path = 'login'
    7779        del self.__parent__[self.mandate_id]
    78         return msg
     80        return msg, redirect_path
  • main/waeup.ikoba/trunk/src/waeup/ikoba/mandates/tests.py

    r12816 r14221  
    8989        # Add and execute a mandate with missing parameters.
    9090        mandate = PasswordMandate()
     91        IUserAccount(customer).setPassword('old_pw')
    9192        self.app['mandates'].addMandate(mandate)
    92         msg = mandate.execute()
     93        (msg, redirect_path) = mandate.execute()
    9394        self.assertEqual(msg, u'Wrong mandate parameters.')
    9495        # Add and execute an expired mandate.
    9596        mandate = PasswordMandate(days=0)
     97        mandate.params['user'] = customer
     98        mandate.params['password'] = 'mypwd1'
    9699        self.app['mandates'].addMandate(mandate)
    97         msg = mandate.execute()
     100        (msg, redirect_path) = mandate.execute()
    98101        self.assertEqual(msg, u'Mandate expired.')
     102        self.assertEqual(redirect_path, '')
     103        # Password has not been set
     104        self.assertTrue(IUserAccount(customer).checkPassword('old_pw'))
    99105        # Add and execute a perfect mandate
    100106        mandate = PasswordMandate()
     
    102108        mandate.params['password'] = 'mypwd1'
    103109        self.app['mandates'].addMandate(mandate)
    104         msg = mandate.execute()
     110        (msg, redirect_path) = mandate.execute()
    105111        # Password has been set.
    106112        self.assertEqual(msg, 'Password has been successfully set. Login with your new password.')
     
    120126        mandate.params['password'] = 'mypwd1'
    121127        self.app['mandates'].addMandate(mandate)
    122         msg = mandate.execute()
     128        (msg, redirect_path) = mandate.execute()
    123129        # Password has been set.
    124130        self.assertEqual(msg, 'Password has been successfully set. Login with your new password.')
     
    142148        self.assertEqual(len(self.app['mandates'].keys()), 1)
    143149        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
     150        logfile = os.path.join(
     151            self.app['datacenter'].storage, 'logs', 'main.log')
     152        logcontent = open(logfile).read()
     153        self.assertTrue('system - 1 mandates purged' in logcontent)
     154
     155    def test_purge_mandates(self):
     156        # mandate1 is an old mandate which just expired.
     157        mandate1 = PasswordMandate(days=0)
     158        self.app['mandates'].addMandate(mandate1)
     159        # mandate2 is a new mandate with default time delta.
     160        mandate2 = PasswordMandate(mandate_id='23456')
     161        self.app['mandates'].addMandate(mandate2)
     162        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     163        self.browser.open('http://localhost/app/configuration')
     164        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
     165        self.assertTrue('<span>expired</span>' in self.browser.contents)
     166        self.browser.getControl("Purge mandates").click()
     167        self.assertTrue('1 mandate(s) were purged' in self.browser.contents)
     168        self.assertEqual(self.app['mandates'].count, (1, 0, 1))
     169        return
    144170
    145171    def test_browser(self):
Note: See TracChangeset for help on using the changeset viewer.