API - test refacto (response errors assertion)

This commit is contained in:
Sam
2022-03-13 08:36:49 +01:00
parent 1245a64e18
commit 2de9cb1857
12 changed files with 281 additions and 552 deletions

View File

@ -209,9 +209,7 @@ class TestGetSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
data = self.assert_404(response)
assert len(data['data']['sports']) == 0
def test_it_gets_a_inactive_sport(
@ -424,11 +422,7 @@ class TestUpdateSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
self.assert_403(response)
def test_returns_error_if_payload_is_invalid(
self, app: Flask, user_1_admin: User
@ -444,10 +438,7 @@ class TestUpdateSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
self.assert_400(response)
def test_it_returns_error_if_sport_does_not_exist(
self, app: Flask, user_1_admin: User
@ -463,7 +454,5 @@ class TestUpdateSport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
data = self.assert_404(response)
assert len(data['data']['sports']) == 0

View File

@ -38,10 +38,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert 'user does not exist' in data['message']
self.assert_404_with_entity(response, 'user')
def test_it_returns_error_if_date_format_is_invalid(
self,
@ -64,13 +61,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
self.assert_500(response)
def test_it_returns_error_if_period_is_invalid(
self,
@ -90,10 +81,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'fail' in data['status']
assert 'Invalid time period.' in data['message']
self.assert_400(response, 'Invalid time period.', 'fail')
def test_it_gets_stats_by_time_all_workouts(
self,
@ -955,10 +943,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert 'user does not exist' in data['message']
self.assert_404_with_entity(response, 'user')
def test_it_returns_error_if_sport_does_not_exist(
self,
@ -978,10 +963,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert 'sport does not exist' in data['message']
self.assert_404_with_entity(response, 'sport')
def test_it_returns_error_if_sport_id_is_invalid(
self,
@ -1001,13 +983,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
self.assert_500(response)
class TestGetAllStats(ApiTestCaseMixin):
@ -1081,8 +1057,4 @@ class TestGetAllStats(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
self.assert_403(response)

View File

@ -107,10 +107,7 @@ class TestGetWorkouts(ApiTestCaseMixin):
response = client.get('/api/workouts')
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']
self.assert_401(response, 'provide a valid auth token')
class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
@ -278,13 +275,7 @@ class TestGetWorkoutsWithPagination(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
self.assert_500(response)
@patch('fittrackee.workouts.workouts.MAX_WORKOUTS_PER_PAGE', 6)
def test_it_gets_max_workouts_per_page_if_per_page_exceeds_max(
@ -1034,10 +1025,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
self.assert_403(response)
def test_it_returns_404_if_workout_does_not_exist(
self, app: Flask, user_1: User
@ -1051,9 +1039,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
data = self.assert_404(response)
assert len(data['data']['workouts']) == 0
def test_it_returns_404_on_getting_gpx_if_workout_does_not_exist(
@ -1069,10 +1055,9 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert f'workout not found (id: {random_short_id})' in data['message']
data = self.assert_404_with_message(
response, f'workout not found (id: {random_short_id})'
)
assert data['data']['gpx'] == ''
def test_it_returns_404_on_getting_chart_data_if_workout_does_not_exist(
@ -1088,10 +1073,9 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert f'workout not found (id: {random_short_id})' in data['message']
data = self.assert_404_with_message(
response, f'workout not found (id: {random_short_id})'
)
assert data['data']['chart_data'] == ''
def test_it_returns_404_on_getting_gpx_if_workout_have_no_gpx(
@ -1111,12 +1095,8 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert (
f'no gpx file for this workout (id: {workout_short_id})'
in data['message']
self.assert_404_with_message(
response, f'no gpx file for this workout (id: {workout_short_id})'
)
def test_it_returns_404_if_workout_have_no_chart_data(
@ -1136,12 +1116,8 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert (
f'no gpx file for this workout (id: {workout_short_id})'
in data['message']
self.assert_404_with_message(
response, f'no gpx file for this workout (id: {workout_short_id})'
)
def test_it_returns_500_on_getting_gpx_if_an_workout_has_invalid_gpx_pathname( # noqa
@ -1161,13 +1137,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
data = self.assert_500(response)
assert 'data' not in data
def test_it_returns_500_on_getting_chart_data_if_an_workout_has_invalid_gpx_pathname( # noqa
@ -1187,13 +1157,7 @@ class TestGetWorkout(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
data = self.assert_500(response)
assert 'data' not in data
def test_it_returns_404_if_workout_has_no_map(
@ -1206,11 +1170,8 @@ class TestGetWorkout(ApiTestCaseMixin):
f'/api/workouts/map/{uuid4().hex}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert 'Map does not exist' in data['message']
self.assert_404_with_message(response, 'Map does not exist')
class TestDownloadWorkoutGpx(ApiTestCaseMixin):
@ -1228,10 +1189,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert 'workout not found' in data['message']
self.assert_404_with_message(response, 'workout not found')
def test_it_returns_404_if_workout_does_not_have_gpx(
self,
@ -1249,10 +1207,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert 'no gpx file for workout' in data['message']
self.assert_404_with_message(response, 'no gpx file for workout')
def test_it_returns_404_if_workout_belongs_to_a_different_user(
self,
@ -1271,10 +1226,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert 'workout not found' in data['message']
self.assert_404_with_message(response, 'workout not found')
def test_it_calls_send_from_directory_if_workout_has_gpx(
self,

View File

@ -1,6 +1,5 @@
import json
import os
import re
from datetime import datetime
from io import BytesIO
from typing import Dict
@ -424,10 +423,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error during gpx processing.' in data['message']
data = self.assert_500(response, 'Error during gpx processing.')
assert 'data' not in data
def test_it_returns_500_if_gpx_has_invalid_xml(
@ -456,10 +452,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error during gpx file parsing.' in data['message']
data = self.assert_500(response, 'Error during gpx file parsing.')
assert 'data' not in data
def test_it_returns_400_if_workout_gpx_has_invalid_extension(
@ -481,10 +474,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert data['status'] == 'fail'
assert data['message'] == 'file extension not allowed'
self.assert_400(response, 'file extension not allowed', 'fail')
def test_it_returns_400_if_sport_id_is_not_provided(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -504,10 +494,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert data['status'] == 'error'
assert data['message'] == 'invalid payload'
self.assert_400(response)
def test_it_returns_500_if_sport_id_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -528,10 +515,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert data['status'] == 'error'
assert data['message'] == 'Sport id: 2 does not exist'
self.assert_500(response, 'Sport id: 2 does not exist')
def test_returns_400_if_no_gpx_file_is_provided(
self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -549,10 +533,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert data['status'] == 'fail'
assert data['message'] == 'no file part'
self.assert_400(response, 'no file part', 'fail')
def test_it_returns_error_if_file_size_exceeds_limit(
self,
@ -576,12 +557,13 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin):
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
assert response.status_code == 413
assert 'fail' in data['status']
assert re.match(
r'Error during workout upload, file size \((.*)\) exceeds 1.0KB.',
data['message'],
data = self.assert_413(
response,
match=(
r'Error during workout upload, '
r'file size \((.*)\) exceeds 1.0KB.'
),
)
assert 'data' not in data
@ -628,10 +610,7 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
self.assert_400(response)
def test_it_returns_500_if_workout_format_is_invalid(
self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -654,10 +633,7 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'fail' in data['status']
assert 'Error during workout save.' in data['message']
self.assert_500(response, 'Error during workout save.', status='fail')
def test_it_adds_workout_with_zero_value(
self,
@ -767,9 +743,7 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'fail' in data['status']
data = self.assert_400(response, error_message=None, status='fail')
assert len(data['data']['workouts']) == 0
def test_it_returns_500_if_one_file_in_zip_archive_is_invalid(
@ -796,10 +770,7 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert 'Error during gpx processing.' in data['message']
data = self.assert_500(response, 'Error during gpx processing.')
assert 'data' not in data
def test_it_imports_only_max_number_of_files(
@ -860,12 +831,11 @@ class TestPostWorkoutWithZipArchive(ApiTestCaseMixin):
Authorization=f'Bearer {auth_token}',
),
)
data = json.loads(response.data.decode())
assert response.status_code == 413
assert 'fail' in data['status']
assert (
'Error during workout upload, file size (2.5KB) exceeds 1.0KB.'
in data['message']
data = self.assert_413(
response,
'Error during workout upload, '
'file size (2.5KB) exceeds 1.0KB.',
)
assert 'data' not in data
@ -943,14 +913,8 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
f'/api/workouts/map/{map_id}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert data['status'] == 'error'
assert (
data['message']
== 'error, please try again or contact the administrator'
)
self.assert_500(response)
def test_it_gets_an_workout_created_with_gpx(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -1068,10 +1032,7 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
),
)
data = json.loads(response.data.decode())
assert response.status_code == 403
assert 'error' in data['status']
assert data['message'] == 'you do not have permissions'
self.assert_403(response)
def test_it_returns_500_on_invalid_segment_id(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -1098,11 +1059,7 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 500
assert 'error' in data['status']
assert data['message'] == 'Incorrect segment id'
assert 'data' not in data
self.assert_500(response, 'Incorrect segment id')
def test_it_returns_404_if_segment_id_does_not_exist(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -1129,10 +1086,9 @@ class TestPostAndGetWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
assert data['message'] == 'No segment with id \'999999\''
data = self.assert_404_with_message(
response, 'No segment with id \'999999\''
)
assert 'data' not in data

View File

@ -169,10 +169,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
),
)
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']
self.assert_403(response)
def test_it_updates_sport(
self,
@ -213,10 +210,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
self.assert_400(response)
def test_it_raises_500_if_sport_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport, gpx_file: str
@ -231,13 +225,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
self.assert_500(response)
class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
@ -409,10 +397,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
self.assert_403(response)
def test_it_updates_an_workout_wo_gpx_with_timezone(
self,
@ -574,10 +559,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 400
assert 'error' in data['status']
assert 'invalid payload' in data['message']
self.assert_400(response)
def test_it_returns_500_if_date_format_is_invalid(
self,
@ -603,14 +585,7 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
self.assert_500(response)
def test_it_returns_404_if_edited_workout_does_not_exists(
self, app: Flask, user_1: User, sport_1_cycling: Sport
@ -632,7 +607,5 @@ class TestEditWorkoutWithoutGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'not found' in data['status']
data = self.assert_404(response)
assert len(data['data']['workouts']) == 0

View File

@ -54,11 +54,7 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
),
)
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']
self.assert_403(response)
def test_it_returns_404_if_workout_does_not_exist(
self, app: Flask, user_1: User
@ -66,12 +62,13 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
client, auth_token = self.get_test_client_and_auth_token(
app, user_1.email
)
response = client.delete(
f'/api/workouts/{get_random_short_id()}',
headers=dict(Authorization=f'Bearer {auth_token}'),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
data = self.assert_404(response)
assert 'not found' in data['status']
def test_it_returns_500_when_deleting_an_workout_with_gpx_invalid_file(
@ -88,14 +85,7 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin):
headers=dict(Authorization=f'Bearer {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']
)
self.assert_500(response)
class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin):
@ -137,8 +127,4 @@ class TestDeleteWorkoutWithoutGpx(ApiTestCaseMixin):
),
)
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']
self.assert_403(response)