-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
8 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,17 @@ | ||
from rest_framework import permissions | ||
from rest_framework.request import Request | ||
from rest_framework.views import APIView | ||
|
||
|
||
class IsGlobalNoticeAthenticated(permissions.IsAuthenticated): | ||
def has_permission(self, request: Request, view: APIView) -> bool: | ||
if request.method not in permissions.SAFE_METHODS: | ||
return request.user.is_staff or request.user.is_superuser | ||
|
||
# SAFE_METHODS는 비로그인 허용 | ||
return True | ||
|
||
|
||
class GlobalNoticePermission(permissions.BasePermission): | ||
def has_permission(self, request: Request, view: APIView) -> bool: | ||
return super().has_permission(request, view) |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
from rest_framework import viewsets | ||
from django.utils import timezone | ||
from rest_framework import permissions, viewsets | ||
|
||
from apps.global_notice.models import GlobalNotice | ||
from apps.global_notice.permissions import ( | ||
GlobalNoticePermission, | ||
IsGlobalNoticeAthenticated, | ||
) | ||
from apps.global_notice.serializers import GlobalNoticeSerializer | ||
|
||
|
||
class GlobalNoticeViewSet(viewsets.ModelViewSet): | ||
queryset = GlobalNotice.objects.all() | ||
queryset = GlobalNotice.objects.filter( | ||
started_at__lte=timezone.now(), expired_at__gte=timezone.now() | ||
) | ||
serializer_class = GlobalNoticeSerializer | ||
|
||
permission_classes = ( | ||
IsGlobalNoticeAthenticated, | ||
GlobalNoticePermission, | ||
) | ||
action_permission_classes = { | ||
"create": (permissions.IsAuthenticated, GlobalNoticePermission) | ||
} | ||
pagination_class = None |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import pytest | ||
from django.utils import timezone | ||
from rest_framework import status | ||
|
||
from apps.core.views import status | ||
from apps.global_notice.models import GlobalNotice | ||
from tests.conftest import RequestSetting, TestCase | ||
|
||
|
||
@pytest.mark.usefixtures("set_admin_client", "set_user_client") | ||
class TestGlobalNotice(TestCase, RequestSetting): | ||
N = 50 | ||
|
||
def test_list(self): | ||
""" | ||
비로그인도 허용 | ||
""" | ||
for i in range(self.N): | ||
GlobalNotice.objects.create( | ||
title=f"global_notice title {i}", | ||
content=f"global_notice content {i}", | ||
started_at=timezone.now() - timezone.timedelta(days=1), | ||
expired_at=timezone.now() + timezone.timedelta(days=1), | ||
) | ||
res = self.http_request(self.user, "get", "global_notices") | ||
assert len(res.data) == self.N | ||
|
||
res = self.http_request(None, "get", "global_notices") | ||
assert len(res.data) == self.N | ||
|
||
def test_get(self): | ||
""" | ||
비로그인도 허용 | ||
""" | ||
global_notice = GlobalNotice.objects.create( | ||
title=f"global_notice title", | ||
content=f"global_notice content", | ||
started_at=timezone.now() - timezone.timedelta(days=1), | ||
expired_at=timezone.now() + timezone.timedelta(days=1), | ||
) | ||
res = self.http_request(self.user, "get", f"global_notices/{global_notice.id}") | ||
assert res.data["title"] == global_notice.title | ||
|
||
res = self.http_request(None, "get", f"global_notices/{global_notice.id}") | ||
assert res.data["title"] == global_notice.title | ||
|
||
def test_filter(self): | ||
""" | ||
expired, not started 필터링 테스트 | ||
""" | ||
global_notice_expired = GlobalNotice.objects.create( | ||
title="global_notice title", | ||
content="global_notice content", | ||
started_at=timezone.now() - timezone.timedelta(days=2), | ||
expired_at=timezone.now() - timezone.timedelta(days=1), | ||
) | ||
global_notice_not_started = GlobalNotice.objects.create( | ||
title="global_notice title", | ||
content="global_notice content", | ||
started_at=timezone.now() + timezone.timedelta(days=1), | ||
expired_at=timezone.now() + timezone.timedelta(days=2), | ||
) | ||
|
||
res = self.http_request( | ||
self.user, "get", f"global_notices/{global_notice_expired.id}" | ||
) | ||
assert res.status_code == status.HTTP_404_NOT_FOUND | ||
|
||
res = self.http_request( | ||
self.user, "get", f"global_notices/{global_notice_not_started.id}" | ||
) | ||
assert res.status_code == status.HTTP_404_NOT_FOUND | ||
|
||
def test_create(self): | ||
notice_data = { | ||
"title": "global_notice title", | ||
"content": "global_notice content", | ||
"started_at": timezone.now() - timezone.timedelta(days=1), | ||
"expired_at": timezone.now() + timezone.timedelta(days=1), | ||
} | ||
res_user = self.http_request(self.user, "post", "global_notices", notice_data) | ||
assert res_user.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
for _ in range(self.N): | ||
res_admin = self.http_request( | ||
self.admin, "post", "global_notices", notice_data | ||
) | ||
assert res_admin.status_code == status.HTTP_201_CREATED | ||
|
||
assert GlobalNotice.objects.count() == self.N | ||
|
||
def test_update(self): | ||
global_notice = GlobalNotice.objects.create( | ||
title="global_notice title", | ||
content="global_notice content", | ||
started_at=timezone.now() - timezone.timedelta(days=1), | ||
expired_at=timezone.now() + timezone.timedelta(days=1), | ||
) | ||
new_title = "new title" | ||
new_content = "new content" | ||
|
||
res_user = self.http_request( | ||
self.user, | ||
"patch", | ||
f"global_notices/{global_notice.id}", | ||
{"title": new_title, "content": new_content}, | ||
) | ||
assert res_user.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
res_admin = self.http_request( | ||
self.admin, | ||
"patch", | ||
f"global_notices/{global_notice.id}", | ||
{"title": new_title, "content": new_content}, | ||
) | ||
assert res_admin.status_code == status.HTTP_200_OK | ||
assert res_admin.data["title"] == new_title | ||
assert res_admin.data["content"] == new_content | ||
|
||
def test_destroy(self): | ||
global_notice = GlobalNotice.objects.create( | ||
title="global_notice title", | ||
content="global_notice content", | ||
started_at=timezone.now() - timezone.timedelta(days=1), | ||
expired_at=timezone.now() + timezone.timedelta(days=1), | ||
) | ||
res_user = self.http_request( | ||
self.user, | ||
"delete", | ||
f"global_notices/{global_notice.id}", | ||
) | ||
assert res_user.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
res_admin = self.http_request( | ||
self.admin, | ||
"delete", | ||
f"global_notices/{global_notice.id}", | ||
) | ||
assert res_admin.status_code == status.HTTP_204_NO_CONTENT | ||
assert GlobalNotice.objects.count() == 0 |