2018-05-11 17:55:46 +02:00
|
|
|
import json
|
|
|
|
import os
|
2020-12-30 19:37:59 +01:00
|
|
|
from uuid import uuid4
|
2018-05-11 17:55:46 +02:00
|
|
|
|
2020-09-16 15:41:02 +02:00
|
|
|
from fittrackee.activities.models import Activity
|
|
|
|
from fittrackee.activities.utils import get_absolute_file_path
|
2018-05-11 17:55:46 +02:00
|
|
|
|
2020-12-30 19:37:59 +01:00
|
|
|
from .utils import post_an_activity
|
|
|
|
|
2018-05-11 17:55:46 +02:00
|
|
|
|
2018-05-13 18:36:31 +02:00
|
|
|
def get_gpx_filepath(activity_id):
|
|
|
|
activity = Activity.query.filter_by(id=activity_id).first()
|
|
|
|
return activity.gpx
|
2018-05-11 17:55:46 +02:00
|
|
|
|
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(
|
|
|
|
self, app, user_1, sport_1_cycling, gpx_file
|
|
|
|
):
|
2020-12-30 19:37:59 +01:00
|
|
|
token, activity_uuid = post_an_activity(app, gpx_file)
|
2020-05-10 15:55:56 +02:00
|
|
|
client = app.test_client()
|
2020-12-30 19:37:59 +01:00
|
|
|
|
2020-05-10 15:55:56 +02:00
|
|
|
response = client.delete(
|
2020-12-30 19:37:59 +01:00
|
|
|
f'/api/activities/{activity_uuid}',
|
|
|
|
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(
|
|
|
|
self, app, user_1, user_2, sport_1_cycling, gpx_file
|
|
|
|
):
|
2020-12-30 19:37:59 +01:00
|
|
|
_, activity_uuid = 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-12-30 19:37:59 +01:00
|
|
|
|
2020-05-10 15:55:56 +02:00
|
|
|
response = client.delete(
|
2020-12-30 19:37:59 +01:00
|
|
|
f'/api/activities/{activity_uuid}',
|
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']
|
|
|
|
|
|
|
|
def test_it_returns_404_if_activity_does_not_exist(self, app, user_1):
|
|
|
|
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 19:37:59 +01:00
|
|
|
f'/api/activities/{uuid4().hex}',
|
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(
|
|
|
|
self, app, user_1, sport_1_cycling, gpx_file
|
|
|
|
):
|
2020-12-30 19:37:59 +01:00
|
|
|
token, activity_uuid = 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 19:37:59 +01:00
|
|
|
f'/api/activities/{activity_uuid}',
|
|
|
|
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(
|
|
|
|
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
|
|
|
):
|
|
|
|
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 19:37:59 +01:00
|
|
|
f'/api/activities/{activity_cycling_user_1.uuid}',
|
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(
|
|
|
|
self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_1
|
|
|
|
):
|
|
|
|
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 19:37:59 +01:00
|
|
|
f'/api/activities/{activity_cycling_user_1.uuid}',
|
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']
|