## $Id: tests.py 8020 2012-04-02 11:05:40Z 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 os
import shutil
import tempfile
from zope.interface.verify import verifyClass, verifyObject
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.uniben.testing import FunctionalLayer
from waeup.kofa.interfaces import IBatchProcessor

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 an two different applicants containers
        self.pgcontainer = ApplicantsContainer()
        self.pgcontainer.code = u'pg2011'
        self.pgcontainer.prefix = u'pg'
        self.app['applicants']['pg2011'] = self.pgcontainer
        self.ugcontainer = ApplicantsContainer()
        self.ugcontainer.code = u'app2011'
        self.ugcontainer.prefix = u'app'
        self.app['applicants']['app2011'] = self.ugcontainer

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

        # Add (customized) applicants
        pgapplicant = createObject(u'waeup.Applicant')
        pgapplicant.firstname = u'Anna'
        pgapplicant.lastname = u'Post'
        self.app['applicants']['pg2011'].addApplicant(pgapplicant)
        self.pgapplication_number = pgapplicant.application_number
        self.pgapplicant = self.app['applicants']['pg2011'][
            self.pgapplication_number]

        ugapplicant = createObject(u'waeup.Applicant')
        ugapplicant.firstname = u'Klaus'
        ugapplicant.lastname = u'Under'
        self.app['applicants']['app2011'].addApplicant(ugapplicant)
        self.ugapplication_number = ugapplicant.application_number
        self.ugapplicant = self.app['applicants']['app2011'][
            self.ugapplication_number]

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

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

    def test_manage_and_view_applicant(self):
        # Managers can manage pg applicants
        pgapplicant_path = ('http://localhost/app/applicants/pg2011/%s'
            % self.pgapplication_number)
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        # The IPGApplicant interface is really used in all pages
        self.browser.open(pgapplicant_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue('Employer' in self.browser.contents)
        self.browser.open(pgapplicant_path + '/manage')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue('Employer' in self.browser.contents)
        self.browser.open(pgapplicant_path + '/edit')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertTrue('Employer' in self.browser.contents)
        self.browser.open(pgapplicant_path + '/application_slip.pdf')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        # If we lool at the applicant in the ug container,
        # the employer field doesn't appear
        ugapplicant_path = ('http://localhost/app/applicants/app2011/%s'
            % self.ugapplication_number)
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(ugapplicant_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertFalse('Employer' in self.browser.contents)
        self.browser.open(ugapplicant_path + '/manage')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertFalse('Employer' in self.browser.contents)
        self.browser.open(ugapplicant_path + '/edit')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertFalse('Employer' in self.browser.contents)
        self.browser.open(ugapplicant_path + '/application_slip.pdf')
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        return