mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-21 23:42:14 +00:00
Port title test to project level
This commit is contained in:
@@ -3,7 +3,7 @@ from assets.tests.test_unit import create_asset_one
|
|||||||
import pytest
|
import pytest
|
||||||
from django.urls import URLPattern, URLResolver, reverse
|
from django.urls import URLPattern, URLResolver, reverse
|
||||||
from django.urls.exceptions import NoReverseMatch
|
from django.urls.exceptions import NoReverseMatch
|
||||||
from pytest_django.asserts import assertContains, assertRedirects, assertTemplateUsed
|
from pytest_django.asserts import assertContains, assertRedirects, assertTemplateUsed, assertInHTML
|
||||||
|
|
||||||
pytestmark = pytest.mark.django_db
|
pytestmark = pytest.mark.django_db
|
||||||
|
|
||||||
@@ -20,24 +20,28 @@ def find_urls_recursive(patterns):
|
|||||||
return urls_to_check
|
return urls_to_check
|
||||||
|
|
||||||
|
|
||||||
|
def get_request_url(url):
|
||||||
|
pattern = str(url.pattern)
|
||||||
|
request_url = ""
|
||||||
|
try:
|
||||||
|
kwargz = {}
|
||||||
|
if ":pk>" in pattern:
|
||||||
|
kwargz['pk'] = 1
|
||||||
|
if ":model>" in pattern:
|
||||||
|
kwargz['model'] = "event"
|
||||||
|
return reverse(url.name, kwargs=kwargz)
|
||||||
|
except NoReverseMatch:
|
||||||
|
print("Couldn't test url " + pattern)
|
||||||
|
|
||||||
|
|
||||||
def test_unauthenticated(client): # Nothing should be available to the unauthenticated
|
def test_unauthenticated(client): # Nothing should be available to the unauthenticated
|
||||||
create_asset_one()
|
create_asset_one()
|
||||||
for url in find_urls_recursive(urls.urlpatterns):
|
for url in find_urls_recursive(urls.urlpatterns):
|
||||||
pattern = str(url.pattern)
|
request_url = get_request_url(url)
|
||||||
request_url = ""
|
if request_url and 'user' not in request_url: # User module is full of edge cases
|
||||||
try:
|
|
||||||
kwargz = {}
|
|
||||||
if ":pk>" in pattern:
|
|
||||||
kwargz['pk'] = 1
|
|
||||||
if ":model>" in pattern:
|
|
||||||
kwargz['model'] = "event"
|
|
||||||
request_url = reverse(url.name, kwargs=kwargz)
|
|
||||||
except NoReverseMatch:
|
|
||||||
print("Couldn't test url " + pattern)
|
|
||||||
if request_url and 'user' not in request_url: # User module is full of edge cases
|
|
||||||
response = client.get(request_url, follow=True, HTTP_HOST='example.com')
|
response = client.get(request_url, follow=True, HTTP_HOST='example.com')
|
||||||
assertContains(response, 'Login')
|
assertContains(response, 'Login')
|
||||||
if 'application/json+oembed' in str(response.content):
|
if 'application/json+oembed' in response.content.decode():
|
||||||
assertTemplateUsed(response, 'login_redirect.html')
|
assertTemplateUsed(response, 'login_redirect.html')
|
||||||
else:
|
else:
|
||||||
if "embed" in str(url):
|
if "embed" in str(url):
|
||||||
@@ -45,3 +49,13 @@ def test_unauthenticated(client): # Nothing should be available to the unauthen
|
|||||||
else:
|
else:
|
||||||
expected_url = "{0}?next={1}".format(reverse('login'), request_url)
|
expected_url = "{0}?next={1}".format(reverse('login'), request_url)
|
||||||
assertRedirects(response, expected_url)
|
assertRedirects(response, expected_url)
|
||||||
|
|
||||||
|
|
||||||
|
def test_page_titles(admin_client):
|
||||||
|
create_asset_one()
|
||||||
|
for url in filter((lambda u: "embed" not in u.name), find_urls_recursive(urls.urlpatterns)):
|
||||||
|
request_url = get_request_url(url)
|
||||||
|
response = admin_client.get(request_url)
|
||||||
|
if hasattr(response, "context_data") and "page_title" in response.context_data:
|
||||||
|
expected_title = response.context_data["page_title"]
|
||||||
|
assertInHTML('<title>{} | Rig Information Gathering System'.format(expected_title), response.content.decode())
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ from reversion import revisions as reversion
|
|||||||
from RIGS import models
|
from RIGS import models
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = 'Adds sample data to use for testing'
|
help = 'Adds sample data to use for testing'
|
||||||
can_import_settings = True
|
can_import_settings = True
|
||||||
@@ -345,30 +344,30 @@ class Command(BaseCommand):
|
|||||||
elif random.randint(0, 2) > 1: # 1 in 3 have been paid
|
elif random.randint(0, 2) > 1: # 1 in 3 have been paid
|
||||||
models.Payment.objects.create(invoice=newInvoice, amount=newInvoice.balance,
|
models.Payment.objects.create(invoice=newInvoice, amount=newInvoice.balance,
|
||||||
date=datetime.date.today())
|
date=datetime.date.today())
|
||||||
if i == 1 or random.randint(0, 5) > 0: # Event 1 and 1 in 5 have a RA
|
if i == 1 or random.randint(0, 5) > 0: # Event 1 and 1 in 5 have a RA
|
||||||
models.RiskAssessment.objects.create(event=newEvent, supervisor_consulted=bool(random.getrandbits(1)), nonstandard_equipment=bool(random.getrandbits(1)),
|
models.RiskAssessment.objects.create(event=newEvent, supervisor_consulted=bool(random.getrandbits(1)), nonstandard_equipment=bool(random.getrandbits(1)),
|
||||||
nonstandard_use=bool(random.getrandbits(1)),
|
nonstandard_use=bool(random.getrandbits(1)),
|
||||||
contractors=bool(random.getrandbits(1)),
|
contractors=bool(random.getrandbits(1)),
|
||||||
other_companies=bool(random.getrandbits(1)),
|
other_companies=bool(random.getrandbits(1)),
|
||||||
crew_fatigue=bool(random.getrandbits(1)),
|
crew_fatigue=bool(random.getrandbits(1)),
|
||||||
big_power=bool(random.getrandbits(1)),
|
big_power=bool(random.getrandbits(1)),
|
||||||
generators=bool(random.getrandbits(1)),
|
generators=bool(random.getrandbits(1)),
|
||||||
other_companies_power=bool(random.getrandbits(1)),
|
other_companies_power=bool(random.getrandbits(1)),
|
||||||
nonstandard_equipment_power=bool(random.getrandbits(1)),
|
nonstandard_equipment_power=bool(random.getrandbits(1)),
|
||||||
multiple_electrical_environments=bool(random.getrandbits(1)),
|
multiple_electrical_environments=bool(random.getrandbits(1)),
|
||||||
noise_monitoring=bool(random.getrandbits(1)),
|
noise_monitoring=bool(random.getrandbits(1)),
|
||||||
known_venue=bool(random.getrandbits(1)),
|
known_venue=bool(random.getrandbits(1)),
|
||||||
safe_loading=bool(random.getrandbits(1)),
|
safe_loading=bool(random.getrandbits(1)),
|
||||||
safe_storage=bool(random.getrandbits(1)),
|
safe_storage=bool(random.getrandbits(1)),
|
||||||
area_outside_of_control=bool(random.getrandbits(1)),
|
area_outside_of_control=bool(random.getrandbits(1)),
|
||||||
barrier_required=bool(random.getrandbits(1)),
|
barrier_required=bool(random.getrandbits(1)),
|
||||||
nonstandard_emergency_procedure=bool(random.getrandbits(1)),
|
nonstandard_emergency_procedure=bool(random.getrandbits(1)),
|
||||||
special_structures=bool(random.getrandbits(1)),
|
special_structures=bool(random.getrandbits(1)),
|
||||||
suspended_structures=bool(random.getrandbits(1)),
|
suspended_structures=bool(random.getrandbits(1)),
|
||||||
outside=bool(random.getrandbits(1)))
|
outside=bool(random.getrandbits(1)))
|
||||||
if i == 0 or random.randint(0, 1) > 0: # Event 1 and 1 in 10 have a Checklist
|
if i == 0 or random.randint(0, 1) > 0: # Event 1 and 1 in 10 have a Checklist
|
||||||
models.EventChecklist.objects.create(event=newEvent, power_mic=random.choice(self.profiles), safe_parking=bool(random.getrandbits(1)),
|
models.EventChecklist.objects.create(event=newEvent, power_mic=random.choice(self.profiles), safe_parking=bool(random.getrandbits(1)),
|
||||||
safe_packing=bool(random.getrandbits(1)), exits=bool(random.getrandbits(1)), trip_hazard=bool(random.getrandbits(1)), warning_signs=bool(random.getrandbits(1)),
|
safe_packing=bool(random.getrandbits(1)), exits=bool(random.getrandbits(1)), trip_hazard=bool(random.getrandbits(1)), warning_signs=bool(random.getrandbits(1)),
|
||||||
ear_plugs=bool(random.getrandbits(1)), hs_location="Locked away safely",
|
ear_plugs=bool(random.getrandbits(1)), hs_location="Locked away safely",
|
||||||
extinguishers_location="Somewhere, I forgot", earthing=bool(random.getrandbits(1)), pat=bool(random.getrandbits(1)),
|
extinguishers_location="Somewhere, I forgot", earthing=bool(random.getrandbits(1)), pat=bool(random.getrandbits(1)),
|
||||||
date=timezone.now(), venue=random.choice(self.venues))
|
date=timezone.now(), venue=random.choice(self.venues))
|
||||||
|
|||||||
@@ -258,16 +258,3 @@ def test_keyholder_access(client):
|
|||||||
response = client.get(url)
|
response = client.get(url)
|
||||||
assertContains(response, 'Purchase Details')
|
assertContains(response, 'Purchase Details')
|
||||||
assertContains(response, 'View Revision History')
|
assertContains(response, 'View Revision History')
|
||||||
|
|
||||||
|
|
||||||
def test_page_titles(admin_client):
|
|
||||||
for url in filter(lambda url: url.name is not None and not any(s in url.name for s in ["json", "embed"]), urls.urlpatterns):
|
|
||||||
request_url = ""
|
|
||||||
if ":pk>" in str(url.pattern):
|
|
||||||
request_url = reverse(url.name, kwargs={'pk': "1"})
|
|
||||||
else:
|
|
||||||
request_url = reverse(url.name)
|
|
||||||
response = admin_client.get(request_url)
|
|
||||||
if hasattr(response, "context_data") and "page_title" in response.context_data:
|
|
||||||
expected_title = response.context_data["page_title"]
|
|
||||||
assertContains(response, '<title>{} | Rig Information Gathering System</title>'.format(expected_title))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user