## $Id: test_browser.py 10876 2014-01-07 07:46:32Z 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 shutil
import tempfile
import datetime
import pytz
from zope.component.hooks import setSite, clearSite
from zope.component import createObject
from zope.testbrowser.testing import Browser
from waeup.kofa.app import University
from waeup.kofa.university.faculty import Faculty
from waeup.kofa.university.department import Department
from waeup.kofa.testing import FunctionalTestCase
from waeup.kofa.applicants.container import ApplicantsContainer
from waeup.fceokene.testing import FunctionalLayer

session = datetime.datetime.now().year - 2

class ApplicantUITest(FunctionalTestCase):
    """Perform some browser tests.
    """
    layer = FunctionalLayer

    def setUp(self):
        super(ApplicantUITest, self).setUp()
        # Setup a sample site for each test
        app = University()
        self.dc_root = tempfile.mkdtemp()
        app['datacenter'].setStoragePath(self.dc_root)

        # Prepopulate the ZODB...
        self.getRootFolder()['app'] = app
        # we add the site immediately after creation to the
        # ZODB. Catalogs and other local utilities are not setup
        # before that step.
        self.app = self.getRootFolder()['app']
        # Set site here. Some of the following setup code might need
        # to access grok.getSite() and should get our new app then
        setSite(app)

        # Add bec applicants container
        self.beccontainer = ApplicantsContainer()
        self.beccontainer.mode = 'create'
        self.beccontainer.code = u'bec%s' % session
        self.beccontainer.prefix = u'bec'
        self.beccontainer.application_category = u'bec'
        self.beccontainer.year = session
        self.beccontainer.application_fee = 300.0
        self.beccontainer.title = u'This is the bec%s container' % session
        self.app['applicants'][self.beccontainer.code] = self.beccontainer
        delta = datetime.timedelta(days=10)
        self.beccontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        self.beccontainer.enddate = datetime.datetime.now(pytz.utc) + delta

        # Add ug applicants container
        self.putmecontainer = ApplicantsContainer()
        self.putmecontainer.mode = 'create'
        self.putmecontainer.code = u'putme%s' % session
        self.putmecontainer.prefix = u'putme'
        self.putmecontainer.application_category = u'bec' # doesn't matter
        self.putmecontainer.year = session
        self.putmecontainer.application_fee = 300.0
        self.putmecontainer.title = u'This is the putme%s container' % session
        self.app['applicants'][self.putmecontainer.code] = self.putmecontainer
        delta = datetime.timedelta(days=10)
        self.putmecontainer.startdate = datetime.datetime.now(pytz.utc) - delta
        self.putmecontainer.enddate = datetime.datetime.now(pytz.utc) + delta

        # Populate university
        self.certificate = createObject('waeup.Certificate')
        self.certificate.code = 'CERT1'
        self.certificate.application_category = 'bec'
        self.certificate.start_level = 100
        self.certificate.end_level = 500
        self.certificate.study_mode = u'nce_ft'
        self.app['faculties']['fac1'] = Faculty()
        self.app['faculties']['fac1']['dep1'] = Department()
        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
            self.certificate)

        # Add (customized) applicants
        becapplicant = createObject(u'waeup.Applicant')
        becapplicant.firstname = u'Anna'
        becapplicant.lastname = u'Post'
        self.app['applicants'][self.beccontainer.code].addApplicant(becapplicant)
        self.becapplication_number = becapplicant.application_number
        self.becapplicant = self.app['applicants'][self.beccontainer.code][
            self.becapplication_number]
        self.becapplicant_path = ('http://localhost/app/applicants/bec%s/%s'
            % (session, self.becapplication_number))

        putmeapplicant = createObject(u'waeup.Applicant')
        putmeapplicant.firstname = u'Anna'
        putmeapplicant.lastname = u'Post'
        self.app['applicants'][self.putmecontainer.code].addApplicant(putmeapplicant)
        self.putmeapplication_number = putmeapplicant.application_number
        self.putmeapplicant = self.app['applicants'][self.putmecontainer.code][
            self.putmeapplication_number]
        self.putmeapplicant_path = ('http://localhost/app/applicants/putme%s/%s'
            % (session, self.putmeapplication_number))

        self.browser = Browser()
        self.browser.handleErrors = False

    def tearDown(self):
        super(ApplicantUITest, self).tearDown()
        shutil.rmtree(self.dc_root)
        clearSite()
        return

    def fill_correct_values(self):
        self.browser.getControl(name="form.reg_number").value = '1234'
        self.browser.getControl(name="form.firstname").value = 'John'
        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.lga").value = ['foreigner']
        self.browser.getControl(name="form.nationality").value = ['NG']
        self.browser.getControl(name="form.sex").value = ['m']
        self.browser.getControl(name="form.email").value = 'xx@yy.zz'

    def test_manage_and_view_bec_applicant(self):
        # Managers can manage bec applicants.
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.becapplicant_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("'O' Level" in self.browser.contents)
        self.browser.open(self.becapplicant_path + '/manage')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("'O' Level" in self.browser.contents)
        self.browser.open(self.becapplicant_path + '/edit')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("'O' Level" in self.browser.contents)
        self.browser.open(self.becapplicant_path + '/application_slip.pdf')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        return

    def test_manage_and_view_putme_applicant(self):
        # Managers can manage bec applicants.
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.putmeapplicant_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("'O' Level" in self.browser.contents)
        self.browser.open(self.putmeapplicant_path + '/manage')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("'O' Level" in self.browser.contents)
        self.browser.open(self.putmeapplicant_path + '/edit')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue("'O' Level" in self.browser.contents)
        self.browser.open(self.putmeapplicant_path + '/application_slip.pdf')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        return
