## $Id: test_browser.py 16326 2020-11-21 08:35:09Z henrik $
##
## Copyright (C) 2013 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
##
"""
Test the applicant-related UI components.
"""
import os
import datetime
from zope.component import createObject, getUtility
from zope.catalog.interfaces import ICatalog
from zope.intid.interfaces import IIntIds
from hurry.workflow.interfaces import IWorkflowState
from kofacustom.edopoly.testing import FunctionalLayer
from waeup.kofa.browser.tests.test_pdf import samples_dir
from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup
from kofacustom.edopoly.applicants.export import CustomApplicantExporter
from kofacustom.edopoly.applicants.batching import CustomApplicantProcessor
from kofacustom.edopoly.configuration import CustomSessionConfiguration

class CustomApplicantUITests(ApplicantsFullSetup):
    # Tests for uploading/browsing the passport image of appplicants

    layer = FunctionalLayer

    def test_pay_admission_checking_fee(self):
        IWorkflowState(self.applicant).setState('admitted')
        self.applicant.screening_score = 55
        self.applicant.course_admitted = self.certificate
        self.login()
        # SessionConfiguration is not set, thus admission checking payment
        # is not necessary. Screening results and course admitted are visible.
        self.assertFalse(
            'Add admission checking payment ticket' in self.browser.contents)
        self.assertTrue('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
        self.assertTrue('<span>55</span>' in self.browser.contents)
        configuration = CustomSessionConfiguration()
        configuration.academic_session = datetime.datetime.now().year - 2
        self.app['configuration'].addSessionConfiguration(configuration)
        # Admission checking fee is 0, thus admission checking payment
        # is not necessary. Screening results and course admitted are visible.
        self.browser.open(self.view_path)
        self.assertFalse(
            'Add admission checking payment ticket' in self.browser.contents)
        self.assertTrue('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
        self.assertTrue('<span>55</span>' in self.browser.contents)
        configuration.admchecking_fee = 22.0
        # Admission checking payment button is now visible, but screening results
        # and course admitted are not.
        self.browser.open(self.view_path)
        self.assertTrue(
            'Add admission checking payment ticket' in self.browser.contents)
        self.assertFalse('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
        self.assertFalse('<span>55</span>' in self.browser.contents)
        # Application slip can't be downloaded
        self.assertFalse('Download application slip' in self.browser.contents)
        slip_path = self.view_path + '/application_slip.pdf'
        self.browser.open(slip_path)
        self.assertTrue(
            'Please pay admission checking fee before trying to download'
            in self.browser.contents)
        # History and state is also missing
        self.assertFalse('...' in self.browser.contents)
        self.assertFalse('Application initialized' in self.browser.contents)
        # Pay admission checking fee.
        self.browser.getControl("Add admission checking").click()
        p_id = self.applicant.keys()[0]
        self.applicant[p_id].p_state = 'paid'
        # Screening results and course admitted are visible after payment.
        self.browser.open(self.view_path)
        self.assertFalse(
            'Add admission checking payment ticket' in self.browser.contents)
        self.assertTrue('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
        self.assertTrue('<span>55</span>' in self.browser.contents)
        # History and state are visible
        self.assertTrue('...' in self.browser.contents)
        self.assertTrue('Application initialized' in self.browser.contents)
        # Application slip can be downloaded again.
        self.browser.getLink("Download application slip").click()
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertEqual(self.browser.headers['Content-Type'],
                         'application/pdf')
        return

class ApplicantExporterTest(ApplicantImportExportSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(ApplicantExporterTest, self).setUp()
        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
        self.cat = getUtility(ICatalog, name='applicants_catalog')
        self.intids = getUtility(IIntIds)
        return

    def setup_applicant(self, applicant):
        # set predictable values for `applicant`
        applicant.reg_number = u'123456'
        applicant.applicant_id = u'dp2011_654321'
        applicant.firstname = u'Anna'
        applicant.lastname = u'Tester'
        applicant.middlename = u'M.'
        applicant.nationality = u'NG'
        applicant.date_of_birth = datetime.date(1981, 2, 4)
        applicant.sex = 'f'
        applicant.email = 'anna@sample.com'
        applicant.phone = u'+234-123-12345'
        applicant.course1 = self.certificate
        applicant.course2 = self.certificate
        applicant.course_admitted = self.certificate
        applicant.notice = u'Some notice\nin lines.'
        applicant.screening_score = 98
        applicant.screening_venue = u'Exam Room'
        applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM'
        applicant.password = 'any password'
        return applicant

class ApplicantsContainerUITests(ApplicantsFullSetup):
    # Tests for ApplicantsContainer class views and pages

    layer = FunctionalLayer

    def test_application_slip(self):
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.slip_path = self.view_path + '/application_slip.pdf'
        self.browser.open(self.manage_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.fill_correct_values()
        self.browser.getControl("Save").click()
        IWorkflowState(self.applicant).setState('submitted')
        self.browser.open(self.view_path)
        self.browser.getLink("Download application slip").click()
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertEqual(self.browser.headers['Content-Type'],
                         'application/pdf')
        path = os.path.join(samples_dir(), 'application_slip.pdf')
        open(path, 'wb').write(self.browser.contents)
        print "Sample application_slip.pdf written to %s" % path
