2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-25 08:41:51 +00:00
troggle/core/TESTS/tests.py

517 lines
23 KiB
Python
Raw Normal View History

"""
2020-06-28 14:42:26 +01:00
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.
2020-06-03 21:57:05 +01:00
The simple redirections to files which exist, e.g. in
/expoweb/
/expofiles/
/expofiles/documents/
etc. will test fine.
But paths like this:
/survey_scans/
2021-03-31 20:18:46 +01:00
/caves/
which rely on database resolution will fail unless a fixture has been set up for
them.
2020-06-03 21:57:05 +01:00
https://docs.djangoproject.com/en/3.0/topics/testing/tools/
"""
2021-04-21 19:08:42 +01:00
todo = """ADD TESTS when we are redirecting /expofiles/ to a remote file-delivering site
2021-11-06 22:36:44 +00:00
- Add test for running cavern to produce a .3d file
"""
2021-04-21 19:08:42 +01:00
2020-07-19 01:23:07 +01:00
import re
2023-01-19 18:35:56 +00:00
2023-01-30 23:04:11 +00:00
from django.test import Client, TestCase
2020-06-03 21:57:05 +01:00
# class SimplePageTest(unittest.TestCase):
2020-07-19 01:23:07 +01:00
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.
"""
2020-07-19 01:23:07 +01:00
@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..
2020-07-19 01:23:07 +01:00
# read in some SQL ?
pass
2020-07-19 01:23:07 +01:00
def setUp(self):
# Every test needs a client.
self.client = Client()
2021-03-31 20:18:46 +01:00
def test_expoweb_root(self):
response = self.client.get("")
2021-03-31 20:18:46 +01:00
content = response.content.decode()
self.assertEqual(response.status_code, 200)
ph = r"CUCC in Austria"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
def test_expoweb_root_slash(self):
response = self.client.get("/")
2021-03-31 20:18:46 +01:00
content = response.content.decode()
self.assertEqual(response.status_code, 200)
ph = r"CUCC in Austria"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
def test_expoweb_paths(self):
response = self.client.get("/pathsreport")
self.assertEqual(response.status_code, 200)
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 + "'")
2021-03-31 20:18:46 +01:00
def test_expoweb_dir(self):
response = self.client.get("/handbook")
2023-01-30 23:04:11 +00:00
response.content.decode()
self.assertEqual(response.status_code, 302) # directory, so redirects to /index.htm
2021-03-31 20:18:46 +01:00
def test_expoweb_dirslash(self):
response = self.client.get("/handbook/")
2023-01-30 23:04:11 +00:00
response.content.decode()
self.assertEqual(response.status_code, 302) # directory, so redirects to /index.htm
2021-03-31 20:18:46 +01:00
def test_expoweb_dir_no_index(self):
response = self.client.get("/handbook/troggle")
2021-03-31 20:18:46 +01:00
content = response.content.decode()
2021-05-03 20:35:35 +01:00
self.assertEqual(response.status_code, 404)
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, 200) # 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, 200) # 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, 200)
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 + "'")
2021-03-31 20:18:46 +01:00
def test_expoweb_htm(self):
response = self.client.get("/handbook/index.htm")
2021-03-31 20:18:46 +01:00
content = response.content.decode()
self.assertEqual(response.status_code, 200)
ph = r"Introduction to expo"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
def test_expoweb_notfound(self):
response = self.client.get("/handbook/_test_zyxxypqrqx.html")
2021-03-31 20:18:46 +01:00
content = response.content.decode()
2021-05-03 20:35:35 +01:00
self.assertEqual(response.status_code, 404)
ph = r"<h1>Page not found"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
def test_expoweb_no_dir(self):
# slash where there should not be one
response = self.client.get("/handbook/_test_zyxxypqrqx/")
2021-03-31 20:18:46 +01:00
self.assertEqual(response.status_code, 200)
2020-07-19 01:23:07 +01:00
content = response.content.decode()
2021-03-31 20:18:46 +01:00
ph = r"<h1>Directory not found"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
2021-04-07 16:04:27 +01:00
def test_expoweb_troggle_default(self):
# default page after logon
response = self.client.get("/troggle")
2021-04-07 16:04:27 +01:00
self.assertEqual(response.status_code, 200)
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 + "'")
2021-04-07 16:04:27 +01:00
def test_expoweb_troggle_default_slash(self):
response = self.client.get("/troggle/")
2021-04-07 16:04:27 +01:00
self.assertEqual(response.status_code, 200)
content = response.content.decode()
ph = r"<h1>Directory not found"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-04-07 16:04:27 +01:00
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, 200)
self.assertEqual(len(response.content), 6057)
2021-03-31 20:18:46 +01:00
def test_cave_kataster_not_found(self):
2022-03-18 20:00:15 +00:00
# database not loaded, so no caves found; so looks for a generic expopage and fails
response = self.client.get("/1623/115.htm")
2022-03-18 20:00:15 +00:00
self.assertEqual(response.status_code, 404)
content = response.content.decode()
ph = r"Page not found 1623/115.htm"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2022-03-18 20:00:15 +00:00
def test_caves_page(self):
response = self.client.get("/caves")
2020-07-19 01:23:07 +01:00
self.assertEqual(response.status_code, 200)
2021-03-31 20:18:46 +01:00
content = response.content.decode()
2022-03-18 20:00:15 +00:00
ph = r"Cave Number Index - kept updated"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
2022-03-18 20:00:15 +00:00
def test_caves_page_kataster_not_found(self):
response = self.client.get("/caves")
2022-03-18 20:00:15 +00:00
self.assertEqual(response.status_code, 200)
content = response.content.decode()
ph = r"115"
phmatch = re.search(ph, content)
self.assertIsNone(phmatch, "Failed to find expected text: '" + ph + "'")
2022-03-18 20:00:15 +00:00
def test_page_ss(self):
response = self.client.get("/survey_scans/")
self.assertEqual(response.status_code, 200)
ph = r"All Survey scans folders "
content = response.content.decode()
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2020-07-19 01:23:07 +01:00
2021-03-31 20:18:46 +01:00
def test_page_admin(self):
# see the login page
response = self.client.get("/admin/login/")
content = response.content.decode()
self.assertEqual(response.status_code, 200)
ph = r'<h1 id="site-name">Troggle database administration</h1>'
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/")
2023-01-30 23:04:11 +00:00
response.content.decode()
self.assertEqual(response.status_code, 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 != 200:
self.assertEqual(response.status_code, 302)
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [
r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/',
r'<a href="/expofiles/photos">/photos/',
r'<a href="/expofiles/surveyscans">/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/")
2021-03-31 23:41:46 +01:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-31 23:41:46 +01:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [
r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/',
r'<a href="/expofiles/photos">/photos/',
r'<a href="/expofiles/surveyscans">/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 != 200:
self.assertEqual(response.status_code, 302)
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [
r'a href="/expofiles/geotiffsurveys">/geotiffsurveys/',
r'<a href="/expofiles/photos">/photos/',
r'<a href="/expofiles/surveyscans">/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 != 200:
self.assertEqual(response.status_code, 302)
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
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/bierbook">/bierbook',
]:
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")
2021-03-31 23:41:46 +01:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-31 23:41:46 +01:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [
r'<a href="/expofiles/surveyscans/2004">/2004/',
r'<a href="/expofiles/surveyscans/1989LUSS">/1989LUSS/',
r'<a href="/expofiles/surveyscans/2018">/2018',
]:
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2020-07-19 01:23:07 +01:00
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, 200)
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 + "'")
2021-03-31 20:18:46 +01:00
def test_page_expofile_documents(self):
# this gets an empty page as the database has not been loaded
response = self.client.get("/expofiles/documents")
2021-03-31 23:41:46 +01:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-31 23:41:46 +01:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
2021-03-31 23:41:46 +01:00
content = response.content.decode()
ph = r"notice_generale_cordes_courant"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
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/")
2021-03-31 23:41:46 +01:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-31 23:41:46 +01:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
2021-03-31 23:41:46 +01:00
content = response.content.decode()
ph = r"notice_generale_cordes_courant"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
def test_page_expofile_document_loeffler_pdf(self):
2020-07-19 01:23:07 +01:00
# Flat file tests.
response = self.client.get("/expofiles/documents/surveying/tunnel-loefflerCP35-only.pdf")
2021-03-24 17:32:45 +00:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-24 17:32:45 +00:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 2299270)
2021-03-24 17:32:45 +00:00
def test_page_expofile_document_rope_pdf(self):
# Flat file tests.
response = self.client.get("/expofiles/documents/rope-age-agm-2019.pdf")
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
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 != 200:
self.assertEqual(response.status_code, 302)
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 69921)
2021-03-24 17:32:45 +00:00
def test_page_expofile_writeup(self):
# Flat file tests.
response = self.client.get("/expofiles/writeups/1982/logbook1982.pdf")
2021-03-24 17:32:45 +00:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-24 17:32:45 +00:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 12915413)
2021-03-31 23:41:46 +01:00
def test_page_site_media_ok(self):
# Flat file tests.
response = self.client.get("/site_media/surveyHover.gif")
2021-03-31 23:41:46 +01:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-31 23:41:46 +01:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 39482) # need to check it is not just an error page
2021-04-02 19:22:53 +01:00
def test_page_site_media_css(self):
# Flat file tests.
response = self.client.get("/site_media/css/trog3.css")
2021-04-02 19:22:53 +01:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-04-02 19:22:53 +01:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
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 + "'")
2021-03-31 23:41:46 +01:00
def test_page_photos_ok(self):
# Flat file tests.
response = self.client.get("/photos/2018/PhilipSargent/corin.jpg") # exists
2021-03-31 23:41:46 +01:00
if response.status_code != 200:
self.assertEqual(response.status_code, 302)
2021-03-31 23:41:46 +01:00
if response.status_code != 302:
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.content), 67487) # need to check it is not just an error page
2021-03-31 23:41:46 +01:00
def test_page_photos_not_ok(self):
# Flat file tests.
response = self.client.get("/photos/2018/PhilipSargent/_corin.jpeg") # does not exist
2021-05-03 20:35:35 +01:00
self.assertEqual(response.status_code, 404)
2021-03-31 23:41:46 +01:00
content = response.content.decode()
ph = r"<title>Page not found 2018/PhilipSargent/_corin.jpeg</title>"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 23:41:46 +01:00
def test_page_photos_dir(self):
# Flat file tests.
response = self.client.get("/photos/2018/PhilipSargent/")
2021-03-31 23:41:46 +01:00
self.assertEqual(response.status_code, 200)
content = response.content.decode()
ph = r"Directory not displayed"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2020-07-19 01:23:07 +01:00
def test_page_survey_scans_empty(self):
2020-07-19 01:23:07 +01:00
# this gets an empty page as the database has not been loaded
response = self.client.get("/survey_scans/")
2020-07-19 01:23:07 +01:00
self.assertEqual(response.status_code, 200)
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, 200)
content = response.content.decode()
2021-03-31 20:18:46 +01:00
ph = r"<h1>Directory not found"
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-03-31 20:18:46 +01:00
2021-05-04 14:16:48 +01:00
def test_page_dwgallfiles_empty(self):
2021-05-04 15:48:11 +01:00
# this gets an empty page as the database has not been loaded
response = self.client.get("/dwgfiles")
2021-05-04 15:48:11 +01:00
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [
r"All Tunnel and Therion files",
r"<th>Wallets</th><th>Scan files in the wallets</th><th>Frames</th></tr>",
]:
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-05-04 15:48:11 +01:00
def test_page_dwgallfiles_empty_slash(self):
2021-05-04 14:16:48 +01:00
# this gets an empty page as the database has not been loaded
response = self.client.get("/dwgfiles/")
2021-05-04 14:16:48 +01:00
self.assertEqual(response.status_code, 200)
content = response.content.decode()
for ph in [
r"All Tunnel and Therion files",
r"<th>Wallets</th><th>Scan files in the wallets</th><th>Frames</th></tr>",
]:
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph + "'")
2021-05-04 14:16:48 +01:00
2021-03-31 20:18:46 +01:00
def test_page_slash_empty(self):
# tslash where there should not be one
response = self.client.get("/expedition/1979/")
2021-03-31 20:18:46 +01:00
self.assertEqual(response.status_code, 200)
content = response.content.decode()
ph = r"<h1>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, 200)
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 + "'")
# 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 != 200:
# self.assertEqual(response.status_code, 302)
# if response.status_code != 302:
# self.assertEqual(response.status_code, 200)
# 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 != 200:
# self.assertEqual(response.status_code, 302)
# if response.status_code != 302:
# self.assertEqual(response.status_code, 200)
# content = response.content.decode()
# for ph in [ r'tunneldate="2014-08-21 11:34:00"',
# r'<sketchsubset subname="Caves of the Loser Plateau"/>',
# 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</a></td>', content)