## $Id: test_applicantcopier.py 7421 2011-12-21 11:00:19Z 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
##
"""
Tests for the creation of student containers with data from admitted applicants.
"""
import os
from hurry.workflow.interfaces import IWorkflowInfo
from zope.component import getUtility
from waeup.sirp.testing import FunctionalLayer
from waeup.sirp.interfaces import IExtFileStore, IFileStoreNameChooser
from waeup.sirp.applicants.tests.test_browser import ApplicantsFullSetup

class ApplicantCopierFunctionalTests(ApplicantsFullSetup):

    layer = FunctionalLayer

    def setUp(self):
        super(ApplicantCopierFunctionalTests, self).setUp()
        return

    def test_copier(self):
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.manage_path)
        self.fill_correct_values()
        self.browser.getControl("Save").click()
        # Upload a passport picture
        ctrl = self.browser.getControl(name='form.passport')
        file_obj = open(
            os.path.join(os.path.dirname(__file__), 'test_image.jpg'),'rb')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(file_obj, filename='my_photo.jpg')
        self.browser.getControl("Save").click() # submit form
        storage = getUtility(IExtFileStore)
        file_id = IFileStoreNameChooser(self.applicant).chooseName()
        # The stored image can be fetched
        fd = storage.getFile(file_id)
        file_len_orig = len(fd.read())
        # Let's try to create the student
        (success, msg) = self.applicant.createStudent()
        self.assertTrue(msg == 'Applicant has not yet been admitted.')
        IWorkflowInfo(self.applicant).fireTransition('start')
        IWorkflowInfo(self.applicant).fireTransition('pay')
        IWorkflowInfo(self.applicant).fireTransition('submit')
        IWorkflowInfo(self.applicant).fireTransition('admit')
        (success, msg) = self.applicant.createStudent()
        self.assertTrue(msg == 'No course admitted provided.')
        self.browser.open(self.manage_path)
        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
        self.browser.getControl("Save").click()
        (success, msg) = self.applicant.createStudent()
        self.assertTrue('created' in msg)
        student_id = msg.split()[1]
        # View student container just created
        student = self.app['students'][student_id]
        student_path = 'http://localhost/app/students/%s' % student_id
        self.browser.open(student_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertEqual(self.browser.url, student_path)
        # Check if passport image has been copied
        storage = getUtility(IExtFileStore)
        file_id = IFileStoreNameChooser(
            student).chooseName(attr='passport.jpg')
        fd = storage.getFile(file_id)
        file_len = len(fd.read())
        self.assertEqual(file_len_orig, file_len)
        # Check if application slip exists and is a PDF file
        file_id = IFileStoreNameChooser(
            student).chooseName(attr='application_slip.pdf')
        pdf = storage.getFile(file_id).read()
        self.assertTrue(len(pdf) > 0)
        self.assertEqual(pdf[:8], '%PDF-1.4')
        # Check if there is an application slip link in UI
        self.assertTrue('Application Slip' in self.browser.contents)
        self.browser.getLink("Application Slip").click()
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.assertEqual(self.browser.headers['Content-Type'],
                         'application/pdf')
        # Has the student been properly indexed?
        # Yes, we can find the student in department
        self.browser.open('http://localhost/app/students')
        self.browser.getControl(name="searchtype").value = ['depcode']
        self.browser.getControl(name="searchterm").value = 'dep1'
        self.browser.getControl("Search").click()
        self.assertMatches('...John Tester...', self.browser.contents)

    def test_copier_wo_passport(self):
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.manage_path)
        self.fill_correct_values()
        # Let's try to create the student
        IWorkflowInfo(self.applicant).fireTransition('start')
        IWorkflowInfo(self.applicant).fireTransition('pay')
        IWorkflowInfo(self.applicant).fireTransition('submit')
        IWorkflowInfo(self.applicant).fireTransition('admit')
        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
        self.browser.getControl("Save").click()
        (success, msg) = self.applicant.createStudent()
        self.assertTrue('created' in msg)
