## $Id: tests.py 11805 2014-09-24 06:06:51Z 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
##
import os
from zope.component import getUtility
from zope.catalog.interfaces import ICatalog
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.aaua.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.certificate.school_fee_1 = 10000.0
        self.certificate.school_fee_2 = 20000.0
        self.certificate.study_mode = 'ug_pt'
        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('...ticket created...',
                           self.browser.contents)
        ctrl = self.browser.getControl(name='val_id')
        self.value = ctrl.options[0]
        self.browser.getLink(self.value).click()
        self.assertMatches('...Amount Authorized...',
                           self.browser.contents)
        self.assertTrue(
            '<span>10000.0</span>' in self.browser.contents)
        self.payment_url = self.browser.url

    def test_interswitch_form(self):

        self.assertEqual(self.student['payments'][self.value].provider_amt, 0.0)
        self.assertEqual(self.student['payments'][self.value].gateway_amt, 0.0)
        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 0.0)
        # Manager can access InterswitchForm for the created school fee ticket
        self.browser.getLink("CollegePAY", index=0).click()
        # Split amounts have been set
        self.assertEqual(self.student['payments'][self.value].provider_amt, 3000.0)
        self.assertEqual(self.student['payments'][self.value].gateway_amt, 150.0)
        self.assertEqual(self.student['payments'][self.value].thirdparty_amt, 0.0)
        self.assertMatches('...Total Amount Authorized:...',
                           self.browser.contents)
        self.assertTrue(
            '<input type="hidden" name="amount" value="1000000" />'
            in self.browser.contents)
        self.assertTrue(
            'item_name="School Fee" item_amt="685000" bank_id="89" acct_num="0321100000000046"'
            in self.browser.contents)
        self.assertTrue(
            'item_name="BT Education" item_amt="300000" bank_id="117" acct_num="6012015294"'
            in self.browser.contents)

        self.certificate.study_mode = 'ug_sw'
        self.browser.open(self.payments_path + '/addop')
        self.browser.getControl(name="form.p_category").value = ['schoolfee']
        self.browser.getControl("Create ticket").click()
        ctrl = self.browser.getControl(name='val_id')
        self.value = ctrl.options[1]
        self.browser.getLink(self.value).click()
        self.assertTrue(
            '<span>10000.0</span>' in self.browser.contents)
        self.browser.getLink("CollegePAY", index=0).click()
        self.assertTrue(
            '<input type="hidden" name="amount" value="1000000" />'
            in self.browser.contents)
        self.assertTrue(
            'item_name="School Fee" item_amt="685000" bank_id="120" acct_num="2461770000021"'
            in self.browser.contents)

        self.certificate.ratio = 0.5
        self.browser.open(self.payments_path + '/addop')
        self.browser.getControl(name="form.p_category").value = ['schoolfee_2']
        self.browser.getControl("Create ticket").click()
        ctrl = self.browser.getControl(name='val_id')
        self.value = ctrl.options[2]
        self.browser.getLink(self.value).click()
        self.assertTrue(
            '<span>5000.0</span>' in self.browser.contents)
        self.browser.getLink("CollegePAY", index=0).click()
        self.assertTrue(
            '<input type="hidden" name="amount" value="500000" />'
            in self.browser.contents)
        self.assertTrue(
            'item_name="School Fee 2nd instalment" item_amt="485000" bank_id="120" acct_num="2461770000021"'
            in self.browser.contents)