diff --git a/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTestsInvitations.py b/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTestsInvitations.py index d08a3a86..63fa178d 100644 --- a/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTestsInvitations.py +++ b/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTestsInvitations.py @@ -77,7 +77,7 @@ default=None) # Additional parameters -get_parser.add_argument('with_uuids', type=bool, help="Include UUIDs in results", default=False) +get_parser.add_argument('full', type=bool, help="Include full information for session, test_type, user, participant, device", default=False) get_parser.add_argument('with_urls', type=bool, help="Include URLs in results", default=False) post_parser = api.parser() @@ -154,9 +154,9 @@ def get(self): # No arguments means we return all accessible invitations - if all(args[arg] is None or arg in ['with_uuids', 'with_urls'] for arg in args): + if all(args[arg] is None or arg in ['full', 'with_urls'] for arg in args): for invitation in accessible_invitations: - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) else: # Go through all args and get the requested information @@ -164,12 +164,12 @@ def get(self): for invitation in TeraTestInvitation.query.filter( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by( id_test_invitation=args['id_test_invitation']).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['test_invitation_key'] is not None: for invitation in TeraTestInvitation.query.filter( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by( test_invitation_key=args['test_invitation_key']).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['user_uuid'] is not None: user : TeraUser = TeraUser.get_user_by_uuid(args['user_uuid']) if user: @@ -178,7 +178,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_user == args['id_user'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['participant_uuid'] is not None: participant : TeraParticipant = TeraParticipant.get_participant_by_uuid(args['participant_uuid']) if participant: @@ -187,7 +187,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_participant == args['id_participant'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['device_uuid'] is not None: device : TeraDevice = TeraDevice.get_device_by_uuid(args['device_uuid']) if device: @@ -196,7 +196,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_device == args['id_device'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['session_uuid'] is not None: session : TeraSession = TeraSession.get_session_by_uuid(args['session_uuid']) if session: @@ -205,7 +205,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_session == args['id_session'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['test_type_uuid'] is not None: test_type : TeraTestType = TeraTestType.get_test_type_by_uuid(args['test_type_uuid']) if test_type: @@ -214,7 +214,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_test_type == args['id_test_type'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['id_project'] is not None: project : TeraProject = TeraProject.get_project_by_id(args['id_project']) if project and project.id_project in service_access.get_accessible_projects_ids(): @@ -223,10 +223,7 @@ def get(self): TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraParticipant.id_project == project.id_project).all(): - invitations.append(invitation.to_json()) - - if args['with_uuids']: - invitations = self._insert_uuids_to_invitations(invitations) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['with_urls']: invitations = self._insert_urls_to_invitations(invitations) @@ -291,17 +288,17 @@ def post(self): try: if invitation['id_test_invitation'] == 0: # create new invitation - new_invitation = TeraTestInvitation() + new_invitation : TeraTestInvitation = TeraTestInvitation() new_invitation.from_json(invitation) TeraTestInvitation.insert(new_invitation) - response_data.append(new_invitation.to_json()) + response_data.append(new_invitation.to_json(minimal=False)) else: # Update existing invitation (only count) - existing_invitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation']) + existing_invitation : TeraTestInvitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation']) if existing_invitation: TeraTestInvitation.update(existing_invitation.id_test_invitation, {'test_invitation_count': invitation['test_invitation_count']}) - response_data.append(existing_invitation.to_json()) + response_data.append(existing_invitation.to_json(minimal=False)) except KeyError: return gettext('Required parameter is missing'), 400 @@ -317,7 +314,7 @@ def post(self): except SchemaError as e: return gettext('Invalid JSON schema') + str(e), 400 - return self._insert_uuids_to_invitations(response_data) + return response_data @api.doc(description='Delete a specific test invitation', responses={200: 'Success', @@ -351,42 +348,6 @@ def delete(self): return '', 200 - def _insert_uuids_to_invitations(self, invitations : list[dict]) -> list[dict]: - """ - Add UUIDs to invitations - """ - for invitation in invitations: - - # Prepare UUIDs with None - invitation['user_uuid'] = None - invitation['participant_uuid'] = None - invitation['device_uuid'] = None - invitation['session_uuid'] = None - invitation['test_type_uuid'] = None - - if 'id_user' in invitation and invitation['id_user'] is not None: - user : TeraUser = TeraUser.get_user_by_id(invitation['id_user']) - if user: - invitation['user_uuid'] = user.user_uuid - if 'id_participant' in invitation and invitation['id_participant'] is not None: - participant : TeraParticipant = TeraParticipant.get_participant_by_id(invitation['id_participant']) - if participant: - invitation['participant_uuid'] = participant.participant_uuid - if 'id_device' in invitation and invitation['id_device'] is not None: - device : TeraDevice = TeraDevice.get_device_by_id(invitation['id_device']) - if device: - invitation['device_uuid'] = device.device_uuid - if 'id_session' in invitation and invitation['id_session'] is not None: - session : TeraSession = TeraSession.get_session_by_id(invitation['id_session']) - if session: - invitation['session_uuid'] = session.session_uuid - if 'id_test_type' in invitation and invitation['id_test_type'] is not None: - test_type : TeraTestType = TeraTestType.get_test_type_by_id(invitation['id_test_type']) - if test_type: - invitation['test_type_uuid'] = test_type.test_type_uuid - - return invitations - def _insert_urls_to_invitations(self, invitations : list[dict]) -> list[dict]: """ Add URLs to invitations diff --git a/teraserver/python/modules/FlaskModule/API/user/UserQueryTestsInvitations.py b/teraserver/python/modules/FlaskModule/API/user/UserQueryTestsInvitations.py index bbb5ad55..b22f3c41 100644 --- a/teraserver/python/modules/FlaskModule/API/user/UserQueryTestsInvitations.py +++ b/teraserver/python/modules/FlaskModule/API/user/UserQueryTestsInvitations.py @@ -77,7 +77,8 @@ default=None) # Additional parameters -get_parser.add_argument('with_uuids', type=bool, help="Include UUIDs in results", default=False) +get_parser.add_argument('full', type=bool, help="Include more information in invitations with user, participant, device, test_type and session.", + default=False) get_parser.add_argument('with_urls', type=bool, help="Include URLs in results", default=False) post_parser = api.parser() @@ -157,21 +158,21 @@ def get(self): invitations : list[dict] = [] # No arguments means we return all accessible invitations - if all(args[arg] is None or arg in ['with_uuids', 'with_urls'] for arg in args): + if all(args[arg] is None or arg in ['full', 'with_urls'] for arg in args): for invitation in accessible_invitations: - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) else: # Go through all args and get the requested information if args['id_test_invitation'] is not None: for invitation in TeraTestInvitation.query.filter( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by( id_test_invitation=args['id_test_invitation']).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['test_invitation_key'] is not None: for invitation in TeraTestInvitation.query.filter( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids)).filter_by( test_invitation_key=args['test_invitation_key']).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['user_uuid'] is not None: user : TeraUser = TeraUser.get_user_by_uuid(args['user_uuid']) if user: @@ -180,7 +181,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_user == args['id_user'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['participant_uuid'] is not None: participant : TeraParticipant = TeraParticipant.get_participant_by_uuid(args['participant_uuid']) if participant: @@ -189,7 +190,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_participant == args['id_participant'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['device_uuid'] is not None: device : TeraDevice = TeraDevice.get_device_by_uuid(args['device_uuid']) if device: @@ -198,7 +199,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_device == args['id_device'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['session_uuid'] is not None: session : TeraSession = TeraSession.get_session_by_uuid(args['session_uuid']) if session: @@ -207,7 +208,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_session == args['id_session'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['test_type_uuid'] is not None: test_type : TeraTestType = TeraTestType.get_test_type_by_uuid(args['test_type_uuid']) if test_type: @@ -216,7 +217,7 @@ def get(self): for invitation in TeraTestInvitation.query.filter(and_( TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraTestInvitation.id_test_type == args['id_test_type'])).all(): - invitations.append(invitation.to_json()) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['id_project'] is not None: project : TeraProject = TeraProject.get_project_by_id(args['id_project']) if project and project.id_project in user_access.get_accessible_projects_ids(): @@ -225,10 +226,7 @@ def get(self): TeraTestInvitation.id_test_invitation.in_(accessible_invitations_ids), TeraParticipant.id_project == project.id_project).all(): - invitations.append(invitation.to_json()) - - if args['with_uuids']: - invitations = self._insert_uuids_to_invitations(invitations) + invitations.append(invitation.to_json(minimal=not args['full'])) if args['with_urls']: invitations = self._insert_urls_to_invitations(invitations) @@ -296,14 +294,14 @@ def post(self): new_invitation = TeraTestInvitation() new_invitation.from_json(invitation) TeraTestInvitation.insert(new_invitation) - response_data.append(new_invitation.to_json()) + response_data.append(new_invitation.to_json(minimal=False)) else: # Update existing invitation (only count) - existing_invitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation']) + existing_invitation : TeraTestInvitation = TeraTestInvitation.get_test_invitation_by_id(test_invitation_id=invitation['id_test_invitation']) if existing_invitation: TeraTestInvitation.update(existing_invitation.id_test_invitation, {'test_invitation_count': invitation['test_invitation_count']}) - response_data.append(existing_invitation.to_json()) + response_data.append(existing_invitation.to_json(minimal=False)) except KeyError: return gettext('Required parameter is missing'), 400 @@ -319,7 +317,7 @@ def post(self): except SchemaError as e: return gettext('Invalid JSON schema') + str(e), 400 - return self._insert_uuids_to_invitations(response_data) + return response_data, 200 @api.doc(description='Delete a specific test invitation', responses={200: 'Success', @@ -352,42 +350,6 @@ def delete(self): return '', 200 - def _insert_uuids_to_invitations(self, invitations : list[dict]) -> list[dict]: - """ - Add UUIDs to invitations - """ - for invitation in invitations: - - # Prepare UUIDs with None - invitation['user_uuid'] = None - invitation['participant_uuid'] = None - invitation['device_uuid'] = None - invitation['session_uuid'] = None - invitation['test_type_uuid'] = None - - if 'id_user' in invitation and invitation['id_user'] is not None: - user : TeraUser = TeraUser.get_user_by_id(invitation['id_user']) - if user: - invitation['user_uuid'] = user.user_uuid - if 'id_participant' in invitation and invitation['id_participant'] is not None: - participant : TeraParticipant = TeraParticipant.get_participant_by_id(invitation['id_participant']) - if participant: - invitation['participant_uuid'] = participant.participant_uuid - if 'id_device' in invitation and invitation['id_device'] is not None: - device : TeraDevice = TeraDevice.get_device_by_id(invitation['id_device']) - if device: - invitation['device_uuid'] = device.device_uuid - if 'id_session' in invitation and invitation['id_session'] is not None: - session : TeraSession = TeraSession.get_session_by_id(invitation['id_session']) - if session: - invitation['session_uuid'] = session.session_uuid - if 'id_test_type' in invitation and invitation['id_test_type'] is not None: - test_type : TeraTestType = TeraTestType.get_test_type_by_id(invitation['id_test_type']) - if test_type: - invitation['test_type_uuid'] = test_type.test_type_uuid - - return invitations - def _insert_urls_to_invitations(self, invitations : list[dict]) -> list[dict]: """ Add URLs to invitations diff --git a/teraserver/python/opentera/services/ServiceAccessManager.py b/teraserver/python/opentera/services/ServiceAccessManager.py index 7805a457..6ba0c623 100644 --- a/teraserver/python/opentera/services/ServiceAccessManager.py +++ b/teraserver/python/opentera/services/ServiceAccessManager.py @@ -511,7 +511,7 @@ def decorated(*args, **kwargs): # Use the service token to get the test invitation response = ServiceAccessManager.service.get_from_opentera('/api/service/tests/invitations', - params={'test_invitation_key': test_invitation_key, 'with_uuids': True}) + params={'test_invitation_key': test_invitation_key, 'full': True}) # Check if the test invitation is valid if response.status_code != 200 or len(response.json()) != 1: diff --git a/teraserver/python/tests/modules/FlaskModule/API/service/test_ServiceQueryTestsInvitations.py b/teraserver/python/tests/modules/FlaskModule/API/service/test_ServiceQueryTestsInvitations.py index b016c87f..78cb24d0 100644 --- a/teraserver/python/tests/modules/FlaskModule/API/service/test_ServiceQueryTestsInvitations.py +++ b/teraserver/python/tests/modules/FlaskModule/API/service/test_ServiceQueryTestsInvitations.py @@ -106,7 +106,7 @@ def test_get_query_no_params_with_token_returns_all_accessible_invitations(self) TeraTestType.delete(test_type_accessible.id_test_type) TeraTestType.delete(test_type_not_accessible.id_test_type) - def test_get_query_no_params_with_token_returns_all_accessible_invitations_with_uuids(self): + def test_get_query_no_params_with_token_returns_all_accessible_invitations_with_full(self): """ Test that an admin can access all invitations """ @@ -122,12 +122,12 @@ def test_get_query_no_params_with_token_returns_all_accessible_invitations_with_ self._create_invitations(create_count, id_test_type=test_type_not_accessible.id_test_type, id_user=1) # Service should access only invitations of its test type - response = self._get_with_service_token_auth(self.test_client, token=self.service_token, params={'with_uuids': True}) + response = self._get_with_service_token_auth(self.test_client, token=self.service_token, params={'full': True}) self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for json_invitation in response.json: - self._validate_json(json_invitation, with_uuids=True) + self._validate_json(json_invitation, full=True) TeraTestType.delete(test_type_accessible.id_test_type) TeraTestType.delete(test_type_not_accessible.id_test_type) @@ -550,7 +550,7 @@ def test_post_query_with_service_token_with_valid_schema_with_no_session(self): id_user=1)) self.assertEqual(200, response.status_code) for json_invitation in response.json: - self._validate_json(json_invitation, with_uuids=True) + self._validate_json(json_invitation, full=True) def test_post_query_with_service_token_with_valid_schema_with_invalid_session(self): @@ -581,7 +581,7 @@ def test_post_query_with_service_token_with_valid_schema_with_valid_session(self id_session=id_session)) self.assertEqual(200, response.status_code) for json_invitation in response.json: - self._validate_json(json_invitation, with_uuids=True) + self._validate_json(json_invitation, full=True) def test_post_query_with_service_token_with_valid_schema_update_count(self): @@ -610,7 +610,7 @@ def test_post_query_with_service_token_with_valid_schema_update_count(self): self.assertEqual(1, len(response.json)) self.assertEqual(invitation_info['test_invitation_count'], response.json[0]['test_invitation_count']) for json_invitation in response.json: - self._validate_json(json_invitation, with_uuids=True) + self._validate_json(json_invitation, full=True) @@ -693,7 +693,7 @@ def _delete_all_invitations(self): for invitation in invitations: TeraTestInvitation.delete(invitation.id_test_invitation) - def _validate_json(self, json: dict, with_uuids: bool = False, with_urls: bool = False): + def _validate_json(self, json: dict, full: bool = False, with_urls: bool = False): """ Validate a json """ @@ -710,18 +710,22 @@ def _validate_json(self, json: dict, with_uuids: bool = False, with_urls: bool = self.assertTrue('id_session' in json) self.assertTrue('test_invitation_key' in json) - if with_uuids: - self.assertTrue('user_uuid' in json) - self.assertTrue('participant_uuid' in json) - self.assertTrue('device_uuid' in json) - self.assertTrue('session_uuid' in json) - self.assertTrue('test_type_uuid' in json) + if full: + self.assertTrue('test_invitation_test_type' in json) + if json['id_user']: + self.assertTrue('test_invitation_user' in json) + if json['id_participant']: + self.assertTrue('test_invitation_participant' in json) + if json['id_device']: + self.assertTrue('test_invitation_device' in json) + if json['id_session']: + self.assertTrue('test_invitation_session' in json) else: - self.assertTrue('user_uuid' not in json) - self.assertTrue('participant_uuid' not in json) - self.assertTrue('device_uuid' not in json) - self.assertTrue('session_uuid' not in json) - self.assertTrue('test_type_uuid' not in json) + self.assertTrue('test_invitation_test_type' not in json) + self.assertTrue('test_invitation_session' not in json) + self.assertTrue('test_invitation_user' not in json) + self.assertTrue('test_invitation_participant' not in json) + self.assertTrue('test_invitation_device' not in json) if with_urls: self.assertTrue('test_invitation_url' in json) diff --git a/teraserver/python/tests/modules/FlaskModule/API/user/test_UserQueryTestsInvitations.py b/teraserver/python/tests/modules/FlaskModule/API/user/test_UserQueryTestsInvitations.py index 031ecb64..5f21f28c 100644 --- a/teraserver/python/tests/modules/FlaskModule/API/user/test_UserQueryTestsInvitations.py +++ b/teraserver/python/tests/modules/FlaskModule/API/user/test_UserQueryTestsInvitations.py @@ -114,14 +114,14 @@ def test_get_query_no_params_as_admin_returns_all_accessible_invitations(self): self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Verify that invitations are not accessible to no access user response = self._get_with_user_http_auth(self.test_client, username='user4', password='user4') self.assertEqual(200, response.status_code) self.assertEqual(0, len(response.json)) - def test_get_query_no_params_as_admin_with_uuids_returns_all_accessible_invitations(self): + def test_get_query_no_params_as_admin_with_full_returns_all_accessible_invitations(self): """ Test that an admin can access all invitations """ @@ -131,11 +131,11 @@ def test_get_query_no_params_as_admin_with_uuids_returns_all_accessible_invitati self._create_invitations(create_count, id_test_type=1, id_user=1) # Admin should access all invitations - response = self._get_with_user_http_auth(self.test_client, username='admin', password='admin', params={'with_uuids': True}) + response = self._get_with_user_http_auth(self.test_client, username='admin', password='admin', params={'full': True}) self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=True) + self._validate_json(invitation, full=True) # Verify that invitations are not accessible to no access user response = self._get_with_user_http_auth(self.test_client, username='user4', password='user4') @@ -179,7 +179,7 @@ def test_get_query_with_id_test_invitation_or_key_as_admin(self): self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.json)) for invitation_response in response.json: - self._validate_json(invitation_response, with_uuids=False) + self._validate_json(invitation_response) # Also test with test_invitation_key response = self._get_with_user_http_auth(self.test_client, username='admin', password='admin', @@ -187,7 +187,7 @@ def test_get_query_with_id_test_invitation_or_key_as_admin(self): self.assertEqual(200, response.status_code) self.assertEqual(1, len(response.json)) for invitation_response in response.json: - self._validate_json(invitation_response, with_uuids=False) + self._validate_json(invitation_response) # Verify that invitations are not accessible to no access user @@ -224,7 +224,7 @@ def test_get_query_with_id_user_as_admin_returns_only_invitation_for_specific_id self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Also test with user_uuid user = TeraUser.get_user_by_id(1) @@ -233,7 +233,7 @@ def test_get_query_with_id_user_as_admin_returns_only_invitation_for_specific_id self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Verify that invitations are not accessible to no access user response = self._get_with_user_http_auth(self.test_client, username='user4', password='user4', @@ -263,7 +263,7 @@ def test_get_query_with_id_participant_as_admin_returns_only_invitation_for_spec self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Also test with participant_uuid participant = TeraParticipant.get_participant_by_id(1) @@ -272,7 +272,7 @@ def test_get_query_with_id_participant_as_admin_returns_only_invitation_for_spec self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Verify that invitations are not accessible to no access user response = self._get_with_user_http_auth(self.test_client, username='user4', password='user4', @@ -302,7 +302,7 @@ def test_get_query_with_id_device_as_admin_returns_only_invitation_for_specific_ self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Also test with device_uuid device = TeraDevice.get_device_by_id(1) @@ -311,7 +311,7 @@ def test_get_query_with_id_device_as_admin_returns_only_invitation_for_specific_ self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Verify that invitations are not accessible to no access user response = self._get_with_user_http_auth(self.test_client, username='user4', password='user4', @@ -341,7 +341,7 @@ def test_get_query_with_id_test_type_as_admin_returns_only_invitation_for_specif self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Also test with test_type_uuid test_type = TeraTestType.get_test_type_by_id(1) @@ -350,8 +350,7 @@ def test_get_query_with_id_test_type_as_admin_returns_only_invitation_for_specif self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) - + self._validate_json(invitation) # Verify that invitations are not accessible to no access user response = self._get_with_user_http_auth(self.test_client, username='user4', password='user4', params={'id_test_type': 1}) @@ -384,7 +383,7 @@ def test_get_query_with_id_session_as_admin_returns_only_invitation_for_specific self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Test with session_uuid session = TeraSession.get_session_by_id(1) @@ -393,7 +392,7 @@ def test_get_query_with_id_session_as_admin_returns_only_invitation_for_specific self.assertEqual(200, response.status_code) self.assertEqual(create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Verify that invitations are not accessible to no access user response = self._get_with_user_http_auth(self.test_client, username='user4', password='user4', @@ -435,7 +434,7 @@ def test_get_query_as_admin_with_valid_id_project_returns_invitations(self): self.assertEqual(200, response.status_code) self.assertEqual(2 * create_count, len(response.json)) for invitation in response.json: - self._validate_json(invitation, with_uuids=False) + self._validate_json(invitation) # Not access user should not see any invitation @@ -576,7 +575,7 @@ def test_post_query_as_admin_with_valid_schema_with_no_session(self): id_user=1)) self.assertEqual(200, response.status_code) for invitation in response.json: - self._validate_json(invitation, with_uuids=True) + self._validate_json(invitation, full=True) def test_post_query_as_admin_with_valid_schema_with_invalid_session(self): @@ -605,7 +604,7 @@ def test_post_query_as_admin_with_valid_schema_with_valid_session(self): id_session=1)) self.assertEqual(200, response.status_code) for invitation in response.json: - self._validate_json(invitation, with_uuids=True) + self._validate_json(invitation, full=True) def test_post_query_as_admin_with_valid_schema_update_count(self): """ @@ -635,7 +634,7 @@ def test_post_query_as_admin_with_valid_schema_update_count(self): self.assertEqual(1, len(response.json)) self.assertEqual(invitation_info['test_invitation_count'], response.json[0]['test_invitation_count']) for invitation in response.json: - self._validate_json(invitation, with_uuids=True) + self._validate_json(invitation, full=True) def _create_tests_invitations_json(self, id_test_type: int, id_user: int = None, @@ -702,7 +701,7 @@ def _delete_all_invitations(self): for invitation in invitations: TeraTestInvitation.delete(invitation.id_test_invitation) - def _validate_json(self, json: dict, with_uuids: bool = False, with_urls: bool = False): + def _validate_json(self, json: dict, full: bool = False, with_urls: bool = False): """ Validate a json """ @@ -719,18 +718,22 @@ def _validate_json(self, json: dict, with_uuids: bool = False, with_urls: bool = self.assertTrue('id_session' in json) self.assertTrue('test_invitation_key' in json) - if with_uuids: - self.assertTrue('user_uuid' in json) - self.assertTrue('participant_uuid' in json) - self.assertTrue('device_uuid' in json) - self.assertTrue('session_uuid' in json) - self.assertTrue('test_type_uuid' in json) + if full: + self.assertTrue('test_invitation_test_type' in json) + if json['id_user']: + self.assertTrue('test_invitation_user' in json) + if json['id_participant']: + self.assertTrue('test_invitation_participant' in json) + if json['id_device']: + self.assertTrue('test_invitation_device' in json) + if json['id_session']: + self.assertTrue('test_invitation_session' in json) else: - self.assertTrue('user_uuid' not in json) - self.assertTrue('participant_uuid' not in json) - self.assertTrue('device_uuid' not in json) - self.assertTrue('session_uuid' not in json) - self.assertTrue('test_type_uuid' not in json) + self.assertTrue('test_invitation_test_type' not in json) + self.assertTrue('test_invitation_session' not in json) + self.assertTrue('test_invitation_user' not in json) + self.assertTrue('test_invitation_participant' not in json) + self.assertTrue('test_invitation_device' not in json) if with_urls: self.assertTrue('test_invitation_url' in json) diff --git a/teraserver/python/tests/opentera/services/test_ServiceAccessManager.py b/teraserver/python/tests/opentera/services/test_ServiceAccessManager.py index 530fe6d3..070e455f 100644 --- a/teraserver/python/tests/opentera/services/test_ServiceAccessManager.py +++ b/teraserver/python/tests/opentera/services/test_ServiceAccessManager.py @@ -234,7 +234,7 @@ def test_endpoint_invitation_key_with_no_key(self): response = self.__service.test_client.get('/api/test_invitations') self.assertEqual(400, response.status_code) - def test_endpoint_invitation_key_with_key_for_user(self): + def test_endpoint_invitation_key_with_key_for_user_participant_device(self): with self.__service.app_context(): # Create test type for this service test_type : TeraTestType = TeraTestType()