From c319122b9d5184946c9d104c9f90509a1f66596b Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 13:41:05 -0600 Subject: [PATCH 1/7] Created a test for GET at the api/users endpoint --- src/chigame/api/tests/test_api.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 5ce0956f..aac96bb0 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -282,3 +282,13 @@ def test_delete_message(self): self.assertEqual(data2["update_on"], Message.objects.get(id=2).update_on) self.assertEqual(data2["content"], Message.objects.get(id=2).content) self.assertEqual(2, Message.objects.get(id=2).token_id) + + +class UserTests(APITestCase): + def test_user_get(self): + user = UserFactory() + + url = reverse("users", kwargs={"pk": user.id}) + + response = self.client.get(url) + assert response.status_code == 200 From 0a685e36ae197588ac213c1b74610f54aedd1494 Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 13:53:21 -0600 Subject: [PATCH 2/7] Appopriately implemented the user list and detail tests --- src/chigame/api/tests/test_api.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index aac96bb0..2e206ae5 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -288,7 +288,13 @@ class UserTests(APITestCase): def test_user_get(self): user = UserFactory() - url = reverse("users", kwargs={"pk": user.id}) + list_url = reverse("api-user-list") + detail_url = reverse("api-user-detail", kwargs={"pk": user.id}) - response = self.client.get(url) - assert response.status_code == 200 + # Your list view test + list_response = self.client.get(list_url) + assert list_response.status_code == 200 + + # Your detail view test + detail_response = self.client.get(detail_url) + assert detail_response.status_code == 200 From 8a1f341afabdd0a42b00c10bfbd0fa7ee13225b0 Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 14:04:58 -0600 Subject: [PATCH 3/7] Removed comments --- src/chigame/api/tests/test_api.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 2e206ae5..3721315c 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -8,7 +8,7 @@ from rest_framework.test import APITestCase from chigame.api.tests.factories import ChatFactory, GameFactory, TournamentFactory, UserFactory -from chigame.games.models import Game, Message +from chigame.games.models import Game, Message, User class GameTests(APITestCase): @@ -291,10 +291,17 @@ def test_user_get(self): list_url = reverse("api-user-list") detail_url = reverse("api-user-detail", kwargs={"pk": user.id}) - # Your list view test list_response = self.client.get(list_url) assert list_response.status_code == 200 - # Your detail view test detail_response = self.client.get(detail_url) assert detail_response.status_code == 200 + + def test_user_delete(self): + user = UserFactory() + + url = reverse("api-user-detail", args=[user.id]) + response = self.client.delete(url, format="json") + + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(User.objects.count(), 0) From 4c201a01c32f54ccd0c5738f7f528c71c07d17f5 Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 15:58:40 -0600 Subject: [PATCH 4/7] adjusted the delete function to ensuerr a new user was created to begin with --- src/chigame/api/tests/test_api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 3721315c..9b777fe8 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -299,6 +299,7 @@ def test_user_get(self): def test_user_delete(self): user = UserFactory() + self.assertEqual(User.objects.count(), 1) url = reverse("api-user-detail", args=[user.id]) response = self.client.delete(url, format="json") From c47b69304e282d56a33eb4d1500a713ad117402f Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Thu, 30 Nov 2023 17:25:00 -0600 Subject: [PATCH 5/7] Finished Deliverable tests for post and patch at the api/users endpoint --- src/chigame/api/tests/test_api.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 9b777fe8..c0cd013d 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -306,3 +306,26 @@ def test_user_delete(self): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(User.objects.count(), 0) + + def test_user_post(self): + user = {"name": "BillyBob", "email": "billy@uchicago.edu"} + + url = reverse("api-user-list") + response = self.client.post(url, data=user, format="json") + + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(User.objects.count(), 1) + self.assertEqual(response.data["email"], user["email"]) + self.assertEqual(response.data["name"], user["name"]) + + def test_user_patch(self): + user = UserFactory() + url = reverse("api-user-detail", kwargs={"pk": user.id}) + + updated_data = {"name": "Johnn"} + + response = self.client.patch(url, data=updated_data, format="json") + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(User.objects.count(), 1) + self.assertEqual(response.data["name"], updated_data["name"]) From 57275b6d315dc7ae0651388192183af2a29f7bd7 Mon Sep 17 00:00:00 2001 From: cuyakonwu Date: Tue, 5 Dec 2023 13:12:29 -0600 Subject: [PATCH 6/7] User post is broken --- src/chigame/api/tests/factories.py | 1 + src/chigame/api/tests/test_api.py | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/chigame/api/tests/factories.py b/src/chigame/api/tests/factories.py index f4d1b5c7..572b74ae 100644 --- a/src/chigame/api/tests/factories.py +++ b/src/chigame/api/tests/factories.py @@ -83,6 +83,7 @@ class Meta: name = Faker("name") email = Faker("email") password = Faker("password") + username = Faker("user_name") class TournamentFactory(DjangoModelFactory): diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index 64b9165d..aa59cafd 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -282,13 +282,14 @@ def test_delete_message(self): self.assertEqual(data2["update_on"], Message.objects.get(id=2).update_on) self.assertEqual(data2["content"], Message.objects.get(id=2).content) self.assertEqual(2, Message.objects.get(id=2).token_id) - + + class UserTests(APITestCase): def test_user_get(self): user = UserFactory() list_url = reverse("api-user-list") - detail_url = reverse("api-user-detail", kwargs={"pk": user.id}) + detail_url = reverse("api-user-detail", kwargs={"slug": user.username}) list_response = self.client.get(list_url) assert list_response.status_code == 200 @@ -300,14 +301,19 @@ def test_user_delete(self): user = UserFactory() self.assertEqual(User.objects.count(), 1) - url = reverse("api-user-detail", args=[user.id]) + url = reverse("api-user-detail", args=[user.username]) response = self.client.delete(url, format="json") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(User.objects.count(), 0) - def test_user_post(self): - user = {"name": "BillyBob", "email": "billy@uchicago.edu"} + """def test_user_post(self): + user = { + "email": "user@example.com", + "name": "John Doe", + "username": "john_doe", + "tokens": 2, + } url = reverse("api-user-list") response = self.client.post(url, data=user, format="json") @@ -315,19 +321,19 @@ def test_user_post(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(User.objects.count(), 1) self.assertEqual(response.data["email"], user["email"]) - self.assertEqual(response.data["name"], user["name"]) + self.assertEqual(response.data["name"], user["name"])""" def test_user_patch(self): user = UserFactory() - url = reverse("api-user-detail", kwargs={"pk": user.id}) + url = reverse("api-user-detail", kwargs={"slug": user.username}) - updated_data = {"name": "Johnn"} + updated_data = {"username": "Johnn"} response = self.client.patch(url, data=updated_data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(User.objects.count(), 1) - self.assertEqual(response.data["name"], updated_data["name"]) + self.assertEqual(response.data["username"], updated_data["username"]) def test_feed_message(self): self.user1 = UserFactory() From 243e064efccd4c690d77e1b4c208d2305ba2ca6b Mon Sep 17 00:00:00 2001 From: giovannimhern Date: Tue, 5 Dec 2023 13:23:10 -0600 Subject: [PATCH 7/7] Minor fix for post test -- missing field --- src/chigame/api/tests/test_api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/chigame/api/tests/test_api.py b/src/chigame/api/tests/test_api.py index aa59cafd..600b154b 100644 --- a/src/chigame/api/tests/test_api.py +++ b/src/chigame/api/tests/test_api.py @@ -307,11 +307,12 @@ def test_user_delete(self): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(User.objects.count(), 0) - """def test_user_post(self): + def test_user_post(self): user = { "email": "user@example.com", "name": "John Doe", "username": "john_doe", + "password": "password", "tokens": 2, } @@ -321,7 +322,7 @@ def test_user_delete(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(User.objects.count(), 1) self.assertEqual(response.data["email"], user["email"]) - self.assertEqual(response.data["name"], user["name"])""" + self.assertEqual(response.data["name"], user["name"]) def test_user_patch(self): user = UserFactory()