Much test refactoring

This commit is contained in:
2021-02-04 13:06:23 +00:00
parent 7eea868575
commit 3853ad0871
15 changed files with 360 additions and 379 deletions

View File

@@ -27,9 +27,7 @@ SECRET_KEY = env('SECRET_KEY', default='gxhy(a#5mhp289_=6xx$7jh=eh$ymxg^ymc+di*0
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG', cast=bool, default=True)
STAGING = env('STAGING', cast=bool, default=False)
CI = env('CI', cast=bool, default=False)
ALLOWED_HOSTS = ['pyrigs.nottinghamtec.co.uk', 'rigs.nottinghamtec.co.uk', 'pyrigs.herokuapp.com']
@@ -55,6 +53,7 @@ if DEBUG:
# Application definition
INSTALLED_APPS = (
'whitenoise.runserver_nostatic',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',

View File

@@ -13,10 +13,12 @@ from RIGS import models as rigsmodels
from . import pages
from envparse import env
from pytest_django.asserts import assertContains
def create_datetime(year, month, day, hour, min):
def create_datetime(year, month, day, hour, minute):
tz = pytz.timezone(settings.TIME_ZONE)
return tz.localize(datetime(year, month, day, hour, min)).astimezone(pytz.utc)
return tz.localize(datetime(year, month, day, hour, minute)).astimezone(tz)
def create_browser():
@@ -60,6 +62,7 @@ class AutoLoginTest(BaseTest):
login_page.login("EventTest", "EventTestPassword")
# FIXME Refactor as a pytest fixture
def screenshot_failure(func):
def wrapper_func(self, *args, **kwargs):
try:
@@ -85,3 +88,27 @@ def screenshot_failure_cls(cls):
def assert_times_equal(first_time, second_time):
assert first_time.replace(microsecond=0, second=0) == second_time.replace(microsecond=0, second=0)
def assert_oembed(alt_event_embed_url, alt_oembed_url, client, event_embed_url, event_url, oembed_url):
# Test the meta tag is in place
response = client.get(event_url, follow=True, HTTP_HOST='example.com')
assertContains(response, '<link rel="alternate" type="application/json+oembed"')
assertContains(response, oembed_url)
# Test that the JSON exists
response = client.get(oembed_url, follow=True, HTTP_HOST='example.com')
assert response.status_code == 200
assertContains(response, event_embed_url)
# Should also work for non-existant events
response = client.get(alt_oembed_url, follow=True, HTTP_HOST='example.com')
assert response.status_code == 200
assertContains(response, alt_event_embed_url)
def login(client, django_user_model):
pwd = 'testuser'
usr = 'TestUser'
user = django_user_model.objects.create_user(username=usr, email="TestUser@test.com", password=pwd, is_superuser=True,
is_active=True, is_staff=True)
assert client.login(username=usr, password=pwd)
return user

View File

@@ -1,11 +1,12 @@
from PyRIGS import urls
from assets.tests.test_unit import create_asset_one
from django.core.management import call_command
import pytest
from django.urls import URLPattern, URLResolver, reverse
from django.urls.exceptions import NoReverseMatch
from django.test.utils import override_settings
from pytest_django.asserts import assertContains, assertRedirects, assertTemplateUsed, assertInHTML
pytestmark = pytest.mark.django_db
from django.template.defaultfilters import striptags
def find_urls_recursive(patterns):
@@ -14,7 +15,7 @@ def find_urls_recursive(patterns):
if isinstance(url, URLResolver):
urls_to_check += find_urls_recursive(url.url_patterns)
elif isinstance(url, URLPattern):
# Skip some thinks that actually don't need auth (mainly OEmbed JSONs that are essentially just a redirect)
# Skip some things that actually don't need auth (mainly OEmbed JSONs that are essentially just a redirect)
if url.name is not None and url.name != "closemodal" and "json" not in str(url):
urls_to_check.append(url)
return urls_to_check
@@ -22,7 +23,6 @@ def find_urls_recursive(patterns):
def get_request_url(url):
pattern = str(url.pattern)
request_url = ""
try:
kwargz = {}
if ":pk>" in pattern:
@@ -34,8 +34,18 @@ def get_request_url(url):
print("Couldn't test url " + pattern)
@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
from django.conf import settings
settings.DEBUG = True
call_command('generateSampleRIGSData') # We need stuff setup so we don't get 404 errors everywhere
create_asset_one()
settings.DEBUG = False
def test_unauthenticated(client): # Nothing should be available to the unauthenticated
create_asset_one()
for url in find_urls_recursive(urls.urlpatterns):
request_url = get_request_url(url)
if request_url and 'user' not in request_url: # User module is full of edge cases
@@ -52,12 +62,11 @@ def test_unauthenticated(client): # Nothing should be available to the unauthen
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"]
expected_title = striptags(response.context_data["page_title"])
# try:
assertInHTML('<title>{} | Rig Information Gathering System'.format(expected_title), response.content.decode())
print("{} | {}".format(request_url, expected_title)) # If test fails, tell me where!