forked from mathialm/secfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Niklas Ziemer
committed
Mar 26, 2025
1 parent
9d7e3ce
commit 6dfaf93
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
|
|
||
|
|