## $Id: tests.py 17503 2023-07-22 06:10:21Z 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, date, timedelta
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 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 InterswitchTestsApplicants(ApplicantsFullSetup):
"""Tests for the Interswitch payment gateway.
"""
layer = FunctionalLayer
def setUp(self):
super(InterswitchTestsApplicants, self).setUp()
certificate = createObject('waeup.Certificate')
certificate.code = 'CERT2'
certificate.title = 'New Certificate'
certificate.application_category = 'basic'
self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
certificate)
configuration = SessionConfiguration()
configuration.academic_session = datetime.now().year - 2
self.app['configuration'].addSessionConfiguration(configuration)
self.configuration = 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.browser.getControl(name="form.programme_type").value = ['regular']
self.applicantscontainer.application_fee = 10000.0
self.browser.getControl(name="form.nationality").value = ['NG']
self.browser.getControl(name="form.course2").value = ['CERT2']
self.browser.getControl(name="transition").value = ['start']
self.browser.getControl("Save").click()
def test_interswitch_form_regular(self):
self.browser.getControl("Add online").click()
self.assertTrue('ticket created' in self.browser.contents)
#self.browser.getLink(value).click()
self.assertTrue('Amount Authorized' in self.browser.contents)
self.assertTrue('10000.0' in self.browser.contents)
self.payment_url = self.browser.url
# Manager can access InterswitchForm
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="WAeAC Portal Fee" item_amt="200000"'
in self.browser.contents)
self.assertTrue(
'item_name="Application Fee" item_amt="1000000" bank_id="123" acct_num="1012332141"'
in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
delta = timedelta(days=8)
self.applicant.values()[0].creation_date -= delta
self.browser.open(self.payment_url)
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
'This payment ticket is too old. Please create a new ticket'
in self.browser.contents)
def deactivated_test_interswitch_form_pg(self):
self.applicant.applicant_id = u'pg_anything'
self.browser.getControl("Add online").click()
self.assertTrue('ticket created' in self.browser.contents)
#self.browser.getLink(value).click()
self.assertTrue('Amount Authorized' in self.browser.contents)
self.assertTrue('10000.0' in self.browser.contents)
self.payment_url = self.browser.url
# Manager can access InterswitchForm
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="WAeAC" item_amt="200000"'
in self.browser.contents)
self.assertTrue(
'item_name="Application Fee" item_amt="580000" bank_id="51" acct_num="5210006575"'
in self.browser.contents)
self.assertTrue(
'item_name="PG Handbook" item_amt="200000" bank_id="51" acct_num="5210006575"'
in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
delta = timedelta(days=8)
self.applicant.values()[0].creation_date -= delta
self.browser.open(self.payment_url)
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
'This payment ticket is too old. Please create a new ticket'
in self.browser.contents)
def deactivated_test_interswitch_form_utme(self):
self.applicant.applicant_id = u'utme_anything'
self.browser.getControl("Add online").click()
self.assertTrue('ticket created' in self.browser.contents)
#self.browser.getLink(value).click()
self.assertTrue('Amount Authorized' in self.browser.contents)
self.assertTrue('10000.0' in self.browser.contents)
self.payment_url = self.browser.url
# Manager can access InterswitchForm
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="WAeAC" item_amt="200000"'
in self.browser.contents)
self.assertTrue(
'item_name="Application Fee" item_amt="780000" bank_id="117" acct_num="1010835352"'
in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
delta = timedelta(days=8)
self.applicant.values()[0].creation_date -= delta
self.browser.open(self.payment_url)
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
'This payment ticket is too old. Please create a new ticket'
in self.browser.contents)
def deactivated_test_interswitch_form_bridge(self):
self.applicant.applicant_id = u'bridge_xyz'
self.browser.getControl("Add online").click()
self.assertTrue('ticket created' in self.browser.contents)
#self.browser.getLink(value).click()
self.assertTrue('Amount Authorized' in self.browser.contents)
self.assertTrue('10000.0' in self.browser.contents)
self.payment_url = self.browser.url
# Manager can access InterswitchForm
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="WAeAC" item_amt="200000"'
in self.browser.contents)
self.assertTrue(
'item_name="Application Fee" item_amt="780000" bank_id="7" acct_num="1014847058"'
in self.browser.contents)
@external_test
def test_webservice(self):
self.browser.open(self.payment_url + '/request_webservice')
self.assertTrue('Unsuccessful callback' in self.browser.contents)
# The payment is now in state failed
self.assertTrue('Failed' in self.browser.contents)
class InterswitchTestsStudents(StudentsFullSetup):
"""Tests for the Interswitch payment gateway.
"""
layer = FunctionalLayer
def setUp(self):
super(InterswitchTestsStudents, self).setUp()
self.app['configuration']['2004'].interswitch_enabled = True
def test_interswitch_form_regular_school_fees(self):
self.certificate.study_mode = 'ug_ft'
#self.student['studycourse'].entry_session = 2013
#self.student['studycourse'].certificate.school_fee_3 = 51750.0
# Manager can access InterswitchForm
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_incl']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in
self.browser.contents)
self.assertTrue('Amount Authorized' in
self.browser.contents)
self.assertTrue('240500.0' in self.browser.contents)
self.payment_url = self.browser.url
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:'
in self.browser.contents)
self.assertEqual(self.student.current_mode, 'ug_ft')
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="Tuition" item_amt="18400000" bank_id="7" '
'acct_num="1022438743"' in self.browser.contents)
self.student['studycourse'].entry_session = 2015
self.browser.open(self.payments_path + '/addop')
self.browser.getControl(name="form.p_category").value = ['schoolfee_2']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in
self.browser.contents)
self.assertTrue('92000.0' in self.browser.contents)
self.payment_url = self.browser.url
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
'Please pay acceptance fee first' in self.browser.contents)
acc_payment = createObject('waeup.StudentOnlinePayment')
acc_payment.p_state = 'paid'
acc_payment.p_category = 'clearance'
self.student['payments']['xyz'] = acc_payment
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="School Fees (2nd instalment)" item_amt="9200000" bank_id="7" acct_num="1022438743"' in self.browser.contents)
self.browser.open(self.payments_path + '/addop')
self.browser.getControl(name="form.p_category").value = ['schoolfee_1']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in
self.browser.contents)
self.assertTrue('148500.0' in self.browser.contents)
self.payment_url = self.browser.url
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="Student Union Dues" item_amt="3000000" bank_id="7" acct_num="1019763348"' in self.browser.contents)
self.assertTrue(
'item_name="Tuition" item_amt="9200000" bank_id="7" acct_num="1022438743"' in self.browser.contents)
self.browser.open(self.payments_path + '/addop')
self.browser.getControl(name="form.p_category").value = ['schoolfee_2']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in
self.browser.contents)
self.assertTrue('92000.0' in self.browser.contents)
self.payment_url = self.browser.url
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="School Fees (2nd instalment)" item_amt="9200000" bank_id="7" acct_num="1022438743"' in self.browser.contents)
def test_interswitch_form_regular_acceptance_fees(self):
self.certificate.study_mode = 'ug_ft'
#self.app['configuration']['2004'].clearance_fee = 43050.0
# Manager can access InterswitchForm
self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
self.browser.open(self.payments_path)
IWorkflowState(self.student).setState('admitted')
self.student.nationality = u'NG'
self.browser.open(self.payments_path + '/addop')
self.browser.getControl(name="form.p_category").value = ['clearance_incl']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in self.browser.contents)
self.assertTrue('Amount Authorized' in self.browser.contents)
self.assertTrue('16800.0' in self.browser.contents)
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(self.student['payments'].values()[0].net_amt == 16800)
self.assertTrue(
'item_name="Acceptance Fee" item_amt="1400000" bank_id="8" '
'acct_num="2000249757"' in self.browser.contents)
self.certificate.study_mode = 'bridge'
self.browser.open(self.payments_path + '/addop')
self.browser.getControl(name="form.p_category").value = ['clearance']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in self.browser.contents)
self.assertTrue('Amount Authorized' in self.browser.contents)
self.assertTrue('14000.0' in self.browser.contents)
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(self.student['payments'].values()[1].net_amt == 14000)
self.assertTrue(
'item_name="Acceptance Fee" item_amt="1400000" bank_id="8" '
'acct_num="2000249757"' in self.browser.contents)
def deactivated_test_interswitch_form_concessional_fees(self):
self.certificate.study_mode = 'ug_ft'
self.app['configuration']['2004'].concessional_fee = 1567.0
# Manager can access InterswitchForm
self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
self.browser.open(self.payments_path)
IWorkflowState(self.student).setState('admitted')
self.student.nationality = u'NG'
self.browser.open(self.payments_path + '/addop')
self.browser.getControl(name="form.p_category").value = ['concessional']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in self.browser.contents)
self.assertTrue('Amount Authorized' in self.browser.contents)
self.assertTrue(
'1567.0' in self.browser.contents)
self.payment_url = self.browser.url
self.certificate.study_mode = 'ug_ft'
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertTrue(
''
in self.browser.contents)
self.assertTrue(
'item_name="Concessional Fee" item_amt="136700" '
'bank_id="117" acct_num="1010827641"'
in self.browser.contents)
def test_interswitch_form_ticket_expired(self):
self.certificate.study_mode = 'ug_pt'
self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
acc_payment = createObject('waeup.StudentOnlinePayment')
acc_payment.p_state = 'unpaid'
acc_payment.p_session = 2004
acc_payment.p_category = 'clearance'
acc_payment.p_id = 'xyz'
acc_payment.pay_item_id = '123'
acc_payment.amount_auth = 876.0
self.student['payments']['xyz'] = acc_payment
self.browser.open(self.payments_path + '/xyz')
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
self.assertEqual(self.student.current_mode, 'ug_pt')
self.assertTrue(
''
in self.browser.contents)
delta = timedelta(days=8)
acc_payment.creation_date -= delta
self.browser.open(self.payments_path + '/xyz')
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue(
'This payment ticket is too old. Please create a new ticket'
in self.browser.contents)
delta = timedelta(days=2)
acc_payment.creation_date += delta
self.browser.open(self.payments_path + '/xyz')
self.browser.getLink("Pay via Interswitch", index=0).click()
self.assertTrue('Total Amount Authorized:' in self.browser.contents)
@external_test
def test_webservice(self):
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_incl']
self.browser.getControl("Create ticket").click()
self.assertTrue('ticket created' in self.browser.contents)
ctrl = self.browser.getControl(name='val_id')
self.value = ctrl.options[0]
self.browser.getLink(self.value).click()
self.payment_url = self.browser.url
# First we have to open InterswitchPageStudent to set provider_amt
# and gateway_amt
self.browser.open(self.payment_url + '/goto_interswitch')
# Now we can call the webservice
self.browser.open(self.payment_url + '/request_webservice')
self.assertTrue('Unsuccessful callback' in self.browser.contents)
# The payment is now in state failed ...
self.assertTrue('Failed' in self.browser.contents)
# ... and the catalog has been updated
cat = getUtility(ICatalog, name='payments_catalog')
results = list(
cat.searchResults(p_state=('failed', 'failed')))
self.assertEqual(len(results), 1)
self.assertEqual(results[0].p_state, 'failed')
# Let's replace the p_id with a valid p_id of the AAUE
# live system. This is definitely not an appropriate
# solution for testing, but we have no choice since
# Interswitch doesn't provide any interface
# for testing.
payment = self.student['payments'][self.value]
payment.p_id = 'p4465649308559'
self.browser.open(self.payment_url + '/request_webservice')
self.assertTrue('Unsuccessful callback: Amount Inconsistency' in
self.browser.contents)
# The payment is now in state failed ...
self.assertTrue('Failed' in self.browser.contents)
# Let's replace the amount autorized with the amount of the
# live system payment
payment.amount_auth = 60250.0
self.browser.open(self.payment_url + '/request_webservice')
self.assertTrue('Successful payment' in
self.browser.contents)
# The payment is now in state paid ...
self.assertTrue('Paid' in self.browser.contents)
# ... and the catalog has been updated
cat = getUtility(ICatalog, name='payments_catalog')
results = list(
cat.searchResults(p_state=('paid', 'paid')))
self.assertEqual(len(results), 1)
self.assertEqual(results[0].p_state, 'paid')
# Approval is logged in students.log ...
logfile = os.path.join(
self.app['datacenter'].storage, 'logs', 'students.log')
logcontent = open(logfile).read()
self.assertTrue(
'zope.mgr - '
'waeup.aaue.interswitch.browser.CustomInterswitchPaymentRequestWebservicePageStudent - '
'E1000000 - successful schoolfee_incl payment: p4465649308559\n'
in logcontent)
# ... and in payments.log
logfile = os.path.join(
self.app['datacenter'].storage, 'logs', 'payments.log')
logcontent = open(logfile).read()
self.assertTrue(
'"zope.mgr",E1000000,p4465649308559,schoolfee_incl,'
'60250.0,00,1500.0,250.0,0.0,,,\n'
in logcontent)