Ignore:
Timestamp:
26 Jun 2011, 09:26:53 (13 years ago)
Author:
Henrik Bettermann
Message:

Increase coverage from 58% to 83%.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py

    r6428 r6479  
    55## $Id$
    66##
    7 ## Copyright (C) 2011 Uli Fouquet
     7## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
    88## This program is free software; you can redistribute it and/or modify
    99## it under the terms of the GNU General Public License as published by
     
    3535from waeup.sirp.app import University
    3636from waeup.sirp.applicants.container import ApplicantsContainer
    37 
    38 class LoginTest(FunctionalTestCase):
    39     # Here we check login view of applicants containers.
    40     #
    41     # Tests in here do only cover login attempts without any PINs
    42     # created before.
     37from waeup.sirp.applicants.applicants import Applicant
     38
     39
     40class ApplicantsUITests(FunctionalTestCase):
     41    # Tests for ApplicantsRoot class
    4342
    4443    layer = FunctionalLayer
    4544
    4645    def setUp(self):
    47         super(LoginTest, self).setUp()
     46        super(ApplicantsUITests, self).setUp()
    4847
    4948        # Setup a sample site for each test
    5049        app = University()
    5150        self.dc_root = tempfile.mkdtemp()
    52         app['datacenter'].setStoragePath(self.dc_root)
    53         self.login_path = 'http://localhost/app/applicants/testapplicants/login'
    54 
    55         # Add an applicants container where we can login (or not)
     51        self.root_path = 'http://localhost/app/applicants/'
     52        self.container_path = 'http://localhost/app/applicants/app2009/'
     53
     54        # Add an applicants container
    5655        applicantscontainer = ApplicantsContainer()
    5756        applicantscontainer.ac_prefix = 'APP'
    58         app['applicants']['testapplicants'] = applicantscontainer
     57        applicantscontainer.prefix = 'app'
     58        applicantscontainer.year = 2009
     59        applicantscontainer.application_category = 'basic'
     60        app['applicants']['app2009'] = applicantscontainer
    5961
    6062        # Put the prepopulated site into test ZODB and prepare test
     
    6365        self.browser = Browser()
    6466        self.browser.handleErrors = False
    65 
    66     def tearDown(self):
    67         super(LoginTest, self).tearDown()
    68         shutil.rmtree(self.dc_root)
    69 
    70     def test_anonymous_access(self):
    71         # Anonymous users can access a login page
    72         self.browser.open(self.login_path)
    73         self.assertEqual(self.browser.headers['Status'], '200 Ok')
    74         return
    75 
    76     def test_anonymous_invalid_creds(self):
    77         # Anonymous users giving invalid credentials stay at the page
    78         self.browser.open(self.login_path)
    79         # We do not give credentials but send the form as-is
    80         submit = self.browser.getControl(name='SUBMIT')
    81         submit.click()
    82         # We are still at the same page...
    83         self.assertEqual(self.browser.url, self.login_path)
    84         self.assertEqual(self.browser.headers['Status'], '200 Ok')
    85         return
    86 
    87     def test_anonymous_invalid_creds_warning(self):
    88         # Entering wrong credentials will yield a warning
    89         self.browser.open(self.login_path)
    90         # We do not give credentials but send the form as-is
    91         submit = self.browser.getControl(name='SUBMIT')
    92         submit.click()
    93         self.assertTrue(
    94             'Entered credentials are invalid' in self.browser.contents)
    95         return
    96 
    97     def test_manager_no_warnings(self):
    98         # Browsing the login screen as a manager, won't raise warnings
    99         # Authenticate ourself as manager
    100         self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
    101         self.browser.open(self.login_path)
    102         # Submit the form w/o any credentials
    103         self.browser.getControl(name="SUBMIT").click()
    104         self.assertTrue(
    105             'Entered credentials are invalid' not in self.browser.contents)
    106         return
    107 
    108     def test_manager_no_redirect(self):
    109         # Browsing the login screen as a manager won't trigger a redirect
    110         # Authenticate ourself as manager
    111         self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
    112         self.browser.open(self.login_path)
    113         # Submit the form w/o any credentials
    114         self.browser.getControl(name="SUBMIT").click()
    115         self.assertEqual(self.browser.url, self.login_path)
    116         return
    117 
    118     def test_display_entered_values(self):
    119         # After submit the entered values are displayed in the form
    120         self.browser.open(self.login_path)
    121         # Enter some value we can look for after submit
    122         ac_series = self.browser.getControl(name="form.ac_series")
    123         ac_series.value = '666'
    124         self.browser.getControl(name="SUBMIT").click()
    125         self.assertTrue('666' in self.browser.contents)
    126         return
    127 
    128 class LoginTestWithPINs(LoginTest):
    129     # Here we check login view of applicants containers with PINs provided.
    130 
    131     # As setting up pins is time-consuming we only set them up when
    132     # really needed (i.e. in this separate TestCase).
    133 
    134     layer = FunctionalLayer
    135 
    136     def setUp(self):
    137         super(LoginTestWithPINs, self).setUp()
    13867
    13968        # Create 5 access codes with prefix'FOO' and cost 9.99 each
     
    14877        self.browser.handleErrors = False
    14978
     79        # Add an applicant
     80        applicant = Applicant()
     81        self.pin_applicant = unicode(self.pins[1])
     82        applicant.access_code = self.pin_applicant
     83        app['applicants']['app2009'][self.pin_applicant] = applicant
     84
     85    def tearDown(self):
     86        super(ApplicantsUITests, self).tearDown()
     87        shutil.rmtree(self.dc_root)
     88
     89    def test_anonymous_access(self):
     90        # Anonymous users can access applicants root and applicants containers
     91        self.browser.open(self.root_path)
     92        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     93        self.assertFalse(
     94            'Manage' in self.browser.contents)
     95        self.browser.open(self.container_path)
     96        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     97        self.assertFalse(
     98            'Manage' in self.browser.contents)
     99        return
     100
     101    def test_manage_access(self):
     102        # Managers can access the manage pages of applicants root and
     103        # applicants containers
     104        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     105        self.browser.open(self.root_path)
     106        self.assertTrue(
     107            'Manage application section' in self.browser.contents)
     108        self.manage_root_path = self.root_path + '/@@manage'
     109        self.manage_container_path = self.container_path + '/@@manage'
     110        self.browser.open(self.manage_root_path)
     111        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     112        self.assertEqual(self.browser.url, self.manage_root_path)
     113        self.browser.open(self.manage_container_path)
     114        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     115        self.assertEqual(self.browser.url, self.manage_container_path)
     116        return
     117
     118    def test_add_delete_container(self):
     119        # Managers can add and delete applicants containers
     120        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     121        self.browser.open(self.root_path)
     122        self.add_container_path = self.root_path + '/@@add'
     123        self.browser.open(self.add_container_path)
     124        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     125        self.assertEqual(self.browser.url, self.add_container_path)
     126        self.browser.getControl(name="form.prefix").value = ['app']
     127        self.browser.getControl("Add applicants container").click()
     128        self.assertTrue(
     129            'There were errors' in self.browser.contents)
     130        self.browser.getControl(name="form.prefix").value = ['app']
     131        self.browser.getControl(name="form.year").value = ['2010']
     132        self.browser.getControl(name="form.provider").value = ['waeup.sirp.applicants.ApplicantsContainer']
     133        self.browser.getControl(name="form.ac_prefix").value = ['APP']
     134        self.browser.getControl(name="form.application_category").value = ['basic']
     135        self.browser.getControl("Add applicants container").click()
     136        self.assertTrue('Added:' in self.browser.contents)
     137        ctrl = self.browser.getControl(name='val_id')
     138        ctrl.getControl(value='app2010').selected = True
     139        self.browser.getControl("Remove selected", index=0).click()
     140        self.assertTrue('Successfully removed:' in self.browser.contents)
     141        return
     142
     143    def test_add_delete_applicants(self):
     144        # Managers can add and delete applicants
     145        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     146        self.add_applicant_path = self.container_path + '/addapplicant'
     147        self.container_manage_path = self.container_path + '/@@manage'
     148        self.browser.open(self.add_applicant_path)
     149        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     150        self.assertEqual(self.browser.url, self.add_applicant_path)
     151        self.browser.getControl(name="form.ac_series").value = self.existing_series
     152        self.browser.getControl(name="form.ac_number").value = self.existing_number
     153        self.browser.getControl("Create application record").click()
     154        self.assertTrue('Application initialized' in self.browser.contents)
     155        self.browser.open(self.container_manage_path)
     156        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     157        ctrl = self.browser.getControl(name='val_id')
     158        ctrl.getControl(value=self.existing_pin).selected = True
     159        self.browser.getControl("Remove selected", index=0).click()
     160        self.assertTrue('Successfully removed:' in self.browser.contents)
     161        return
     162
     163    def test_manage_applicant(self):
     164        # Managers can manage applicants
     165        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     166        self.applicant_path = 'http://localhost/app/applicants/app2009/%s' % self.pin_applicant
     167        self.applicant_view_path = self.applicant_path + '/index'
     168        self.applicant_manage_path = self.applicant_path + '/edit_full'
     169        self.applicant_slip_path = self.applicant_path + '/application_slip.pdf'
     170        self.browser.open(self.applicant_view_path)
     171        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     172        self.browser.open(self.applicant_slip_path)
     173        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     174        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
     175        self.browser.open(self.applicant_manage_path)
     176        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     177        self.browser.getControl(name="form.firstname").value = 'Jonathan'
     178        self.browser.getControl("Save").click()
     179        self.assertTrue('Required input is missing' in self.browser.contents)
     180        return
     181
     182class LoginTest(FunctionalTestCase):
     183    # Here we check login view of applicants containers.
     184    #
     185    # Tests in here do only cover login attempts without any PINs
     186    # created before.
     187
     188    layer = FunctionalLayer
     189
     190    def setUp(self):
     191        super(LoginTest, self).setUp()
     192
     193        # Setup a sample site for each test
     194        app = University()
     195        self.dc_root = tempfile.mkdtemp()
     196        app['datacenter'].setStoragePath(self.dc_root)
     197        self.login_path = 'http://localhost/app/applicants/testapplicants/login'
     198
     199        # Add an applicants container where we can login (or not)
     200        applicantscontainer = ApplicantsContainer()
     201        applicantscontainer.ac_prefix = 'APP'
     202        app['applicants']['testapplicants'] = applicantscontainer
     203
     204        # Put the prepopulated site into test ZODB and prepare test
     205        # browser
     206        self.getRootFolder()['app'] = app
     207        self.browser = Browser()
     208        self.browser.handleErrors = False
     209
     210    def tearDown(self):
     211        super(LoginTest, self).tearDown()
     212        shutil.rmtree(self.dc_root)
     213
     214    def test_anonymous_access(self):
     215        # Anonymous users can access a login page
     216        self.browser.open(self.login_path)
     217        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     218        return
     219
     220    def test_anonymous_invalid_creds(self):
     221        # Anonymous users giving invalid credentials stay at the page
     222        self.browser.open(self.login_path)
     223        # We do not give credentials but send the form as-is
     224        submit = self.browser.getControl(name='SUBMIT')
     225        submit.click()
     226        # We are still at the same page...
     227        self.assertEqual(self.browser.url, self.login_path)
     228        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     229        return
     230
     231    def test_anonymous_invalid_creds_warning(self):
     232        # Entering wrong credentials will yield a warning
     233        self.browser.open(self.login_path)
     234        # We do not give credentials but send the form as-is
     235        submit = self.browser.getControl(name='SUBMIT')
     236        submit.click()
     237        self.assertTrue(
     238            'Entered credentials are invalid' in self.browser.contents)
     239        return
     240
     241    def test_manager_no_warnings(self):
     242        # Browsing the login screen as a manager, won't raise warnings
     243        # Authenticate ourself as manager
     244        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     245        self.browser.open(self.login_path)
     246        # Submit the form w/o any credentials
     247        self.browser.getControl(name="SUBMIT").click()
     248        self.assertTrue(
     249            'Entered credentials are invalid' not in self.browser.contents)
     250        return
     251
     252    def test_manager_no_redirect(self):
     253        # Browsing the login screen as a manager won't trigger a redirect
     254        # Authenticate ourself as manager
     255        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     256        self.browser.open(self.login_path)
     257        # Submit the form w/o any credentials
     258        self.browser.getControl(name="SUBMIT").click()
     259        self.assertEqual(self.browser.url, self.login_path)
     260        return
     261
     262    def test_display_entered_values(self):
     263        # After submit the entered values are displayed in the form
     264        self.browser.open(self.login_path)
     265        # Enter some value we can look for after submit
     266        ac_series = self.browser.getControl(name="form.ac_series")
     267        ac_series.value = '666'
     268        self.browser.getControl(name="SUBMIT").click()
     269        self.assertTrue('666' in self.browser.contents)
     270        return
     271
     272class LoginTestWithPINs(LoginTest):
     273    # Here we check login view of applicants containers with PINs provided.
     274
     275    # As setting up pins is time-consuming we only set them up when
     276    # really needed (i.e. in this separate TestCase).
     277
     278    layer = FunctionalLayer
     279
     280    def setUp(self):
     281        super(LoginTestWithPINs, self).setUp()
     282
     283        # Create 5 access codes with prefix'FOO' and cost 9.99 each
     284        pin_container = self.getRootFolder()['app']['accesscodes']
     285        pin_container.createBatch(
     286            datetime.now(), 'some_userid', 'APP', 9.99, 5)
     287        pins = pin_container[pin_container.keys()[0]].values()
     288        self.pins = [x.representation for x in pins]
     289        self.existing_pin = self.pins[0]
     290        parts = self.existing_pin.split('-')[1:]
     291        self.existing_series, self.existing_number = parts
     292        self.browser.handleErrors = False
     293
    150294    def tearDown(self):
    151295        super(LoginTestWithPINs, self).tearDown()
     
    184328    suite = unittest.TestSuite()
    185329    for testcase in [
     330        ApplicantsUITests,
    186331        LoginTest,
    187332        LoginTestWithPINs,
Note: See TracChangeset for help on using the changeset viewer.