Tests - minor refactor
This commit is contained in:
0
fittrackee/tests/activities/__init__.py
Normal file
0
fittrackee/tests/activities/__init__.py
Normal file
924
fittrackee/tests/activities/test_activities_api_0_get.py
Normal file
924
fittrackee/tests/activities/test_activities_api_0_get.py
Normal file
@ -0,0 +1,924 @@
|
||||
import json
|
||||
|
||||
|
||||
class TestGetActivities:
|
||||
def test_it_gets_all_activities_for_authenticated_user(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_cycling_user_2,
|
||||
activity_running_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.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,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_running_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.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
|
||||
|
||||
def test_it_returns_401_if_user_is_not_authenticated(self, app):
|
||||
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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_running_user_1,
|
||||
):
|
||||
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,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
seven_activities_user_1,
|
||||
sport_2_running,
|
||||
activity_running_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
self, app, user_1, sport_1_cycling, seven_activities_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.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(
|
||||
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.get(
|
||||
'/api/activities/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']) == 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(
|
||||
self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_2
|
||||
):
|
||||
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/1',
|
||||
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.get(
|
||||
'/api/activities/11',
|
||||
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(
|
||||
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.get(
|
||||
'/api/activities/11/gpx',
|
||||
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 'Activity not found (id: 11)' in data['message']
|
||||
assert data['data']['gpx'] == ''
|
||||
|
||||
def test_it_returns_404_on_getting_chart_data_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.get(
|
||||
'/api/activities/11/chart_data',
|
||||
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 'Activity not found (id: 11)' in data['message']
|
||||
assert data['data']['chart_data'] == ''
|
||||
|
||||
def test_it_returns_404_on_getting_gpx_if_activity_have_no_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.get(
|
||||
'/api/activities/1/gpx',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'error' in data['status']
|
||||
assert 'No gpx file for this activity (id: 1)' in data['message']
|
||||
|
||||
def test_it_returns_404_if_activity_have_no_chart_data(
|
||||
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.get(
|
||||
'/api/activities/1/chart_data',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 404
|
||||
assert 'error' in data['status']
|
||||
assert 'No gpx file for this activity (id: 1)' in data['message']
|
||||
|
||||
def test_it_returns_500_on_getting_gpx_if_an_activity_has_invalid_gpx_pathname( # noqa
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
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(
|
||||
'/api/activities/1/gpx',
|
||||
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 'internal error' in data['message']
|
||||
assert 'data' not in data
|
||||
|
||||
def test_it_returns_500_on_getting_chart_data_if_an_activity_has_invalid_gpx_pathname( # noqa
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
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(
|
||||
'/api/activities/1/chart_data',
|
||||
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 'internal error' in data['message']
|
||||
assert 'data' not in data
|
||||
|
||||
def test_it_returns_404_if_activity_has_no_map(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.get(
|
||||
'/api/activities/map/123',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 404
|
||||
assert 'error' in data['status']
|
||||
assert 'Map does not exist' in data['message']
|
1322
fittrackee/tests/activities/test_activities_api_1_post.py
Normal file
1322
fittrackee/tests/activities/test_activities_api_1_post.py
Normal file
File diff suppressed because it is too large
Load Diff
768
fittrackee/tests/activities/test_activities_api_2_patch.py
Normal file
768
fittrackee/tests/activities/test_activities_api_2_patch.py
Normal file
@ -0,0 +1,768 @@
|
||||
import json
|
||||
from io import BytesIO
|
||||
|
||||
from fittrackee.activities.models import Activity
|
||||
|
||||
|
||||
def assert_activity_data_with_gpx(data):
|
||||
assert 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
== data['data']['activities'][0]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['activities'][0]['user']
|
||||
assert '0:04:10' == data['data']['activities'][0]['duration']
|
||||
assert data['data']['activities'][0]['ascent'] == 0.4
|
||||
assert data['data']['activities'][0]['ave_speed'] == 4.61
|
||||
assert data['data']['activities'][0]['descent'] == 23.4
|
||||
assert data['data']['activities'][0]['distance'] == 0.32
|
||||
assert data['data']['activities'][0]['max_alt'] == 998.0
|
||||
assert data['data']['activities'][0]['max_speed'] == 5.12
|
||||
assert data['data']['activities'][0]['min_alt'] == 975.0
|
||||
assert data['data']['activities'][0]['moving'] == '0:04:10'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is True
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[0]['value'] == 5.12
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[1]['value'] == '0:04:10'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[2]['value'] == 0.32
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Tue, 13 Mar 2018 12:44:45 GMT'
|
||||
assert records[3]['value'] == 4.61
|
||||
|
||||
|
||||
class TestEditActivityWithGpx:
|
||||
def test_it_updates_title_for_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2, title="Activity test")),
|
||||
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 2 == data['data']['activities'][0]['sport_id']
|
||||
assert data['data']['activities'][0]['title'] == 'Activity test'
|
||||
assert_activity_data_with_gpx(data)
|
||||
|
||||
def test_it_adds_notes_for_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(notes="test notes")),
|
||||
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 data['data']['activities'][0]['title'] == 'just an activity'
|
||||
assert data['data']['activities'][0]['notes'] == 'test notes'
|
||||
|
||||
def test_it_raises_403_when_editing_an_activity_from_different_user(
|
||||
self, app, user_1, user_2, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2, title="Activity test")),
|
||||
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_updates_sport(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(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 2 == data['data']['activities'][0]['sport_id']
|
||||
assert data['data']['activities'][0]['title'] == 'just an activity'
|
||||
assert_activity_data_with_gpx(data)
|
||||
|
||||
def test_it_returns_400_if_payload_is_empty(
|
||||
self, app, user_1, sport_1_cycling, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict()),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 400
|
||||
assert 'error' in data['status']
|
||||
assert 'Invalid payload.' in data['message']
|
||||
|
||||
def test_it_raises_500_if_sport_does_not_exists(
|
||||
self, app, user_1, sport_1_cycling, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(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 == 500
|
||||
assert 'error' in data['status']
|
||||
assert (
|
||||
'Error. Please try again or contact the administrator.'
|
||||
in data['message']
|
||||
)
|
||||
|
||||
|
||||
class TestEditActivityWithoutGpx:
|
||||
def test_it_updates_an_activity_wo_gpx(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 15:05',
|
||||
distance=8,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
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 (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 2
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] == 'Activity test'
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 8.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
assert data['data']['activities'][0]['map'] is None
|
||||
assert data['data']['activities'][0]['weather_start'] is None
|
||||
assert data['data']['activities'][0]['weather_end'] is None
|
||||
assert data['data']['activities'][0]['notes'] is None
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[0]['value'] == 8.0
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[2]['value'] == 8.0
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Tue, 15 May 2018 15:05:00 GMT'
|
||||
assert records[3]['value'] == 8.0
|
||||
|
||||
def test_it_adds_notes_to_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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(notes='test notes')),
|
||||
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 (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 1
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] is None
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 10.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 10.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 10.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
assert data['data']['activities'][0]['map'] is None
|
||||
assert data['data']['activities'][0]['weather_start'] is None
|
||||
assert data['data']['activities'][0]['weather_end'] is None
|
||||
assert data['data']['activities'][0]['notes'] == 'test notes'
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 1
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[0]['value'] == 10.0
|
||||
assert records[1]['sport_id'] == 1
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 1
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[2]['value'] == 10.0
|
||||
assert records[3]['sport_id'] == 1
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[3]['value'] == 10.0
|
||||
|
||||
def test_returns_403_when_editing_an_activity_wo_gpx_from_different_user(
|
||||
self, app, user_1, user_2, sport_1_cycling, activity_cycling_user_2
|
||||
):
|
||||
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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 15:05',
|
||||
distance=8,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
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_updates_an_activity_wo_gpx_with_timezone(
|
||||
self,
|
||||
app,
|
||||
user_1_paris,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 15:05',
|
||||
distance=8,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
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 (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 2
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] == 'Activity test'
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 8.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 8.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[0]['value'] == 8.0
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[2]['value'] == 8.0
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Tue, 15 May 2018 13:05:00 GMT'
|
||||
assert records[3]['value'] == 8.0
|
||||
|
||||
def test_it_updates_only_sport_and_distance_an_activity_wo_gpx(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=2, distance=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 'creation_date' in data['data']['activities'][0]
|
||||
assert (
|
||||
data['data']['activities'][0]['activity_date']
|
||||
== 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
)
|
||||
assert data['data']['activities'][0]['user'] == 'test'
|
||||
assert data['data']['activities'][0]['sport_id'] == 2
|
||||
assert data['data']['activities'][0]['duration'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['title'] is None
|
||||
assert data['data']['activities'][0]['ascent'] is None
|
||||
assert data['data']['activities'][0]['ave_speed'] == 20.0
|
||||
assert data['data']['activities'][0]['descent'] is None
|
||||
assert data['data']['activities'][0]['distance'] == 20.0
|
||||
assert data['data']['activities'][0]['max_alt'] is None
|
||||
assert data['data']['activities'][0]['max_speed'] == 20.0
|
||||
assert data['data']['activities'][0]['min_alt'] is None
|
||||
assert data['data']['activities'][0]['moving'] == '1:00:00'
|
||||
assert data['data']['activities'][0]['pauses'] is None
|
||||
assert data['data']['activities'][0]['with_gpx'] is False
|
||||
|
||||
records = data['data']['activities'][0]['records']
|
||||
assert len(records) == 4
|
||||
assert records[0]['sport_id'] == 2
|
||||
assert records[0]['activity_id'] == 1
|
||||
assert records[0]['record_type'] == 'MS'
|
||||
assert records[0]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[0]['value'] == 20.0
|
||||
assert records[1]['sport_id'] == 2
|
||||
assert records[1]['activity_id'] == 1
|
||||
assert records[1]['record_type'] == 'LD'
|
||||
assert records[1]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[1]['value'] == '1:00:00'
|
||||
assert records[2]['sport_id'] == 2
|
||||
assert records[2]['activity_id'] == 1
|
||||
assert records[2]['record_type'] == 'FD'
|
||||
assert records[2]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[2]['value'] == 20.0
|
||||
assert records[3]['sport_id'] == 2
|
||||
assert records[3]['activity_id'] == 1
|
||||
assert records[3]['record_type'] == 'AS'
|
||||
assert records[3]['activity_date'] == 'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
assert records[3]['value'] == 20.0
|
||||
|
||||
def test_it_returns_400_if_payload_is_empty(
|
||||
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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict()),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 400
|
||||
assert 'error' in data['status']
|
||||
assert 'Invalid payload.' in data['message']
|
||||
|
||||
def test_it_returns_500_if_date_format_is_invalid(
|
||||
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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='15/2018',
|
||||
distance=10,
|
||||
)
|
||||
),
|
||||
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_returns_404_if_edited_activity_doens_not_exists(
|
||||
self, app, user_1, sport_1_cycling
|
||||
):
|
||||
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.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-15 14:05',
|
||||
distance=10,
|
||||
)
|
||||
),
|
||||
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
|
||||
|
||||
|
||||
class TestRefreshActivityWithGpx:
|
||||
def test_refresh_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
# Edit some activity data
|
||||
activity = Activity.query.filter_by(id=1).first()
|
||||
activity.ascent = 1000
|
||||
activity.min_alt = -100
|
||||
|
||||
response = client.patch(
|
||||
'/api/activities/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(refresh=True)),
|
||||
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 1 == data['data']['activities'][0]['sport_id']
|
||||
assert 0.4 == data['data']['activities'][0]['ascent']
|
||||
assert 975.0 == data['data']['activities'][0]['min_alt']
|
188
fittrackee/tests/activities/test_activities_api_3_delete.py
Normal file
188
fittrackee/tests/activities/test_activities_api_3_delete.py
Normal file
@ -0,0 +1,188 @@
|
||||
import json
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
from fittrackee.activities.models import Activity
|
||||
from fittrackee.activities.utils import get_absolute_file_path
|
||||
|
||||
|
||||
def get_gpx_filepath(activity_id):
|
||||
activity = Activity.query.filter_by(id=activity_id).first()
|
||||
return activity.gpx
|
||||
|
||||
|
||||
class TestDeleteActivityWithGpx:
|
||||
def test_it_deletes_an_activity_with_gpx(
|
||||
self, app, user_1, sport_1_cycling, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
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, gpx_file
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(dict(email='toto@toto.com', password='87654321')),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
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(
|
||||
'/api/activities/9999',
|
||||
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
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities',
|
||||
data=dict(
|
||||
file=(BytesIO(str.encode(gpx_file)), 'example.gpx'),
|
||||
data='{"sport_id": 1}',
|
||||
),
|
||||
headers=dict(
|
||||
content_type='multipart/form-data',
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token'],
|
||||
),
|
||||
)
|
||||
|
||||
gpx_filepath = get_gpx_filepath(1)
|
||||
gpx_filepath = get_absolute_file_path(gpx_filepath)
|
||||
os.remove(gpx_filepath)
|
||||
|
||||
response = client.delete(
|
||||
'/api/activities/1',
|
||||
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']
|
||||
)
|
||||
|
||||
|
||||
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(
|
||||
'/api/activities/1',
|
||||
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(
|
||||
'/api/activities/1',
|
||||
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']
|
60
fittrackee/tests/activities/test_activities_model.py
Normal file
60
fittrackee/tests/activities/test_activities_model.py
Normal file
@ -0,0 +1,60 @@
|
||||
class TestActivityModel:
|
||||
def test_activity_model(
|
||||
self, app, sport_1_cycling, user_1, activity_cycling_user_1
|
||||
):
|
||||
activity_cycling_user_1.title = 'Test'
|
||||
|
||||
assert 1 == activity_cycling_user_1.id
|
||||
assert 1 == activity_cycling_user_1.user_id
|
||||
assert 1 == activity_cycling_user_1.sport_id
|
||||
assert '2018-01-01 00:00:00' == str(
|
||||
activity_cycling_user_1.activity_date
|
||||
)
|
||||
assert 10.0 == float(activity_cycling_user_1.distance)
|
||||
assert '1:00:00' == str(activity_cycling_user_1.duration)
|
||||
assert 'Test' == activity_cycling_user_1.title
|
||||
assert '<Activity \'Cycling\' - 2018-01-01 00:00:00>' == str(
|
||||
activity_cycling_user_1
|
||||
)
|
||||
|
||||
serialized_activity = activity_cycling_user_1.serialize()
|
||||
assert 1 == serialized_activity['id']
|
||||
assert 'test' == serialized_activity['user']
|
||||
assert 1 == serialized_activity['sport_id']
|
||||
assert serialized_activity['title'] == 'Test'
|
||||
assert 'creation_date' in serialized_activity
|
||||
assert serialized_activity['modification_date'] is not None
|
||||
assert (
|
||||
str(serialized_activity['activity_date']) == '2018-01-01 00:00:00'
|
||||
)
|
||||
assert serialized_activity['duration'] == '1:00:00'
|
||||
assert serialized_activity['pauses'] is None
|
||||
assert serialized_activity['moving'] == '1:00:00'
|
||||
assert serialized_activity['distance'] == 10.0
|
||||
assert serialized_activity['max_alt'] is None
|
||||
assert serialized_activity['descent'] is None
|
||||
assert serialized_activity['ascent'] is None
|
||||
assert serialized_activity['max_speed'] == 10.0
|
||||
assert serialized_activity['ave_speed'] == 10.0
|
||||
assert serialized_activity['with_gpx'] is False
|
||||
assert serialized_activity['bounds'] == []
|
||||
assert serialized_activity['previous_activity'] is None
|
||||
assert serialized_activity['next_activity'] is None
|
||||
assert serialized_activity['segments'] == []
|
||||
assert serialized_activity['records'] != []
|
||||
assert serialized_activity['map'] is None
|
||||
assert serialized_activity['weather_start'] is None
|
||||
assert serialized_activity['weather_end'] is None
|
||||
assert serialized_activity['notes'] is None
|
||||
|
||||
def test_activity_segment_model(
|
||||
self,
|
||||
app,
|
||||
sport_1_cycling,
|
||||
user_1,
|
||||
activity_cycling_user_1,
|
||||
activity_cycling_user_1_segment,
|
||||
):
|
||||
assert '<Segment \'0\' for activity \'1\'>' == str(
|
||||
activity_cycling_user_1_segment
|
||||
)
|
946
fittrackee/tests/activities/test_records_api.py
Normal file
946
fittrackee/tests/activities/test_records_api.py
Normal file
@ -0,0 +1,946 @@
|
||||
import json
|
||||
|
||||
|
||||
class TestGetRecords:
|
||||
def test_it_gets_records_for_authenticated_user(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_1,
|
||||
activity_cycling_user_2,
|
||||
):
|
||||
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/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 'value' in data['data']['records'][0]
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 'value' in data['data']['records'][1]
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
)
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert 'value' in data['data']['records'][2]
|
||||
|
||||
assert (
|
||||
'Mon, 01 Jan 2018 00:00:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 'value' in data['data']['records'][3]
|
||||
|
||||
def test_it_gets_no_records_if_user_has_no_activity(
|
||||
self,
|
||||
app,
|
||||
user_1,
|
||||
user_2,
|
||||
sport_1_cycling,
|
||||
sport_2_running,
|
||||
activity_cycling_user_2,
|
||||
):
|
||||
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/records',
|
||||
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']['records']) == 0
|
||||
|
||||
def test_it_gets_no_records_if_activity_has_zero_value(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=0,
|
||||
activity_date='2018-05-14 14:05',
|
||||
distance=0,
|
||||
title='Activity test',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 0
|
||||
|
||||
def test_it_gets_updated_records_after_activities_post_and_patch(
|
||||
self, app, user_1, sport_1_cycling
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-14 14:05',
|
||||
distance=7,
|
||||
title='Activity test 1',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# Post activity with lower duration (same sport)
|
||||
# => 2 new records: Average speed and Max speed
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3000,
|
||||
activity_date='2018-05-15 14:05',
|
||||
distance=7,
|
||||
title='Activity test 2',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 8.4 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 8.4 == data['data']['records'][3]['value']
|
||||
|
||||
# Post activity with no new records
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3500,
|
||||
activity_date='2018-05-16 14:05',
|
||||
distance=6.5,
|
||||
title='Activity test 3',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 8.4 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 8.4 == data['data']['records'][3]['value']
|
||||
|
||||
# Edit last activity
|
||||
# 1 new record: Longest duration
|
||||
client.patch(
|
||||
'/api/activities/3',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(duration=4000)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 8.4 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Tue, 15 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 8.4 == data['data']['records'][3]['value']
|
||||
|
||||
# delete activity 2 => AS and MS record update
|
||||
client.delete(
|
||||
'/api/activities/2',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# add an activity with the same data as activity 1 except with a
|
||||
# later date => no change in record
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-20 14:05',
|
||||
distance=7,
|
||||
title='Activity test 4',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# add an activity with the same data as activity 1 except with
|
||||
# an earlier date
|
||||
# => record update (activity 5 replace activity 1)
|
||||
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-14 08:05',
|
||||
distance=7,
|
||||
title='Activity test 5',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 4
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 08:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 5 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 08:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 5 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 3 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:06:40' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 08:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 5 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
# delete all activities - no more records
|
||||
client.delete(
|
||||
'/api/activities/1',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.delete(
|
||||
'/api/activities/3',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.delete(
|
||||
'/api/activities/4',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.delete(
|
||||
'/api/activities/5',
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 0
|
||||
|
||||
def test_it_gets_updated_records_after_sport_change(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running
|
||||
):
|
||||
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',
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3600,
|
||||
activity_date='2018-05-14 14:05',
|
||||
distance=7,
|
||||
title='Activity test 1',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3600,
|
||||
activity_date='2018-05-16 16:05',
|
||||
distance=20,
|
||||
title='Activity test 2',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=1,
|
||||
duration=3000,
|
||||
activity_date='2018-05-17 17:05',
|
||||
distance=3,
|
||||
title='Activity test 3',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
client.post(
|
||||
'/api/activities/no_gpx',
|
||||
content_type='application/json',
|
||||
data=json.dumps(
|
||||
dict(
|
||||
sport_id=2,
|
||||
duration=3000,
|
||||
activity_date='2018-05-18 18:05',
|
||||
distance=10,
|
||||
title='Activity test 4',
|
||||
)
|
||||
),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 8
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 1 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 7.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 1 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 7.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 1 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 7.0 == data['data']['records'][3]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][4]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][4]['user']
|
||||
assert 2 == data['data']['records'][4]['sport_id']
|
||||
assert 2 == data['data']['records'][4]['activity_id']
|
||||
assert 'AS' == data['data']['records'][4]['record_type']
|
||||
assert 20.0 == data['data']['records'][4]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][5]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][5]['user']
|
||||
assert 2 == data['data']['records'][5]['sport_id']
|
||||
assert 2 == data['data']['records'][5]['activity_id']
|
||||
assert 'FD' == data['data']['records'][5]['record_type']
|
||||
assert 20.0 == data['data']['records'][5]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][6]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][6]['user']
|
||||
assert 2 == data['data']['records'][6]['sport_id']
|
||||
assert 2 == data['data']['records'][6]['activity_id']
|
||||
assert 'LD' == data['data']['records'][6]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][6]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][7]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][7]['user']
|
||||
assert 2 == data['data']['records'][7]['sport_id']
|
||||
assert 2 == data['data']['records'][7]['activity_id']
|
||||
assert 'MS' == data['data']['records'][7]['record_type']
|
||||
assert 20.0 == data['data']['records'][7]['value']
|
||||
|
||||
client.patch(
|
||||
'/api/activities/2',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(sport_id=1)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
response = client.get(
|
||||
'/api/records',
|
||||
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']['records']) == 8
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][0]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][0]['user']
|
||||
assert 1 == data['data']['records'][0]['sport_id']
|
||||
assert 2 == data['data']['records'][0]['activity_id']
|
||||
assert 'AS' == data['data']['records'][0]['record_type']
|
||||
assert 20.0 == data['data']['records'][0]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][1]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][1]['user']
|
||||
assert 1 == data['data']['records'][1]['sport_id']
|
||||
assert 2 == data['data']['records'][1]['activity_id']
|
||||
assert 'FD' == data['data']['records'][1]['record_type']
|
||||
assert 20.0 == data['data']['records'][1]['value']
|
||||
|
||||
assert (
|
||||
'Mon, 14 May 2018 14:05:00 GMT'
|
||||
== data['data']['records'][2]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][2]['user']
|
||||
assert 1 == data['data']['records'][2]['sport_id']
|
||||
assert 1 == data['data']['records'][2]['activity_id']
|
||||
assert 'LD' == data['data']['records'][2]['record_type']
|
||||
assert '1:00:00' == data['data']['records'][2]['value']
|
||||
|
||||
assert (
|
||||
'Wed, 16 May 2018 16:05:00 GMT'
|
||||
== data['data']['records'][3]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][3]['user']
|
||||
assert 1 == data['data']['records'][3]['sport_id']
|
||||
assert 2 == data['data']['records'][3]['activity_id']
|
||||
assert 'MS' == data['data']['records'][3]['record_type']
|
||||
assert 20.0 == data['data']['records'][3]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][4]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][4]['user']
|
||||
assert 2 == data['data']['records'][4]['sport_id']
|
||||
assert 4 == data['data']['records'][4]['activity_id']
|
||||
assert 'AS' == data['data']['records'][4]['record_type']
|
||||
assert 12.0 == data['data']['records'][4]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][5]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][5]['user']
|
||||
assert 2 == data['data']['records'][5]['sport_id']
|
||||
assert 4 == data['data']['records'][5]['activity_id']
|
||||
assert 'FD' == data['data']['records'][5]['record_type']
|
||||
assert 10.0 == data['data']['records'][5]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][6]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][6]['user']
|
||||
assert 2 == data['data']['records'][6]['sport_id']
|
||||
assert 4 == data['data']['records'][6]['activity_id']
|
||||
assert 'LD' == data['data']['records'][6]['record_type']
|
||||
assert '0:50:00' == data['data']['records'][6]['value']
|
||||
|
||||
assert (
|
||||
'Fri, 18 May 2018 18:05:00 GMT'
|
||||
== data['data']['records'][7]['activity_date']
|
||||
) # noqa
|
||||
assert 'test' == data['data']['records'][7]['user']
|
||||
assert 2 == data['data']['records'][7]['sport_id']
|
||||
assert 4 == data['data']['records'][7]['activity_id']
|
||||
assert 'MS' == data['data']['records'][7]['record_type']
|
||||
assert 12.0 == data['data']['records'][7]['value']
|
135
fittrackee/tests/activities/test_records_model.py
Normal file
135
fittrackee/tests/activities/test_records_model.py
Normal file
@ -0,0 +1,135 @@
|
||||
import datetime
|
||||
|
||||
from fittrackee.activities.models import Record
|
||||
|
||||
|
||||
class TestRecordModel:
|
||||
def test_record_model(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
assert 'test' == record_ld.user.username
|
||||
assert 1 == record_ld.sport_id
|
||||
assert 1 == record_ld.activity_id
|
||||
assert 'LD' == record_ld.record_type
|
||||
assert '2018-01-01 00:00:00' == str(record_ld.activity_date)
|
||||
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert 'id' in record_serialize
|
||||
assert 'user' in record_serialize
|
||||
assert 'sport_id' in record_serialize
|
||||
assert 'activity_id' in record_serialize
|
||||
assert 'record_type' in record_serialize
|
||||
assert 'activity_date' in record_serialize
|
||||
assert 'value' in record_serialize
|
||||
|
||||
def test_record_model_with_none_value(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
record_ld.value = None
|
||||
assert 'test' == record_ld.user.username
|
||||
assert 1 == record_ld.sport_id
|
||||
assert 1 == record_ld.activity_id
|
||||
assert 'LD' == record_ld.record_type
|
||||
assert '2018-01-01 00:00:00' == str(record_ld.activity_date)
|
||||
assert '<Record Cycling - LD - 2018-01-01>' == str(record_ld)
|
||||
assert record_ld.value is None
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert record_serialize['value'] is None
|
||||
|
||||
def test_average_speed_records(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_as = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='AS',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_as.value, float)
|
||||
assert record_as.value == 10.0
|
||||
assert record_as._value == 1000
|
||||
|
||||
record_serialize = record_as.serialize()
|
||||
assert record_serialize.get('value') == 10.0
|
||||
assert isinstance(record_serialize.get('value'), float)
|
||||
|
||||
def test_add_farest_distance_records(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_fd = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='FD',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_fd.value, float)
|
||||
assert record_fd.value == 10.0
|
||||
assert record_fd._value == 10000
|
||||
|
||||
record_serialize = record_fd.serialize()
|
||||
assert record_serialize.get('value') == 10.0
|
||||
assert isinstance(record_serialize.get('value'), float)
|
||||
|
||||
def test_add_longest_duration_records(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_ld.value, datetime.timedelta)
|
||||
assert str(record_ld.value) == '1:00:00'
|
||||
assert record_ld._value == 3600
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert record_serialize.get('value') == '1:00:00'
|
||||
assert isinstance(record_serialize.get('value'), str)
|
||||
|
||||
def test_add_longest_duration_records_with_zero(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ld = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='LD',
|
||||
).first()
|
||||
record_ld.value = datetime.timedelta(seconds=0)
|
||||
|
||||
assert isinstance(record_ld.value, datetime.timedelta)
|
||||
assert str(record_ld.value) == '0:00:00'
|
||||
assert record_ld._value == 0
|
||||
|
||||
record_serialize = record_ld.serialize()
|
||||
assert record_serialize.get('value') == '0:00:00'
|
||||
assert isinstance(record_serialize.get('value'), str)
|
||||
|
||||
def test_max_speed_records_no_value(
|
||||
self, app, user_1, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
record_ms = Record.query.filter_by(
|
||||
user_id=activity_cycling_user_1.user_id,
|
||||
sport_id=activity_cycling_user_1.sport_id,
|
||||
record_type='MS',
|
||||
).first()
|
||||
|
||||
assert isinstance(record_ms.value, float)
|
||||
assert record_ms.value == 10.0
|
||||
assert record_ms._value == 1000
|
||||
|
||||
record_serialize = record_ms.serialize()
|
||||
assert record_serialize.get('value') == 10.0
|
||||
assert isinstance(record_serialize.get('value'), float)
|
408
fittrackee/tests/activities/test_sports_api.py
Normal file
408
fittrackee/tests/activities/test_sports_api.py
Normal file
@ -0,0 +1,408 @@
|
||||
import json
|
||||
|
||||
expected_sport_1_cycling_result = {
|
||||
'id': 1,
|
||||
'label': 'Cycling',
|
||||
'img': None,
|
||||
'is_active': True,
|
||||
}
|
||||
expected_sport_1_cycling_admin_result = expected_sport_1_cycling_result.copy()
|
||||
expected_sport_1_cycling_admin_result['has_activities'] = False
|
||||
|
||||
expected_sport_2_running_result = {
|
||||
'id': 2,
|
||||
'label': 'Running',
|
||||
'img': None,
|
||||
'is_active': True,
|
||||
}
|
||||
expected_sport_2_running_admin_result = expected_sport_2_running_result.copy()
|
||||
expected_sport_2_running_admin_result['has_activities'] = False
|
||||
|
||||
expected_sport_1_cycling_inactive_result = {
|
||||
'id': 1,
|
||||
'label': 'Cycling',
|
||||
'img': None,
|
||||
'is_active': False,
|
||||
}
|
||||
expected_sport_1_cycling_inactive_admin_result = (
|
||||
expected_sport_1_cycling_inactive_result.copy()
|
||||
)
|
||||
expected_sport_1_cycling_inactive_admin_result['has_activities'] = False
|
||||
|
||||
|
||||
class TestGetSports:
|
||||
def test_it_gets_all_sports(
|
||||
self, app, user_1, sport_1_cycling, sport_2_running
|
||||
):
|
||||
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/sports',
|
||||
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']['sports']) == 2
|
||||
assert data['data']['sports'][0] == expected_sport_1_cycling_result
|
||||
assert data['data']['sports'][1] == expected_sport_2_running_result
|
||||
|
||||
def test_it_gets_all_sports_with_inactive_one(
|
||||
self, app, user_1, sport_1_cycling_inactive, sport_2_running
|
||||
):
|
||||
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/sports',
|
||||
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']['sports']) == 2
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_result
|
||||
)
|
||||
assert data['data']['sports'][1] == expected_sport_2_running_result
|
||||
|
||||
def test_it_gets_all_sports_with_admin_rights(
|
||||
self, app, user_1_admin, sport_1_cycling_inactive, sport_2_running
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.get(
|
||||
'/api/sports',
|
||||
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']['sports']) == 2
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_admin_result
|
||||
)
|
||||
assert (
|
||||
data['data']['sports'][1] == expected_sport_2_running_admin_result
|
||||
)
|
||||
|
||||
|
||||
class TestGetSport:
|
||||
def test_it_gets_a_sport(self, app, user_1, sport_1_cycling):
|
||||
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/sports/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']['sports']) == 1
|
||||
assert data['data']['sports'][0] == expected_sport_1_cycling_result
|
||||
|
||||
def test_it_returns_404_if_sport_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.get(
|
||||
'/api/sports/1',
|
||||
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']['sports']) == 0
|
||||
|
||||
def test_it_gets_a_inactive_sport(
|
||||
self, app, user_1, sport_1_cycling_inactive
|
||||
):
|
||||
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/sports/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']['sports']) == 1
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_result
|
||||
)
|
||||
|
||||
def test_it_get_an_inactive_sport_with_admin_rights(
|
||||
self, app, user_1_admin, sport_1_cycling_inactive
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.get(
|
||||
'/api/sports/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']['sports']) == 1
|
||||
assert (
|
||||
data['data']['sports'][0]
|
||||
== expected_sport_1_cycling_inactive_admin_result
|
||||
)
|
||||
|
||||
|
||||
class TestUpdateSport:
|
||||
def test_it_disables_a_sport(self, app, user_1_admin, sport_1_cycling):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
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']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is False
|
||||
assert data['data']['sports'][0]['has_activities'] is False
|
||||
|
||||
def test_it_enables_a_sport(self, app, user_1_admin, sport_1_cycling):
|
||||
sport_1_cycling.is_active = False
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=True)),
|
||||
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']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is True
|
||||
assert data['data']['sports'][0]['has_activities'] is False
|
||||
|
||||
def test_it_disables_a_sport_with_activities(
|
||||
self, app, user_1_admin, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
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']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is False
|
||||
assert data['data']['sports'][0]['has_activities'] is True
|
||||
|
||||
def test_it_enables_a_sport_with_activities(
|
||||
self, app, user_1_admin, sport_1_cycling, activity_cycling_user_1
|
||||
):
|
||||
sport_1_cycling.is_active = False
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=True)),
|
||||
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']['sports']) == 1
|
||||
assert data['data']['sports'][0]['is_active'] is True
|
||||
assert data['data']['sports'][0]['has_activities'] is True
|
||||
|
||||
def test_returns_error_if_user_has_no_admin_rights(
|
||||
self, app, user_1, sport_1_cycling
|
||||
):
|
||||
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.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
data = json.loads(response.data.decode())
|
||||
|
||||
assert response.status_code == 403
|
||||
assert 'success' not in data['status']
|
||||
assert 'error' in data['status']
|
||||
assert 'You do not have permissions.' in data['message']
|
||||
|
||||
def test_returns_error_if_payload_is_invalid(self, app, user_1_admin):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict()),
|
||||
headers=dict(
|
||||
Authorization='Bearer '
|
||||
+ json.loads(resp_login.data.decode())['auth_token']
|
||||
),
|
||||
)
|
||||
|
||||
data = json.loads(response.data.decode())
|
||||
assert response.status_code == 400
|
||||
assert 'error' in data['status']
|
||||
assert 'Invalid payload.' in data['message']
|
||||
|
||||
def test_it_returns_error_if_sport_does_not_exist(self, app, user_1_admin):
|
||||
client = app.test_client()
|
||||
resp_login = client.post(
|
||||
'/api/auth/login',
|
||||
data=json.dumps(
|
||||
dict(email='admin@example.com', password='12345678')
|
||||
),
|
||||
content_type='application/json',
|
||||
)
|
||||
response = client.patch(
|
||||
'/api/sports/1',
|
||||
content_type='application/json',
|
||||
data=json.dumps(dict(is_active=False)),
|
||||
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']['sports']) == 0
|
28
fittrackee/tests/activities/test_sports_model.py
Normal file
28
fittrackee/tests/activities/test_sports_model.py
Normal file
@ -0,0 +1,28 @@
|
||||
class TestSportModel:
|
||||
@staticmethod
|
||||
def assert_sport_model(sport, is_admin=False):
|
||||
assert 1 == sport.id
|
||||
assert 'Cycling' == sport.label
|
||||
assert '<Sport \'Cycling\'>' == str(sport)
|
||||
|
||||
serialized_sport = sport.serialize(is_admin)
|
||||
assert 1 == serialized_sport['id']
|
||||
assert 'Cycling' == serialized_sport['label']
|
||||
assert serialized_sport['is_active'] is True
|
||||
return serialized_sport
|
||||
|
||||
def test_sport_model(self, app, sport_1_cycling):
|
||||
serialized_sport = self.assert_sport_model(sport_1_cycling)
|
||||
assert 'has_activities' not in serialized_sport
|
||||
|
||||
def test_sport_model_with_activity(
|
||||
self, app, sport_1_cycling, user_1, activity_cycling_user_1
|
||||
):
|
||||
serialized_sport = self.assert_sport_model(sport_1_cycling)
|
||||
assert 'has_activities' not in serialized_sport
|
||||
|
||||
def test_sport_model_with_activity_as_admin(
|
||||
self, app, sport_1_cycling, user_1, activity_cycling_user_1
|
||||
):
|
||||
serialized_sport = self.assert_sport_model(sport_1_cycling, True)
|
||||
assert serialized_sport['has_activities'] is True
|
1074
fittrackee/tests/activities/test_stats_api.py
Normal file
1074
fittrackee/tests/activities/test_stats_api.py
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user