Commit ec6d94dc authored by Kevin Mentzoni Halvarsson's avatar Kevin Mentzoni Halvarsson
Browse files

Added system tests for favorites endpoint.

parent 307c83a5
Pipeline #116149 passed with stages
in 1 minute and 7 seconds
......@@ -37,6 +37,11 @@ class WorkoutPermissionTest(TestCase):
self.exerciseInstance = json.loads(
self.authenticatedClient1.post("/api/exercises/", json.dumps({"name": "Sit-up", "description": "Lie down, then sit up using your core", "unit": "Reps"}), content_type="application/json").content.decode("UTF-8"))
self.exerciseInstance2 = json.loads(
self.authenticatedClient1.post("/api/exercises/", json.dumps(
{"name": "Push-up", "description": "Push your body upwards using your chest and arms", "unit": "Reps"}),
content_type="application/json").content.decode("UTF-8"))
self.workout = json.loads(
self.authenticatedClient1.post("/api/workouts/",
{"name": "Extreme sit-up workout", "notes": "hello notes",
......@@ -223,3 +228,51 @@ class WorkoutPermissionTest(TestCase):
if os.path.exists("test.png"):
os.remove("test.png")
def testFavorites(self):
#Test GET
favorites = json.loads(self.authenticatedClient1.get("/api/favorites/").content.decode("UTF-8"))["results"]
self.assertEqual(len(favorites), 0)
#Test POST
favoriteData = {"user": self.user1["id"], "exercise": self.exerciseInstance["id"]}
response = self.authenticatedClient1.post("/api/favorites/", favoriteData)
self.assertEqual(response.status_code, 201)
newFavorite = json.loads(response.content.decode("UTF-8"))
self.assertEqual(favoriteData["user"], newFavorite["user"])
self.assertEqual(favoriteData["exercise"], newFavorite["exercise"])
favorites = json.loads(self.authenticatedClient1.get("/api/favorites/").content.decode("UTF-8"))["results"]
self.assertEqual(len(favorites), 1)
#Test GET at /api/favorites/:id
newFavoriteFromGET = json.loads(self.authenticatedClient1.get(f"/api/favorites/{newFavorite['id']}/").content.decode("UTF-8"))
self.assertEqual(newFavoriteFromGET["id"], newFavorite["id"])
self.assertEqual(newFavoriteFromGET["user"], newFavorite["user"])
self.assertEqual(newFavoriteFromGET["exercise"], newFavorite["exercise"])
#Test DELETE /api/favorites/:id
response = self.authenticatedClient1.delete(f"/api/exercises/{newFavorite['id']}/")
self.assertEqual(response.status_code, 204)
favoritesAfterDelete = json.loads(self.authenticatedClient1.get("/api/favorites/").content.decode("UTF-8"))["results"]
self.assertFalse(len(favorites) == len(favoritesAfterDelete))
def testFavoritesRobustness(self):
# Missing data
favoriteData = {"user": self.user1["id"]}
response = self.authenticatedClient1.post("/api/favorites/", favoriteData)
self.assertEqual(response.status_code, 400)
#Trying non existing user
favoriteData = {"user": 72, "exercise": self.exerciseInstance["id"]}
response = self.authenticatedClient1.post("/api/favorites/", favoriteData)
self.assertEqual(response.status_code, 400)
# Trying non existing exercise
favoriteData = {"user": self.user1["id"], "exercise": 123123}
response = self.authenticatedClient1.post("/api/favorites/", favoriteData)
self.assertEqual(response.status_code, 400)
# Trying erroneous data
favoriteData = {"user": self.user1["username"], "exercise": self.exerciseInstance["name"]}
response = self.authenticatedClient1.post("/api/favorites/", favoriteData)
self.assertEqual(response.status_code, 400)
......@@ -359,7 +359,7 @@ class FavoriteExercise(
mixins.DestroyModelMixin,
generics.GenericAPIView
):
permission_classes = [permissions.IsAuthenticated & (IsOwner | IsReadOnly)]
permission_classes = [permissions.IsAuthenticated]
serializer_class = FavoriteSerializer
queryset = Favorite.objects.all()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment