def test_find_by_subject(session, client, jwt): """Assert that user find by subject is working as expected.""" user = User.find_by_sub(TEST_TOKEN['sub']) if not user: User.create_from_jwt_token(TEST_TOKEN, 'PS12345') user = User.find_by_sub(TEST_TOKEN['sub']) assert user assert user.id assert user.username == 'username_TEST1' assert user.iss == 'issuer_TEST1' assert user.sub == 'subject_TEST1' assert user.firstname == 'given_name_TEST1' assert user.lastname == 'family_name_TEST1'
def test_update_user_profile(session, client, jwt): """Assert that updating a user profile is working as expected.""" user = User.find_by_jwt_token(TEST_TOKEN) if not user: user = User.create_from_jwt_token(TEST_TOKEN, 'PS12345') user.user_profile = UserProfile.create_from_json(ALL_JSON, user.id) user_profile = user.user_profile user_profile.update_profile(COMBO_JSON) save_json = user_profile.json assert save_json['paymentConfirmationDialog'] == COMBO_JSON[ 'paymentConfirmationDialog'] assert save_json['selectConfirmationDialog'] == COMBO_JSON[ 'selectConfirmationDialog']
def test_create_user_profile(session, client, jwt): """Assert that creating a user profile is working as expected.""" user = User.find_by_jwt_token(TEST_TOKEN) if not user: user = User.create_from_jwt_token(TEST_TOKEN, 'PS12345') user_profile = UserProfile.create_from_json(ALL_JSON, user.id) user_profile.save() save_json = user_profile.json assert save_json['paymentConfirmationDialog'] == ALL_JSON[ 'paymentConfirmationDialog'] assert save_json['selectConfirmationDialog'] == ALL_JSON[ 'selectConfirmationDialog'] assert save_json['defaultDropDowns'] == ALL_JSON['defaultDropDowns'] assert save_json['defaultTableFilters'] == ALL_JSON['defaultTableFilters']
def test_get_or_create(session, client, jwt): """Assert that get or create user is working as expected.""" user = User.get_or_create_user_by_jwt(TEST_TOKEN, 'PS12345') assert user assert user.id assert user.username == 'username_TEST1' assert user.iss == 'issuer_TEST1' assert user.sub == 'subject_TEST1' assert user.firstname == 'given_name_TEST1' assert user.lastname == 'family_name_TEST1'
def get(): """Get existing user profile UI settings for the user represented by the request JWT.""" try: # Quick check: always require an account ID. account_id = resource_utils.get_account_id(request) if not is_staff(jwt) and account_id is None: return resource_utils.account_required_response() # Verify request JWT and account ID if not authorized(account_id, jwt): return resource_utils.unauthorized_error_response(account_id) token = g.jwt_oidc_token_info current_app.logger.debug( f'Getting user profile for account {account_id} with token: {token}' ) # Try to fetch existing user from JWT. user = User.find_by_jwt_token(token, account_id) current_app.logger.debug( f'User profile query completed for account {account_id}.') if not user: # If user does not exist, create user and user profile with the default settings. current_app.logger.debug( f'No user found for {account_id} request token: creating records.' ) user = User.create_from_jwt_token(token, account_id) user.user_profile = UserProfile.create_from_json(None, user.id) user.user_profile.save() return user.user_profile.json, HTTPStatus.OK except BusinessException as exception: return resource_utils.business_exception_response(exception) except Exception as default_exception: # noqa: B902; return nicer default error current_app.logger.error( f'Get user profile {account_id} failed: ' + repr(default_exception)) return resource_utils.default_exception_response(default_exception)
def patch(): """Update user profile UI settings for the user represented by the request JWT.""" try: # Quick check: always require an account ID. account_id = resource_utils.get_account_id(request) if not is_staff(jwt) and account_id is None: return resource_utils.account_required_response() # Verify request JWT and account ID if not authorized(account_id, jwt): return resource_utils.unauthorized_error_response(account_id) request_json = request.get_json(silent=True) current_app.logger.debug( f'Updating user profile for {account_id} with values: {request_json}' ) # Validate against the schema. if not bypass_validation(request_json): valid_format, errors = schema_utils.validate( request_json, 'userProfile', 'common') if not valid_format: return resource_utils.validation_error_response( errors, VAL_ERROR) token = g.jwt_oidc_token_info current_app.logger.debug( f'Updating user profile for {account_id} with token: {token}') # Try to fetch existing user from JWT. user = User.find_by_jwt_token(token) if not user: # If user does not exist, create user and user profile with the default settings. current_app.logger.error( f'Update user profile no user found for {account_id} request token.' ) return resource_utils.not_found_error_response( 'user profile', account_id) user_profile = user.user_profile user_profile.update_profile(request_json) return user_profile.json, HTTPStatus.OK except BusinessException as exception: return resource_utils.business_exception_response(exception) except Exception as default_exception: # noqa: B902; return nicer default error current_app.logger.error( f'Get user profile {account_id} failed: ' + repr(default_exception)) return resource_utils.default_exception_response(default_exception)
def test_jwt_properties(session, client, jwt, token): """Assert that user jwt properties are as expected.""" assert jwt firstname = token.get('given_name', None) if not firstname: firstname = token.get('firstname', None) lastname = token.get('family_name', None) if not lastname: lastname = token.get('lastname', None) user = User(username=token.get('username', None), firstname=firstname, lastname=lastname, iss=token['iss'], sub=token['sub']) assert user.username == 'username' assert user.iss == 'issuer' assert user.sub == 'subject' assert user.firstname == 'given_name' assert user.lastname == 'family_name'