## $Id: tests.py 8754 2012-06-18 17:02:17Z 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.aaue.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 EtranzactTestsStudent(StudentsFullSetup):
    """Tests for the eTranzact payment gateway.
    """

    layer = FunctionalLayer

    CONFIRMATION_NO = '500856521315472785095'

    def setUp(self):
        super(EtranzactTestsStudent, self).setUp()
        # Managers can add online payment tickets
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.app['configuration']['2004'].school_fee_base = 1234.0
        self.browser.open(self.payments_path)
        IWorkflowState(self.student).setState('cleared')
        self.browser.open(self.payments_path + '/addop')
        self.browser.getControl("Create ticket").click()
        self.assertMatches('...ticket created...',
                           self.browser.contents)

        ctrl = self.browser.getControl(name='val_id')
        value = ctrl.options[0]
        self.p_id = value
        self.browser.getLink(value).click()
        self.assertMatches('...Amount Authorized...',
                           self.browser.contents)
        self.payment_url = self.browser.url

    def test_enterpin(self):
        self.browser.getLink("Query eTranzact History").click()
        self.assertMatches(
            '...Confirmation Number (PIN):...',
            self.browser.contents)

    def test_webservice(self):
        self.browser.open('http://localhost/app/feerequest?PAYEE_ID=%s' % self.p_id)
        self.assertEqual(self.browser.contents,
            'FULL_NAME=Anna Tester&'
            'FACULTY=fac1&'
            'DEPARTMENT=dep1&'
            'RETURN_TYPE=%s&'
            'PROGRAMME_TYPE=CERT1&'
            'PAYMENT_TYPE=School Fee 1st instalment&'
            'ACADEMIC_SESSION=2004/2005&'
            'MATRIC_NO=E1000000&'
            'FEE_AMOUNT=1234.0&'
            'TRANSACTION_STATUS=unpaid' % self.p_id)

    @external_test
    def test_etranzact_query_history(self):

        self.browser.open(self.payment_url + '/query_history?confirmation_number=%s'
            % self.CONFIRMATION_NO)
        self.assertMatches('...Invalid or unsuccessful callback:...',
                          self.browser.contents)
        #self.assertMatches('...Wrong amount...',
        #                  self.browser.contents)
        #self.student['payments'][value].amount_auth = self.student[
        #    'payments'][value].r_amount_approved
        #self.browser.open(self.payment_url + '/query_history?confirmation_number=%s'
        #    % self.CONFIRMATION_NO)
        #self.assertMatches('...Wrong transaction id...',
        #                  self.browser.contents)

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

    layer = FunctionalLayer

    CONFIRMATION_NO = '500856521315472785095'

    def setUp(self):
        super(EtranzactTestsApplicants, self).setUp()
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.manage_path)
        #IWorkflowState(self.student).setState('started')
        super(EtranzactTestsApplicants, self).fill_correct_values()
        self.browser.getControl(name="transition").value = ['start']
        self.browser.getControl("Save").click()
        self.browser.getControl("Add online").click()
        self.assertTrue(
            'Session configuration object is not available'
            in self.browser.contents)
        configuration = SessionConfiguration()
        configuration.academic_session = 2009
        configuration.application_fee = 1000.0
        self.app['configuration'].addSessionConfiguration(configuration)
        self.browser.open(self.manage_path)
        self.browser.getControl("Add online").click()
        self.assertMatches('...ticket created...',
                           self.browser.contents)
        self.assertMatches('...Amount Authorized...',
                           self.browser.contents)
        self.assertMatches(
            '...<span>1000.0</span>...',
            self.browser.contents)
        self.payment_url = self.browser.url

    @external_test
    def test_etranzact_query_history(self):

        self.browser.open(self.payment_url + '/query_history?confirmation_number=%s'
            % self.CONFIRMATION_NO)
        self.assertMatches('...Invalid or unsuccessful callback:...',
                          self.browser.contents)
        #self.assertMatches('...Wrong amount...',
        #                  self.browser.contents)
        #self.applicant[value].amount_auth = self.applicant[
        #    value].r_amount_approved
        #self.browser.open(self.payment_url + '/query_history?confirmation_number=%s'
        #    % self.CONFIRMATION_NO)
        #self.assertMatches('...Wrong transaction id...',
        #                  self.browser.contents)
