FitTrackee/fittrackee/tests/activities/test_activities_api_0_get.py

1047 lines
34 KiB
Python
Raw Normal View History

import json
from uuid import uuid4
2021-01-02 19:28:03 +01:00
from fittrackee.activities.models import Activity, Sport
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
2020-05-10 15:55:56 +02:00
class TestGetActivities:
def test_it_gets_all_activities_for_authenticated_user(
self,
2021-01-02 19:28:03 +01:00
app: Flask,
user_1: User,
user_2: User,
sport_1_cycling: Sport,
sport_2_running: Sport,
activity_cycling_user_1: Activity,
activity_cycling_user_2: Activity,
activity_running_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.get(
'/api/activities',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert 'creation_date' in data['data']['activities'][0]
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert 'test' == data['data']['activities'][0]['user']
assert 2 == data['data']['activities'][0]['sport_id']
assert 12.0 == data['data']['activities'][0]['distance']
assert '1:40:00' == data['data']['activities'][0]['duration']
assert 'creation_date' in data['data']['activities'][1]
assert (
'Mon, 01 Jan 2018 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
assert 'test' == data['data']['activities'][1]['user']
assert 1 == data['data']['activities'][1]['sport_id']
assert 10.0 == data['data']['activities'][1]['distance']
assert '1:00:00' == data['data']['activities'][1]['duration']
def test_it_gets_no_activities_for_authenticated_user_with_no_activities(
self,
2021-01-02 19:28:03 +01:00
app: Flask,
user_1: User,
user_2: User,
sport_1_cycling: Sport,
sport_2_running: Sport,
activity_cycling_user_1: Activity,
activity_running_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.get(
'/api/activities',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 0
2021-01-02 19:28:03 +01:00
def test_it_returns_401_if_user_is_not_authenticated(
self, app: Flask
) -> None:
2020-05-10 15:55:56 +02:00
client = app.test_client()
response = client.get('/api/activities')
data = json.loads(response.data.decode())
assert response.status_code == 401
assert 'error' in data['status']
assert 'Provide a valid auth token.' in data['message']
class TestGetActivitiesWithPagination:
def test_it_gets_activities_with_default_pagination(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 5
assert 'creation_date' in data['data']['activities'][0]
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert '0:50:00' == data['data']['activities'][0]['duration']
assert 'creation_date' in data['data']['activities'][4]
assert (
'Mon, 01 Jan 2018 00:00:00 GMT'
== data['data']['activities'][4]['activity_date']
)
assert '0:17:04' == data['data']['activities'][4]['duration']
def test_it_gets_first_page(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?page=1',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 5
assert 'creation_date' in data['data']['activities'][0]
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert '0:50:00' == data['data']['activities'][0]['duration']
assert 'creation_date' in data['data']['activities'][4]
assert (
'Mon, 01 Jan 2018 00:00:00 GMT'
== data['data']['activities'][4]['activity_date']
)
assert '0:17:04' == data['data']['activities'][4]['duration']
def test_it_gets_second_page(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert 'creation_date' in data['data']['activities'][0]
assert (
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert '0:57:36' == data['data']['activities'][0]['duration']
assert 'creation_date' in data['data']['activities'][1]
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
assert '0:17:04' == data['data']['activities'][1]['duration']
def test_it_gets_empty_third_page(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?page=3',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 0
def test_it_returns_error_on_invalid_page_value(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?page=A',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
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']
)
def test_it_gets_5_activities_per_page(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?per_page=10',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 7
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][6]['activity_date']
)
def test_it_gets_3_activities_per_page(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?per_page=3',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 3
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['activities'][2]['activity_date']
)
class TestGetActivitiesWithFilters:
def test_it_gets_activities_with_date_filter(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?from=2018-02-01&to=2018-02-28',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert 'creation_date' in data['data']['activities'][0]
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert '0:10:00' == data['data']['activities'][0]['duration']
assert 'creation_date' in data['data']['activities'][1]
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
assert '0:16:40' == data['data']['activities'][1]['duration']
def test_it_gets_no_activities_with_date_filter(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?from=2018-03-01&to=2018-03-30',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 0
def test_if_gets_activities_with_date_filter_from(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?from=2018-04-01',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert 'creation_date' in data['data']['activities'][0]
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
def test_it_gets_activities_with_date_filter_to(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?to=2017-12-31',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
def test_it_gets_activities_with_ascending_order(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 5
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['activities'][4]['activity_date']
)
def test_it_gets_activities_with_distance_filter(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?distance_from=5&distance_to=8',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
def test_it_gets_activities_with_duration_filter(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?duration_from=00:52&duration_to=01:20',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert (
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
def test_it_gets_activities_with_average_speed_filter(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?ave_speed_from=5&ave_speed_to=10',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert (
'Fri, 23 Feb 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
def test_it_gets_activities_with_max_speed_filter(
self,
2021-01-02 19:28:03 +01:00
app: Flask,
user_1: User,
sport_1_cycling: Sport,
sport_2_running: Sport,
activity_cycling_user_1: Activity,
activity_running_user_1: Activity,
) -> None:
2020-05-10 15:55:56 +02:00
activity_cycling_user_1.max_speed = 25
activity_running_user_1.max_speed = 11
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.get(
'/api/activities?max_speed_from=10&max_speed_to=20',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
def test_it_gets_activities_with_sport_filter(
self,
2021-01-02 19:28:03 +01:00
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_user_1: Activity,
sport_2_running: Sport,
activity_running_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.get(
'/api/activities?sport_id=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
class TestGetActivitiesWithFiltersAndPagination:
def test_it_gets_page_2_with_date_filter(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?from=2017-01-01&page=2',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Thu, 01 Jun 2017 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Mon, 20 Mar 2017 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
def test_it_get_page_2_with_date_filter_and_ascending_order(
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
seven_activities_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.get(
'/api/activities?from=2017-01-01&page=2&order=asc',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 2
assert (
'Sun, 01 Apr 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert (
'Wed, 09 May 2018 00:00:00 GMT'
== data['data']['activities'][1]['activity_date']
)
class TestGetActivity:
def test_it_gets_an_activity(
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.get(
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 == 200
assert 'success' in data['status']
assert len(data['data']['activities']) == 1
assert 'creation_date' in data['data']['activities'][0]
assert (
'Mon, 01 Jan 2018 00:00:00 GMT'
== data['data']['activities'][0]['activity_date']
)
assert 'test' == data['data']['activities'][0]['user']
assert 1 == data['data']['activities'][0]['sport_id']
assert 10.0 == data['data']['activities'][0]['distance']
assert '1:00:00' == data['data']['activities'][0]['duration']
def test_it_returns_403_if_activity_belongs_to_a_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_2: 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.get(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_cycling_user_2.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.get(
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']
assert len(data['data']['activities']) == 0
def test_it_returns_404_on_getting_gpx_if_activity_does_not_exist(
2021-01-02 19:28:03 +01:00
self, app: Flask, user_1: User
) -> None:
2020-12-30 22:07:43 +01:00
random_short_id = get_random_short_id()
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.get(
2020-12-30 22:07:43 +01:00
f'/api/activities/{random_short_id}/gpx',
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']
2020-12-30 22:07:43 +01:00
assert f'Activity not found (id: {random_short_id})' in data['message']
2020-05-10 15:55:56 +02:00
assert data['data']['gpx'] == ''
def test_it_returns_404_on_getting_chart_data_if_activity_does_not_exist(
2021-01-02 19:28:03 +01:00
self, app: Flask, user_1: User
) -> None:
2020-12-30 22:07:43 +01:00
random_short_id = get_random_short_id()
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.get(
2020-12-30 22:07:43 +01:00
f'/api/activities/{random_short_id}/chart_data',
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']
2020-12-30 22:07:43 +01:00
assert f'Activity not found (id: {random_short_id})' in data['message']
2020-05-10 15:55:56 +02:00
assert data['data']['chart_data'] == ''
def test_it_returns_404_on_getting_gpx_if_activity_have_no_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-12-30 22:07:43 +01:00
activity_short_id = activity_cycling_user_1.short_id
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.get(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_short_id}/gpx',
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
2021-01-01 16:39:25 +01:00
assert 'not found' in data['status']
assert (
2020-12-30 22:07:43 +01:00
f'No gpx file for this activity (id: {activity_short_id})'
in data['message']
)
2020-05-10 15:55:56 +02:00
def test_it_returns_404_if_activity_have_no_chart_data(
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-12-30 22:07:43 +01:00
activity_short_id = activity_cycling_user_1.short_id
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.get(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_short_id}/chart_data',
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
2021-01-01 16:39:25 +01:00
assert 'not found' in data['status']
assert (
2020-12-30 22:07:43 +01:00
f'No gpx file for this activity (id: {activity_short_id})'
in data['message']
)
2020-05-10 15:55:56 +02:00
def test_it_returns_500_on_getting_gpx_if_an_activity_has_invalid_gpx_pathname( # noqa
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
activity_cycling_user_1.gpx = "some path"
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.get(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_cycling_user_1.short_id}/gpx',
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 == 500
assert 'error' in data['status']
2021-01-01 16:39:25 +01:00
assert (
'Error. Please try again or contact the administrator.'
in data['message']
)
2020-05-10 15:55:56 +02:00
assert 'data' not in data
def test_it_returns_500_on_getting_chart_data_if_an_activity_has_invalid_gpx_pathname( # noqa
2021-01-02 19:28:03 +01:00
self,
app: Flask,
user_1: User,
sport_1_cycling: Sport,
activity_cycling_user_1: Activity,
) -> None:
activity_cycling_user_1.gpx = 'some path'
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.get(
2020-12-30 22:07:43 +01:00
f'/api/activities/{activity_cycling_user_1.short_id}/chart_data',
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 == 500
assert 'error' in data['status']
2021-01-01 16:39:25 +01:00
assert (
'Error. Please try again or contact the administrator.'
in data['message']
)
2020-05-10 15:55:56 +02:00
assert 'data' not in data
2021-01-02 19:28:03 +01:00
def test_it_returns_404_if_activity_has_no_map(
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.get(
f'/api/activities/map/{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
2021-01-01 16:39:25 +01:00
assert 'not found' in data['status']
2020-05-10 15:55:56 +02:00
assert 'Map does not exist' in data['message']