From a82a699110f79a0fc0ab70d9d1267d5cdd6332dc Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Thu, 10 Aug 2023 19:30:10 +0200 Subject: [PATCH] feat: Reset password: allow to receive "translated" email (#781) * Reset password: allow to provide a `country` and/or a `language` to the receive the email in the desired language * Add a test --- lib/src/open_food_api_client.dart | 16 ++++++++++++++++ test/user_management_test_prod.dart | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/src/open_food_api_client.dart b/lib/src/open_food_api_client.dart index c9c1d2bdee..dea33ec180 100644 --- a/lib/src/open_food_api_client.dart +++ b/lib/src/open_food_api_client.dart @@ -1128,6 +1128,9 @@ class OpenFoodAPIClient { /// } /// ``` /// + /// If the user wants to receive the [newsletter], by default it will be in + /// English. If you want to change this behavior, please provide a [language] + /// and/or a [country] for a localized content. static Future register({ required User user, required String name, @@ -1185,9 +1188,14 @@ class OpenFoodAPIClient { /// Uses reset_password.pl to send a password reset Email /// needs only /// Returns [Status.status] 200 = complete; 400 = wrong inputs or other error + [Status.error]; 500 = server error; + /// + /// By default the email will be sent in English, please provide a [language] + /// and/or a [country] to have a localized content static Future resetPassword( String emailOrUserID, { QueryType? queryType, + final OpenFoodFactsLanguage? language, + final OpenFoodFactsCountry? country, }) async { var passwordResetUri = UriHelper.getUri( path: '/cgi/reset_password.pl', @@ -1195,6 +1203,14 @@ class OpenFoodAPIClient { addUserAgentParameters: false, ); + if (language != null || country != null) { + passwordResetUri = UriHelper.replaceSubdomain( + passwordResetUri, + language: language, + country: country, + ); + } + Map data = { 'userid_or_email': emailOrUserID, 'action': 'process', diff --git a/test/user_management_test_prod.dart b/test/user_management_test_prod.dart index ee1900fe5a..bcb729d6cc 100644 --- a/test/user_management_test_prod.dart +++ b/test/user_management_test_prod.dart @@ -130,6 +130,19 @@ void main() { expect(status.status, 200); }); + + test('Reset password in French', () async { + Status status = await OpenFoodAPIClient.resetPassword( + TestConstants.TEST_USER.userId, + language: OpenFoodFactsLanguage.FRENCH, + ); + + expect( + status.body!, + contains( + 'Un e-mail avec un lien pour vous permettre de changer le mot de passe a été envoyé'), + ); + }); } String _generateRandomString(int length) {