FitTrackee/fittrackee/tests/workouts/utils.py

41 lines
1.1 KiB
Python
Raw Normal View History

import json
from io import BytesIO
2021-07-14 14:54:20 +02:00
from typing import Optional, Tuple
2020-12-30 22:07:43 +01:00
from uuid import uuid4
2021-01-02 19:28:03 +01:00
from flask import Flask
2021-01-20 16:47:00 +01:00
from fittrackee.workouts.utils_id import encode_uuid
2020-12-30 22:07:43 +01:00
2021-01-02 19:28:03 +01:00
def get_random_short_id() -> str:
2020-12-30 22:07:43 +01:00
return encode_uuid(uuid4())
2021-07-14 14:54:20 +02:00
def post_an_workout(
app: Flask, gpx_file: str, notes: Optional[str] = None
) -> Tuple[str, str]:
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
token = json.loads(resp_login.data.decode())['auth_token']
2021-07-14 14:54:20 +02:00
workout_data = '{"sport_id": 1'
if notes is not None:
workout_data += f', "notes": "{notes}"'
workout_data += '}'
response = client.post(
'/api/workouts',
data=dict(
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
2021-07-14 14:54:20 +02:00
data=workout_data,
),
headers=dict(
content_type='multipart/form-data', Authorization=f'Bearer {token}'
),
)
data = json.loads(response.data.decode())
return token, data['data']['workouts'][0]['id']