From 2afcfd93aa781643963f1330c2efd6379336041b Mon Sep 17 00:00:00 2001 From: DonHaul Date: Thu, 15 Aug 2024 13:49:35 +0200 Subject: [PATCH] tests viewset and admin --- backoffice/backoffice/workflows/admin.py | 77 ++++++++++++------- backoffice/backoffice/workflows/api/views.py | 1 - .../backoffice/workflows/tests/test_views.py | 41 ++++++++++ 3 files changed, 92 insertions(+), 27 deletions(-) diff --git a/backoffice/backoffice/workflows/admin.py b/backoffice/backoffice/workflows/admin.py index 308def4b..fc3dd09f 100644 --- a/backoffice/backoffice/workflows/admin.py +++ b/backoffice/backoffice/workflows/admin.py @@ -3,7 +3,7 @@ from django_json_widget.widgets import JSONEditorWidget from backoffice.management.permissions import IsAdminOrCuratorUser -from backoffice.workflows.models import Workflow +from backoffice.workflows.models import Decision, Workflow class WorkflowsAdminSite(admin.AdminSite): @@ -30,8 +30,37 @@ def has_permission(self, request): ) +class ModelAdmin(admin.ModelAdmin): + def has_view_permission(self, request, obj=None): + """ + Returns True if the user has permission to view the Workflow model. + """ + permission_check = IsAdminOrCuratorUser() + return request.user.is_superuser or permission_check.has_permission( + request, self + ) + + def has_change_permission(self, request, obj=None): + """ + Returns True if the user has permission to change the Workflow model. + """ + permission_check = IsAdminOrCuratorUser() + return request.user.is_superuser or permission_check.has_permission( + request, self + ) + + def has_delete_permission(self, request, obj=None): + """ + Returns True if the user has permission to delete the Workflow model. + """ + permission_check = IsAdminOrCuratorUser() + return request.user.is_superuser or permission_check.has_permission( + request, self + ) + + @admin.register(Workflow) -class WorkflowAdmin(admin.ModelAdmin): +class WorkflowAdmin(ModelAdmin): """ Admin class for Workflow model. Define get, update and delete permissions. """ @@ -60,29 +89,25 @@ class WorkflowAdmin(admin.ModelAdmin): JSONField: {"widget": JSONEditorWidget}, } - def has_view_permission(self, request, obj=None): - """ - Returns True if the user has permission to view the Workflow model. - """ - permission_check = IsAdminOrCuratorUser() - return request.user.is_superuser or permission_check.has_permission( - request, self - ) - def has_change_permission(self, request, obj=None): - """ - Returns True if the user has permission to change the Workflow model. - """ - permission_check = IsAdminOrCuratorUser() - return request.user.is_superuser or permission_check.has_permission( - request, self - ) +@admin.register(Decision) +class DecisionAdmin(admin.ModelAdmin): + """ + Admin class for Decision model. Define get, update and delete permissions. + """ - def has_delete_permission(self, request, obj=None): - """ - Returns True if the user has permission to delete the Workflow model. - """ - permission_check = IsAdminOrCuratorUser() - return request.user.is_superuser or permission_check.has_permission( - request, self - ) + ordering = ("-_updated_at",) + search_fields = ["id", "data"] + list_display = ( + "id", + "action", + "user", + ) + list_filter = [ + "action", + "user", + ] + + formfield_overrides = { + JSONField: {"widget": JSONEditorWidget}, + } diff --git a/backoffice/backoffice/workflows/api/views.py b/backoffice/backoffice/workflows/api/views.py index e7c25753..625a6b22 100644 --- a/backoffice/backoffice/workflows/api/views.py +++ b/backoffice/backoffice/workflows/api/views.py @@ -199,7 +199,6 @@ def resolve(self, request, pk=None): serializer = AuthorResolutionSerializer(data=request.data) if serializer.is_valid(raise_exception=True): extra_data = serializer.validated_data - extra_data["email"] = str(request.user.email) logger.info( "Trigger Airflow DAG: %s for %s", ResolutionDags[serializer.validated_data["value"]], diff --git a/backoffice/backoffice/workflows/tests/test_views.py b/backoffice/backoffice/workflows/tests/test_views.py index 0c8737dd..f1f3dd65 100644 --- a/backoffice/backoffice/workflows/tests/test_views.py +++ b/backoffice/backoffice/workflows/tests/test_views.py @@ -11,6 +11,8 @@ from django.test import TransactionTestCase from django.urls import reverse from django_opensearch_dsl.registries import registry +from opensearch_dsl import Index +from rest_framework import status from rest_framework.test import APIClient from backoffice.workflows import airflow_utils @@ -20,6 +22,7 @@ User = get_user_model() Workflow = apps.get_model(app_label="workflows", model_name="Workflow") +Decision = apps.get_model(app_label="workflows", model_name="Decision") class BaseTransactionTestCase(TransactionTestCase): @@ -471,3 +474,41 @@ def test_ordering(self): if previous_date is not None: assert cur_date < previous_date previous_date = cur_date + + +class TestDecisionsViewSet(BaseTransactionTestCase): + endpoint = "/api/decisions" + reset_sequences = True + fixtures = ["backoffice/fixtures/groups.json"] + + def setUp(self): + super().setUp() + self.workflow = Workflow.objects.create( + data={}, status="running", core=True, is_update=False + ) + + def test_create_decision(self): + self.api_client.force_authenticate(user=self.curator) + data = { + "workflow_id": self.workflow.id, + "action": "accept", + } + + url = reverse("api:decisions-list") + response = self.api_client.post(url, format="json", data=data) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + def test_get_decisoin(self): + self.api_client.force_authenticate(user=self.curator) + + decision = Decision.objects.create( + workflow_id=self.workflow, action="accept", user=self.curator + ) + + url = reverse("api:decisions-detail", kwargs={"pk": decision.id}) + + print("wawa") + print(url) + response = self.api_client.get(f"{url}/{decision.id}") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(decision.id, response.data[id])