diff --git a/backend/tests/data/test.jpg b/backend/tests/data/test.jpg new file mode 100644 index 0000000..a01bed0 Binary files /dev/null and b/backend/tests/data/test.jpg differ diff --git a/backend/tests/data/test.mp4 b/backend/tests/data/test.mp4 new file mode 100644 index 0000000..76a549a Binary files /dev/null and b/backend/tests/data/test.mp4 differ diff --git a/backend/tests/data/test.png b/backend/tests/data/test.png new file mode 100644 index 0000000..62b2052 Binary files /dev/null and b/backend/tests/data/test.png differ diff --git a/backend/tests/test_tc001.py b/backend/tests/test_tc001.py new file mode 100644 index 0000000..4f2db06 --- /dev/null +++ b/backend/tests/test_tc001.py @@ -0,0 +1,85 @@ +from rest_framework.test import APITestCase +from django.contrib.auth import get_user_model + +class TestFileuploadAPI(APITestCase): + def setUp(self): + self.athlete = get_user_model().objects.create_user( + username="athlete", + password="athletetest" + ) + + self.coach = get_user_model().objects.create_user( + username="coach", + password="coachtest", + isCoach=True, + specialism="specialism" + ) + + self.client.force_authenticate(self.athlete) + + # Create a workout that will be enhanced with either an image or a video, store the workout ID + response = self.client.post( + "/api/workouts/", + { + "name": "Test workout", + "date": "2000-01-01", + "notes": "Test", + "visibility": "CO", + "owner": self.athlete.id, + "exercise_instances": [] + }, + format="json" + ) + self.workout_id = response.data["id"] + + # Add the coach as coach and store offer url to accept the offer + response = self.client.post( + "/api/offers/", + { + "recipient": f"/api/users/{self.coach.id}/", + "status": "p", + }, + format="json" + ) + self.offer_url = response.data["url"] + + # Login as coach + self.client.force_authenticate(self.coach) + + # Accept the offer, so the coach can see the workout and add files + self.client.put( + self.offer_url, + { + "recipient": f"/api/users/{self.coach.id}", + "status": "a", + }, + format="json") + + def test_jpg(self): + self.__upload_file_abstract("test.jpg", "jpg") + + def test_png(self): + self.__upload_file_abstract("test.png", "png") + + def test_mp4(self): + self.__upload_file_abstract("test.mp4", "mp4") + + def __upload_file_abstract(self, file_name, file_type): + + with open(f"tests/data/{file_name}", "rb") as file: + response = self.client.post( + "/api/athlete-files/", + format="multipart", + data={ + "athlete": f"/api/users/{self.athlete.id}/", + "owner": f"/api/users/{self.coach.id}/", + "file": file + } + ) + + self.assertEqual(response.status_code, 201) + self.assertEqual(response.data["file"].split(".")[-1], file_type) + + file.close() + + \ No newline at end of file