## $Id: tests.py 14221 2016-10-20 21:13:17Z henrik $
##
## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""
Tests for mandates.
"""
import tempfile
import shutil
import os
from zope.testbrowser.testing import Browser
from datetime import datetime, timedelta
from zope.interface.verify import verifyClass, verifyObject
from zope.component import createObject
from zope.component.hooks import setSite, clearSite
from waeup.ikoba.app import Company
from waeup.ikoba.interfaces import IUserAccount
from waeup.ikoba.mandates.interfaces import (
    IMandatesContainer, IMandate)
from waeup.ikoba.mandates.container import MandatesContainer
from waeup.ikoba.mandates.mandate import PasswordMandate
from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)

class MandatesContainerTestCase(FunctionalTestCase):

    layer = FunctionalLayer

    def test_interfaces(self):
        # Make sure the correct interfaces are implemented.
        self.assertTrue(
            verifyClass(
                IMandatesContainer, MandatesContainer)
            )
        self.assertTrue(
            verifyObject(
                IMandatesContainer, MandatesContainer())
            )
        self.assertTrue(
            verifyClass(
                IMandate, PasswordMandate)
            )
        self.assertTrue(
            verifyObject(
                IMandate, PasswordMandate())
            )
        return

    def setUp(self):
        super(MandatesContainerTestCase, self).setUp()

        # Setup a sample site for each test
        app = Company()
        self.dc_root = tempfile.mkdtemp()
        app['datacenter'].setStoragePath(self.dc_root)

        # Prepopulate the ZODB...
        self.getRootFolder()['app'] = app
        # we add the site immediately after creation to the
        # ZODB. Catalogs and other local utilities are not setup
        # before that step.
        self.app = self.getRootFolder()['app']
        # Set site here. Some of the following setup code might need
        # to access grok.getSite() and should get our new app then
        setSite(app)

        self.browser = Browser()
        self.browser.handleErrors = False

    def tearDown(self):
        super(MandatesContainerTestCase, self).tearDown()
        clearSite()
        shutil.rmtree(self.dc_root)

    def test_set_customer_password(self):
        customer = createObject('waeup.Customer')
        # Add and execute a mandate with missing parameters.
        mandate = PasswordMandate()
        IUserAccount(customer).setPassword('old_pw')
        self.app['mandates'].addMandate(mandate)
        (msg, redirect_path) = mandate.execute()
        self.assertEqual(msg, u'Wrong mandate parameters.')
        # Add and execute an expired mandate.
        mandate = PasswordMandate(days=0)
        mandate.params['user'] = customer
        mandate.params['password'] = 'mypwd1'
        self.app['mandates'].addMandate(mandate)
        (msg, redirect_path) = mandate.execute()
        self.assertEqual(msg, u'Mandate expired.')
        self.assertEqual(redirect_path, '')
        # Password has not been set
        self.assertTrue(IUserAccount(customer).checkPassword('old_pw'))
        # Add and execute a perfect mandate
        mandate = PasswordMandate()
        mandate.params['user'] = customer
        mandate.params['password'] = 'mypwd1'
        self.app['mandates'].addMandate(mandate)
        (msg, redirect_path) = mandate.execute()
        # Password has been set.
        self.assertEqual(msg, 'Password has been successfully set. Login with your new password.')
        self.assertTrue(IUserAccount(customer).checkPassword('mypwd1'))
        # All mandates have been removed.
        self.assertEqual(len(self.app['mandates'].keys()), 0)
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'main.log')
        logcontent = open(logfile).read()
        self.assertTrue('system - PasswordMandate used: K1000000' in logcontent)

    def test_set_officer_password(self):
        self.app['users'].addUser('bob', 'bobssecret')
        officer = self.app['users']['bob']
        mandate = PasswordMandate()
        mandate.params['user'] = officer
        mandate.params['password'] = 'mypwd1'
        self.app['mandates'].addMandate(mandate)
        (msg, redirect_path) = mandate.execute()
        # Password has been set.
        self.assertEqual(msg, 'Password has been successfully set. Login with your new password.')
        self.assertTrue(IUserAccount(officer).checkPassword('mypwd1'))
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'main.log')
        logcontent = open(logfile).read()
        self.assertTrue('system - PasswordMandate used: bob' in logcontent)

    def test_remove_expired(self):
        # mandate1 is an old mandate which just expired.
        mandate1 = PasswordMandate(days=0)
        self.app['mandates'].addMandate(mandate1)
        # mandate2 is a new mandate with default time delta.
        mandate2 = PasswordMandate(mandate_id='23456')
        self.app['mandates'].addMandate(mandate2)
        self.assertEqual(len(self.app['mandates'].keys()), 2)
        num_deleted = self.app['mandates'].removeExpired()
        self.assertEqual(num_deleted, 1)
        # Only the new mandate remains in the container.
        self.assertEqual(len(self.app['mandates'].keys()), 1)
        self.assertEqual([i for i in self.app['mandates'].keys()], [u'23456'])
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'main.log')
        logcontent = open(logfile).read()
        self.assertTrue('system - 1 mandates purged' in logcontent)

    def test_purge_mandates(self):
        # mandate1 is an old mandate which just expired.
        mandate1 = PasswordMandate(days=0)
        self.app['mandates'].addMandate(mandate1)
        # mandate2 is a new mandate with default time delta.
        mandate2 = PasswordMandate(mandate_id='23456')
        self.app['mandates'].addMandate(mandate2)
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open('http://localhost/app/configuration')
        self.assertEqual(self.app['mandates'].count, (1, 1, 2))
        self.assertTrue('<span>expired</span>' in self.browser.contents)
        self.browser.getControl("Purge mandates").click()
        self.assertTrue('1 mandate(s) were purged' in self.browser.contents)
        self.assertEqual(self.app['mandates'].count, (1, 0, 1))
        return

    def test_browser(self):
        customer = createObject('waeup.Customer')
        self.app['customers'].addCustomer(customer)
        mandate = PasswordMandate()
        mandate.params['user'] = customer
        mandate.params['password'] = 'mypwd1'
        self.app['mandates'].addMandate(mandate)
        self.browser.open('http://localhost/app/mandate?mandate_id=%s'
            % mandate.mandate_id)
        # Password has been set.
        self.assertTrue(
            'Password has been successfully set. Login with your new password.'
            in self.browser.contents)
        self.assertTrue(IUserAccount(customer).checkPassword('mypwd1'))
        # All mandates have been removed.
        self.assertEqual(len(self.app['mandates'].keys()), 0)
        # We redirect to login page not to the frontpage.
        self.assertEqual(
            self.browser.url,
            'http://localhost/app/login?camefrom=PasswordMandate')
        # After login we redirect to the changepassword page.
        self.browser.getControl(name="form.login").value = customer.customer_id
        self.browser.getControl(name="form.password").value = 'mypwd1'
        self.browser.getControl("Login").click()
        self.assertEqual(
            self.browser.url,
            'http://localhost/app/customers/%s/changepassword'
            % customer.customer_id)
