[7193] | 1 | ## $Id: test_configuration.py 17782 2024-05-14 07:27:45Z henrik $ |
---|
| 2 | ## |
---|
[6919] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
[7193] | 8 | ## |
---|
[6919] | 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
[7193] | 13 | ## |
---|
[6919] | 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """Tests for configuration containers and related. |
---|
| 19 | """ |
---|
[17782] | 20 | import os |
---|
| 21 | import shutil |
---|
| 22 | import tempfile |
---|
| 23 | import unittest |
---|
| 24 | from zope.component import queryUtility |
---|
| 25 | from zope.component.hooks import setSite, clearSite |
---|
[6919] | 26 | from zope.component.interfaces import IFactory |
---|
| 27 | from zope.interface import verify |
---|
[7811] | 28 | from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase |
---|
[17782] | 29 | from waeup.kofa.app import University |
---|
[7811] | 30 | from waeup.kofa.configuration import ( |
---|
[17782] | 31 | ConfigurationContainer, SessionConfiguration, SessionConfigurationFactory, |
---|
| 32 | ConfigurationContainerExporter, ConfigurationExporter) |
---|
| 33 | from waeup.kofa.interfaces import(ICSVExporter, |
---|
[6921] | 34 | IConfigurationContainer, ISessionConfiguration, ISessionConfigurationAdd) |
---|
[6919] | 35 | |
---|
| 36 | class ConfigurationTest(FunctionalTestCase): |
---|
| 37 | |
---|
| 38 | layer = FunctionalLayer |
---|
| 39 | |
---|
| 40 | def setUp(self): |
---|
| 41 | super(ConfigurationTest, self).setUp() |
---|
| 42 | self.confcontainer = ConfigurationContainer() |
---|
| 43 | self.sessionconf = SessionConfiguration() |
---|
| 44 | return |
---|
| 45 | |
---|
| 46 | def tearDown(self): |
---|
| 47 | super(ConfigurationTest, self).tearDown() |
---|
| 48 | return |
---|
| 49 | |
---|
| 50 | def test_interfaces(self): |
---|
| 51 | verify.verifyClass(IConfigurationContainer, ConfigurationContainer) |
---|
| 52 | verify.verifyObject(IConfigurationContainer, self.confcontainer) |
---|
| 53 | verify.verifyClass(ISessionConfiguration, SessionConfiguration) |
---|
| 54 | verify.verifyObject(ISessionConfiguration, self.sessionconf) |
---|
[6921] | 55 | verify.verifyClass(ISessionConfigurationAdd, SessionConfiguration) |
---|
| 56 | verify.verifyObject(ISessionConfigurationAdd, self.sessionconf) |
---|
[6919] | 57 | |
---|
| 58 | class SessionConfigurationFactoryTest(FunctionalTestCase): |
---|
| 59 | |
---|
| 60 | layer = FunctionalLayer |
---|
| 61 | |
---|
| 62 | def setUp(self): |
---|
| 63 | super(SessionConfigurationFactoryTest, self).setUp() |
---|
| 64 | self.factory = SessionConfigurationFactory() |
---|
| 65 | |
---|
| 66 | def tearDown(self): |
---|
| 67 | super(SessionConfigurationFactoryTest, self).tearDown() |
---|
| 68 | |
---|
| 69 | def test_interfaces(self): |
---|
| 70 | verify.verifyClass(IFactory, SessionConfigurationFactory) |
---|
| 71 | verify.verifyObject(IFactory, self.factory) |
---|
| 72 | |
---|
| 73 | def test_factory(self): |
---|
| 74 | obj = self.factory() |
---|
| 75 | assert isinstance(obj, SessionConfiguration) |
---|
| 76 | |
---|
| 77 | def test_getInterfaces(self): |
---|
| 78 | implemented_by = self.factory.getInterfaces() |
---|
| 79 | assert implemented_by.isOrExtends(ISessionConfiguration) |
---|
[17782] | 80 | |
---|
| 81 | |
---|
| 82 | class ConfigurationImportExportSetup(FunctionalTestCase): |
---|
| 83 | |
---|
| 84 | layer = FunctionalLayer |
---|
| 85 | |
---|
| 86 | def setUp(self): |
---|
| 87 | super(ConfigurationImportExportSetup, self).setUp() |
---|
| 88 | # Setup a sample site for each test |
---|
| 89 | app = University() |
---|
| 90 | self.dc_root = tempfile.mkdtemp() |
---|
| 91 | app['datacenter'].setStoragePath(self.dc_root) |
---|
| 92 | self.getRootFolder()['app'] = app |
---|
| 93 | self.app = self.getRootFolder()['app'] |
---|
| 94 | setSite(app) |
---|
| 95 | self.workdir = tempfile.mkdtemp() |
---|
| 96 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
| 97 | return |
---|
| 98 | |
---|
| 99 | def tearDown(self): |
---|
| 100 | super(ConfigurationImportExportSetup, self).tearDown() |
---|
| 101 | shutil.rmtree(self.workdir) |
---|
| 102 | shutil.rmtree(self.dc_root) |
---|
| 103 | clearSite() |
---|
| 104 | return |
---|
| 105 | |
---|
| 106 | class BaseConfigurationExportTests(ConfigurationImportExportSetup): |
---|
| 107 | |
---|
| 108 | def test_ifaces(self): |
---|
| 109 | # make sure we fullfill interface contracts |
---|
| 110 | obj = ConfigurationContainerExporter() |
---|
| 111 | verify.verifyObject(ICSVExporter, obj) |
---|
| 112 | verify.verifyClass(ICSVExporter, ConfigurationContainerExporter) |
---|
| 113 | return |
---|
| 114 | |
---|
| 115 | def test_get_as_utility(self): |
---|
| 116 | # we can get a faculty exporter as utility |
---|
| 117 | result = queryUtility(ICSVExporter, name="base_configuration") |
---|
| 118 | self.assertTrue(result is not None) |
---|
| 119 | return |
---|
| 120 | |
---|
| 121 | def test_export_all(self): |
---|
| 122 | exporter = ConfigurationContainerExporter() |
---|
| 123 | exporter.export_all(self.app, self.outfile) |
---|
| 124 | result = open(self.outfile, 'rb').read() |
---|
| 125 | self.assertTrue( |
---|
| 126 | 'acronym,captcha,carry_over,current_academic_session,email_admin,' |
---|
| 127 | 'email_subject,export_disabled_message,frontpage,frontpage_dict,' |
---|
| 128 | 'maintmode_enabled_by,name,name_admin,next_matric_integer,' |
---|
| 129 | 'next_matric_integer_2,next_matric_integer_3,' |
---|
| 130 | 'next_matric_integer_4,smtp_mailer,curr_stud_id\r\n' |
---|
| 131 | 'WAeUP.Kofa,No captcha,0,,contact@waeup.org,Kofa Contact,,' |
---|
| 132 | '"<h1>Welcome to WAeUP.Kofa\n<br>' in result |
---|
| 133 | ) |
---|
| 134 | return |
---|
| 135 | |
---|
| 136 | class SessionConfigurationExportTests(ConfigurationImportExportSetup): |
---|
| 137 | |
---|
| 138 | def test_ifaces(self): |
---|
| 139 | # make sure we fullfill interface contracts |
---|
| 140 | obj = ConfigurationExporter() |
---|
| 141 | verify.verifyObject(ICSVExporter, obj) |
---|
| 142 | verify.verifyClass(ICSVExporter, ConfigurationExporter) |
---|
| 143 | return |
---|
| 144 | |
---|
| 145 | def test_get_as_utility(self): |
---|
| 146 | # we can get a faculty exporter as utility |
---|
| 147 | result = queryUtility(ICSVExporter, name="configurations") |
---|
| 148 | self.assertTrue(result is not None) |
---|
| 149 | return |
---|
| 150 | |
---|
| 151 | def test_export_all(self): |
---|
| 152 | self.sessionconfig = SessionConfiguration() |
---|
| 153 | self.sessionconfig.academic_session = 2022 |
---|
| 154 | self.sessionconfig.clearance_fee = 3000.6 |
---|
| 155 | self.app['configuration'].addSessionConfiguration(self.sessionconfig) |
---|
| 156 | exporter = ConfigurationExporter() |
---|
| 157 | exporter.export_all(self.app, self.outfile) |
---|
| 158 | result = open(self.outfile, 'rb').read() |
---|
| 159 | self.assertEqual(result, |
---|
| 160 | 'academic_session,booking_fee,clearance_enabled,clearance_fee,' |
---|
| 161 | 'coursereg_deadline,late_registration_fee,maint_fee,' |
---|
| 162 | 'payment_disabled,transcript_fee,transfer_fee\r\n' |
---|
| 163 | '2022,0.0,0,3000.6,,0.0,0.0,[],0.0,0.0\r\n' |
---|
| 164 | ) |
---|
| 165 | return |
---|