From bbbaeab4c770c9ffdc664cae2a04606ef2d8651f Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 10 Nov 2021 11:41:51 +0100 Subject: [PATCH] #19 Tests * moves dummy data creation from BaseViewTestCase into BaseTestCase --- konova/tests/test_views.py | 132 ++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/konova/tests/test_views.py b/konova/tests/test_views.py index 27a360f3..7e4efe44 100644 --- a/konova/tests/test_views.py +++ b/konova/tests/test_views.py @@ -26,6 +26,11 @@ class BaseTestCase(TestCase): groups = None superuser = None user = None + intervention = None + compensation = None + eco_account = None + comp_state = None + comp_action = None superuser_pw = "root" user_pw = "root" @@ -58,72 +63,6 @@ class BaseTestCase(TestCase): class Meta: abstract = True - -class BaseViewTestCase(BaseTestCase): - """ Wraps basic test functionality, reusable for every specialized ViewTestCase - - """ - login_url = None - intervention = None - compensation = None - eco_account = None - comp_state = None - comp_action = None - - @classmethod - def setUpTestData(cls) -> None: - cls.create_users() - cls.create_groups() - cls.create_dummy_intervention() - cls.create_dummy_compensation() - cls.create_dummy_eco_account() - cls.login_url = reverse("simple-sso-login") - - def assert_url_success(self, client: Client, urls: list): - """ Assert for all given urls a direct 200 response - - Args: - client (Client): The performing client - urls (list): An iterable list of urls to be checked - - Returns: - - """ - for url in urls: - response = client.get(url) - self.assertEqual(response.status_code, 200, msg=f"Failed for {url}") - - def assert_url_success_redirect(self, client: Client, urls: dict): - """ Assert for all given urls a 302 response to a certain location. - - Assert the redirect being the expected behaviour. - - Args: - client (Client): The performing client - urls (dict): An iterable dict of (urls, redirect_to_url) pairs to be checked - - Returns: - - """ - for url, redirect_to in urls.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 assert_url_fail(self, client: Client, urls: list): - """ Assert for all given urls a direct 302 response - - Args: - client (Client): The performing client - urls (list): An iterable list of urls to be checked - - Returns: - - """ - for url in urls: - response = client.get(url) - self.assertEqual(response.status_code, 302, msg=f"Failed for {url}") - @classmethod def create_dummy_intervention(cls): """ Creates an intervention which can be used for tests @@ -238,6 +177,67 @@ class BaseViewTestCase(BaseTestCase): return cls.comp_action +class BaseViewTestCase(BaseTestCase): + """ Wraps basic test functionality, reusable for every specialized ViewTestCase + + """ + login_url = None + + @classmethod + def setUpTestData(cls) -> None: + cls.create_users() + cls.create_groups() + cls.create_dummy_intervention() + cls.create_dummy_compensation() + cls.create_dummy_eco_account() + cls.login_url = reverse("simple-sso-login") + + def assert_url_success(self, client: Client, urls: list): + """ Assert for all given urls a direct 200 response + + Args: + client (Client): The performing client + urls (list): An iterable list of urls to be checked + + Returns: + + """ + for url in urls: + response = client.get(url) + self.assertEqual(response.status_code, 200, msg=f"Failed for {url}") + + def assert_url_success_redirect(self, client: Client, urls: dict): + """ Assert for all given urls a 302 response to a certain location. + + Assert the redirect being the expected behaviour. + + Args: + client (Client): The performing client + urls (dict): An iterable dict of (urls, redirect_to_url) pairs to be checked + + Returns: + + """ + for url, redirect_to in urls.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 assert_url_fail(self, client: Client, urls: list): + """ Assert for all given urls a direct 302 response + + Args: + client (Client): The performing client + urls (list): An iterable list of urls to be checked + + Returns: + + """ + for url in urls: + response = client.get(url) + self.assertEqual(response.status_code, 302, msg=f"Failed for {url}") + + class KonovaViewTestCase(BaseViewTestCase): """ Holds tests for all regular views, which are not app specific