Skip to content

Commit

Permalink
feat(global-notice): add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ddungiii committed Jan 2, 2024
1 parent 3698979 commit e45fa08
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 8 deletions.
17 changes: 17 additions & 0 deletions apps/global_notice/permissions.py
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)
2 changes: 1 addition & 1 deletion apps/global_notice/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class GlobalNoticeSerializer(MetaDataModelSerializer):
class Meta:
model = GlobalNotice
fields = ["title", "content"]
fields = ["title", "content", "started_at", "expired_at"]
3 changes: 0 additions & 3 deletions apps/global_notice/tests.py

This file was deleted.

2 changes: 1 addition & 1 deletion apps/global_notice/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
from apps.global_notice.views import GlobalNoticeViewSet

router = DefaultRouter()
router.register(r"/api/globalNotice", GlobalNoticeViewSet, basename="globalNotice")
router.register(r"api/global_notices", GlobalNoticeViewSet, basename="global_notices")

urlpatterns = router.urls
20 changes: 18 additions & 2 deletions apps/global_notice/views.py
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
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def set_admin_client(request):
agree_terms_of_service_at=timezone.now(),
)
client = APIClient()
client.force_authenticate(user=request.cls.user)
client.force_authenticate(user=request.cls.admin)
request.cls.api_client = client


Expand Down
140 changes: 140 additions & 0 deletions tests/test_global_notice.py
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

0 comments on commit e45fa08

Please sign in to comment.