"""Tests for the added pages: regions, routes, sources, logos, about, data, SEO files, the bulk archives and the response headers.""" import html import os import re import subprocess import sys import unittest import zipfile WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = os.path.dirname(WEB) sys.path.insert(0, WEB) import serve # noqa: E402 from olt import docs, home, records, regions as regions_mod, seo, site, words # noqa: E402 OUT = os.path.join(WEB, ".cache", "test-site") def setUpModule(): os.makedirs(os.path.dirname(OUT), exist_ok=True) subprocess.run([sys.executable, os.path.join(WEB, "build.py"), "--out", OUT], check=True, stdout=subprocess.DEVNULL) def read_out(relpath): with open(os.path.join(OUT, *relpath.split("/")), encoding="utf-8") as f: return f.read() def entry_ids(doc): return re.findall(r'
  • {slug}") self.assertTrue(os.path.isfile(os.path.join(OUT, "regions", "index.html"))) def test_each_record_on_each_region_page(self): for rec in self.records: for code in rec.get("regions", []): if code == words.ALL_CANADA: page = read_out("regions/index.html") else: slug = words.region_key(code, rec["country"]).lower() page = read_out(f"regions/{slug}/index.html") self.assertIn(f'href="/trails/{rec["id"]}/"', page, f'{rec["id"]} in {code}') def test_all_routes_on_routes_page(self): routes = [r for r in self.records if r["derived"]["kind"] == "route"] self.assertEqual(len(routes), 34) page = read_out("routes/index.html") import html as _html text = _html.unescape(page) for r in routes: self.assertIn(f'href="/trails/{r["id"]}/"', page, r["id"]) self.assertIn(r["route"]["creator"], text, r["id"]) def test_every_logo_on_logos_page(self): page = read_out("logos/index.html") for r in self.records: if "shield" in r["derived"]["files"]: self.assertIn(f'href="/trails/{r["id"]}/"', page, r["id"]) self.assertIn("no-logo-exists", page) self.assertIn("logo-not-found", page) def test_added_pages_exist(self): for rel in ("sources/index.html", "about/index.html", "data/index.html", "data/schema/index.html", "data/changelog/index.html", "licences/LICENSE", "licences/MIT.txt", "robots.txt"): self.assertTrue(os.path.isfile(os.path.join(OUT, *rel.split("/"))), rel) def test_no_dead_internal_links_in_added_pages(self): import re for rel in ("regions/index.html", "routes/index.html", "sources/index.html", "logos/index.html", "about/index.html", "data/index.html", "data/schema/index.html", "data/changelog/index.html"): html = read_out(rel) for url in re.findall(r'(?:href|src)="(/[^"]*)"', html): path = url.split("#", 1)[0].split("?", 1)[0] if not path: continue if path.endswith("/"): path += "index.html" target = os.path.join(OUT, *path.lstrip("/").split("/")) self.assertTrue(os.path.isfile(target), f"{rel} -> {url}") def test_region_map_limited_to_region_ids(self): page = read_out("regions/us-vt/index.html") self.assertIn('data-map="overview"', page) self.assertIn("data-ids=", page) self.assertIn("long-trail", page) def test_home_region_links_point_to_region_pages(self): page = read_out("index.html") self.assertIn('href="/regions/us-vt/"', page) def test_country_and_region_counts_match_the_regions_pages(self): home_doc = read_out("index.html") index = read_out("regions/index.html") by_slug, canada_wide = regions_mod.collect_regions(self.records) for country, label in (("US", "United States"), ("CA", "Canada")): shown = int(re.search(rf'value="{country}">{label} \((\d+)\)', home_doc).group(1)) on_index = int(re.search(rf'data-country-count="{country}">(\d+)<', index).group(1)) listed = set() for slug, info in by_slug.items(): if info["country"] == country: listed |= set(entry_ids(read_out(f"regions/{slug}/index.html"))) if country == "CA": listed |= {r["id"] for r in canada_wide} self.assertEqual(shown, on_index, country) self.assertEqual(shown, len(listed), country) # every row of those records says it touches the country for tid in listed: attrs = re.search(rf'
  • ]*)>', home_doc).group(1) self.assertIn(country, re.search(r'data-countries="([^"]*)"', attrs).group(1).split(), tid) self.assertIn('data-countries="US CA"', re.search( r'
  • ]*>', home_doc).group(0)) self.assertIn('data-countries="US CA"', re.search( r'
  • ]*>', home_doc).group(0)) # each region option's count is the number of records on that region's page for value, n in re.findall(r'', home_doc): page = read_out(f"regions/{value.lower()}/index.html") self.assertEqual(int(n), len(entry_ids(page)), value) canada = re.search(r'', home_doc) self.assertEqual(int(canada.group(1)), len(canada_wide)) self.assertIn('href="/?region=CA-*"', index) def test_list_pages_share_the_home_list(self): for rel in ("index.html", "regions/us-co/index.html", "routes/index.html"): doc = read_out(rel) results = doc[doc.index('
    '):] for part in ('