## $Id: test_browser.py 14579 2017-02-23 17:57:10Z 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 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
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):
        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.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("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.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):
        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("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
