From e04a994d37f76dce4ac24f679cce084895b53693 Mon Sep 17 00:00:00 2001 From: Marie PUPO JEAMMET Date: Mon, 26 Aug 2024 19:10:43 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=BD=EF=B8=8F(mailboxes)=20fix=20mailbo?= =?UTF-8?q?x=20creation=20after=20dimail=20api=20improvement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Latest dimail modification lead to a bug in our app, preventing mailbox creation from working properly. I swapped old dimail url to new one, mirrored dimail modification and fixed tests and tada! --- CHANGELOG.md | 6 +++++- src/backend/mailbox_manager/factories.py | 4 +++- .../mailboxes/test_api_mailboxes_create.py | 11 ++-------- .../tests/test_models_mailboxes.py | 7 +++++-- src/backend/mailbox_manager/utils/dimail.py | 21 +++++++++++++------ src/backend/people/settings.py | 2 +- .../env.d/preprod/values.desk.yaml.gotmpl | 2 +- .../env.d/production/values.desk.yaml.gotmpl | 2 +- .../env.d/staging/values.desk.yaml.gotmpl | 2 +- 9 files changed, 34 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 531ba87..4921a81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,11 @@ and this project adheres to ### Added -🔧Runtime config for the frontend #345 +- 🔧Runtime config for the frontend (#345) + +### Fixed + +- 👽️(mailboxes) fix mailbox creation after dimail api improvement (#360) ## [1.0.1] - 2024-08-19 diff --git a/src/backend/mailbox_manager/factories.py b/src/backend/mailbox_manager/factories.py index 0a4458c..862f6a3 100644 --- a/src/backend/mailbox_manager/factories.py +++ b/src/backend/mailbox_manager/factories.py @@ -96,7 +96,9 @@ class MailboxFactory(factory.django.DjangoModelFactory): ) rsps.add( rsps.POST, - re.compile(rf".*/domains/{domain.name}/mailboxes/"), + re.compile( + rf".*/domains/{domain.name}/mailboxes/{kwargs['local_part']}" + ), body=str( { "email": f"{kwargs['local_part']}@{domain.name}", diff --git a/src/backend/mailbox_manager/tests/api/mailboxes/test_api_mailboxes_create.py b/src/backend/mailbox_manager/tests/api/mailboxes/test_api_mailboxes_create.py index 45d4513..809262a 100644 --- a/src/backend/mailbox_manager/tests/api/mailboxes/test_api_mailboxes_create.py +++ b/src/backend/mailbox_manager/tests/api/mailboxes/test_api_mailboxes_create.py @@ -79,10 +79,7 @@ def test_api_mailboxes__create_viewer_failure(): @pytest.mark.parametrize( "role", - [ - enums.MailDomainRoleChoices.OWNER, - enums.MailDomainRoleChoices.ADMIN, - ], + [enums.MailDomainRoleChoices.OWNER, enums.MailDomainRoleChoices.ADMIN], ) def test_api_mailboxes__create_roles_success(role): """Users with owner or admin role should be able to create mailbox on the mail domain.""" @@ -139,10 +136,7 @@ def test_api_mailboxes__create_roles_success(role): @pytest.mark.parametrize( "role", - [ - enums.MailDomainRoleChoices.OWNER, - enums.MailDomainRoleChoices.ADMIN, - ], + [enums.MailDomainRoleChoices.OWNER, enums.MailDomainRoleChoices.ADMIN], ) def test_api_mailboxes__create_with_accent_success(role): """Users with proper abilities should be able to create mailbox on the mail domain with a @@ -345,7 +339,6 @@ def test_api_mailboxes__domain_owner_or_admin_successful_creation_and_provisioni payload = json.loads(rsps.calls[1].request.body) assert payload == { "displayName": f"{mailbox_data['first_name']} {mailbox_data['last_name']}", - "email": f"{mailbox_data['local_part']}@{access.domain.name}", "givenName": mailbox_data["first_name"], "surName": mailbox_data["last_name"], } diff --git a/src/backend/mailbox_manager/tests/test_models_mailboxes.py b/src/backend/mailbox_manager/tests/test_models_mailboxes.py index 8cab9e4..0278776 100644 --- a/src/backend/mailbox_manager/tests/test_models_mailboxes.py +++ b/src/backend/mailbox_manager/tests/test_models_mailboxes.py @@ -246,12 +246,15 @@ def test_models_mailboxes__create_mailbox_success(mock_info, mock_error): # Logger assert not mock_error.called - assert mock_info.call_count == 1 + assert mock_info.call_count == 2 assert mock_info.call_args_list[0][0] == ( + "Token succesfully granted by mail-provisioning API.", + ) + assert mock_info.call_args_list[1][0] == ( "Mailbox successfully created on domain %s", domain.name, ) - assert mock_info.call_args_list[0][1] == ( + assert mock_info.call_args_list[1][1] == ( { "extra": { "response": str( diff --git a/src/backend/mailbox_manager/utils/dimail.py b/src/backend/mailbox_manager/utils/dimail.py index 1a21aad..3c1e676 100644 --- a/src/backend/mailbox_manager/utils/dimail.py +++ b/src/backend/mailbox_manager/utils/dimail.py @@ -28,13 +28,15 @@ session.mount("https://", adapter) class DimailAPIClient: """A dimail-API client.""" + API_URL = settings.MAIL_PROVISIONING_API_URL + def get_headers(self, domain): """Build header dict from domain object.""" # self.secret is the encoded basic auth, to request a new token from dimail-api headers = {"Content-Type": "application/json"} response = requests.get( - f"{settings.MAIL_PROVISIONING_API_URL}/token/", + f"{self.API_URL}/token/", headers={"Authorization": f"Basic {domain.secret}"}, timeout=status.HTTP_200_OK, ) @@ -46,6 +48,7 @@ class DimailAPIClient: if "access_token" in response.json(): headers["Authorization"] = f"Bearer {response.json()['access_token']}" + logger.info("Token succesfully granted by mail-provisioning API.") return headers @@ -53,7 +56,6 @@ class DimailAPIClient: """Send a CREATE mailbox request to mail provisioning API.""" payload = { - "email": f"{mailbox.local_part}@{mailbox.domain}", "givenName": mailbox.first_name, "surName": mailbox.last_name, "displayName": f"{mailbox.first_name} {mailbox.last_name}", @@ -61,18 +63,19 @@ class DimailAPIClient: try: response = session.post( - f"{settings.MAIL_PROVISIONING_API_URL}/domains/{mailbox.domain}/mailboxes/", + f"{self.API_URL}/domains/{mailbox.domain}/mailboxes/{mailbox.local_part}/", json=payload, headers=self.get_headers(mailbox.domain), verify=True, timeout=10, ) - except requests.exceptions.ConnectionError as e: + except requests.exceptions.ConnectionError as error: logger.error( "Connection error while trying to reach %s.", - settings.MAIL_PROVISIONING_API_URL, - exc_info=e, + self.API_URL, + exc_info=error, ) + raise error if response.status_code == status.HTTP_201_CREATED: extra = {"response": response.content.decode("utf-8")} @@ -92,4 +95,10 @@ class DimailAPIClient: raise exceptions.PermissionDenied( f"Please check secret of the mail domain {mailbox.domain.name}" ) + else: + logger.error( + "Unexpected response: %s", + response.content.decode("utf-8"), + ) + return response diff --git a/src/backend/people/settings.py b/src/backend/people/settings.py index 4e80bea..b86eac0 100755 --- a/src/backend/people/settings.py +++ b/src/backend/people/settings.py @@ -366,7 +366,7 @@ class Base(Configuration): # mailboxes provisioning API MAIL_PROVISIONING_API_URL = values.Value( - default="https://main.dev.ox.numerique.gouv.fr", + default="https://api.dev.ox.numerique.gouv.fr", environ_name="MAIL_PROVISIONING_API_URL", environ_prefix=None, ) diff --git a/src/helm/env.d/preprod/values.desk.yaml.gotmpl b/src/helm/env.d/preprod/values.desk.yaml.gotmpl index 9c06a46..ae41f13 100644 --- a/src/helm/env.d/preprod/values.desk.yaml.gotmpl +++ b/src/helm/env.d/preprod/values.desk.yaml.gotmpl @@ -84,7 +84,7 @@ backend: secretKeyRef: name: redis.redis.libre.sh key: url - MAIL_PROVISIONING_API_URL: "https://main.dev.ox.numerique.gouv.fr" + MAIL_PROVISIONING_API_URL: "https://api.dev.ox.numerique.gouv.fr" FEATURE_TEAMS: False createsuperuser: diff --git a/src/helm/env.d/production/values.desk.yaml.gotmpl b/src/helm/env.d/production/values.desk.yaml.gotmpl index a5c17d9..fb24eb7 100644 --- a/src/helm/env.d/production/values.desk.yaml.gotmpl +++ b/src/helm/env.d/production/values.desk.yaml.gotmpl @@ -84,7 +84,7 @@ backend: secretKeyRef: name: redis.redis.libre.sh key: url - MAIL_PROVISIONING_API_URL: "https://main.dev.ox.numerique.gouv.fr" + MAIL_PROVISIONING_API_URL: "https://api.dev.ox.numerique.gouv.fr" FEATURE_TEAMS: False createsuperuser: diff --git a/src/helm/env.d/staging/values.desk.yaml.gotmpl b/src/helm/env.d/staging/values.desk.yaml.gotmpl index f683205..dd326af 100644 --- a/src/helm/env.d/staging/values.desk.yaml.gotmpl +++ b/src/helm/env.d/staging/values.desk.yaml.gotmpl @@ -84,7 +84,7 @@ backend: secretKeyRef: name: redis.redis.libre.sh key: url - MAIL_PROVISIONING_API_URL: "https://main.dev.ox.numerique.gouv.fr" + MAIL_PROVISIONING_API_URL: "https://api.dev.ox.numerique.gouv.fr" createsuperuser: