From e0de67e01dff014c5ea01001115ac6196ffd7a48 Mon Sep 17 00:00:00 2001 From: JinHoooooou Date: Tue, 13 Jul 2021 14:33:15 +0900 Subject: [PATCH 1/5] add test to success create_comment_to_post api --- .../step2/jinho/djangogirls/blog/tests.py | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py b/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py index 7f7579f..67001f9 100644 --- a/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py +++ b/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py @@ -1,11 +1,12 @@ import json from http.client import NOT_FOUND, OK +from http import HTTPStatus from django.contrib.auth.models import User from django.test import TestCase from django.urls import reverse -from .models import Post +from .models import Post, Comment class TestPostMixin: @@ -172,4 +173,27 @@ def test_post_update_with_error_with_post_on_404(self): self.assertEqual(post.text, "test text") response = json.loads(response.content) # And: 응답 메세지로 post를 찾을 수 없습니다. 를 리턴 해야 한다. - self.assertEqual(response["message"], "post를 찾을 수 없습니다.") \ No newline at end of file + self.assertEqual(response["message"], "post를 찾을 수 없습니다.") + + +class TestCommentCreate(TestPostMixin, TestCase): + def setUp(self): + super().setUp() + self.post = Post.objects.create(author=self.author, title="test title", text="test text") + + def test_create_comment_to_post(self): + # Given: request body가 주어지고 + request_body = {"author": "test by jinho", "post_id": self.post.id, "text": "test comment"} + + # When: create_comment_to_post api를 호출한다. + response = self.client.post(reverse("create_comment_to_post"), data=request_body) + + # Then: 상태코드는 201이고 + self.assertEqual(response.status_code, HTTPStatus.CREATED) + # And: 응답값에서 생성된 comment의 author, post_id, text를 리턴한다. + response = json.loads(response.content)["comment"] + self.assertEqual(response["author"], "test by jinho") + self.assertEqual(response["post_id"], self.post.id) + self.assertEqual(response["text"], "test comment") + # And: post의 comment는 1개이다. + self.assertEqual(Comment.objects.filter(post_id=self.post.id).count(), 1) From ca17e3b79deeab85ae88f29a992e5d9b2e8139b8 Mon Sep 17 00:00:00 2001 From: JinHoooooou Date: Tue, 13 Jul 2021 14:34:14 +0900 Subject: [PATCH 2/5] add create_comment_to_post api url --- django_girls_tutorial/step2/jinho/djangogirls/blog/api/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_girls_tutorial/step2/jinho/djangogirls/blog/api/urls.py b/django_girls_tutorial/step2/jinho/djangogirls/blog/api/urls.py index 0cb200f..51c4dc6 100644 --- a/django_girls_tutorial/step2/jinho/djangogirls/blog/api/urls.py +++ b/django_girls_tutorial/step2/jinho/djangogirls/blog/api/urls.py @@ -1,10 +1,10 @@ from django.urls import path from . import views - urlpatterns = [ path("posts/", views.retrieve_post_list, name="retrieve_post_list"), path("posts/", views.retrieve_post_detail, name="retrieve_post_detail"), path("posts/create", views.create_post, name="create_post"), path("posts//put/update", views.update_post_with_put, name="update_post_with_put"), + path("comments/create", views.create_comment_to_post, name="create_comment_to_post"), ] From 8424277dbd22d3ba1324c27db327234ca9c89c8f Mon Sep 17 00:00:00 2001 From: JinHoooooou Date: Tue, 13 Jul 2021 14:36:27 +0900 Subject: [PATCH 3/5] add create_comment_to_post method --- .../step2/jinho/djangogirls/blog/api/views.py | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py b/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py index 015c98b..08616f0 100644 --- a/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py +++ b/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py @@ -1,12 +1,13 @@ import json -from http.client import NOT_FOUND, OK +from http import HTTPStatus from django.contrib.auth.models import User from django.http import JsonResponse, QueryDict from django.utils import timezone from django.views.decorators.http import require_http_methods -from blog.models import Post +from blog.form import CommentForm +from blog.models import Post, Comment def retrieve_post_list(request): @@ -87,3 +88,24 @@ def update_post_with_put(request, id): FYI, request.body 활용 """ + + +@require_http_methods(["POST"]) +def create_comment_to_post(request): + body = request.POST + comment_form = CommentForm(body) + if comment_form.is_valid(): + comment = comment_form.save(commit=False) + comment.author = body["author"] + comment.post = Post.objects.get(id=body["post_id"]) + comment.text = body["text"] + comment.save() + return JsonResponse({ + "comment": { + "id": comment.id, + "author": comment.author, + "post_id": comment.post.id, + "text": comment.text, + } + }, status=HTTPStatus.CREATED) + return JsonResponse({"message": "form is invalid"}, status=HTTPStatus.BAD_REQUEST) From 752e522e0e11c09e205add9f8061ecc57dd88e4a Mon Sep 17 00:00:00 2001 From: JinHoooooou Date: Tue, 13 Jul 2021 14:48:09 +0900 Subject: [PATCH 4/5] add test to fail create_comment_to_post api --- .../step2/jinho/djangogirls/blog/tests.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py b/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py index 67001f9..1e9be43 100644 --- a/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py +++ b/django_girls_tutorial/step2/jinho/djangogirls/blog/tests.py @@ -197,3 +197,19 @@ def test_create_comment_to_post(self): self.assertEqual(response["text"], "test comment") # And: post의 comment는 1개이다. self.assertEqual(Comment.objects.filter(post_id=self.post.id).count(), 1) + + def test_create_comment_to_post_with_invalid_post(self): + # Given: request body에 유효하지 않은 post_id가 주어진다. + invalid_post_id = 369 + request_body = {"author": "test by jinho", "post_id": invalid_post_id, "text": "test comment"} + + # When: create_comment_to_post api를 호출 + response = self.client.post(reverse("create_comment_to_post"), data=request_body) + + # Then: 상태코드는 404이고 + self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND) + # And: 응답메세지로 "Post를 찾을 수 없습니다."를 리턴한다. + message = json.loads(response.content)["message"] + self.assertEqual(message, "Post를 찾을 수 없습니다.") + # And: comment는 추가되지 않으므로 개수는 0개이다. + self.assertEqual(Comment.objects.all().count(), 0) From 66251a0c1654208534f83b2e3d8874d94b146452 Mon Sep 17 00:00:00 2001 From: JinHoooooou Date: Tue, 13 Jul 2021 14:49:37 +0900 Subject: [PATCH 5/5] modify create_comment_to_post method --- .../step2/jinho/djangogirls/blog/api/views.py | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py b/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py index 08616f0..b448e23 100644 --- a/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py +++ b/django_girls_tutorial/step2/jinho/djangogirls/blog/api/views.py @@ -93,19 +93,24 @@ def update_post_with_put(request, id): @require_http_methods(["POST"]) def create_comment_to_post(request): body = request.POST - comment_form = CommentForm(body) - if comment_form.is_valid(): - comment = comment_form.save(commit=False) - comment.author = body["author"] - comment.post = Post.objects.get(id=body["post_id"]) - comment.text = body["text"] - comment.save() - return JsonResponse({ - "comment": { - "id": comment.id, - "author": comment.author, - "post_id": comment.post.id, - "text": comment.text, - } - }, status=HTTPStatus.CREATED) + try: + post_of_comment = Post.objects.get(id=body["post_id"]) + except Post.DoesNotExist: + return JsonResponse({"message": "Post를 찾을 수 없습니다."}, status=HTTPStatus.NOT_FOUND) + else: + comment_form = CommentForm(body) + if comment_form.is_valid(): + comment = comment_form.save(commit=False) + comment.author = body["author"] + comment.post = post_of_comment + comment.text = body["text"] + comment.save() + return JsonResponse({ + "comment": { + "id": comment.id, + "author": comment.author, + "post_id": comment.post.id, + "text": comment.text, + } + }, status=HTTPStatus.CREATED) return JsonResponse({"message": "form is invalid"}, status=HTTPStatus.BAD_REQUEST)