Skip to content

Commit

Permalink
Merge pull request #699 from ita-social-projects/694-tests-and-swagge…
Browse files Browse the repository at this point in the history
…r-implement-automatic-moderation-time-setting

Added tests for automoderation timeout setting and getting, extended …
  • Loading branch information
YanZhylavy authored Jul 19, 2024
2 parents d3acee3 + 054f157 commit 1b63cbe
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 17 deletions.
15 changes: 15 additions & 0 deletions BackEnd/administration/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from rest_framework.permissions import BasePermission, SAFE_METHODS


class IsStaffUser(BasePermission):
"""
Custom is staff permission.
"""

def has_permission(self, request, view):
return request.user.is_staff


class IsStaffUserOrReadOnly(BasePermission):
def has_permission(self, request, view):
return request.user.is_staff or request.method in SAFE_METHODS
2 changes: 2 additions & 0 deletions BackEnd/administration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class Meta:


class AutoModerationHoursSerializer(serializers.ModelSerializer):
auto_moderation_hours = serializers.IntegerField(min_value=1, max_value=48)

class Meta:
model = AutoModeration
fields = ("auto_moderation_hours",)
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from rest_framework.test import APITestCase
from rest_framework import status

from administration.factories import AdminUserFactory


class TestAutomoderationSettingisStaff(APITestCase):
def setUp(self):
self.user = AdminUserFactory()
self.client.force_authenticate(self.user)

def test_positive_get_automoderation_timeout(self):
response = self.client.get("/api/admin/automoderation/")
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_positive_set_automoderation_timeout(self):
response = self.client.put(
"/api/admin/automoderation/", {"auto_moderation_hours": 24}
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
updated_response = self.client.get("/api/admin/automoderation/")
self.assertEqual(updated_response.status_code, status.HTTP_200_OK)
self.assertEqual(updated_response.data["auto_moderation_hours"], 24)

def test_negative_set_incorrect_timeout(self):
response = self.client.put(
"/api/admin/automoderation/", {"auto_moderation_hours": 50}
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_negative_set_incorrect_negative_timeout(self):
response = self.client.put(
"/api/admin/automoderation/", {"auto_moderation_hours": -12}
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_negative_set_nonnumeric_timeout(self):
response = self.client.put(
"/api/admin/automoderation/", {"auto_moderation_hours": "four"}
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)


class TestAutomoderationSettingRegularUser(APITestCase):
def setUp(self):
self.user = AdminUserFactory(is_staff=False)
self.client.force_authenticate(self.user)

def test_positive_get_automoderation_common_user(self):
response = self.client.get("/api/admin/automoderation/")
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_negative_set_automoderation_common_user(self):
response = self.client.put(
"/api/admin/automoderation/", {"auto_moderation_hours": 20}
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)


class TestAutomoderationSettingAnonymousUser(APITestCase):
def test_negative_get_automoderation_anonymous_user(self):
response = self.client.put("/api/admin/automoderation/")
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_negative_set_automoderation_anonymous_user(self):
response = self.client.put(
"/api/admin/automoderation/", {"auto_moderation_hours": 20}
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
48 changes: 37 additions & 11 deletions BackEnd/administration/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from drf_spectacular.utils import (
extend_schema,
OpenApiExample,
OpenApiResponse,
)

from rest_framework.permissions import (
BasePermission,
)
Expand All @@ -19,15 +25,7 @@
from administration.models import AutoModeration
from authentication.models import CustomUser
from profiles.models import Profile


class IsStaffUser(BasePermission):
"""
Custom is staff permission.
"""

def has_permission(self, request, view):
return request.user.is_staff
from .permissions import IsStaffUser, IsStaffUserOrReadOnly


class UsersListView(ListAPIView):
Expand Down Expand Up @@ -85,13 +83,41 @@ class ProfileDetailView(RetrieveUpdateDestroyAPIView):
)


@extend_schema(
request=AutoModerationHoursSerializer,
responses={
200: OpenApiResponse(
response=AutoModerationHoursSerializer,
examples=[
OpenApiExample(
"Valid example",
summary="Valid example",
description="A valid example with auto_moderation_hours set to 24",
value={"auto_moderation_hours": 24},
)
],
),
400: OpenApiResponse(
response=AutoModerationHoursSerializer,
examples=[
OpenApiExample(
"Invalid example",
summary="Invalid example",
description="An invalid example with auto_moderation_hours set to 50 (out of range)",
value={"auto_moderation_hours": 50},
)
],
),
},
)
class AutoModerationHoursView(RetrieveUpdateAPIView):
"""
View for retrieving and updating 'auto_moderation_hours' - a value that sets
the auto-approve delay (part of the moderation functionality)
the auto-approve delay (part of the moderation functionality).
Value must be an integer between 1 and 48
"""

permission_classes = [IsStaffUser]
permission_classes = [IsStaffUserOrReadOnly]
serializer_class = AutoModerationHoursSerializer

def get_object(self):
Expand Down
1 change: 0 additions & 1 deletion BackEnd/forum/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class TokenDestroyViewExtension(OpenApiViewExtension):
target_class = TokenDestroyView

def view_replacement(self):

class Fixed(self.target_class):
serializer_class = None

Expand Down
1 change: 0 additions & 1 deletion BackEnd/images/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def image_directory_path(instance, filename):


class ProfileImage(models.Model):

BANNER = "banner"
LOGO = "logo"

Expand Down
1 change: 0 additions & 1 deletion BackEnd/images/tests/test_add_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class TestBannerChange(APITestCase):

def setUp(self) -> None:
self.right_banner = open(
os.path.join(os.getcwd(), "images/tests/img/img_2mb.png"),
Expand Down
1 change: 0 additions & 1 deletion BackEnd/images/tests/test_delete_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class TestBannerChange(APITestCase):

def setUp(self) -> None:
self.banner_path = os.path.join(
os.getcwd(), "images", "tests", "img", "img_2mb.png"
Expand Down
1 change: 0 additions & 1 deletion BackEnd/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Meta:


class ProfileImageField(serializers.Field):

def to_representation(self, value):
if value.is_deleted == False:
return {
Expand Down
1 change: 0 additions & 1 deletion BackEnd/search/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class ProfileImageField(serializers.Field):

def to_representation(self, value):
if value.is_deleted == False:
return {
Expand Down

0 comments on commit 1b63cbe

Please sign in to comment.