Skip to content

Commit

Permalink
Add test_tc001
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Ziemer committed Mar 26, 2025
1 parent 9d7e3ce commit 6dfaf93
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
Binary file added backend/tests/data/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/tests/data/test.mp4
Binary file not shown.
Binary file added backend/tests/data/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions backend/tests/test_tc001.py
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()


0 comments on commit 6dfaf93

Please sign in to comment.