""" We are using unittest for troggle. Note that the database has not been parsed from the source files when these tests are run, so any path that relies on data being in the database will fail. The simple redirections to files which exist, e.g. in /expoweb/ /expofiles/ /expofiles/documents/ etc. will test fine. But paths like this: /survey_scans/ /caves/ which rely on database resolution will fail unless a fixture has been set up for them. https://docs.djangoproject.com/en/dev/topics/testing/tools/ """ todo = """ADD TESTS when we are redirecting /expofiles/ to a remote file-delivering site - Add test for running cavern to produce a .3d file """ import re from http import HTTPStatus from django.test import Client, TestCase # class SimplePageTest(unittest.TestCase): class PageTests(TestCase): """These tests may appear to be redundant, but in fact they exercise different bits of code. The urls.py dispatcher is sending these URLs view via different 'view' handlers, and they all need verifying. """ @classmethod def setUpTestData(cls): # Set up data for the whole TestCase # cls.foo = Foo.objects.create(bar="Test") # Some test using self.foo in tests below.. # read in some SQL ? pass def setUp(self): # Every test needs a client. self.client = Client() def test_expoweb_root(self): response = self.client.get("") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) ph = r"CUCC in Austria" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_root_slash(self): response = self.client.get("/") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) ph = r"CUCC in Austria" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_paths(self): response = self.client.get("/pathsreport") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"This report is generated from" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_dir(self): response = self.client.get("/handbook") response.content.decode() self.assertEqual(response.status_code, HTTPStatus.FOUND) # 302 directory, so redirects to /index.htm def test_expoweb_dirslash(self): response = self.client.get("/handbook/") response.content.decode() self.assertEqual(response.status_code, HTTPStatus.FOUND) # 302 directory, so redirects to /index.htm def test_expoweb_dir_no_index(self): response = self.client.get("/handbook/troggle") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND) ph = r"Page not found handbook/troggle/index.html" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_dir_with_index_htm(self): response = self.client.get("/years/1999/index.htm") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) # directory, so redirects to /index.htm ph = r"Passage descriptions for 1999" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_dir_with_index_html(self): response = self.client.get("/years/2015/index.html") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) # directory, so redirects to /index.htm ph = r"Things left at top camp 2014" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_dir_with_index2(self): response = self.client.get("/handbook/index.htm") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) ph = r"Introduction to expo" phmatch = re.search(ph, content) # print("\n ! - test_expoweb_dir_with_index2\n{}\n{}".format(response.reason_phrase, content)) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_htm(self): response = self.client.get("/handbook/index.htm") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) ph = r"Introduction to expo" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_notfound(self): response = self.client.get("/handbook/_test_zyxxypqrqx.html") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND) ph = r"