## $Id: test_browser.py 15530 2019-07-31 10:49:59Z 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 grok
import datetime
import pytz
import os
from StringIO import StringIO
from zope.event import notify
from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
from zope.component import createObject, getUtility
from waeup.kofa.configuration import SessionConfiguration
from waeup.kofa.interfaces import (
    IUserAccount, IExtFileStore, IFileStoreNameChooser)
from waeup.kofa.applicants.container import ApplicantsContainer
from waeup.kofa.browser.tests.test_pdf import samples_dir
from waeup.aaue.testing import FunctionalLayer
from waeup.kofa.applicants.tests.test_browser import (
    ApplicantsFullSetup, container_name_1, session_1)

class CustomApplicantUITest(ApplicantsFullSetup):
    """Perform some browser tests.
    """

    layer = FunctionalLayer

    def test_show_credentials_on_landing_page(self):
        # An applicant can register himself.
        self.applicant.reg_number = u'1234'
        notify(grok.ObjectModifiedEvent(self.applicant))
        self.browser.open('http://localhost/app/applicants/%s/' % container_name_1)
        self.browser.getLink("Register for application").click()
        # Fill the edit form with suitable values
        self.browser.getControl(name="form.firstname").value = 'Klaus'
        self.browser.getControl(name="form.lastname").value = 'Lutz'
        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
        self.browser.getControl("Send login credentials").click()
        self.assertMatches('...Your registration was successful...',
            self.browser.contents)
        self.assertTrue('<td>Password:</td>' in self.browser.contents)
        return

    def test_payment(self):
        certificate = createObject('waeup.Certificate')
        certificate.code = 'CERT2'
        certificate.title = 'New Certificate'
        certificate.application_category = 'basic'
        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
            certificate)
        certificate = createObject('waeup.Certificate')
        certificate.code = 'CERT3'
        certificate.title = 'New Certificate 2'
        certificate.application_category = 'basic'
        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
            certificate)
        configuration = SessionConfiguration()
        configuration.academic_session = session_1
        self.app['configuration'].addSessionConfiguration(configuration)
        self.applicantscontainer.application_fee = 200.0
        self.login()
        self.browser.open(self.edit_path)
        self.browser.getControl(name="form.course2").value = ['CERT2']
        self.browser.getControl(name="form.course3").value = ['CERT3']
        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
        #self.browser.getControl(name="form.programme_type").value = ['regular']
        self.browser.getControl(name="form.nationality").value = ['NG']
        self.browser.getControl(name="form.sex").value = ['m']
        self.browser.getControl(name="form.lastname").value = 'Merkel'
        self.browser.getControl(name="form.fst_sit_fname").value = 'name'
        self.browser.getControl(name="form.fst_sit_no").value = 'no'
        self.browser.getControl(name="form.fst_sit_sc_pin").value = 'pin'
        self.browser.getControl(name="form.fst_sit_sc_serial_number").value = 'serial'
        self.browser.getControl(name="form.fst_sit_date").value = '09/09/2000'
        self.browser.getControl(name="form.fst_sit_type").value = ['ssce']
        self.browser.getControl("Save").click()
        self.browser.getControl("Add online payment ticket").click()
        self.assertMatches('...Payment ticket created...',
                           self.browser.contents)
        # Payment tickets can be downloaded without submitting the form.
        self.browser.getLink("Download payment slip").click()
        self.assertEqual(self.browser.headers['Content-Type'],
                 'application/pdf')
        # AAUE applicants never see the 'Remove Selected Tickets' button.
        self.browser.open(self.edit_path)
        self.assertFalse('Remove' in self.browser.contents)
        return

    def test_screening_invitation_download(self):
        self.login()
        self.applicant.screening_date = u'29th August 2016 @ 8:30 am'
        self.applicant.screening_venue = u'Main Auditorium'
        self.applicant.lastname = u'Cox'
        self.applicant.firstname = u'Jo'
        self.applicant.email = 'xx@yy.zz'
        self.browser.open(self.view_path)
        self.assertFalse("Download application slip" in self.browser.contents)
        IWorkflowState(self.applicant).setState('submitted')
        self.browser.open(self.view_path)
        self.browser.getLink("screening invitation").click()
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertEqual(
            self.browser.headers['Content-Type'], 'application/pdf')
        path = os.path.join(samples_dir(), 'screening_invitation.pdf')
        open(path, 'wb').write(self.browser.contents)
        print "Sample PDF screening_invitation.pdf written to %s" % path
        return

    def test_application_slips(self):
        delattr(ApplicantsContainer, 'prefix')
        self.applicantscontainer.prefix = 'ude'
        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('admitted')
        self.applicant.course_admitted = self.certificate
        self.browser.open(self.manage_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

    def test_transcript_application_manage(self):
        # Add trans applicants container
        self.transcontainer = ApplicantsContainer()
        self.transcontainer.mode = 'create'
        self.transcontainer.code = u'trans%s' % session_1
        self.transcontainer.prefix = u'trans'
        self.transcontainer.application_category = u'no'
        self.transcontainer.year = session_1
        self.transcontainer.application_fee = 300.0
        self.transcontainer.title = u'This is the trans%s container' % session_1
        self.app['applicants'][self.transcontainer.code] = self.transcontainer
        delta = datetime.timedelta(days=10)
        self.transcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        self.transcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
        # Add applicant
        transapplicant = createObject(u'waeup.Applicant')
        transapplicant.firstname = u'Anna'
        transapplicant.lastname = u'Post'
        self.app['applicants'][self.transcontainer.code].addApplicant(transapplicant)
        self.transapplication_number = transapplicant.application_number
        self.transapplicant = self.app['applicants'][self.transcontainer.code][
            self.transapplication_number]
        self.transapplicant_path = ('http://localhost/app/applicants/trans%s/%s'
            % (session_1, self.transapplication_number))
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.transapplicant_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("Dispatch Address" in self.browser.contents)

    def _prepare_cert_container(self):
        # Add cert applicants container
        self.certcontainer = ApplicantsContainer()
        self.certcontainer.mode = 'update'
        self.certcontainer.code = u'cert%s' % session_1
        self.certcontainer.prefix = u'cert'
        self.certcontainer.application_category = u'no'
        self.certcontainer.year = session_1
        self.certcontainer.application_fee = 300.0
        self.certcontainer.title = u'This is the cert%s container' % session_1
        self.app['applicants'][self.certcontainer.code] = self.certcontainer
        delta = datetime.timedelta(days=10)
        self.certcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        self.certcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
        # Add applicant
        certapplicant = createObject(u'waeup.Applicant')
        certapplicant.firstname = u'Anna'
        certapplicant.lastname = u'Post'
        certapplicant.reg_number = u'1234%s' % self.certcontainer.code
        self.app['applicants'][self.certcontainer.code].addApplicant(certapplicant)
        self.certapplication_number = certapplicant.application_number
        self.certapplicant = self.app['applicants'][self.certcontainer.code][
            self.certapplication_number]
        self.certcontainer_path = ('http://localhost/app/applicants/cert%s'
            % session_1)
        self.certapplicant_path = ('http://localhost/app/applicants/cert%s/%s'
            % (session_1, self.certapplication_number))

    def test_certificate_request_manage(self):
        self._prepare_cert_container()
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.certapplicant_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        #self.assertTrue("Dispatch Address" in self.browser.contents)

    def test_certificate_request_update(self):
        self._prepare_cert_container()
        #IWorkflowState(self.applicant).setState('initialized')
        self.browser.open(self.certcontainer_path + '/register')
        self.browser.getControl(name="form.lastname").value = 'post'
        self.browser.getControl(name="form.reg_number").value = '1234'
        self.browser.getControl(name="form.email").value = 'new@yy.zz'
        self.browser.getControl("Send login credentials").click()
        # Yeah, we succeded ...
        self.assertTrue('Your registration was successful.'
            in self.browser.contents)

    def test_pg_application_manage(self):
        # Add pg applicants container
        self.pgcontainer = ApplicantsContainer()
        self.pgcontainer.mode = 'create'
        self.pgcontainer.code = u'pgft%s' % session_1
        self.pgcontainer.prefix = u'pgft'
        self.pgcontainer.application_category = u'no'
        self.pgcontainer.year = session_1
        self.pgcontainer.application_fee = 300.0
        self.pgcontainer.title = u'This is the pgft%s container' % session_1
        self.app['applicants'][self.pgcontainer.code] = self.pgcontainer
        delta = datetime.timedelta(days=10)
        self.pgcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        self.pgcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
        # Add applicant
        pgapplicant = createObject(u'waeup.Applicant')
        pgapplicant.firstname = u'Anna'
        pgapplicant.lastname = u'Post'
        self.app['applicants'][self.pgcontainer.code].addApplicant(pgapplicant)
        self.pgapplication_number = pgapplicant.application_number
        self.pgapplicant = self.app['applicants'][self.pgcontainer.code][
            self.pgapplication_number]
        self.pgapplicant_path = ('http://localhost/app/applicants/pgft%s/%s'
            % (session_1, self.pgapplication_number))
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.pgapplicant_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("3rd Higher Education Record" in self.browser.contents)

    def test_transcript_payment(self):
        configuration = SessionConfiguration()
        configuration.academic_session = session_1
        self.app['configuration'].addSessionConfiguration(configuration)
        # Add special application container
        applicantscontainer = ApplicantsContainer()
        applicantscontainer.year = session_1
        applicantscontainer.application_fee = 200.0
        applicantscontainer.code = u'trans1234'
        applicantscontainer.prefix = 'trans'
        applicantscontainer.title = u'This is a trans container'
        applicantscontainer.application_category = 'no'
        applicantscontainer.mode = 'create'
        applicantscontainer.strict_deadline = True
        delta = datetime.timedelta(days=10)
        applicantscontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        applicantscontainer.enddate = datetime.datetime.now(pytz.utc) + delta
        self.app['applicants']['trans1234'] = applicantscontainer
        # Add an applicant
        applicant = createObject('waeup.Applicant')
        # reg_number is the only field which has to be preset here
        # because managers are allowed to edit this required field
        applicant.reg_number = u'12345'
        self.app['applicants']['trans1234'].addApplicant(applicant)
        IUserAccount(
            self.app['applicants']['trans1234'][
            applicant.application_number]).setPassword('apwd')
        # Login
        self.browser.open(self.login_path)
        self.browser.getControl(
            name="form.login").value = applicant.applicant_id
        self.browser.getControl(name="form.password").value = 'apwd'
        self.browser.getControl("Login").click()
        self.browser.getLink("Edit").click()
        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
        self.browser.getControl(name="form.nationality").value = ['NG']
        #self.browser.getControl(name="form.sex").value = ['f']
        self.browser.getControl(name="form.firstname").value = 'Angela'
        self.browser.getControl(name="form.lastname").value = 'Merkel'
        self.browser.getControl(name="form.no_copies").value = ['3']
        self.browser.getControl(name="form.course_studied").value = ['CERT1']
        self.browser.getControl(name="form.matric_number").value = '234'
        self.browser.getControl(name="form.place_of_birth").value = 'Bochum'
        self.browser.getControl(name="form.dispatch_address").value = 'Kuensche'
        self.browser.getControl(name="form.perm_address").value = 'Kuensche'
        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
        self.browser.getControl(name="form.entry_session").value = ['2014']
        self.browser.getControl(name="form.end_session").value = ['2015']
        self.browser.getControl(name="form.phone.country").value = ['+234']
        self.browser.getControl(name="form.phone.ext").value = '5678'
        self.browser.getControl("Save").click()
        self.browser.getControl("Add online payment ticket").click()
        self.assertTrue('Payment ticket created' in self.browser.contents)
        self.assertTrue('<span>100.0</span>' in self.browser.contents)
        self.assertEqual(applicant.values()[0].amount_auth, 100.0)
        return

    def test_view_course_admitted(self):
        self.applicant.course_admitted = self.certificate
        self.login()
        self.assertFalse('Admitted Course of Study' in self.browser.contents)
        return

    def test_upload_stateresult_by_manager(self):
        # Add trans applicants container
        self.transcontainer = ApplicantsContainer()
        self.transcontainer.mode = 'create'
        self.transcontainer.code = u'trans%s' % session_1
        self.transcontainer.prefix = u'trans'
        self.transcontainer.application_category = u'no'
        self.transcontainer.year = session_1
        self.transcontainer.application_fee = 300.0
        self.transcontainer.title = u'This is the trans%s container' % session_1
        self.app['applicants'][self.transcontainer.code] = self.transcontainer
        delta = datetime.timedelta(days=10)
        self.transcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        self.transcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
        # Add applicant
        transapplicant = createObject(u'waeup.Applicant')
        transapplicant.firstname = u'Anna'
        transapplicant.lastname = u'Post'
        self.app['applicants'][self.transcontainer.code].addApplicant(transapplicant)
        self.transapplicant = self.app['applicants'][self.transcontainer.code][
            transapplicant.application_number]
        self.transapplicant_view_path = ('http://localhost/app/applicants/trans%s/%s'
            % (session_1, transapplicant.application_number))
        self.transapplicant_manage_path = ('http://localhost/app/applicants/trans%s/%s/manage'
            % (session_1, transapplicant.application_number))
        # Login
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.transapplicant_manage_path)
        # Create a pseudo file with acceptable size
        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
        pseudo_pdf = StringIO(pdf_content)
        ctrl = self.browser.getControl(name='form.stateresult')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
        self.browser.getControl("Save").click() # submit form
        # Even though the form could not be saved ...
        self.assertTrue('Required input is missing' in self.browser.contents)
        # ... the file has been successfully uploaded
        pdf_url = self.transapplicant_manage_path.replace('manage', 'stateresult.pdf')
        self.browser.open(pdf_url)
        self.assertEqual(
            self.browser.headers['content-type'], 'application/pdf')
        self.assertEqual(len(self.browser.contents), 307200)
        # There is really a file stored for the applicant
        storage = getUtility(IExtFileStore)
        file_id = IFileStoreNameChooser(self.transapplicant).chooseName(
            attr='stateresult.pdf')
        # The stored file can be fetched
        fd = storage.getFile(file_id)
        file_len = len(fd.read())
        self.assertEqual(file_len, 307200)
        # A file link is displayed on the edit view ...
        self.browser.open(self.transapplicant_manage_path)
        self.assertTrue('<a href="stateresult.pdf">' in self.browser.contents)
        # ... and on the dislay view
        self.browser.open(self.transapplicant_view_path)
        self.assertTrue('<a href="stateresult.pdf">Statement of Result</a>'
            in self.browser.contents)
        # Adding file is properly logged
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'applicants.log')
        logcontent = open(logfile).read()
        self.assertTrue(
            'zope.mgr - waeup.aaue.applicants.browser.CustomApplicantManageFormPage'
            ' - %s - saved: stateresult'
            % (self.transapplicant.applicant_id)
            in logcontent)
        # When an applicant is removed, also the pdf files are gone.
        del self.app['applicants'][self.transcontainer.code][self.transapplicant.application_number]
        fd = storage.getFile(file_id)
        self.assertTrue(fd is None)

    def test_upload_verificationdoc_by_manager(self):
        # Add ver applicants container
        self.vercontainer = ApplicantsContainer()
        self.vercontainer.mode = 'create'
        self.vercontainer.code = u'ver%s' % session_1
        self.vercontainer.prefix = u'ver'
        self.vercontainer.application_category = u'no'
        self.vercontainer.year = session_1
        self.vercontainer.application_fee = 300.0
        self.vercontainer.title = u'This is the ver%s container' % session_1
        self.app['applicants'][self.vercontainer.code] = self.vercontainer
        delta = datetime.timedelta(days=10)
        self.vercontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        self.vercontainer.enddate = datetime.datetime.now(pytz.utc) + delta
        # Add applicant
        verapplicant = createObject(u'waeup.Applicant')
        verapplicant.firstname = u'Anna'
        verapplicant.lastname = u'Post'
        self.app['applicants'][self.vercontainer.code].addApplicant(verapplicant)
        self.verapplicant = self.app['applicants'][self.vercontainer.code][
            verapplicant.application_number]
        self.verapplicant_view_path = ('http://localhost/app/applicants/ver%s/%s'
            % (session_1, verapplicant.application_number))
        self.verapplicant_manage_path = ('http://localhost/app/applicants/ver%s/%s/manage'
            % (session_1, verapplicant.application_number))
        # Login
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.verapplicant_manage_path)
        # Create a pseudo file with acceptable size
        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
        pseudo_pdf = StringIO(pdf_content)
        ctrl = self.browser.getControl(name='form.verificationdoc')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
        self.browser.getControl("Save").click() # submit form
        # Even though the form could not be saved ...
        self.assertTrue('Required input is missing' in self.browser.contents)
        # ... the file has been successfully uploaded
        pdf_url = self.verapplicant_manage_path.replace('manage', 'verificationdoc.pdf')
        self.browser.open(pdf_url)
        self.assertEqual(
            self.browser.headers['content-type'], 'application/pdf')
        self.assertEqual(len(self.browser.contents), 307200)
        # There is really a file stored for the applicant
        storage = getUtility(IExtFileStore)
        file_id = IFileStoreNameChooser(self.verapplicant).chooseName(
            attr='verificationdoc.pdf')
        # The stored file can be fetched
        fd = storage.getFile(file_id)
        file_len = len(fd.read())
        self.assertEqual(file_len, 307200)
        # A file link is displayed on the edit view ...
        self.browser.open(self.verapplicant_manage_path)
        self.assertTrue('<a href="verificationdoc.pdf">' in self.browser.contents)
        # ... and on the dislay view
        self.browser.open(self.verapplicant_view_path)
        self.assertTrue('<a href="verificationdoc.pdf">Result/Certificate Document</a>'
            in self.browser.contents)
        # Adding file is properly logged
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'applicants.log')
        logcontent = open(logfile).read()
        self.assertTrue(
            'zope.mgr - waeup.aaue.applicants.browser.CustomApplicantManageFormPage'
            ' - %s - saved: verificationdoc'
            % (self.verapplicant.applicant_id)
            in logcontent)
        # When an applicant is removed, also the pdf files are gone.
        del self.app['applicants'][self.vercontainer.code][self.verapplicant.application_number]
        fd = storage.getFile(file_id)
        self.assertTrue(fd is None)