## $Id: tests.py 11767 2014-07-23 12:42:22Z 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 datetime import datetime
from zope.component import createObject, 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 kofacustom.nigeria.testing import FunctionalLayer

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.student.nationality = u'NG'
        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.assertMatches(
            '...<span>40000.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('...<input type="hidden" name="pay_item_id" />...',
                           self.browser.contents)
        self.assertMatches('...Total Amount Authorized:...',
                           self.browser.contents)
        self.assertEqual(self.student.current_mode, 'ug_ft')
        self.assertMatches(
            '...<input type="hidden" name="amount" value="4000000" />...',
            self.browser.contents)

        # Create school fee ticket for returning students. Payment is made
        # for next session.
        current_payment_key = self.student['payments'].keys()[0]
        self.certificate.study_mode = u'ug_pt'
        IWorkflowState(self.student).setState('returning')
        configuration = createObject('waeup.SessionConfiguration')
        configuration.academic_session = 2005
        self.app['configuration'].addSessionConfiguration(configuration)
        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')
        value = ctrl.options[1]
        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
        self.browser.getLink(value).click()
        self.browser.getLink("CollegePAY", index=0).click()
        # Split amounts have been set.
        self.assertEqual(self.student['payments'][value].provider_amt, 0.0)
        self.assertEqual(self.student['payments'][value].gateway_amt, 0.0)
        self.assertMatches('...<input type="hidden" name="pay_item_id" />...',
                           self.browser.contents)
        self.assertMatches(
            '...<input type="hidden" name="amount" value="2000000" />...',
            self.browser.contents)

class InterswitchTestsApplicants(ApplicantsFullSetup):
    """Tests for the Interswitch payment gateway.
    """

    layer = FunctionalLayer

    def setUp(self):
        super(InterswitchTestsApplicants, self).setUp()
        configuration = SessionConfiguration()
        configuration.academic_session = datetime.now().year - 2
        self.app['configuration'].addSessionConfiguration(configuration)
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.manage_path)
        #IWorkflowState(self.student).setState('started')
        super(InterswitchTestsApplicants, self).fill_correct_values()
        self.applicantscontainer.application_fee = 1000.0
        self.browser.getControl(name="form.nationality").value = ['NG']
        self.browser.getControl(name="transition").value = ['start']
        self.browser.getControl("Save").click()
        self.browser.getControl("Add online").click()
        self.assertMatches('...ticket created...',
                           self.browser.contents)
        #ctrl = self.browser.getControl(name='val_id')
        #value = ctrl.options[0]
        #self.browser.getLink(value).click()
        self.payment_url = self.browser.url

    def test_interswitch_form(self):
        self.assertMatches('...Amount Authorized...',
                           self.browser.contents)
        self.assertMatches(
            '...<span>1000.0</span>...',
            self.browser.contents)
        # Manager can access InterswitchForm
        self.browser.getLink("CollegePAY", index=0).click()
        self.assertMatches('...Total Amount Authorized:...',
                           self.browser.contents)
        self.assertMatches(
            '...<input type="hidden" name="amount" value="100000" />...',
            self.browser.contents)

