## $Id: test_browser.py 14027 2016-07-13 04:36:41Z 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 zope.component import createObject, getUtility
from waeup.kofa.configuration import SessionConfiguration
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("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_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 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)
