## $Id: test_browser.py 16546 2021-07-13 16:05:29Z 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 grok
import datetime
import pytz
import os
from StringIO import StringIO
from zope.event import notify
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 zope.component import createObject, getUtility
from waeup.kofa.configuration import SessionConfiguration
from waeup.kofa.interfaces import (
    IUserAccount, IExtFileStore, IFileStoreNameChooser)
from kofacustom.iuokada.testing import FunctionalLayer
from waeup.kofa.applicants.container import ApplicantsContainer
from waeup.kofa.browser.tests.test_pdf import samples_dir
from waeup.kofa.applicants.tests.test_browser import (ApplicantsFullSetup,
    ApplicantsFullSetup, container_name_1, session_1)
from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup
from kofacustom.iuokada.applicants.export import CustomApplicantExporter
from kofacustom.iuokada.applicants.batching import CustomApplicantProcessor

SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')

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

    layer = FunctionalLayer

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 fill_correct_values(self):
        # Fill the edit form with suitable values
        self.browser.getControl(name="form.firstname").value = 'John'
        self.browser.getControl(name="form.middlename").value = 'Anthony'
        self.browser.getControl(name="form.lastname").value = 'Tester'
        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
        self.browser.getControl(name="form.sex").value = ['m']
        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
        self.browser.getControl(name="form.perm_address").value = 'Adresse'

    def test_manage_application(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(name="custom.course1").value = ['']
        self.browser.getControl("Save").click()
        self.assertTrue('Please select your 1st Choice Course of Study.'
            in self.browser.contents)
        self.browser.getControl(name="custom.course1").value = ['CERT1']
        self.browser.getControl("Save").click()
        self.assertEqual(self.applicant.course1, self.certificate)
        self.assertEqual(self.applicant.course2, None)
        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
        # Log entries have been added (course1 is missing because it was already stored)
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'applicants.log')
        logcontent = open(logfile).read()
        self.assertTrue(
            'zope.mgr - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage - '
            '%s - saved: firstname + middlename + lastname + date_of_birth '
            '+ sex + email'
            % (self.applicant.applicant_id)
            in logcontent)

    def test_upload_res_stat_by_manager(self):
        # Add trans applicants container
        self.transcontainer = ApplicantsContainer()
        self.transcontainer.mode = 'create'
        self.transcontainer.code = u'ug%s' % session_1
        self.transcontainer.prefix = u'ug'
        self.transcontainer.application_category = u'no'
        self.transcontainer.year = session_1
        self.transcontainer.application_fee = 300.0
        self.transcontainer.title = u'This is the ug%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'
        transapplicant.subtype = 'transfer'
        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/ug%s/%s'
            % (session_1, transapplicant.application_number))
        self.transapplicant_manage_path = ('http://localhost/app/applicants/ug%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)
        image = open(SAMPLE_IMAGE, 'rb')
        ctrl = self.browser.getControl(name='res_stat')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(image, filename='my_scan.jpg')
        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
        image_url = self.transapplicant_manage_path.replace('manage', 'res_stat')
        self.browser.open(image_url)
        self.assertEqual(
            self.browser.headers['content-type'], 'image/jpeg')
        self.assertEqual(len(self.browser.contents), 2787)
        # There is really a file stored for the applicant
        storage = getUtility(IExtFileStore)
        file_id = IFileStoreNameChooser(self.transapplicant).chooseName(
            attr='res_stat.pdf')
        # The stored file can be fetched
        fd = storage.getFile(file_id)
        file_len = len(fd.read())
        self.assertEqual(file_len, 2787)
        # A file link is displayed on the edit view ...
        self.browser.open(self.transapplicant_manage_path)
        self.assertTrue('<a href="res_stat">' in self.browser.contents)
        # ... and on the dislay view
        self.browser.open(self.transapplicant_view_path)
        self.assertTrue('res_stat">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 - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage'
            ' - %s - saved: res_stat'
            % (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)
