From 32f7994bb26af084bce3b53671b98c2fa7663801 Mon Sep 17 00:00:00 2001 From: wlsrn3684 <50126441+wlsrn3684@users.noreply.github.com> Date: Wed, 7 Jul 2021 22:36:42 +0900 Subject: [PATCH 1/2] Add Delete Post With Delete API --- .../step2/jingu/djangogirls/blog/api/urls.py | 1 + .../step2/jingu/djangogirls/blog/api/views.py | 22 ++- .../step2/jingu/djangogirls/blog/tests.py | 129 ++++++++++++++++-- 3 files changed, 138 insertions(+), 14 deletions(-) diff --git a/django_girls_tutorial/step2/jingu/djangogirls/blog/api/urls.py b/django_girls_tutorial/step2/jingu/djangogirls/blog/api/urls.py index 0cb200f..199cd46 100644 --- a/django_girls_tutorial/step2/jingu/djangogirls/blog/api/urls.py +++ b/django_girls_tutorial/step2/jingu/djangogirls/blog/api/urls.py @@ -7,4 +7,5 @@ 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("posts//delete/delete", views.delete_post_with_delete, name="delete_post_with_delete") ] diff --git a/django_girls_tutorial/step2/jingu/djangogirls/blog/api/views.py b/django_girls_tutorial/step2/jingu/djangogirls/blog/api/views.py index 22a9821..e169025 100644 --- a/django_girls_tutorial/step2/jingu/djangogirls/blog/api/views.py +++ b/django_girls_tutorial/step2/jingu/djangogirls/blog/api/views.py @@ -3,6 +3,11 @@ from django.views.decorators.http import require_http_methods from blog.models import Post +from django.contrib.auth.models import User +from blog.form import PostForm + +from http.client import NOT_FOUND, FORBIDDEN, NO_CONTENT +import json def retrieve_post_list(request): @@ -83,4 +88,19 @@ def update_post_with_put(request, id): 자세한 조건은 테스트를 확인해보고 파악하자 FYI, request.body 활용 - """ \ No newline at end of file + """ + + +@require_http_methods(["DELETE"]) +def delete_post_with_delete(request, id): + body = json.loads(request.body) + try: + post = Post.objects.get(id=id) + except Post.DoesNotExist: + return JsonResponse({"message": "post를 찾을 수 없습니다."}, status=NOT_FOUND) + + if body["author"] != post.author.id: + return JsonResponse({"message": "유효하지 않은 사용자입니다."}, status=FORBIDDEN) + + post.delete() + return JsonResponse({}, status=NO_CONTENT) diff --git a/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py b/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py index 7f7579f..6007022 100644 --- a/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py +++ b/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py @@ -1,5 +1,5 @@ import json -from http.client import NOT_FOUND, OK +from http.client import NOT_FOUND, OK, FORBIDDEN, NO_CONTENT from django.contrib.auth.models import User from django.test import TestCase @@ -58,7 +58,9 @@ def setUp(self): super().setUp() def test_post_detail(self): - post = self._create_post(author=self.author, title="test title", text="test text") + post = self._create_post( + author=self.author, title="test title", text="test text" + ) post.publish() response = self.client.get(reverse("retrieve_post_detail", kwargs={"id": 1})) response_data = json.loads(response.content)["post"] @@ -80,7 +82,11 @@ def setUp(self): def test_post_create(self): # Given: 유효한 request body 값이 주어질 때, - request_body = {"author": self.author.id, "title": "test title", "text": "test text"} + request_body = { + "author": self.author.id, + "title": "test title", + "text": "test text", + } # When: post 생성 api를 호출하면, response = self.client.post(reverse("create_post"), data=request_body) @@ -96,7 +102,11 @@ def test_post_create(self): def test_post_create_with_error_on_404(self): # Given: 유효하지 않은 author_id가 주어질 때, invalid_author_id = 123123123 - request_body = {"author": invalid_author_id, "title": "test title", "text": "test text"} + request_body = { + "author": invalid_author_id, + "title": "test title", + "text": "test text", + } # When: post 생성 api를 호출하면, response = self.client.post(reverse("create_post"), data=request_body) @@ -112,14 +122,25 @@ def test_post_create_with_error_on_404(self): class TestPostUpdate(TestPostMixin, TestCase): def setUp(self): super().setUp() - self.post = Post.objects.create(author=self.author, title="test title", text="test text") + self.post = Post.objects.create( + author=self.author, title="test title", text="test text" + ) def test_post_update_with_put(self): # Given: 업데이트 하기 위한 유효한 request body 값이 주어지고, - request_body_for_put_update = json.dumps({"author": self.author.id, "title": "test test title", "text": "test test text"}) + request_body_for_put_update = json.dumps( + { + "author": self.author.id, + "title": "test test title", + "text": "test test text", + } + ) # When: 1번 post에 대한 업데이트 api를 호출 할 때, - response = self.client.put(reverse("update_post_with_put", kwargs={"id": self.post.id}), data=request_body_for_put_update) + response = self.client.put( + reverse("update_post_with_put", kwargs={"id": self.post.id}), + data=request_body_for_put_update, + ) # Then: 상태코드는 200이고, self.assertEqual(response.status_code, 200) @@ -133,15 +154,22 @@ def test_post_update_with_put(self): self.assertEqual(response["title"], "test test title") self.assertEqual(response["text"], "test test text") - def test_post_update_with_error_with_author_on_404(self): # Given: 업데이트 하기 위한 유효하지 않은 author_id 값이 주어지고, invalid_author_id = 123123123 request_body_for_put_update = json.dumps( - {"author": invalid_author_id, "title": "test test title", "text": "test test text"}) + { + "author": invalid_author_id, + "title": "test test title", + "text": "test test text", + } + ) # When: 1번 post에 대한 업데이트 api를 호출 할 때, - response = self.client.put(reverse("update_post_with_put", kwargs={"id": self.post.id}), data=request_body_for_put_update) + response = self.client.put( + reverse("update_post_with_put", kwargs={"id": self.post.id}), + data=request_body_for_put_update, + ) # Then: 상태코드는 404이고, self.assertEqual(response.status_code, 404) @@ -157,11 +185,19 @@ def test_post_update_with_error_with_author_on_404(self): def test_post_update_with_error_with_post_on_404(self): # Given: 업데이트 하기 위한 유효하지 않은 post_id 값이 주어지고, request_body_for_put_update = json.dumps( - {"author": self.author.id, "title": "test test title", "text": "test test text"}) + { + "author": self.author.id, + "title": "test test title", + "text": "test test text", + } + ) invalid_post_id = 12345 # When: # When: 유효하지 않은 post에 대한 업데이트 api를 호출 할 때, - response = self.client.put(reverse("update_post_with_put", kwargs={"id": invalid_post_id}), data=request_body_for_put_update) + response = self.client.put( + reverse("update_post_with_put", kwargs={"id": invalid_post_id}), + data=request_body_for_put_update, + ) # Then: 상태코드는 404이고, self.assertEqual(response.status_code, 404) @@ -172,4 +208,71 @@ 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 TestPostDelete(TestPostMixin, TestCase): + def setUp(self): + super().setUp() + self.post = Post.objects.create( + author=self.author, title="test title", text="test text" + ) + + def test_post_delete_with_delete(self): + # Given: 삭제 하기 위한 유효한 request body 값이 주어지고, + request_body = json.dumps({"author": self.author.id}) + + # When: 1번 post에 대한 삭제 api를 호출할 때, + response = self.client.delete( + reverse("delete_post_with_delete", kwargs={"id": self.post.id}), + data=request_body, + ) + + # Then: 상태코드는 204이고, + self.assertEqual(response.status_code, NO_CONTENT) + # And: Post의 개수는 0개이다 + self.assertEqual(Post.objects.all().count(), 0) + + def test_post_delete_with_error_with_post_on_404(self): + # Given: 삭제 하기 위한 유효하지 않은 post_id 값이 주어지고, + request_body = json.dumps({"author": self.author.id}) + invalid_post_id = 12345 + + # When: 유효하지 않은 post에 대한 삭제 api를 호출할 때, + response = self.client.delete( + reverse("delete_post_with_delete", kwargs={"id": invalid_post_id}), + data=request_body, + ) + + # Then: 상태코드는 404이고, + self.assertEqual(response.status_code, NOT_FOUND) + # And: post는 삭제되지 않아야 한다. + post = Post.objects.all()[0] + self.assertEqual(post.author.id, self.author.id) + self.assertEqual(post.title, "test title") + self.assertEqual(post.text, "test text") + # And: 응답 메세지로 post를 찾을 수 없습니다. 를 리턴해야 한다. + response = json.loads(response.content) + self.assertEqual(response["message"], "post를 찾을 수 없습니다.") + + def test_post_delete_with_error_with_invalid_author_on_403(self): + # Given: 삭제하기 위한 유효하지 않은 author_id 값이 주어지고, + invalid_author_id = 12345 + request_body = json.dumps({"author": invalid_author_id}) + + # When: 1번 post에 대한 삭제 api를 호출할 때, + response = self.client.delete( + reverse("delete_post_with_delete", kwargs={"id": self.post.id}), + data=request_body, + ) + + # Then: 상태코드는 403이고, + self.assertEqual(response.status_code, FORBIDDEN) + # And: post는 삭제되지 않아야 한다. + post = Post.objects.all()[0] + self.assertEqual(post.author.id, self.author.id) + self.assertEqual(post.title, "test title") + self.assertEqual(post.text, "test text") + # And: 응답 메세지로 유효하지 않은 사용자입니다. 를 리턴해야 한다. + response = json.loads(response.content) + self.assertEqual(response["message"], "유효하지 않은 사용자입니다.") From 6fdbe8ff442237a3d3b24aa6dd230143ae5ae7bb Mon Sep 17 00:00:00 2001 From: wlsrn3684 <50126441+wlsrn3684@users.noreply.github.com> Date: Fri, 9 Jul 2021 10:52:01 +0900 Subject: [PATCH 2/2] Fix Comment And Delete API Test Set Up Code --- .../step2/jingu/djangogirls/blog/tests.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py b/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py index 6007022..d40a807 100644 --- a/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py +++ b/django_girls_tutorial/step2/jingu/djangogirls/blog/tests.py @@ -214,7 +214,7 @@ def test_post_update_with_error_with_post_on_404(self): class TestPostDelete(TestPostMixin, TestCase): def setUp(self): super().setUp() - self.post = Post.objects.create( + self.post = self._create_post( author=self.author, title="test title", text="test text" ) @@ -234,8 +234,9 @@ def test_post_delete_with_delete(self): self.assertEqual(Post.objects.all().count(), 0) def test_post_delete_with_error_with_post_on_404(self): - # Given: 삭제 하기 위한 유효하지 않은 post_id 값이 주어지고, + # Given: 삭제 하기 위한 유효한 request body 값과 request_body = json.dumps({"author": self.author.id}) + # And: 유효하지 않은 post_id 값이 주어지고, invalid_post_id = 12345 # When: 유효하지 않은 post에 대한 삭제 api를 호출할 때, @@ -256,10 +257,11 @@ def test_post_delete_with_error_with_post_on_404(self): self.assertEqual(response["message"], "post를 찾을 수 없습니다.") def test_post_delete_with_error_with_invalid_author_on_403(self): - # Given: 삭제하기 위한 유효하지 않은 author_id 값이 주어지고, - invalid_author_id = 12345 + # Given: 삭제 하기 위한 유효한 request body 값과 request_body = json.dumps({"author": invalid_author_id}) - + # And: 유효하지 않은 author_id 값이 주어지고, + invalid_author_id = 12345 + # When: 1번 post에 대한 삭제 api를 호출할 때, response = self.client.delete( reverse("delete_post_with_delete", kwargs={"id": self.post.id}),