generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #699 from ita-social-projects/694-tests-and-swagge…
…r-implement-automatic-moderation-time-setting Added tests for automoderation timeout setting and getting, extended …
- Loading branch information
Showing
10 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
BackEnd/administration/tests/test_automoderation_timeout_setting.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters