mpeltriaux
023bdda545
* adds tests for views in intervention app * fixes bugs detected by test writing
294 lines
9.4 KiB
Python
294 lines
9.4 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 TestCase, Client
|
|
|
|
from django.contrib.auth.models import User, Group
|
|
from django.urls import reverse
|
|
|
|
from intervention.models import Intervention, LegalData, ResponsibilityData
|
|
from konova.management.commands.setup_data import GROUPS_DATA
|
|
from konova.models import Geometry
|
|
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
|
from user.models import UserActionLogEntry, UserAction
|
|
|
|
|
|
class ViewTestCase(TestCase):
|
|
def setUp(self) -> None:
|
|
# Create superuser and regular user
|
|
self.superuser = User.objects.create_superuser(
|
|
username="root",
|
|
email="root@root.com",
|
|
password="root",
|
|
)
|
|
self.user = User.objects.create_user(
|
|
username="user1",
|
|
email="user@root.com",
|
|
password="user1"
|
|
)
|
|
# Create groups
|
|
for group_data in GROUPS_DATA:
|
|
name = group_data.get("name")
|
|
Group.objects.get_or_create(
|
|
name=name,
|
|
)
|
|
|
|
# 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
|
|
intervention = Intervention.objects.create(
|
|
identifier="TEST",
|
|
title="Test_title",
|
|
responsible=responsibility_data,
|
|
legal=legal_data,
|
|
created=action,
|
|
geometry=geometry,
|
|
comment="Test",
|
|
)
|
|
intervention.generate_access_token(make_unique=True)
|
|
|
|
# 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=(intervention.id,))
|
|
self.log_url = reverse("intervention:log", args=(intervention.id,))
|
|
self.edit_url = reverse("intervention:edit", args=(intervention.id,))
|
|
self.remove_url = reverse("intervention:remove", args=(intervention.id,))
|
|
self.share_url = reverse("intervention:share", args=(intervention.id, intervention.access_token,))
|
|
self.share_create_url = reverse("intervention:share-create", args=(intervention.id,))
|
|
self.run_check_url = reverse("intervention:run-check", args=(intervention.id,))
|
|
self.record_url = reverse("intervention:record", args=(intervention.id,))
|
|
self.report_url = reverse("intervention:report", args=(intervention.id,))
|
|
self.login_url = reverse("simple-sso-login")
|
|
|
|
def test_views_logged_in_no_groups(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assertion: User logged in but has no groups
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username="root", password="root")
|
|
|
|
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,
|
|
]
|
|
|
|
for url in success_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 200, msg=f"Failed for {url}")
|
|
|
|
for url in fail_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 302, msg=f"Failed for {url}")
|
|
|
|
def test_views_anonymous_user(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assertion: User logged in but has no groups
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Unknown client
|
|
client = Client()
|
|
|
|
success_urls = [
|
|
self.report_url,
|
|
]
|
|
fail_urls = [
|
|
self.detail_url,
|
|
self.index_url,
|
|
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,
|
|
]
|
|
|
|
for url in success_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 200, msg=f"Failed for {url}")
|
|
|
|
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
|
|
|
|
Assertion: User logged in and is default group member
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username="root", password="root")
|
|
|
|
# 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
|
|
}
|
|
|
|
for url in success_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 200, msg=f"Failed for {url}")
|
|
|
|
for url in fail_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 302, msg=f"Failed for {url}")
|
|
|
|
for url, redirect_to in success_urls_redirect.items():
|
|
response = client.get(url, follow=True)
|
|
# Expect redirects to the landing page
|
|
self.assertEqual(response.redirect_chain[0], (redirect_to, 302), msg=f"Failed for {url}")
|
|
|
|
def test_views_logged_in_zb_group(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assertion: User logged in and is registration office member
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username="root", password="root")
|
|
|
|
# Add user to default group
|
|
zb_group = Group.objects.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
|
|
}
|
|
|
|
for url in success_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 200, msg=f"Failed for {url}")
|
|
|
|
for url in fail_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 302, msg=f"Failed for {url}")
|
|
|
|
for url, redirect_to in success_urls_redirect.items():
|
|
response = client.get(url, follow=True)
|
|
# Expect redirects to the landing page
|
|
self.assertEqual(response.redirect_chain[0], (redirect_to, 302), msg=f"Failed for {url}")
|
|
|
|
def test_views_logged_in_ets_group(self):
|
|
""" Check correct status code for all requests
|
|
|
|
Assertion: User logged in and is registration office member
|
|
|
|
Returns:
|
|
|
|
"""
|
|
# Login client
|
|
client = Client()
|
|
client.login(username="root", password="root")
|
|
|
|
# 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
|
|
}
|
|
|
|
for url in success_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 200, msg=f"Failed for {url}")
|
|
|
|
for url in fail_urls:
|
|
response = client.get(url)
|
|
self.assertEqual(response.status_code, 302, msg=f"Failed for {url}")
|
|
|
|
for url, redirect_to in success_urls_redirect.items():
|
|
response = client.get(url, follow=True)
|
|
# Expect redirects to the landing page
|
|
self.assertEqual(response.redirect_chain[0], (redirect_to, 302), msg=f"Failed for {url}")
|