| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | """ |
|---|
| 4 | PyLucid unittest |
|---|
| 5 | ~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | Test the API to the internal page files. |
|---|
| 8 | |
|---|
| 9 | TODO: |
|---|
| 10 | - Check .css and .js file, too. |
|---|
| 11 | - Check all rellay used internal pages (Parse all plugins) |
|---|
| 12 | |
|---|
| 13 | Last commit info: |
|---|
| 14 | ~~~~~~~~~~~~~~~~~ |
|---|
| 15 | $LastChangedDate$ |
|---|
| 16 | $Rev$ |
|---|
| 17 | $Author$ |
|---|
| 18 | |
|---|
| 19 | :copyleft: 2008 by the PyLucid team, see AUTHORS for more details. |
|---|
| 20 | :license: GNU GPL v3, see LICENSE.txt for more details. |
|---|
| 21 | """ |
|---|
| 22 | import os |
|---|
| 23 | |
|---|
| 24 | import tests |
|---|
| 25 | from tests.utils.FakeRequest import get_fake_context |
|---|
| 26 | |
|---|
| 27 | from django.conf import settings |
|---|
| 28 | |
|---|
| 29 | from PyLucid.system import internal_page |
|---|
| 30 | from PyLucid.models import Page |
|---|
| 31 | |
|---|
| 32 | # Open only one traceback in a browser (=True) ? |
|---|
| 33 | #ONE_BROWSER_TRACEBACK = False |
|---|
| 34 | ONE_BROWSER_TRACEBACK = True |
|---|
| 35 | |
|---|
| 36 | # A minimalistic template, how contains only needfull information |
|---|
| 37 | TEST_TEMPLATE = ( |
|---|
| 38 | "{% if messages %}" |
|---|
| 39 | "{% for message in messages %}{{ message }}\n{% endfor %}" |
|---|
| 40 | "{% endif %}" |
|---|
| 41 | "\n" |
|---|
| 42 | "{{ PAGE.content }}" |
|---|
| 43 | ) |
|---|
| 44 | |
|---|
| 45 | TEST_CONTENT = "This is a test content..." |
|---|
| 46 | |
|---|
| 47 | class InternalPageTest(tests.TestCase): |
|---|
| 48 | |
|---|
| 49 | one_browser_traceback = ONE_BROWSER_TRACEBACK |
|---|
| 50 | _open = [] |
|---|
| 51 | |
|---|
| 52 | def setUp(self): |
|---|
| 53 | self.fake_context = get_fake_context() |
|---|
| 54 | Page.objects.all().delete() # Delete all existins pages |
|---|
| 55 | |
|---|
| 56 | self.template = tests.create_template(content = TEST_TEMPLATE) |
|---|
| 57 | |
|---|
| 58 | # Create the test pages defined in content_test_utils.py |
|---|
| 59 | # assign the test template to all pages |
|---|
| 60 | tests.create_pages( |
|---|
| 61 | tests.TEST_PAGES, |
|---|
| 62 | template=self.template, |
|---|
| 63 | ) |
|---|
| 64 | |
|---|
| 65 | #-------------------------------------------------------------------------- |
|---|
| 66 | |
|---|
| 67 | def _get_filepath(self, internal_page_dir, plugin_name, internal_page_name, |
|---|
| 68 | slug): |
|---|
| 69 | return os.path.join( |
|---|
| 70 | settings.MEDIA_ROOT, |
|---|
| 71 | settings.PYLUCID_MEDIA_DIR, |
|---|
| 72 | internal_page_dir, |
|---|
| 73 | plugin_name, |
|---|
| 74 | internal_page_name + "." + slug |
|---|
| 75 | ) |
|---|
| 76 | |
|---|
| 77 | def _get_file(self, internal_page_dir, plugin_name, internal_page_name, |
|---|
| 78 | slug): |
|---|
| 79 | |
|---|
| 80 | file_path = self._get_filepath( |
|---|
| 81 | internal_page_dir, plugin_name, internal_page_name, slug |
|---|
| 82 | ) |
|---|
| 83 | content = file(file_path, "r").read() |
|---|
| 84 | content = content.decode(settings.FILE_CHARSET) |
|---|
| 85 | return content |
|---|
| 86 | |
|---|
| 87 | def _get_default_ipage(self, plugin_name, internal_page_name, slug): |
|---|
| 88 | """ |
|---|
| 89 | returns the file content of a file in the default internal page dir |
|---|
| 90 | """ |
|---|
| 91 | return self._get_file( |
|---|
| 92 | settings.INTERNAL_PAGE_DIR, |
|---|
| 93 | plugin_name, internal_page_name, slug |
|---|
| 94 | ) |
|---|
| 95 | |
|---|
| 96 | def _get_custom_ipage(self, plugin_name, internal_page_name, slug): |
|---|
| 97 | """ |
|---|
| 98 | returns the file content of a file in the custom internal page dir |
|---|
| 99 | """ |
|---|
| 100 | return self._get_file( |
|---|
| 101 | settings.CUSTOM_INTERNAL_PAGE_DIR, |
|---|
| 102 | plugin_name, internal_page_name, slug |
|---|
| 103 | ) |
|---|
| 104 | |
|---|
| 105 | #-------------------------------------------------------------------------- |
|---|
| 106 | |
|---|
| 107 | def test_get_internal_page(self): |
|---|
| 108 | """ |
|---|
| 109 | Test a existing internal page |
|---|
| 110 | """ |
|---|
| 111 | plugin_name = "auth" |
|---|
| 112 | internal_page_name = "input_username" |
|---|
| 113 | |
|---|
| 114 | is_content = internal_page.get_internal_page( |
|---|
| 115 | self.fake_context, plugin_name, internal_page_name |
|---|
| 116 | ) |
|---|
| 117 | must_content = self._get_default_ipage( |
|---|
| 118 | plugin_name, internal_page_name, "html" |
|---|
| 119 | ) |
|---|
| 120 | assert is_content == must_content |
|---|
| 121 | |
|---|
| 122 | def test_wrong_page_name(self): |
|---|
| 123 | """ |
|---|
| 124 | Test a wrong internal page name |
|---|
| 125 | """ |
|---|
| 126 | self.assertRaises( |
|---|
| 127 | internal_page.InternalPageNotFound, |
|---|
| 128 | internal_page.get_internal_page, |
|---|
| 129 | context = self.fake_context, |
|---|
| 130 | plugin_name = "auth", |
|---|
| 131 | internal_page_name = "not existing" |
|---|
| 132 | ) |
|---|
| 133 | |
|---|
| 134 | def test_wrong_plugin_name(self): |
|---|
| 135 | """ |
|---|
| 136 | Test a wrong plugin name |
|---|
| 137 | """ |
|---|
| 138 | self.assertRaises( |
|---|
| 139 | internal_page.InternalPageNotFound, |
|---|
| 140 | internal_page.get_internal_page, |
|---|
| 141 | context = self.fake_context, |
|---|
| 142 | plugin_name = "not existing", |
|---|
| 143 | internal_page_name = "not existing" |
|---|
| 144 | ) |
|---|
| 145 | |
|---|
| 146 | def test_custom_ipage(self): |
|---|
| 147 | """ |
|---|
| 148 | Create a custom internal page file and check if this page would be used |
|---|
| 149 | and not the default one. |
|---|
| 150 | |
|---|
| 151 | -create the custom path e.g.: ./media/PyLucid/custom/auth |
|---|
| 152 | -create the internal path file e.g.: ...custom/auth/input_username.html |
|---|
| 153 | -get the internal page and compare the content |
|---|
| 154 | -delete the test internal page file |
|---|
| 155 | -delete all created path parts |
|---|
| 156 | """ |
|---|
| 157 | plugin_name = "auth" |
|---|
| 158 | internal_page_name = "input_username" |
|---|
| 159 | |
|---|
| 160 | # For the test it may not exist an custom file, yet. |
|---|
| 161 | self.assertRaises( |
|---|
| 162 | IOError, |
|---|
| 163 | self._get_custom_ipage, |
|---|
| 164 | plugin_name, internal_page_name, "html" |
|---|
| 165 | ) |
|---|
| 166 | |
|---|
| 167 | # Check if the path e.g. ./media/PyLucid/custom/auth/ exists |
|---|
| 168 | # create and delete it, if is not exists |
|---|
| 169 | base_path = os.path.join( |
|---|
| 170 | settings.MEDIA_ROOT, |
|---|
| 171 | settings.PYLUCID_MEDIA_DIR, |
|---|
| 172 | settings.CUSTOM_INTERNAL_PAGE_DIR, |
|---|
| 173 | ) |
|---|
| 174 | custom_path = os.path.join(base_path, plugin_name) |
|---|
| 175 | paths = [ |
|---|
| 176 | [base_path, False], [custom_path, False] |
|---|
| 177 | ] |
|---|
| 178 | # Create all path parts, which doesn't exists yet. |
|---|
| 179 | # Remember the created directories for later remove. |
|---|
| 180 | for path in paths: |
|---|
| 181 | if os.path.isdir(path[0]): |
|---|
| 182 | path[1] = True |
|---|
| 183 | else: |
|---|
| 184 | os.mkdir(path[0]) |
|---|
| 185 | |
|---|
| 186 | filepath = self._get_filepath( |
|---|
| 187 | settings.CUSTOM_INTERNAL_PAGE_DIR, |
|---|
| 188 | plugin_name, internal_page_name, "html" |
|---|
| 189 | ) |
|---|
| 190 | try: |
|---|
| 191 | # Create the custom internal page |
|---|
| 192 | f = file(filepath, "w") |
|---|
| 193 | f.write(TEST_CONTENT) |
|---|
| 194 | f.close() |
|---|
| 195 | |
|---|
| 196 | # Get the content via the productive used function |
|---|
| 197 | is_content = internal_page.get_internal_page( |
|---|
| 198 | self.fake_context, plugin_name, internal_page_name |
|---|
| 199 | ) |
|---|
| 200 | # Check if the content is the test content and not the default |
|---|
| 201 | # internal page content |
|---|
| 202 | assert is_content == TEST_CONTENT |
|---|
| 203 | finally: |
|---|
| 204 | try: |
|---|
| 205 | # remove the test file |
|---|
| 206 | os.remove(filepath) |
|---|
| 207 | finally: |
|---|
| 208 | # Remove all dir parts, how doesn't exists before the test |
|---|
| 209 | paths.reverse() |
|---|
| 210 | for path, exists in paths: |
|---|
| 211 | if exists == False: |
|---|
| 212 | os.rmdir(path) |
|---|
| 213 | |
|---|
| 214 | #-------------------------------------------------------------------------- |
|---|
| 215 | |
|---|
| 216 | def test_real_request(self): |
|---|
| 217 | """ |
|---|
| 218 | Test a login request, who used a internal page. |
|---|
| 219 | """ |
|---|
| 220 | response = self.client.get("/_command/1/auth/login/") |
|---|
| 221 | content = response.content |
|---|
| 222 | |
|---|
| 223 | self.failUnlessEqual(response.status_code, 200) |
|---|
| 224 | self.assertResponse(response, |
|---|
| 225 | must_contain=( |
|---|
| 226 | "PyLucidPlugins auth", "auth_login", |
|---|
| 227 | "Username:", "sha_button", "plaintext_button" |
|---|
| 228 | ) |
|---|
| 229 | ) |
|---|
| 230 | |
|---|
| 231 | def test_not_existing(self): |
|---|
| 232 | """ |
|---|
| 233 | Test a request to a _command url with a not existing internal page. |
|---|
| 234 | |
|---|
| 235 | -rename a internal page for the login (/auth/input_username.html) |
|---|
| 236 | -request the login page |
|---|
| 237 | -check the error messages in the response |
|---|
| 238 | """ |
|---|
| 239 | plugin_name = "auth" |
|---|
| 240 | internal_page_name = "input_username" |
|---|
| 241 | |
|---|
| 242 | filepath = self._get_filepath( |
|---|
| 243 | settings.INTERNAL_PAGE_DIR, plugin_name, internal_page_name, "html" |
|---|
| 244 | ) |
|---|
| 245 | assert os.path.isfile(filepath) |
|---|
| 246 | try: |
|---|
| 247 | # rename the orignal internal page file |
|---|
| 248 | os.rename(filepath, filepath + "_unittest") |
|---|
| 249 | assert not os.path.isfile(filepath) |
|---|
| 250 | |
|---|
| 251 | response = self.client.get("/_command/1/auth/login/") |
|---|
| 252 | self.assertResponse(response, |
|---|
| 253 | must_contain=( |
|---|
| 254 | "Internal page 'input_username' not found!", |
|---|
| 255 | ), |
|---|
| 256 | must_not_contain=("Run plugin auth.login Error",) |
|---|
| 257 | ) |
|---|
| 258 | finally: |
|---|
| 259 | # rename back |
|---|
| 260 | os.rename(filepath + "_unittest", filepath) |
|---|
| 261 | assert os.path.isfile(filepath) |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | if __name__ == "__main__": |
|---|
| 266 | # Run this unitest directly |
|---|
| 267 | import os |
|---|
| 268 | os.chdir("../") |
|---|
| 269 | filename = os.path.splitext(os.path.basename(__file__))[0] |
|---|
| 270 | tests.run_tests(test_labels=[filename]) |
|---|