Commit 3a67cb89 authored by Johan-Henrik Fylling's avatar Johan-Henrik Fylling
Browse files

Add tests to verify updating workouts.

parent 5d88fba5
Pipeline #129784 failed with stages
No preview for this file type
......@@ -10,6 +10,176 @@ from PIL import Image
# Create your tests here.
class WorkoutUpdateTest(TestCase):
def generate_photo_file(self):
file = io.BytesIO()
image = Image.new("RGBA", size=(100, 100), color=(155, 0, 0))
image.save(file, "png")
file.name = "test.png"
file.seek(0)
return file
def setUp(self):
self.unauthenticatedClient = Client()
user1 = {
"username": "dev",
"password": "devpassword",
"password1": "devpassword",
"phone_number": 12312312,
"email": f"dev@dev.dev",
"country": "",
"city": "",
"street_address": "",
}
self.user1 = json.loads(
self.unauthenticatedClient.post("/api/users/", user1).content
)
response = json.loads(
self.unauthenticatedClient.post(
"/api/token/",
json.dumps(
{"username": user1["username"], "password": user1["password"]}
),
content_type="application/json",
).content.decode("UTF-8")
)
self.authenticatedClient1 = Client(
HTTP_AUTHORIZATION=f"Bearer {response['access']}"
)
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",
"exercise_instances": [
{
"exercise": self.exerciseInstance["url"],
"id": self.exerciseInstance["id"],
"sets": "1",
"number": "1",
}
],
"date": "2021-03-05T10:34:00Z",
},
content_type="application/json",
).content.decode("UTF-8")
)
self.workout2 = json.loads(
self.authenticatedClient1.post(
"/api/workouts/",
{
"name": "Extreme sit-up workout V2",
"notes": "hello notes",
"visibility": "PU",
"date": "2021-03-05T10:34:00Z",
"exercise_instances": "[]",
"files": [self.generate_photo_file()],
},
).content.decode("UTF-8")
)
def test_can_update(self):
workoutId = self.workout["id"]
response = self.authenticatedClient1.put(
f"/api/workouts/{workoutId}/",
{
"name": "Slow sit-up workout",
"notes": "hello notes",
"exercise_instances": [
{
"exercise": self.exerciseInstance["url"],
"id": self.exerciseInstance["id"],
"sets": "1",
"number": "1",
}
],
"date": "2021-03-05T10:34:00Z",
},
content_type="application/json",
)
self.assertEqual(
response.status_code,
200,
)
updatedWorkout = json.loads(self.authenticatedClient1.get(f"/api/workouts/{workoutId}/").content.decode("UTF-8"))
self.assertEqual(
updatedWorkout["name"],
"Slow sit-up workout",
)
def test_can_update_with_file(self):
workoutId = self.workout2["id"]
updatedWorkoutResponse = self.authenticatedClient1.put(
f"/api/workouts/{workoutId}/",
{
"name": "Extreme sit-up workout V2",
"notes": "hello notes",
"visibility": "PU",
"date": "2021-03-05T10:34:00Z",
"exercise_instances": [
{
"exercise": self.exerciseInstance["url"],
"id": self.exerciseInstance["id"],
"sets": "1",
"number": "1",
}
]
},
content_type="application/json",
)
self.assertEqual(updatedWorkoutResponse.status_code, 200)
response = self.authenticatedClient1.get(f"/api/workouts/{workoutId}/").content.decode("UTF-8")
updatedWorkoutGet = json.loads(response)
self.assertEqual(
self.authenticatedClient1.get(updatedWorkoutGet["files"][0]["url"]).status_code,
200,
)
self.assertEqual(
updatedWorkoutGet["exercise_instances"][0]["exercise"],
self.exerciseInstance["url"],
)
if os.path.exists("test.png"):
os.remove("test.png")
class WorkoutPermissionTest(TestCase):
def setUp(self):
......
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