Skip to content

Commit

Permalink
✅Test : 게시물 좋아요 api에 대한 post 테스트 실패 #12
Browse files Browse the repository at this point in the history
- 인증되지 않은 사용자가 게시물에 post 요청을 수행 시 실패 테스트 케이스
- 인증된 사용자가 게시물에 post 요청을 수행 시 실패 테스트 케이스

Related to #12
  • Loading branch information
Chestnut90 committed Oct 29, 2023
1 parent c3a1a54 commit dc83f45
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions shares/tests/test_shares_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from posts.models import Post


class SharesAPITestCase(APITestCase):
"""
Expand All @@ -11,3 +15,39 @@ class SharesAPITestCase(APITestCase):

def test_post_failure_no_api(self):
path = reverse(self.viewname, kwargs={"content_id": None})

def test_post_without_auth(self):
"""
인증되지 않은 사용자가 shares에 post 요청을 전달하고,
401(unauth) 응답을 얻습니다.
"""

post = Post.objects.create(title="title")
self.client.logout()
response = self.client.post(
path=reverse(self.viewname, kwargs={"content_id": post.content_id}),
data=None,
)

self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # status 401

def test_post_with_auth(self):
"""
인증된 사용자가 shares에 post 요청을 전달하고,
200(Ok) 응답을 얻으며 share_count가 증가합니다.
"""

post = Post.objects.create(title="title")

user = get_user_model().objects.create(username="username")
self.client.force_authenticate(user=user)

response = self.client.post(
path=reverse(self.viewname, kwargs={"content_id": post.content_id}),
data=None,
)

self.assertEqual(response.status_code, status.HTTP_200_OK) # status 200

post_after_response = Post.objects.get(content_id=post.content_id)
self.assertEqual(post.share_count + 1, post_after_response.share_count) # share_count diff 1

0 comments on commit dc83f45

Please sign in to comment.