From 1f098fb2ce4d947d4ccb1a5877113286d9d24ab9 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 24 Apr 2022 10:08:15 +0200 Subject: [PATCH] API - update user email when email sending is disabled (admin) --- fittrackee/tests/users/test_users_api.py | 27 +++++++++++++++++++++--- fittrackee/users/users.py | 1 + 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/fittrackee/tests/users/test_users_api.py b/fittrackee/tests/users/test_users_api.py index bb387a1e..55c5703e 100644 --- a/fittrackee/tests/users/test_users_api.py +++ b/fittrackee/tests/users/test_users_api.py @@ -1214,27 +1214,48 @@ class TestUpdateUser(ApiTestCaseMixin): user_email_updated_to_new_address_mock.send.assert_not_called() - def test_it_updates_user_email( + def test_it_updates_user_email_to_confirm_when_email_sending_is_enabled( self, app: Flask, user_1_admin: User, user_2: User ) -> None: client, auth_token = self.get_test_client_and_auth_token( app, user_1_admin.email ) + new_email = 'new.' + user_2.email user_2_email = user_2.email user_2_confirmation_token = user_2.confirmation_token response = client.patch( f'/api/users/{user_2.username}', content_type='application/json', - data=json.dumps(dict(new_email='new.' + user_2.email)), + data=json.dumps(dict(new_email=new_email)), headers=dict(Authorization=f'Bearer {auth_token}'), ) assert response.status_code == 200 assert user_2.email == user_2_email - assert user_2.email_to_confirm == 'new.' + user_2.email + assert user_2.email_to_confirm == new_email assert user_2.confirmation_token != user_2_confirmation_token + def test_it_updates_user_email_when_email_sending_is_disabled( + self, app_wo_email_activation: Flask, user_1_admin: User, user_2: User + ) -> None: + client, auth_token = self.get_test_client_and_auth_token( + app_wo_email_activation, user_1_admin.email + ) + new_email = 'new.' + user_2.email + + response = client.patch( + f'/api/users/{user_2.username}', + content_type='application/json', + data=json.dumps(dict(new_email=new_email)), + headers=dict(Authorization=f'Bearer {auth_token}'), + ) + + assert response.status_code == 200 + assert user_2.email == new_email + assert user_2.email_to_confirm is None + assert user_2.confirmation_token is None + def test_it_calls_email_updated_to_new_address_when_password_reset_is_successful( # noqa self, app: Flask, diff --git a/fittrackee/users/users.py b/fittrackee/users/users.py index 10559716..0ce6a051 100644 --- a/fittrackee/users/users.py +++ b/fittrackee/users/users.py @@ -526,6 +526,7 @@ def update_user(auth_user: User, user_name: str) -> Union[Dict, HttpResponse]: activate=user_data.get('activate', False), reset_password=reset_password, new_email=new_email, + with_confirmation=current_app.config['CAN_SEND_EMAILS'], ) if current_app.config['CAN_SEND_EMAILS']: