From 230ce9022e5a48f9c284713aff25627611226f6f Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 21 May 2023 16:13:14 +0200 Subject: [PATCH] API - OAuth2 scopes tests refacto --- .../tests/application/test_app_config_api.py | 12 +--- fittrackee/tests/users/test_auth_api.py | 72 +++---------------- fittrackee/tests/users/test_users_api.py | 42 ++--------- fittrackee/tests/utils.py | 10 +++ fittrackee/tests/workouts/test_records_api.py | 11 +-- fittrackee/tests/workouts/test_sports_api.py | 32 ++------- fittrackee/tests/workouts/test_stats_api.py | 31 ++------ .../tests/workouts/test_workouts_api_0_get.py | 32 ++------- .../workouts/test_workouts_api_1_post.py | 21 +----- .../workouts/test_workouts_api_2_patch.py | 11 +-- .../workouts/test_workouts_api_3_delete.py | 11 +-- 11 files changed, 46 insertions(+), 239 deletions(-) diff --git a/fittrackee/tests/application/test_app_config_api.py b/fittrackee/tests/application/test_app_config_api.py index fb01b748..927b9ccd 100644 --- a/fittrackee/tests/application/test_app_config_api.py +++ b/fittrackee/tests/application/test_app_config_api.py @@ -11,7 +11,7 @@ from fittrackee.application.models import AppConfig from fittrackee.users.models import User from ..mixins import ApiTestCaseMixin -from ..utils import jsonify_dict +from ..utils import OAUTH_SCOPES, jsonify_dict class TestGetConfig(ApiTestCaseMixin): @@ -432,15 +432,7 @@ class TestUpdateConfig(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', True), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'application:write': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/users/test_auth_api.py b/fittrackee/tests/users/test_auth_api.py index 4b6cf5e7..362176d9 100644 --- a/fittrackee/tests/users/test_auth_api.py +++ b/fittrackee/tests/users/test_auth_api.py @@ -19,7 +19,7 @@ from fittrackee.users.utils.token import get_user_token from fittrackee.workouts.models import Sport from ..mixins import ApiTestCaseMixin -from ..utils import jsonify_dict +from ..utils import OAUTH_SCOPES, jsonify_dict USER_AGENT = ( 'Mozilla/5.0 (X11; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0' @@ -622,15 +622,7 @@ class TestUserProfile(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', True), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'profile:read': True}.items(), ) def test_expected_scopes_are_defined( self, app: Flask, user_1: User, client_scope: str, can_access: bool @@ -719,15 +711,7 @@ class TestUserProfileUpdate(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', True), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'profile:write': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1389,15 +1373,7 @@ class TestUserAccountUpdate(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', True), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'profile:write': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1503,15 +1479,7 @@ class TestUserPreferencesUpdate(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', True), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'profile:write': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1721,15 +1689,7 @@ class TestUserSportPreferencesUpdate(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', True), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'profile:write': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1812,15 +1772,7 @@ class TestUserSportPreferencesReset(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', True), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'profile:write': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1979,15 +1931,7 @@ class TestUserPicture(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', True), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'profile:write': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/users/test_users_api.py b/fittrackee/tests/users/test_users_api.py index b28d4d3a..ea0e4acf 100644 --- a/fittrackee/tests/users/test_users_api.py +++ b/fittrackee/tests/users/test_users_api.py @@ -12,7 +12,7 @@ from fittrackee.utils import get_readable_duration from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin -from ..utils import jsonify_dict +from ..utils import OAUTH_SCOPES, jsonify_dict class TestGetUser(ApiTestCaseMixin): @@ -135,15 +135,7 @@ class TestGetUser(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', True), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'users:read': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -925,15 +917,7 @@ class TestGetUsers(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', True), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'users:read': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1436,15 +1420,7 @@ class TestUpdateUser(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', True), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'users:write': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1713,15 +1689,7 @@ class TestDeleteUser(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', True), - ('workouts:read', False), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'users:write': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/utils.py b/fittrackee/tests/utils.py index eb4df16c..d3caa1cd 100644 --- a/fittrackee/tests/utils.py +++ b/fittrackee/tests/utils.py @@ -53,3 +53,13 @@ TEST_OAUTH_CLIENT_METADATA = { 'redirect_uris': [random_domain()], 'scope': 'profile:read workouts:read', } + +OAUTH_SCOPES = { + "application:write": False, + "profile:read": False, + "profile:write": False, + "users:read": False, + "users:write": False, + "workouts:read": False, + "workouts:write": False, +} diff --git a/fittrackee/tests/workouts/test_records_api.py b/fittrackee/tests/workouts/test_records_api.py index 352ffbf1..57bbe240 100644 --- a/fittrackee/tests/workouts/test_records_api.py +++ b/fittrackee/tests/workouts/test_records_api.py @@ -7,6 +7,7 @@ from fittrackee.users.models import User from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin +from ..utils import OAUTH_SCOPES class TestGetRecords(ApiTestCaseMixin): @@ -901,15 +902,7 @@ class TestGetRecords(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/workouts/test_sports_api.py b/fittrackee/tests/workouts/test_sports_api.py index 3d167c3b..ba03339d 100644 --- a/fittrackee/tests/workouts/test_sports_api.py +++ b/fittrackee/tests/workouts/test_sports_api.py @@ -8,7 +8,7 @@ from fittrackee.users.models import User, UserSportPreference from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin -from ..utils import jsonify_dict +from ..utils import OAUTH_SCOPES, jsonify_dict class TestGetSports(ApiTestCaseMixin): @@ -141,15 +141,7 @@ class TestGetSports(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -281,15 +273,7 @@ class TestGetSport(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -520,15 +504,7 @@ class TestUpdateSport(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', True), - ], + {**OAUTH_SCOPES, 'workouts:write': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/workouts/test_stats_api.py b/fittrackee/tests/workouts/test_stats_api.py index f0258ef0..ebfc7d96 100644 --- a/fittrackee/tests/workouts/test_stats_api.py +++ b/fittrackee/tests/workouts/test_stats_api.py @@ -7,6 +7,7 @@ from fittrackee.users.models import User from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin +from ..utils import OAUTH_SCOPES class TestGetStatsByTime(ApiTestCaseMixin): @@ -864,15 +865,7 @@ class TestGetStatsByTime(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1045,15 +1038,7 @@ class TestGetStatsBySport(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1164,15 +1149,7 @@ class TestGetAllStats(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/workouts/test_workouts_api_0_get.py b/fittrackee/tests/workouts/test_workouts_api_0_get.py index ef802c2a..5ff8b3e3 100644 --- a/fittrackee/tests/workouts/test_workouts_api_0_get.py +++ b/fittrackee/tests/workouts/test_workouts_api_0_get.py @@ -12,7 +12,7 @@ from fittrackee.users.models import User from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin -from ..utils import jsonify_dict +from ..utils import OAUTH_SCOPES, jsonify_dict from .utils import get_random_short_id @@ -106,15 +106,7 @@ class TestGetWorkouts(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1316,15 +1308,7 @@ class TestGetWorkout(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) @pytest.mark.parametrize( 'endpoint', @@ -1450,15 +1434,7 @@ class TestDownloadWorkoutGpx(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', True), - ('workouts:write', False), - ], + {**OAUTH_SCOPES, 'workouts:read': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/workouts/test_workouts_api_1_post.py b/fittrackee/tests/workouts/test_workouts_api_1_post.py index 6587070b..5c8658ca 100644 --- a/fittrackee/tests/workouts/test_workouts_api_1_post.py +++ b/fittrackee/tests/workouts/test_workouts_api_1_post.py @@ -13,6 +13,7 @@ from fittrackee.users.models import User from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin, CallArgsMixin +from ..utils import OAUTH_SCOPES def assert_workout_data_with_gpx(data: Dict) -> None: @@ -939,15 +940,7 @@ class TestPostWorkoutWithGpx(ApiTestCaseMixin, CallArgsMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', True), - ], + {**OAUTH_SCOPES, 'workouts:write': True}.items(), ) def test_expected_scopes_are_defined( self, @@ -1280,15 +1273,7 @@ class TestPostWorkoutWithoutGpx(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', True), - ], + {**OAUTH_SCOPES, 'workouts:write': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/workouts/test_workouts_api_2_patch.py b/fittrackee/tests/workouts/test_workouts_api_2_patch.py index 25a27bda..99cbb7bd 100644 --- a/fittrackee/tests/workouts/test_workouts_api_2_patch.py +++ b/fittrackee/tests/workouts/test_workouts_api_2_patch.py @@ -9,6 +9,7 @@ from fittrackee.users.models import User from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin +from ..utils import OAUTH_SCOPES from .utils import get_random_short_id, post_a_workout @@ -227,15 +228,7 @@ class TestEditWorkoutWithGpx(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', True), - ], + {**OAUTH_SCOPES, 'workouts:write': True}.items(), ) def test_expected_scopes_are_defined( self, diff --git a/fittrackee/tests/workouts/test_workouts_api_3_delete.py b/fittrackee/tests/workouts/test_workouts_api_3_delete.py index 6b059683..9b9165cc 100644 --- a/fittrackee/tests/workouts/test_workouts_api_3_delete.py +++ b/fittrackee/tests/workouts/test_workouts_api_3_delete.py @@ -5,6 +5,7 @@ from fittrackee.users.models import User from fittrackee.workouts.models import Sport, Workout from ..mixins import ApiTestCaseMixin +from ..utils import OAUTH_SCOPES from .utils import get_random_short_id, post_a_workout @@ -104,15 +105,7 @@ class TestDeleteWorkoutWithGpx(ApiTestCaseMixin): @pytest.mark.parametrize( 'client_scope, can_access', - [ - ('application:write', False), - ('profile:read', False), - ('profile:write', False), - ('users:read', False), - ('users:write', False), - ('workouts:read', False), - ('workouts:write', True), - ], + {**OAUTH_SCOPES, 'workouts:write': True}.items(), ) def test_expected_scopes_are_defined( self,