Skip to content

Commit

Permalink
feat: Reset password: allow to receive "translated" email (openfoodfa…
Browse files Browse the repository at this point in the history
…cts#781)

* Reset password: allow to provide a `country` and/or a `language` to the receive the email in the desired language

* Add a test
  • Loading branch information
g123k authored Aug 10, 2023
1 parent 0d492fe commit a82a699
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/src/open_food_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<SignUpStatus> register({
required User user,
required String name,
Expand Down Expand Up @@ -1185,16 +1188,29 @@ 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<Status> resetPassword(
String emailOrUserID, {
QueryType? queryType,
final OpenFoodFactsLanguage? language,
final OpenFoodFactsCountry? country,
}) async {
var passwordResetUri = UriHelper.getUri(
path: '/cgi/reset_password.pl',
queryType: queryType,
addUserAgentParameters: false,
);

if (language != null || country != null) {
passwordResetUri = UriHelper.replaceSubdomain(
passwordResetUri,
language: language,
country: country,
);
}

Map<String, String> data = <String, String>{
'userid_or_email': emailOrUserID,
'action': 'process',
Expand Down
13 changes: 13 additions & 0 deletions test/user_management_test_prod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a82a699

Please sign in to comment.