From a5e5e140d6d419562aec4021a0c04e21919b7555 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 16 Dec 2020 23:42:44 +0100 Subject: [PATCH] admin: add full api tests --- authentik/admin/api/tasks.py | 2 +- authentik/admin/api/version.py | 2 +- authentik/admin/tests/test_api.py | 36 ++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/authentik/admin/api/tasks.py b/authentik/admin/api/tasks.py index dbaf5d5e1..314d7cae7 100644 --- a/authentik/admin/api/tasks.py +++ b/authentik/admin/api/tasks.py @@ -66,7 +66,7 @@ class TaskViewSet(ViewSet): "successful": True, } ) - except ImportError: + except ImportError: # pragma: no cover # if we get an import error, the module path has probably changed task.delete() return Response({"successful": False}) diff --git a/authentik/admin/api/version.py b/authentik/admin/api/version.py index 93c099151..d12584f7c 100644 --- a/authentik/admin/api/version.py +++ b/authentik/admin/api/version.py @@ -28,7 +28,7 @@ class VersionSerializer(Serializer): def get_version_latest(self, _) -> str: """Get latest version from cache""" version_in_cache = cache.get(VERSION_CACHE_KEY) - if not version_in_cache: + if not version_in_cache: # pragma: no cover update_latest_version.delay() return __version__ return version_in_cache diff --git a/authentik/admin/tests/test_api.py b/authentik/admin/tests/test_api.py index fbcf762f1..6e0ee5547 100644 --- a/authentik/admin/tests/test_api.py +++ b/authentik/admin/tests/test_api.py @@ -19,6 +19,37 @@ class TestAdminAPI(TestCase): self.group.save() self.client.force_login(self.user) + def test_tasks(self): + """Test Task API""" + response = self.client.get(reverse("authentik_api:admin_system_tasks-list")) + self.assertEqual(response.status_code, 200) + body = loads(response.content) + self.assertTrue( + any([task["task_name"] == "clean_expired_models" for task in body]) + ) + + def test_tasks_retry(self): + """Test Task API (retry)""" + response = self.client.post( + reverse( + "authentik_api:admin_system_tasks-retry", + kwargs={"pk": "clean_expired_models"}, + ) + ) + self.assertEqual(response.status_code, 200) + body = loads(response.content) + self.assertTrue(body["successful"]) + + def test_tasks_retry_404(self): + """Test Task API (retry, 404)""" + response = self.client.post( + reverse( + "authentik_api:admin_system_tasks-retry", + kwargs={"pk": "qwerqewrqrqewrqewr"}, + ) + ) + self.assertEqual(response.status_code, 404) + def test_version(self): """Test Version API""" response = self.client.get(reverse("authentik_api:admin_version-list")) @@ -37,8 +68,3 @@ class TestAdminAPI(TestCase): """Test metrics API""" response = self.client.get(reverse("authentik_api:admin_metrics-list")) self.assertEqual(response.status_code, 200) - - def test_tasks(self): - """Test tasks metrics API""" - response = self.client.get(reverse("authentik_api:admin_system_tasks-list")) - self.assertEqual(response.status_code, 200)