## $Id: test_browser.py 13593 2016-01-11 09:34:58Z 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.wdu.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.wdu.applicants.export import CustomApplicantExporter
from kofacustom.wdu.applicants.batching import CustomApplicantProcessor

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

    def test_export_reimport_all(self):
        # we can export all applicants in a portal
        # set values we can expect in export file
        self.applicant = self.setup_applicant(self.applicant)
        exporter = CustomApplicantExporter()
        exporter.export_all(self.app, self.outfile)
        result = open(self.outfile, 'rb').read()
        self.assertMatches(result,
            'aggregate,applicant_id,application_date,application_number,'
            'body_responsible_address,container_code,course1,course2,'
            'course_admitted,date_of_birth,display_fullname,email,emp2_end,'
            'emp2_position,emp2_reason,emp2_start,emp_end,emp_position,'
            'emp_reason,emp_start,employer,employer2,firstname,fst_sit_date,'
            'fst_sit_fname,fst_sit_no,fst_sit_results,fst_sit_type,history,'
            'hq_degree,hq_disc,hq_fname,hq_matric_no,hq_school,hq_session,'
            'hq_type,jamb_reg_number,jamb_score,jamb_subjects,lastname,'
            'lga,locked,marit_stat,middlename,nationality,next_kin_address,'
            'notice,nysc_lga,nysc_year,olevel_exam_date,olevel_exam_date2,'
            'olevel_exam_number,olevel_exam_number2,olevel_results,'
            'olevel_results2,olevel_school,olevel_school2,olevel_type,'
            'olevel_type2,password,phone,presently_inst,reg_number,religion,'
            'result_uploaded,scd_sit_date,scd_sit_fname,scd_sit_no,'
            'scd_sit_results,scd_sit_type,screening_date,screening_score,'
            'screening_venue,sex,special,special_application,state,'
            'student_id,suspended,translated_state\r\n,'
            'dp2011_654321,,654321,,dp2011,CERT1,CERT1,CERT1,'
            '1981-02-04#,Anna M. Tester,anna@sample.com,,,,,,,,,,,Anna,,,,,,'
            '[u\'2016-01-11 10:28:58 WAT - Application initialized by system\']'
            ',,,,,,,,,,,Tester,,0,,M.,NG,,"Some notice\nin lines.",,,,,,,,,,,,,'
            'any password,+234-123-12345#,,123456,,,,,,,,'
            '"Saturday, 16th June 2012 2:00:00 PM",98,Exam Room,'
            'f,,,initialized,,0,initialized\r\n')
        # We can import the same file if we ignore some columns.
        # Since the applicants_catalog hasn't been notified, the same
        # record with same reg_number can be imported twice.
        processor = CustomApplicantProcessor()
        result = processor.doImport(
            self.outfile,
            ['aggregate','ignore_applicant_id','application_date','ignore_application_number',
            'body_responsible_address','container_code','course1','course2',
            'course_admitted','date_of_birth','ignore_display_fullname','email',
            'emp2_end','emp2_position','emp2_reason','emp2_start','emp_end',
            'emp_position','emp_reason','emp_start','employer','employer2',
            'firstname','fst_sit_date','fst_sit_fname','fst_sit_no',
            'fst_sit_results','fst_sit_type','ignore_history','hq_degree','hq_disc',
            'hq_fname','hq_matric_no','hq_school','hq_session','hq_type',
            'jamb_reg_number','jamb_score','jamb_subjects','lastname','lga',
            'locked','marit_stat','middlename','nationality','next_kin_address',
            'notice','nysc_lga','nysc_year','olevel_exam_date',
            'olevel_exam_date2','olevel_exam_number','olevel_exam_number2',
            'olevel_results','olevel_results2','olevel_school','olevel_school2',
            'olevel_type','olevel_type2','password','phone','presently_inst',
            'reg_number','religion','result_uploaded','scd_sit_date',
            'scd_sit_fname','scd_sit_no','scd_sit_results','scd_sit_type',
            'screening_date','screening_score','screening_venue','sex',
            'ignore_special','special_application','state','student_id',
            'suspended','ignore_translated_state'],
            mode='create')
        num_succ, num_fail, finished_path, failed_path = result
        self.assertEqual(num_succ,1)
        self.assertEqual(num_fail,0)
        # Now we ignore also the container_code and import the same file
        # in update mode which means that INigeriaApplicantUpdateByRegNo
        # is used for field conversion. applicant_id must be ignored
        # too since the previous import has notified the applicants_catalog
        # so that the portal 'knows' that reg_number is in use.
        processor = CustomApplicantProcessor()
        result = processor.doImport(
            self.outfile,
            ['aggregate','ignore_applicant_id','application_date','ignore_application_number',
            'body_responsible_address','ignore_container_code','course1','course2',
            'course_admitted','date_of_birth','ignore_display_fullname','email',
            'emp2_end','emp2_position','emp2_reason','emp2_start','emp_end',
            'emp_position','emp_reason','emp_start','employer','employer2',
            'firstname','fst_sit_date','fst_sit_fname','fst_sit_no',
            'fst_sit_results','fst_sit_type','ignore_history','hq_degree','hq_disc',
            'hq_fname','hq_matric_no','hq_school','hq_session','hq_type',
            'jamb_reg_number','jamb_score','jamb_subjects','lastname','lga',
            'locked','marit_stat','middlename','nationality','next_kin_address',
            'notice','nysc_lga','nysc_year','olevel_exam_date',
            'olevel_exam_date2','olevel_exam_number','olevel_exam_number2',
            'olevel_results','olevel_results2','olevel_school','olevel_school2',
            'olevel_type','olevel_type2','password','phone','presently_inst',
            'reg_number','religion','result_uploaded','scd_sit_date',
            'scd_sit_fname','scd_sit_no','scd_sit_results','scd_sit_type',
            'screening_date','screening_score','screening_venue','sex',
            'ignore_special','special_application','state','student_id',
            'suspended','ignore_translated_state'],
            mode='update')
        num_succ, num_fail, finished_path, failed_path = result
        self.assertEqual(num_succ,1)
        self.assertEqual(num_fail,0)
        return


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.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