## $Id: tests.py 9630 2012-11-13 13:03:01Z henrik $
##
## Copyright (C) 2011 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
##
from hurry.workflow.interfaces import IWorkflowState
from waeup.kofa.students.tests.test_browser import StudentsFullSetup
from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
from waeup.kofa.configuration import SessionConfiguration
from waeup.futminna.testing import FunctionalLayer

# Also run tests that send requests to external servers?
#   If you enable this, please make sure the external services
#   do exist really and are not bothered by being spammed by a test programme.
EXTERNAL_TESTS = False

def external_test(func):
    if not EXTERNAL_TESTS:
        myself = __file__
        if myself.endswith('.pyc'):
            myself = myself[:-2]
        print "WARNING: external tests are skipped!"
        print "WARNING: edit %s to enable them." % myself
        return
    return func


class InterswitchTestsStudents(StudentsFullSetup):
    """Tests for the Interswitch payment gateway.
    """

    layer = FunctionalLayer

    def setUp(self):
        super(InterswitchTestsStudents, self).setUp()
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.payments_path)
        IWorkflowState(self.student).setState('cleared')
        self.browser.open(self.payments_path + '/addop')
        self.browser.getControl(name="form.p_category").value = ['schoolfee']
        self.browser.getControl("Create ticket").click()
        self.assertMatches('...Payment ticket created...',
                           self.browser.contents)
        ctrl = self.browser.getControl(name='val_id')
        value = ctrl.options[0]
        self.browser.getLink(value).click()
        self.assertMatches('...Amount Authorized...',
                           self.browser.contents)
        self.assertMatches(
            '...<span>131500.0</span>...',
            self.browser.contents)
        self.payment_url = self.browser.url

    def test_interswitch_form(self):
        # Manager can access InterswitchForm
        self.browser.getLink("CollegePAY", index=0).click()
        # The total amount to be processed by Interswitch
        # has been reduced by the Interswitch fee of 150 Nairas
        self.assertMatches('...Total Amount Authorized:...',
                           self.browser.contents)
        self.assertMatches(
            '...<input type="hidden" name="amount" value="13150000.0" />...',
            self.browser.contents)
        self.assertMatches(
            '...item_name="School Fee" item_amt="12970000" bank_id="31" acct_num="0021030851"...',
            self.browser.contents)
        self.assertMatches(
            '...item_name="BT Education" item_amt="150000" bank_id="31" acct_num="0026781725"...',
            self.browser.contents)

        # Create clearance fee ticket
        self.browser.open(self.payments_path + '/addop')
        self.browser.getControl(name="form.p_category").value = ['clearance']
        self.browser.getControl("Create ticket").click()
        self.assertMatches('...ticket created...',
                           self.browser.contents)
        ctrl = self.browser.getControl(name='val_id')
        value = ctrl.options[1]
        self.browser.getLink(value).click()
        self.assertMatches('...Amount Authorized...',
                           self.browser.contents)
        self.assertMatches(
            '...<span>20000.0</span>...',
            self.browser.contents)
        # Manager can access InterswitchForm
        self.browser.getLink("CollegePAY", index=0).click()
        # The total amount to be processed by Interswitch
        # has been reduced by the Interswitch fee of 150 Nairas
        self.assertMatches('...Total Amount Authorized:...',
                           self.browser.contents)
        self.assertMatches(
            '...<input type="hidden" name="amount" value="2000000.0" />...',
            self.browser.contents)
        self.assertMatches(
            '...item_name="Acceptance Fee" item_amt="1970000" bank_id="120" acct_num="1750005063"...',
            self.browser.contents)
        # BT does nor charge a fee for clearance
        self.assertFalse("BT Education" in self.browser.contents)

        # Create hostel maintenance ticket
        self.browser.open(self.payments_path + '/addop')
        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
        self.browser.getControl("Create ticket").click()
        self.assertMatches('...You have not yet booked accommodation...',
                           self.browser.contents)
        # Students have to book bed first
        self.browser.open(self.acco_path)
        IWorkflowState(self.student).setState('admitted')
        self.browser.getLink("Book accommodation").click()
        self.assertFalse('Activation Code:' in self.browser.contents)
        self.browser.getControl("Create bed ticket").click()
        # Bed is randomly selected but, since there is only
        # one bed for this student, we know that ...
        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
                           self.browser.contents)
        self.browser.open(self.payments_path + '/addop')
        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
        self.browser.getControl("Create ticket").click()
        self.assertMatches('...ticket created...',
                           self.browser.contents)
        ctrl = self.browser.getControl(name='val_id')
        value = ctrl.options[2]
        self.browser.getLink(value).click()
        self.assertMatches('...Amount Authorized...',
                           self.browser.contents)
        self.assertMatches(
            '...<span>10000.0</span>...',
            self.browser.contents)
        # Manager can access InterswitchForm
        self.browser.getLink("CollegePAY", index=0).click()
        # The total amount to be processed by Interswitch
        # has been reduced by the Interswitch fee of 150 Nairas
        self.assertMatches('...Total Amount Authorized:...',
                           self.browser.contents)
        self.assertMatches(
            '...<input type="hidden" name="amount" value="1000000.0" />...',
            self.browser.contents)
        self.assertMatches(
            '...item_name="Hostel Maintenance Fee" item_amt="970000" bank_id="8" acct_num="2018856637"...',
            self.browser.contents)
        # BT does nor charge a fee for clearance
        self.assertFalse("BT Education" in self.browser.contents)


    @external_test
    def test_webservice(self):
        self.browser.open(self.payment_url + '/request_webservice')
        self.assertMatches('...Unsuccessful callback...',
                          self.browser.contents)
        # The payment is now in state failed
        self.assertMatches('...<span>Failed</span>...',
                          self.browser.contents)