## $Id: test_browser.py 11237 2014-02-22 14:11:26Z uli $
##
## Copyright (C) 2014 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
##
"""
Test the applicant-related UI components.
"""

import os
import pytz
import grok
from datetime import datetime, date, timedelta
from zope.event import notify
from StringIO import StringIO
from zope.component import createObject, getUtility
from waeup.kofa.applicants.container import ApplicantsContainer
from waeup.kofa.applicants.tests.test_browser import (
    ApplicantsFullSetup, container_name_1,
    )
from waeup.kofa.interfaces import (
    IExtFileStore, IFileStoreNameChooser)

from waeup.imostate.testing import FunctionalLayer

PH_LEN = 15911  # Length of placeholder file


class ApplicantUITests(ApplicantsFullSetup):
    # Tests for uploading/browsing the passport image of appplicants

    layer = FunctionalLayer

    def test_upload_image_by_student(self):
        self.login()
        self.browser.open(self.edit_path)
        # Create a pseudo image file and select it to be uploaded in form
        photo_content = 'A' * 1024 * 21  # A string of 21 KB size
        pseudo_image = StringIO(photo_content)
        ctrl = self.browser.getControl(name='form.passport')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
        self.browser.getControl("Save").click() # submit form
        # There is a correct <img> link included
        self.assertTrue(
            '<img src="passport.jpg" height="180px" />' in self.browser.contents)
        # We get a warning message
        self.assertTrue(
            'Uploaded image is too big' in self.browser.contents)
        # Browsing the image gives us the default image, not the
        # uploaded one.
        image_url = self.edit_path.replace('edit', 'passport.jpg')
        self.browser.open(image_url)
        self.assertEqual(
            self.browser.headers['content-type'], 'image/jpeg')
        self.assertEqual(len(self.browser.contents), PH_LEN)
        # There is really no file stored for the applicant
        img = getUtility(IExtFileStore).getFile(
            IFileStoreNameChooser(self.applicant).chooseName())
        self.assertTrue(img is None)

    def test_upload_extraform_by_student(self):
        self.login()
        self.browser.open(self.edit_path)
        # Create a pseudo file and select it to be uploaded in form
        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
        pseudo_file = StringIO(pdf_content)
        ctrl = self.browser.getControl(name='form.extraform')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_file, filename='myform.pdf')
        self.browser.getControl("Save").click() # submit form
        # We get a warning message
        self.assertTrue(
            'Uploaded file is too big' in self.browser.contents)
        # Create a pseudo file with acceptable size
        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
        pseudo_file = StringIO(pdf_content)
        ctrl = self.browser.getControl(name='form.extraform')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_file, filename='myform.pdf')
        self.browser.getControl("Save").click() # submit form
        # Even though the form could not be saved ...
        self.assertTrue('Required input is missing' in self.browser.contents)
        # ... the file has been successfully uploaded
        pdf_url = self.edit_path.replace('edit', 'extraform.pdf')
        self.browser.open(pdf_url)
        self.assertEqual(
            self.browser.headers['content-type'], 'application/pdf')
        self.assertEqual(len(self.browser.contents), 307200)
        # There is rally a file stored for the applicant
        storage = getUtility(IExtFileStore)
        file_id = IFileStoreNameChooser(self.applicant).chooseName(
            attr='extraform.pdf')
        # The stored file can be fetched
        fd = storage.getFile(file_id)
        file_len = len(fd.read())
        self.assertEqual(file_len, 307200)
        # A file link is displayed on the edit view ...
        self.browser.open(self.edit_path)
        self.assertTrue('<a href="extraform.pdf">' in self.browser.contents)
        # ... and on the dislay view
        self.browser.open(self.view_path)
        self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>'
            in self.browser.contents)

    def test_upload_extraform_by_manager(self):
        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
        self.browser.open(self.manage_path)
        # Create a pseudo file with acceptable size
        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
        pseudo_file = StringIO(pdf_content)
        ctrl = self.browser.getControl(name='form.extraform')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_file, filename='myform.pdf')
        self.browser.getControl("Save").click() # submit form
        # Even though the form could not be saved ...
        self.assertTrue('Required input is missing' in self.browser.contents)
        # ... the file has been successfully uploaded
        pdf_url = self.manage_path.replace('manage', 'extraform.pdf')
        self.browser.open(pdf_url)
        self.assertEqual(
            self.browser.headers['content-type'], 'application/pdf')
        self.assertEqual(len(self.browser.contents), 307200)
        # There is rally a file stored for the applicant
        storage = getUtility(IExtFileStore)
        file_id = IFileStoreNameChooser(self.applicant).chooseName(
            attr='extraform.pdf')
        # The stored file can be fetched
        fd = storage.getFile(file_id)
        file_len = len(fd.read())
        self.assertEqual(file_len, 307200)
        # A file link is displayed on the edit view ...
        self.browser.open(self.manage_path)
        self.assertTrue('<a href="extraform.pdf">' in self.browser.contents)
        # ... and on the dislay view
        self.browser.open(self.view_path)
        self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>'
            in self.browser.contents)
        # Adding file is properly logged
        logfile = os.path.join(
            self.app['datacenter'].storage, 'logs', 'applicants.log')
        logcontent = open(logfile).read()
        self.assertTrue(
            'zope.mgr - '
            'waeup.imostate.applicants.browser.CustomApplicantManageFormPage'
            ' - %s - saved: extraform'
            % (self.applicant.applicant_id)
            in logcontent)
        # When an applicant is removed, also the pdf files are gone.
        del self.app['applicants'][container_name_1][
            self.applicant.application_number]
        fd = storage.getFile(file_id)
        self.assertTrue(fd is None)

    def test_applicant_workflow(self):
        # Applicants can edit their record
        self.browser.open(self.login_path)
        self.login()
        self.assertTrue(
            'You logged in.' in self.browser.contents)
        self.browser.open(self.edit_path)
        self.assertTrue(self.browser.url != self.login_path)
        self.assertEqual(self.browser.headers['Status'], '200 Ok')
        self.fill_correct_values()
        self.browser.getControl(name="form.nationality").value = ['NG']
        self.browser.getControl("Save").click()
        self.assertTrue('Form has been saved' in self.browser.contents)
        self.assertTrue('started ' in self.browser.contents)

        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
        pseudo_file = StringIO(pdf_content)
        ctrl = self.browser.getControl(name='form.passport')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_file, filename='myphoto.jpg')
        ctrl = self.browser.getControl(name='form.extraform')
        file_ctrl = ctrl.mech_control
        file_ctrl.add_file(pseudo_file, filename='myform.pdf')

        self.browser.getControl(name="confirm_passport").value = True
        self.browser.getControl("Save").click()
        return
