parent
5de3f4c24e
commit
4ec8e1ae07
@ -1,3 +0,0 @@
|
|||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
# Create your tests here.
|
|
7
user/tests/__init__.py
Normal file
7
user/tests/__init__.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 30.05.22
|
||||||
|
|
||||||
|
"""
|
112
user/tests/test_views.py
Normal file
112
user/tests/test_views.py
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 30.05.22
|
||||||
|
|
||||||
|
"""
|
||||||
|
from django.test import Client
|
||||||
|
|
||||||
|
from django.contrib.auth.models import Group
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from intervention.models import Revocation
|
||||||
|
from konova.settings import DEFAULT_GROUP, ZB_GROUP, ETS_GROUP
|
||||||
|
from konova.tests.test_views import BaseViewTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class UserViewTestCase(BaseViewTestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls) -> None:
|
||||||
|
super().setUpTestData()
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
super().setUp()
|
||||||
|
self.team.users.add(self.superuser)
|
||||||
|
self.team.admins.add(self.superuser)
|
||||||
|
# Prepare urls
|
||||||
|
self.index_url = reverse("user:index", args=())
|
||||||
|
self.notification_url = reverse("user:notifications", args=())
|
||||||
|
self.api_token_url = reverse("user:api-token", args=())
|
||||||
|
self.contact_url = reverse("user:contact", args=(self.superuser.id,))
|
||||||
|
self.team_url = reverse("user:team-index", args=())
|
||||||
|
self.new_team_url = reverse("user:team-new", args=())
|
||||||
|
self.data_team_url = reverse("user:team-data", args=(self.team.id,))
|
||||||
|
self.edit_team_url = reverse("user:team-edit", args=(self.team.id,))
|
||||||
|
self.remove_team_url = reverse("user:team-remove", args=(self.team.id,))
|
||||||
|
self.leave_team_url = reverse("user:team-leave", args=(self.team.id,))
|
||||||
|
|
||||||
|
def test_views_anonymous_user(self):
|
||||||
|
""" Check correct status code for all requests
|
||||||
|
|
||||||
|
Assumption: User not logged in
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
# Unknown client
|
||||||
|
client = Client()
|
||||||
|
|
||||||
|
login_redirect_base = f"{self.login_url}?next="
|
||||||
|
fail_urls = {
|
||||||
|
self.index_url: f"{login_redirect_base}{self.index_url}",
|
||||||
|
self.notification_url: f"{login_redirect_base}{self.notification_url}",
|
||||||
|
self.api_token_url: f"{login_redirect_base}{self.api_token_url}",
|
||||||
|
self.contact_url: f"{login_redirect_base}{self.contact_url}",
|
||||||
|
self.team_url: f"{login_redirect_base}{self.team_url}",
|
||||||
|
self.new_team_url: f"{login_redirect_base}{self.new_team_url}",
|
||||||
|
self.data_team_url: f"{login_redirect_base}{self.data_team_url}",
|
||||||
|
self.edit_team_url: f"{login_redirect_base}{self.edit_team_url}",
|
||||||
|
self.remove_team_url: f"{login_redirect_base}{self.remove_team_url}",
|
||||||
|
self.leave_team_url: f"{login_redirect_base}{self.leave_team_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(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)
|
||||||
|
self.superuser.groups.set([])
|
||||||
|
success_urls = [
|
||||||
|
self.index_url,
|
||||||
|
self.notification_url,
|
||||||
|
self.contact_url,
|
||||||
|
self.team_url,
|
||||||
|
self.new_team_url,
|
||||||
|
self.data_team_url,
|
||||||
|
self.edit_team_url,
|
||||||
|
self.remove_team_url,
|
||||||
|
self.leave_team_url,
|
||||||
|
]
|
||||||
|
|
||||||
|
fail_urls = [
|
||||||
|
self.api_token_url, # expects default permission
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assert_url_success(client, success_urls)
|
||||||
|
self.assert_url_fail(client, fail_urls)
|
||||||
|
|
||||||
|
# Check for modified default user permission
|
||||||
|
self.superuser.groups.add(
|
||||||
|
Group.objects.get(
|
||||||
|
name=DEFAULT_GROUP
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
success_url = [
|
||||||
|
self.api_token_url, # must work now
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assert_url_success(client, success_url)
|
||||||
|
|
158
user/tests/test_workflow.py
Normal file
158
user/tests/test_workflow.py
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
"""
|
||||||
|
Author: Michel Peltriaux
|
||||||
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||||
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||||
|
Created on: 30.05.22
|
||||||
|
|
||||||
|
"""
|
||||||
|
from django.urls import reverse
|
||||||
|
from konova.tests.test_views import BaseWorkflowTestCase
|
||||||
|
from user.models import Team
|
||||||
|
|
||||||
|
|
||||||
|
class UserWorkflowTestCase(BaseWorkflowTestCase):
|
||||||
|
""" This test case adds workflow tests
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
super().setUpTestData()
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
super().setUp()
|
||||||
|
|
||||||
|
# Add user to team
|
||||||
|
self.team.users.add(self.superuser)
|
||||||
|
|
||||||
|
def test_new_team(self):
|
||||||
|
"""
|
||||||
|
Check a normal creation of a new team.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
team_name = self.create_dummy_string()
|
||||||
|
team_description = self.create_dummy_string()
|
||||||
|
|
||||||
|
new_url = reverse("user:team-new", args=())
|
||||||
|
|
||||||
|
post_data = {
|
||||||
|
"name": team_name,
|
||||||
|
"description": team_description,
|
||||||
|
"members": [self.superuser.id],
|
||||||
|
}
|
||||||
|
response = self.client_user.post(
|
||||||
|
new_url,
|
||||||
|
post_data
|
||||||
|
)
|
||||||
|
response_code = response.status_code
|
||||||
|
self.assertEqual(response_code, 302, msg=f"Unexpected status code received from response ({response_code})")
|
||||||
|
new_team = Team.objects.get(
|
||||||
|
name=team_name
|
||||||
|
)
|
||||||
|
self.assertEqual(new_team.description, team_description)
|
||||||
|
self.assertEqual([self.superuser], list(new_team.users.all()))
|
||||||
|
self.assertEqual([self.superuser], list(new_team.admins.all()), msg="Creator is not admin by default but should!")
|
||||||
|
|
||||||
|
def test_edit_team(self):
|
||||||
|
"""
|
||||||
|
Check editing of an existing team.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
existing_team = self.team
|
||||||
|
existing_team_name = existing_team.name
|
||||||
|
existing_team_description = existing_team.description
|
||||||
|
|
||||||
|
edited_team_name = self.create_dummy_string()
|
||||||
|
edited_team_description = self.create_dummy_string()
|
||||||
|
|
||||||
|
new_url = reverse("user:team-edit", args=(existing_team.id,))
|
||||||
|
|
||||||
|
post_data = {
|
||||||
|
"name": edited_team_name,
|
||||||
|
"description": edited_team_description,
|
||||||
|
}
|
||||||
|
# Expect the first try to fail since user is member but not admin of the team
|
||||||
|
response = self.client_user.post(
|
||||||
|
new_url,
|
||||||
|
post_data
|
||||||
|
)
|
||||||
|
response_code = response.status_code
|
||||||
|
self.assertEqual(response_code, 404, msg=f"Unexpected status code received from response ({response_code})")
|
||||||
|
|
||||||
|
# Now add the user to the list of team admins and try again!
|
||||||
|
existing_team.admins.add(self.superuser)
|
||||||
|
response = self.client_user.post(
|
||||||
|
new_url,
|
||||||
|
post_data
|
||||||
|
)
|
||||||
|
response_code = response.status_code
|
||||||
|
self.assertEqual(response_code, 200, msg=f"Unexpected status code received from response ({response_code})")
|
||||||
|
|
||||||
|
existing_team.refresh_from_db()
|
||||||
|
self.assertEqual(existing_team.description, existing_team_description)
|
||||||
|
self.assertEqual(existing_team.name, existing_team_name)
|
||||||
|
self.assertEqual([self.superuser], list(existing_team.users.all()))
|
||||||
|
self.assertEqual([self.superuser], list(existing_team.admins.all()), msg="Creator is not admin by default but should!")
|
||||||
|
|
||||||
|
def test_leave_team(self):
|
||||||
|
"""
|
||||||
|
Checks leaving of a user from an existing team.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
existing_team = self.team
|
||||||
|
|
||||||
|
new_url = reverse("user:team-leave", args=(existing_team.id,))
|
||||||
|
|
||||||
|
post_data = {
|
||||||
|
"confirm": True,
|
||||||
|
}
|
||||||
|
response = self.client_user.post(
|
||||||
|
new_url,
|
||||||
|
post_data
|
||||||
|
)
|
||||||
|
response_code = response.status_code
|
||||||
|
self.assertEqual(response_code, 302, msg=f"Unexpected status code received from response ({response_code})")
|
||||||
|
existing_team.refresh_from_db()
|
||||||
|
|
||||||
|
self.assertEqual([], list(existing_team.users.all()))
|
||||||
|
self.assertEqual([], list(existing_team.admins.all()))
|
||||||
|
|
||||||
|
def test_remove_team(self):
|
||||||
|
"""
|
||||||
|
Checks removing of an existing team.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
existing_team = self.team
|
||||||
|
|
||||||
|
new_url = reverse("user:team-remove", args=(existing_team.id,))
|
||||||
|
|
||||||
|
post_data = {
|
||||||
|
"confirm": True,
|
||||||
|
}
|
||||||
|
# User is member but not admin. This response must fail!
|
||||||
|
response = self.client_user.post(
|
||||||
|
new_url,
|
||||||
|
post_data
|
||||||
|
)
|
||||||
|
response_code = response.status_code
|
||||||
|
self.assertEqual(response_code, 404, msg=f"Unexpected status code received from response ({response_code})")
|
||||||
|
|
||||||
|
# Add user to admins and try again
|
||||||
|
existing_team.admins.add(self.superuser)
|
||||||
|
response = self.client_user.post(
|
||||||
|
new_url,
|
||||||
|
post_data
|
||||||
|
)
|
||||||
|
response_code = response.status_code
|
||||||
|
self.assertEqual(response_code, 302, msg=f"Unexpected status code received from response ({response_code})")
|
||||||
|
existing_team.refresh_from_db()
|
||||||
|
self.assertIsNotNone(existing_team.deleted, msg="Deleted action not created")
|
||||||
|
self.assertNotIn(existing_team, self.superuser.shared_teams)
|
Loading…
Reference in New Issue
Block a user