mpeltriaux
bfe541f83e
* refactors test logic into base test classes * adds tests for views in konova app * fixes bugs/warnings detected by testing
253 lines
8.3 KiB
Python
253 lines
8.3 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
Created on: 26.10.21
|
|
|
|
"""
|
|
from django.test import Client
|
|
|
|
from django.contrib.auth.models import Group
|
|
from django.urls import reverse
|
|
|
|
from intervention.models import Intervention, LegalData, ResponsibilityData
|
|
from konova.models import Geometry
|
|
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
|
from konova.tests.test_views import BaseViewTestCase
|
|
from user.models import UserActionLogEntry, UserAction
|
|
|
|
|
|
class ViewTestCase(BaseViewTestCase):
|
|
intervention = None
|
|
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
self.create_dummy_data()
|
|
|
|
# Prepare urls
|
|
self.index_url = reverse("intervention:index", args=())
|
|
self.new_url = reverse("intervention:new", args=())
|
|
self.new_id_url = reverse("intervention:new-id", args=())
|
|
self.detail_url = reverse("intervention:detail", args=(self.intervention.id,))
|
|
self.log_url = reverse("intervention:log", args=(self.intervention.id,))
|
|
self.edit_url = reverse("intervention:edit", args=(self.intervention.id,))
|
|
self.remove_url = reverse("intervention:remove", args=(self.intervention.id,))
|
|
self.share_url = reverse("intervention:share", args=(self.intervention.id, self.intervention.access_token,))
|
|
self.share_create_url = reverse("intervention:share-create", args=(self.intervention.id,))
|
|
self.run_check_url = reverse("intervention:run-check", args=(self.intervention.id,))
|
|
self.record_url = reverse("intervention:record", args=(self.intervention.id,))
|
|
self.report_url = reverse("intervention:report", args=(self.intervention.id,))
|
|
|
|
def create_dummy_data(self):
|
|
""" Creates an intervention which can be used for tests
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Create dummy data
|
|
# Create log entry
|
|
action = UserActionLogEntry.objects.create(
|
|
user=self.superuser,
|
|
action=UserAction.CREATED,
|
|
)
|
|
# Create legal data object (without M2M laws first)
|
|
legal_data = LegalData.objects.create()
|
|
# Create responsible data object
|
|
responsibility_data = ResponsibilityData.objects.create()
|
|
geometry = Geometry.objects.create()
|
|
# Finally create main object, holding the other objects
|
|
self.intervention = Intervention.objects.create(
|
|
identifier="TEST",
|
|
title="Test_title",
|
|
responsible=responsibility_data,
|
|
legal=legal_data,
|
|
created=action,
|
|
geometry=geometry,
|
|
comment="Test",
|
|
)
|
|
self.intervention.generate_access_token(make_unique=True)
|
|
|
|
def test_views_logged_in_no_groups(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assumption: User logged in but has no groups
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
|
|
|
success_urls = [
|
|
self.index_url,
|
|
self.report_url,
|
|
self.detail_url,
|
|
]
|
|
fail_urls = [
|
|
self.log_url,
|
|
self.new_id_url,
|
|
self.new_url,
|
|
self.edit_url,
|
|
self.remove_url,
|
|
self.share_url,
|
|
self.share_create_url,
|
|
self.run_check_url,
|
|
self.record_url,
|
|
]
|
|
|
|
self.assert_url_success(client, success_urls)
|
|
self.assert_url_fail(client, fail_urls)
|
|
|
|
def test_views_anonymous_user(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assumption: User not logged in
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Unknown client
|
|
client = Client()
|
|
|
|
success_urls = [
|
|
self.report_url,
|
|
]
|
|
login_redirect_base = f"{self.login_url}?next="
|
|
fail_urls = {
|
|
self.detail_url: f"{login_redirect_base}{self.detail_url}",
|
|
self.index_url: f"{login_redirect_base}{self.index_url}",
|
|
self.log_url: f"{login_redirect_base}{self.log_url}",
|
|
self.new_id_url: f"{login_redirect_base}{self.new_id_url}",
|
|
self.new_url: f"{login_redirect_base}{self.new_url}",
|
|
self.edit_url: f"{login_redirect_base}{self.edit_url}",
|
|
self.remove_url: f"{login_redirect_base}{self.remove_url}",
|
|
self.share_url: f"{login_redirect_base}{self.share_url}",
|
|
self.share_create_url: f"{login_redirect_base}{self.share_create_url}",
|
|
self.run_check_url: f"{login_redirect_base}{self.run_check_url}",
|
|
self.record_url: f"{login_redirect_base}{self.record_url}",
|
|
}
|
|
|
|
self.assert_url_success(client, success_urls)
|
|
|
|
for url in fail_urls:
|
|
response = client.get(url, follow=True)
|
|
self.assertEqual(response.redirect_chain[0], (f"{self.login_url}?next={url}", 302), msg=f"Failed for {url}. Redirect chain is {response.redirect_chain}")
|
|
|
|
def test_views_logged_in_default_group(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assumption: User logged in and is default group member
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
|
|
|
# Add user to default group
|
|
default_group = Group.objects.get(name=DEFAULT_GROUP)
|
|
self.superuser.groups.set([default_group])
|
|
|
|
success_urls = [
|
|
self.index_url,
|
|
self.report_url,
|
|
self.detail_url,
|
|
self.log_url,
|
|
self.new_id_url,
|
|
self.new_url,
|
|
self.edit_url,
|
|
self.remove_url,
|
|
self.share_create_url,
|
|
]
|
|
fail_urls = [
|
|
self.run_check_url,
|
|
self.record_url,
|
|
]
|
|
success_urls_redirect = {
|
|
self.share_url: self.detail_url
|
|
}
|
|
|
|
self.assert_url_success(client, success_urls)
|
|
self.assert_url_fail(client, fail_urls)
|
|
self.assert_url_success_redirect(client, success_urls_redirect)
|
|
|
|
def test_views_logged_in_zb_group(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assumption: User logged in and is registration office member
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
|
|
|
# Add user to default group
|
|
zb_group = self.groups.get(name=ZB_GROUP)
|
|
self.superuser.groups.set([zb_group])
|
|
|
|
success_urls = [
|
|
self.index_url,
|
|
self.report_url,
|
|
self.detail_url,
|
|
self.run_check_url,
|
|
]
|
|
fail_urls = [
|
|
self.log_url,
|
|
self.new_id_url,
|
|
self.new_url,
|
|
self.edit_url,
|
|
self.remove_url,
|
|
self.share_create_url,
|
|
self.record_url,
|
|
]
|
|
success_urls_redirect = {
|
|
self.share_url: self.detail_url
|
|
}
|
|
|
|
self.assert_url_success(client, success_urls)
|
|
self.assert_url_fail(client, fail_urls)
|
|
self.assert_url_success_redirect(client, success_urls_redirect)
|
|
|
|
def test_views_logged_in_ets_group(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assumption: User logged in and is registration office member
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username=self.superuser.username, password=self.superuser_pw)
|
|
|
|
# Add user to default group
|
|
ets_group = Group.objects.get(name=ETS_GROUP)
|
|
self.superuser.groups.set([ets_group])
|
|
|
|
success_urls = [
|
|
self.index_url,
|
|
self.report_url,
|
|
self.detail_url,
|
|
self.record_url,
|
|
]
|
|
fail_urls = [
|
|
self.log_url,
|
|
self.new_id_url,
|
|
self.new_url,
|
|
self.edit_url,
|
|
self.remove_url,
|
|
self.share_create_url,
|
|
self.run_check_url,
|
|
]
|
|
success_urls_redirect = {
|
|
self.share_url: self.detail_url
|
|
}
|
|
|
|
self.assert_url_success(client, success_urls)
|
|
self.assert_url_fail(client, fail_urls)
|
|
self.assert_url_success_redirect(client, success_urls_redirect)
|