From f9b6bc4b606ce78193ce7c986cc899dae1da6dd2 Mon Sep 17 00:00:00 2001 From: Morgane Alonso Date: Wed, 1 Mar 2023 09:06:18 +0100 Subject: [PATCH] =?UTF-8?q?fixup!=20=E2=9C=A8(models)=20issue-196:=20add?= =?UTF-8?q?=20models=20and=20endpoints=20to=20user=20wishlist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../joanie/tests/core/test_api_wishlist.py | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/backend/joanie/tests/core/test_api_wishlist.py b/src/backend/joanie/tests/core/test_api_wishlist.py index 82267c1744..6483f8fd75 100644 --- a/src/backend/joanie/tests/core/test_api_wishlist.py +++ b/src/backend/joanie/tests/core/test_api_wishlist.py @@ -11,7 +11,7 @@ def get_payload(wish): """ - According to an CourseWish object, return a valid payload required by + According to a CourseWish object, return a valid payload required by create wish api routes. """ return { @@ -64,15 +64,18 @@ def test_api_wish_get_wish_for_new_user(self): """If we try to get wish for a user not in db, we create a new user first""" username = "panoramix" token = self.get_user_token(username) + + self.assertFalse(models.User.objects.filter(username=username).exists()) + response = self.client.get( "/api/v1.0/wishlist/", HTTP_AUTHORIZATION=f"Bearer {token}" ) self.assertEqual(response.status_code, 200) results = response.data["results"] self.assertEqual(len(results), 0) - self.assertEqual(models.User.objects.get(username=username).username, username) + self.assertTrue(models.User.objects.filter(username=username).exists()) - def test_api_wish_get_wish(self): + def test_api_wish_get_wishes(self): """Get wish for a user in db with two wish linked to him""" user = factories.UserFactory() course1 = factories.CourseFactory() @@ -92,6 +95,42 @@ def test_api_wish_get_wish(self): self.assertEqual(results[1]["course"], course2.code) self.assertEqual(results[1]["id"], str(wish2.id)) + def test_api_wish_get_wishes_filter_by_code(self): + """Get wish for a user in db with two wish linked to him""" + user = factories.UserFactory() + course = factories.CourseFactory() + token = self.get_user_token(user.username) + wish = factories.CourseWishFactory.create(owner=user, course=course) + factories.CourseWishFactory.create(owner=user) + + get_url = f"/api/v1.0/wishlist/?course_code={course.code}" + response = self.client.get( + get_url, HTTP_AUTHORIZATION=f"Bearer {token}" + ) + self.assertEqual(response.status_code, 200) + + results = response.data["results"] + self.assertEqual(len(results), 1) + self.assertEqual(results[0]["course"], course.code) + self.assertEqual(results[0]["id"], str(wish.id)) + + def test_api_wish_get_wish(self): + """Get wish for a user in db with two wish linked to him""" + user = factories.UserFactory() + token = self.get_user_token(user.username) + wish = factories.CourseWishFactory.create(owner=user) + factories.CourseWishFactory.create(owner=user) + + get_url = f"/api/v1.0/wishlist/{wish.id}/" + response = self.client.get( + get_url, HTTP_AUTHORIZATION=f"Bearer {token}" + ) + self.assertEqual(response.status_code, 200) + + data = response.data + self.assertEqual(data["course"], wish.course.code) + self.assertEqual(data["id"], str(wish.id)) + def test_api_wish_create_without_authorization(self): """Create/update user wish not allowed without HTTP AUTH""" # Try to create wish without Authorization