""" 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/ /photos/ 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"

Page not found" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_no_dir(self): # slash where there should not be one response = self.client.get("/handbook/_test_zyxxypqrqx/") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_troggle_default(self): # default page after logon response = self.client.get("/troggle") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"expeditions the club has undertaken" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_troggle_default_slash(self): response = self.client.get("/troggle/") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_expoweb_via_areaid(self): # the dispatcher takes a detour via the cave renering procedure for this response = self.client.get("/guidebook/t/via201.jpg") self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(response.content), 6057) def test_cave_kataster_not_found(self): # database not loaded, so no caves found; so looks for a generic expopage and fails # NEEDS TO BE REDONE AFETR cave id rewriting removed after the data is fixed in all teh cave description # pages that link to photos response = self.client.get("/1234/115.htm") self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND) content = response.content.decode() ph = r"Page not found 1234/115.htm" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_caves_page(self): # Throws up lots of cave error msgs because it is looking at something which is not loaded for the tests # but the test itself does not fail response = self.client.get("/caves") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"Cave Number Index - kept updated" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_caves_page_kataster_not_found(self): response = self.client.get("/caves") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"115" phmatch = re.search(ph, content) self.assertIsNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_ss(self): response = self.client.get("/survey_scans/") self.assertEqual(response.status_code, HTTPStatus.OK) ph = r"All Survey scans folders " content = response.content.decode() phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_admin(self): # see the login page response = self.client.get("/admin/login/") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) ph = r'

Troggle database administration

' phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_admindocs_exped(self): # Get redirected to login page response = self.client.get("/admin/doc/models/core.expedition/") response.content.decode() self.assertEqual(response.status_code, HTTPStatus.FOUND) # 302 def test_page_expofiles_root_dir(self): # Root expofiles - odd interaction with url parsing so needs testing response = self.client.get("/expofiles") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/', r'/photos/', r'/surveyscans/', ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_expofiles_root_slash_dir(self): # Root expofiles - odd interaction with url parsing so needs testing response = self.client.get("/expofiles/") if response.status_code != HTTPStatus.OK: # 200 self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: # 302 self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/', r'/photos/', r'/surveyscans/', ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_expofiles_badness(self): # should display expofiles directory contents not its parent response = self.client.get("/expofiles/99badness99") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/', r'/photos/', r'/surveyscans/', ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_expofiles_docs_dir(self): # Flat file tests. response = self.client.get("/expofiles/documents/") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r'a href="/expofiles/documents/bier-tent-instructions.pdf">bier-tent-instructions.pdf', r'a href="/expofiles/documents/boc.pdf">boc.pdf', r'a href="/expofiles/documents/idiots-guide-expo-git.pdf"', ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_survey_scans_dir(self): # Flat file tests. response = self.client.get("/expofiles/surveyscans") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r'/2004/', r'/1989LUSS/', r'/2018', ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_folk(self): # This page is separately generated, so it has the full data content response = self.client.get("/folk/index.htm") content = response.content.decode() self.assertEqual(response.status_code, HTTPStatus.OK) for ph in [ r"involves some active contribution", r"Naomi Griffiths", r"Gail Smith", r"Phil Wigglesworth", r"A more obscure record of longest gap between expos has", ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_expofile_documents(self): # this gets an empty page as the database has not been loaded response = self.client.get("/expofiles/documents") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"notice_generale_cordes_courant" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_expofile_documents_slash(self): # this gets an empty page as the database has not been loaded response = self.client.get("/expofiles/documents/") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"notice_generale_cordes_courant" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_expofile_document_loeffler_pdf(self): # Flat file tests. response = self.client.get("/expofiles/documents/surveying/tunnel-loefflerCP35-only.pdf") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(response.content), 2299270) def test_page_expofile_document_rope_pdf(self): # Flat file tests. response = self.client.get("/expofiles/documents/ropes/rope-age-agm-2019.pdf") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(response.content), 76197) def test_page_expofile_document_png(self): # Flat file tests. response = self.client.get("/expofiles/documents/callout-2012.png") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(response.content), 69921) def test_page_expofile_writeup(self): # Flat file tests. response = self.client.get("/expofiles/writeups/1982/logbook1982.pdf") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(response.content), 12915413) def test_page_site_media_ok(self): # Flat file tests. response = self.client.get("/site_media/surveyHover.gif") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(response.content), 39482) # need to check it is not just an error page def test_page_site_media_css(self): # Flat file tests. response = self.client.get("/site_media/css/trog3.css") if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() # need to check it is not just an error page ph = r"This text is used by the test system to determine that trog3.css loaded correctly" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_photos_ok(self): # Flat file tests. response = self.client.get("/photos/2018/PhilipSargent/corin.jpg") # exists if response.status_code != HTTPStatus.OK: self.assertEqual(response.status_code, HTTPStatus.FOUND) if response.status_code != HTTPStatus.FOUND: self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(response.content), 67487) # need to check it is not just an error page def test_page_photos_not_ok(self): # Flat file tests. response = self.client.get("/photos/2018/PhilipSargent/_corin.jpeg") # does not exist self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND) content = response.content.decode() ph = r"Page not found 2018/PhilipSargent/_corin.jpeg" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_photos_dir(self): # Flat file tests. response = self.client.get("/photos/2018/PhilipSargent/") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"Directory not displayed" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_survey_scans_empty(self): # this gets an empty page as the database has not been loaded response = self.client.get("/survey_scans/") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"contains the scanned original in-cave survey notes and sketches" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_dwgdataraw_empty(self): # this gets an empty page as the database has not been loaded response = self.client.get("/dwgdataraw/") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_dwgallfiles_empty(self): # this gets an empty page as the database has not been loaded response = self.client.get("/dwgfiles") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r"All Tunnel and Therion files", r"WalletsScan files in the walletsFrames", ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_dwgallfiles_empty_slash(self): # this gets an empty page as the database has not been loaded response = self.client.get("/dwgfiles/") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r"All Tunnel and Therion files", r"WalletsScan files in the walletsFrames", ]: phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_page_slash_empty(self): # tslash where there should not be one response = self.client.get("/expedition/1979/") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_not_found_survexfile_cave(self): response = self.client.get("/survexfile/not_a_real_cave_number") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"Cave Identifier not found in database" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_dataissues(self): # Needs another test with test data response = self.client.get("/dataissues") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"as well as these import/parsing issues" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_therionissues(self): # Needs another test with test data response = self.client.get("/therionissues") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"! Un-parsed image filename" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_surveximport(self): # Needs another test with test data response = self.client.get("/surveximport") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() # with open('_test_response.html', 'w') as f: # f.write(content) ph = r"The number at the left-hand margin is the depth" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_survexdebug(self): # Needs another test with test data response = self.client.get("/survexdebug") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"Running list of warnings during import" phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") def test_stations(self): # Needs another test with test data response = self.client.get("/stations") self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = "Survex Stationx " phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'") # ADD TESTS when we are redirecting /expofiles/ to get the actual files using e.g. # import requests # page = requests.get("http://dataquestio.github.io/web-scraping-pages/simple.html") # these need a fixture to load the datbase before they will pass # we also need tests for invalid queries to check that error pages are right # def test_page_survey_scans_khplan2_png(self): # # this has an error as the database has not been loaded yet in the tests # response = self.client.get('/survey_scans/smkhs/khplan2.png') # if response.status_code != HTTPStatus.OK: # self.assertEqual(response.status_code, HTTPStatus.FOUND) # if response.status_code != HTTPStatus.FOUND: # self.assertEqual(response.status_code, HTTPStatus.OK) # self.assertEqual(len(response.content), 823304) # fails, but is working manually! # def test_page_dwgdataraw_107sketch_xml(self): # # this has an error as the database has not been loaded yet in the tests # response = self.client.get('/dwgdataraw/107/107sketch-v2.xml') # if response.status_code != HTTPStatus.OK: # self.assertEqual(response.status_code, HTTPStatus.FOUND) # if response.status_code != HTTPStatus.FOUND: # self.assertEqual(response.status_code, HTTPStatus.OK) # content = response.content.decode() # for ph in [ r'tunneldate="2014-08-21 11:34:00"', # r'', # r'sfsketch="ollyjen107drawings', # r'sfsketch="surveyscans/2014/2014#01', # r'aa-js-plan.png"' ]: # phmatch = re.search(ph, content) # self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") # database not loaded yet: # response = self.client.get('/survey_scans/1991surveybook/page0002.png') # response = self.client.get('/survey_scans/1991surveybook/') # content = response.content.decode() # print(content) # png93 = re.search(r'/page0093.png">page0093.png', content)