From d1dac92034f4bf5a8489ea90f8c66703c92adbec Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Fri, 24 Feb 2023 17:38:06 +0000 Subject: Now using HTTP status codes properly --- core/TESTS/tests.py | 189 ++++++++++++++++++++++++++-------------------------- 1 file changed, 95 insertions(+), 94 deletions(-) (limited to 'core/TESTS/tests.py') diff --git a/core/TESTS/tests.py b/core/TESTS/tests.py index 8f49a40..c663df6 100644 --- a/core/TESTS/tests.py +++ b/core/TESTS/tests.py @@ -26,6 +26,7 @@ todo = """ADD TESTS when we are redirecting /expofiles/ to a remote file-deliver """ import re +from http import HTTPStatus from django.test import Client, TestCase @@ -51,7 +52,7 @@ class PageTests(TestCase): def test_expoweb_root(self): response = self.client.get("") content = response.content.decode() - self.assertEqual(response.status_code, 200) + 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 + "'") @@ -59,14 +60,14 @@ class PageTests(TestCase): def test_expoweb_root_slash(self): response = self.client.get("/") content = response.content.decode() - self.assertEqual(response.status_code, 200) + 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, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"This report is generated from" phmatch = re.search(ph, content) @@ -75,17 +76,17 @@ class PageTests(TestCase): def test_expoweb_dir(self): response = self.client.get("/handbook") response.content.decode() - self.assertEqual(response.status_code, 302) # directory, so redirects to /index.htm + 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, 302) # directory, so redirects to /index.htm + 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, 404) + 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 + "'") @@ -93,7 +94,7 @@ class PageTests(TestCase): 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 + 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 + "'") @@ -101,7 +102,7 @@ class PageTests(TestCase): 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 + 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 + "'") @@ -109,7 +110,7 @@ class PageTests(TestCase): def test_expoweb_dir_with_index2(self): response = self.client.get("/handbook/index.htm") content = response.content.decode() - self.assertEqual(response.status_code, 200) + 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)) @@ -118,7 +119,7 @@ class PageTests(TestCase): def test_expoweb_htm(self): response = self.client.get("/handbook/index.htm") content = response.content.decode() - self.assertEqual(response.status_code, 200) + 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 + "'") @@ -126,7 +127,7 @@ class PageTests(TestCase): def test_expoweb_notfound(self): response = self.client.get("/handbook/_test_zyxxypqrqx.html") content = response.content.decode() - self.assertEqual(response.status_code, 404) + 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 + "'") @@ -134,7 +135,7 @@ class PageTests(TestCase): 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, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) @@ -143,7 +144,7 @@ class PageTests(TestCase): def test_expoweb_troggle_default(self): # default page after logon response = self.client.get("/troggle") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"expeditions the club has undertaken" phmatch = re.search(ph, content) @@ -151,7 +152,7 @@ class PageTests(TestCase): def test_expoweb_troggle_default_slash(self): response = self.client.get("/troggle/") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) @@ -160,13 +161,13 @@ class PageTests(TestCase): 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(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 response = self.client.get("/1623/115.htm") - self.assertEqual(response.status_code, 404) + self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND) content = response.content.decode() ph = r"Page not found 1623/115.htm" phmatch = re.search(ph, content) @@ -174,7 +175,7 @@ class PageTests(TestCase): def test_caves_page(self): response = self.client.get("/caves") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"Cave Number Index - kept updated" phmatch = re.search(ph, content) @@ -182,7 +183,7 @@ class PageTests(TestCase): def test_caves_page_kataster_not_found(self): response = self.client.get("/caves") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"115" phmatch = re.search(ph, content) @@ -190,7 +191,7 @@ class PageTests(TestCase): def test_page_ss(self): response = self.client.get("/survey_scans/") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) ph = r"All Survey scans folders " content = response.content.decode() @@ -201,7 +202,7 @@ class PageTests(TestCase): # see the login page response = self.client.get("/admin/login/") content = response.content.decode() - self.assertEqual(response.status_code, 200) + 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 + "'") @@ -210,15 +211,15 @@ class PageTests(TestCase): # Get redirected to login page response = self.client.get("/admin/doc/models/core.expedition/") response.content.decode() - self.assertEqual(response.status_code, 302) + 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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/', @@ -231,10 +232,10 @@ class PageTests(TestCase): 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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/', @@ -247,10 +248,10 @@ class PageTests(TestCase): 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) + 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/', @@ -263,10 +264,10 @@ class PageTests(TestCase): 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) + 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', @@ -279,10 +280,10 @@ class PageTests(TestCase): def test_page_survey_scans_dir(self): # Flat file tests. response = self.client.get("/expofiles/surveyscans") - if response.status_code != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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/', @@ -296,7 +297,7 @@ class PageTests(TestCase): # 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) + self.assertEqual(response.status_code, HTTPStatus.OK) for ph in [ r"involves some active contribution", r"Naomi Griffiths", @@ -310,10 +311,10 @@ class PageTests(TestCase): 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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) @@ -322,10 +323,10 @@ class PageTests(TestCase): 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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) @@ -334,55 +335,55 @@ class PageTests(TestCase): 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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/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) + 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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 != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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) @@ -391,16 +392,16 @@ class PageTests(TestCase): def test_page_photos_ok(self): # Flat file tests. response = self.client.get("/photos/2018/PhilipSargent/corin.jpg") # exists - if response.status_code != 200: - self.assertEqual(response.status_code, 302) - if response.status_code != 302: - self.assertEqual(response.status_code, 200) + 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, 404) + 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) @@ -409,7 +410,7 @@ class PageTests(TestCase): def test_page_photos_dir(self): # Flat file tests. response = self.client.get("/photos/2018/PhilipSargent/") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"Directory not displayed" phmatch = re.search(ph, content) @@ -418,7 +419,7 @@ class PageTests(TestCase): 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, 200) + 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) @@ -427,7 +428,7 @@ class PageTests(TestCase): 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) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) @@ -436,7 +437,7 @@ class PageTests(TestCase): 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, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r"All Tunnel and Therion files", @@ -448,7 +449,7 @@ class PageTests(TestCase): 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, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() for ph in [ r"All Tunnel and Therion files", @@ -460,7 +461,7 @@ class PageTests(TestCase): def test_page_slash_empty(self): # tslash where there should not be one response = self.client.get("/expedition/1979/") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"

Directory not found" phmatch = re.search(ph, content) @@ -468,7 +469,7 @@ class PageTests(TestCase): def test_not_found_survexfile_cave(self): response = self.client.get("/survexfile/not_a_real_cave_number") - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, HTTPStatus.OK) content = response.content.decode() ph = r"Cave Identifier not found in database" phmatch = re.search(ph, content) @@ -485,19 +486,19 @@ class PageTests(TestCase): # 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) +# 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 != 200: -# self.assertEqual(response.status_code, 302) -# if response.status_code != 302: -# self.assertEqual(response.status_code, 200) +# 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'', -- cgit v1.2.3