FitTrackee/fittrackee/tests/activities/test_activities_api_3_delete.py

155 lines
5.0 KiB
Python
Raw Normal View History

import json
import os
2021-01-02 19:28:03 +01:00
from fittrackee.activities.models import Activity, Sport
from fittrackee.activities.utils import get_absolute_file_path
2021-01-02 19:28:03 +01:00
from fittrackee.users.models import User
from flask import Flask
2020-12-30 22:07:43 +01:00
from .utils import get_random_short_id, post_an_activity
2021-01-02 19:28:03 +01:00
def get_gpx_filepath(activity_id: int) -> str:
2018-05-13 18:36:31 +02:00
activity = Activity.query.filter_by(id=activity_id).first()
return activity.gpx
2018-05-13 18:36:31 +02:00
2020-05-10 15:55:56 +02:00
class TestDeleteActivityWithGpx:
def test_it_deletes_an_activity_with_gpx(
2021-01-02 19:28:03 +01:00
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
2020-12-30 22:07:43 +01:00
token, activity_short_id = post_an_activity(app, gpx_file)
2020-05-10 15:55:56 +02:00
client = app.test_client()
2020-05-10 15:55:56 +02:00
response = client.delete(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_short_id}',
headers=dict(Authorization=f'Bearer {token}'),
2020-05-10 15:55:56 +02:00
)
assert response.status_code == 204
def test_it_returns_403_when_deleting_an_activity_from_different_user(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
user_2: User,
sport_1_cycling: Sport,
gpx_file: str,
) -> None:
2020-12-30 22:07:43 +01:00
_, activity_short_id = post_an_activity(app, gpx_file)
2020-05-10 15:55:56 +02:00
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
content_type='application/json',
)
2020-05-10 15:55:56 +02:00
response = client.delete(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_short_id}',
2020-05-10 15:55:56 +02:00
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 403
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']
2021-01-02 19:28:03 +01:00
def test_it_returns_404_if_activity_does_not_exist(
self, app: Flask, user_1: User
) -> None:
2020-05-10 15:55:56 +02:00
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',
)
response = client.delete(
2020-12-30 22:07:43 +01:00
f'/api/activities/{get_random_short_id()}',
2020-05-10 15:55:56 +02:00
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
def test_it_returns_500_when_deleting_an_activity_with_gpx_invalid_file(
2021-01-02 19:28:03 +01:00
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
) -> None:
2020-12-30 22:07:43 +01:00
token, activity_short_id = post_an_activity(app, gpx_file)
2020-05-10 15:55:56 +02:00
client = app.test_client()
gpx_filepath = get_gpx_filepath(1)
gpx_filepath = get_absolute_file_path(gpx_filepath)
os.remove(gpx_filepath)
response = client.delete(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_short_id}',
headers=dict(Authorization=f'Bearer {token}'),
2020-05-10 15:55:56 +02:00
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert (
'Error. Please try again or contact the administrator.'
in data['message']
)
class TestDeleteActivityWithoutGpx:
def test_it_deletes_an_activity_wo_gpx(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
activity_cycling_user_1: Activity,
) -> None:
2020-05-10 15:55:56 +02:00
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',
)
response = client.delete(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_cycling_user_1.short_id}',
2020-05-10 15:55:56 +02:00
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
assert response.status_code == 204
def test_it_returns_403_when_deleting_an_activity_from_different_user(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
user_2: User,
sport_1_cycling: Sport,
activity_cycling_user_1: Activity,
) -> None:
2020-05-10 15:55:56 +02:00
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
content_type='application/json',
)
response = client.delete(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_cycling_user_1.short_id}',
2020-05-10 15:55:56 +02:00
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 403
assert 'error' in data['status']
assert 'You do not have permissions.' in data['message']