## $Id: test_browser.py 10192 2013-05-22 12:48: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 os
import shutil
import tempfile
from StringIO import StringIO
from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
from zope.component.hooks import setSite, clearSite
from zope.component import getUtility, createObject
from zope.interface import verify
from waeup.kofa.app import University
from waeup.kofa.students.tests.test_browser import (
    StudentsFullSetup, SAMPLE_IMAGE)
from waeup.kofa.students.accommodation import BedTicket
from waeup.kofa.testing import FunctionalTestCase
from waeup.kofa.interfaces import (
    IExtFileStore, IFileStoreNameChooser)
from waeup.kofa.students.interfaces import IStudentsUtils
from waeup.aaua.testing import FunctionalLayer


class StudentProcessorTest(FunctionalTestCase):
    """Perform some batching tests.
    """

    layer = FunctionalLayer

    def setUp(self):
        super(StudentProcessorTest, 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)


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

class StudentUITests(StudentsFullSetup):
    """Tests for customized student class views and pages
    """

    layer = FunctionalLayer

    def setUp(self):
        super(StudentUITests, self).setUp()

        bedticket = BedTicket()
        bedticket.booking_session = 2004
        bedticket.bed_type = u'any bed type'
        bedticket.bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
        bedticket.bed_coordinates = u'My bed coordinates'
        self.student['accommodation'].addBedTicket(bedticket)

    def test_student_start_clearance(self):
        self.browser.open(self.login_path)
        self.browser.getControl(name="form.login").value = self.student_id
        self.browser.getControl(name="form.password").value = 'spwd'
        self.browser.getControl("Login").click()

        IWorkflowInfo(self.student).fireTransition('admit')
        self.browser.open(self.student_path + '/change_portrait')
        image = open(SAMPLE_IMAGE, 'rb')
        ctrl = self.browser.getControl(name='passportuploadedit')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(image, filename='my_photo.jpg')
        self.browser.getControl(
            name='upload_passportuploadedit').click()
        self.browser.open(self.student_path + '/start_clearance')
        # In AAUA the students can just start clearance without entering
        # an activation code.
        self.browser.getControl("Start clearance now").click()
        self.assertMatches('...Clearance process has been started...',
                           self.browser.contents)

    def test_next_session_allowed(self):
        # Let's see if next_session_allowed works as expected
        # A, ug_ft, 100
        IWorkflowState(self.student).setState('returning')
        self.assertTrue(self.student['studycourse'].next_session_allowed)
        # Uniben special PG programmes have the same workflow
        # as UG students
        self.certificate.study_mode = 'special_pg'
        self.assertTrue(self.student['studycourse'].next_session_allowed)
        IWorkflowState(self.student).setState('school fee paid')
        self.assertFalse(self.student['studycourse'].next_session_allowed)
        # Now we convert the certificate into a 'regular
        # postgraduate certificate ...
        self.certificate.study_mode = 'pg_xx'
        # ... and voila next session registration is allowed
        self.assertTrue(self.student['studycourse'].next_session_allowed)

    def test_set_payment_details(self):
        self.certificate.school_fee_1 = 100.0
        self.certificate.school_fee_2 = 200.0
        utils = getUtility(IStudentsUtils)

        IWorkflowState(self.student).setState('cleared')
        self.certificate.study_mode = 'ug_ft'
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(payment.p_level, 100)
        self.assertEqual(payment.p_session, 2004)
        self.assertEqual(payment.amount_auth, 100.0)
        self.assertEqual(payment.p_item, u'CERT1')
        self.assertEqual(error, None)

        IWorkflowState(self.student).setState('returning')
        configuration = createObject('waeup.SessionConfiguration')
        configuration.academic_session = 2005
        self.app['configuration'].addSessionConfiguration(configuration)
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(payment.p_level, 200)
        self.assertEqual(payment.p_session, 2005)
        self.assertEqual(payment.amount_auth, 200.0)
        self.assertEqual(payment.p_item, u'CERT1')
        self.assertEqual(error, None)

        error, payment = utils.setPaymentDetails('schoolfee_1',self.student)
        self.assertEqual(error, u'Payment by instalments not allowed.')
        self.certificate.ratio = 0.0
        error, payment = utils.setPaymentDetails('schoolfee_1',self.student)
        self.assertEqual(error, u'Payment by instalments not allowed.')
        error, payment = utils.setPaymentDetails('schoolfee_2',self.student)
        self.assertEqual(error, u'Payment by instalments not allowed.')
        self.certificate.ratio = 0.4
        error, payment = utils.setPaymentDetails('schoolfee',self.student)
        self.assertEqual(error, u'Payment by instalments required.')
        error, payment = utils.setPaymentDetails('schoolfee_1',self.student)
        self.assertEqual(payment.amount_auth, 80.0)
        IWorkflowState(self.student).setState('cleared')
        error, payment = utils.setPaymentDetails('schoolfee_1',self.student)
        self.assertEqual(payment.amount_auth, 40.0)

        # schoolfee_2 payment is state-independent
        self.student['studycourse'].entry_session = 2003
        error, payment = utils.setPaymentDetails('schoolfee_2',self.student)
        self.assertEqual(payment.amount_auth, 120.0)
        self.student['studycourse'].entry_session = 2004
        error, payment = utils.setPaymentDetails('schoolfee_2',self.student)
        self.assertEqual(payment.amount_auth, 60.0)

        # Returning students with entry session 2005-2007
        # and study mode ug_pt get a reduction of 20000
        self.certificate.study_mode = 'ug_pt'
        IWorkflowState(self.student).setState('returning')
        self.student['studycourse'].entry_session = 2005
        error, payment = utils.setPaymentDetails('schoolfee_1',self.student)
        self.assertEqual(payment.amount_auth, -19920.0)
