Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
tests viewset and admin
Browse files Browse the repository at this point in the history
  • Loading branch information
DonHaul committed Aug 15, 2024
1 parent 8237006 commit 2afcfd9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 27 deletions.
77 changes: 51 additions & 26 deletions backoffice/backoffice/workflows/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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},
}
1 change: 0 additions & 1 deletion backoffice/backoffice/workflows/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]],
Expand Down
41 changes: 41 additions & 0 deletions backoffice/backoffice/workflows/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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])

0 comments on commit 2afcfd9

Please sign in to comment.