Skip to content

Commit

Permalink
fixup! ✨(models) issue-196: add models and endpoints to user wishlist
Browse files Browse the repository at this point in the history
  • Loading branch information
MorganeAD committed Mar 1, 2023
1 parent c990d57 commit f9b6bc4
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/backend/joanie/tests/core/test_api_wishlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit f9b6bc4

Please sign in to comment.