Skip to content

Commit

Permalink
feat: 893 - 4 new fields for user (#903)
Browse files Browse the repository at this point in the history
* feat: 893 - 4 new fields for user

Impacted files:
* `login_status.dart`: added 4 fields and deprecated "email"
* `open_food_api_client.dart`: upgraded the max length of user name (60) and user id (40)
* `user_management_test_prod.dart`: tested the new 4 login status fields
* `user_management_test_test_env.dart`: tested the new 4 login status fields and remove a test on "email"

* feat: 893 - 'cc' json field instead of 'country'

* Minor changes
  • Loading branch information
monsieurtanuki authored Jul 7, 2024
1 parent c370e19 commit a7e195b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
54 changes: 40 additions & 14 deletions lib/src/model/login_status.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import '../interface/json_object.dart';
import '../utils/country_helper.dart';
import '../utils/json_helper.dart';
import '../utils/language_helper.dart';

/// Status after an attempt to log in.
///
Expand All @@ -14,40 +17,60 @@ import '../interface/json_object.dart';
/// "status_verbose":"user signed-in",
/// "user_id":"gqwbgsvvod",
/// "user":{
/// "email":"blababla@gmail.com",
/// "name":"Mr. John Doe"
/// "name":"Mr. John Doe",
/// "preferred_language":"fr",
/// "cc":"be",
/// "country":"en:belgium",
/// "admin":0,
/// "moderator":1
/// }
/// }
class LoginStatus {
LoginStatus({
required this.status,
required this.statusVerbose,
this.userEmail,
String? userEmail,
this.userName,
this.userId,
this.preferredLanguage,
this.country,
this.isModerator,
this.isAdmin,
this.cookie,
});

final int status;
final String statusVerbose;
final String? userEmail;
// TODO: deprecated from 2024-04-09; remove when old enough
@Deprecated('Not retrieved anymore from the server')
final String? userEmail = null;
final String? userName;
final String? userId;
final OpenFoodFactsLanguage? preferredLanguage;
final OpenFoodFactsCountry? country;
final bool? isModerator;
final bool? isAdmin;

/// The cookie is necessary for some GET requests that require an
/// authenticated user.
final String? cookie;

factory LoginStatus.fromJson(Map<String, dynamic> json,
[Map<String, String>? headers]) =>
LoginStatus(
status: JsonObject.parseInt(json['status'])!,
statusVerbose: json['status_verbose'] as String,
userId: json['user_id'] as String?,
userEmail: json['user']?['email'] as String?,
userName: json['user']?['name'] as String?,
cookie: headers?['set-cookie'],
);
[Map<String, String>? headers]) {
final details = json['user'];
return LoginStatus(
status: JsonObject.parseInt(json['status'])!,
statusVerbose: json['status_verbose'] as String,
userId: json['user_id'] as String?,
userName: details?['name'] as String?,
preferredLanguage:
OpenFoodFactsLanguage.fromOffTag(details?['preferred_language']),
country: OpenFoodFactsCountry.fromOffTag(details?['cc']),
isModerator: JsonHelper.boolFromJSON(details?['moderator']),
isAdmin: JsonHelper.boolFromJSON(details?['admin']),
cookie: headers?['set-cookie'],
);
}

/// Was the login successful?
bool get successful => status == 1;
Expand All @@ -57,8 +80,11 @@ class LoginStatus {
'status:$status'
',statusVerbose:$statusVerbose'
'${userId == null ? '' : ',userId:$userId'}'
'${userEmail == null ? '' : ',userEmail:$userEmail'}'
'${userName == null ? '' : ',userName:$userName'}'
'${preferredLanguage == null ? '' : ',preferredLanguage:$preferredLanguage'}'
'${country == null ? '' : ',country:$country'}'
'${isAdmin == null ? '' : ',isAdmin:$isAdmin'}'
'${isModerator == null ? '' : ',isModerator:$isModerator'}'
'${cookie == null ? '' : ',cookie:$cookie'}'
')';
}
18 changes: 14 additions & 4 deletions lib/src/open_food_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -941,15 +941,19 @@ class OpenFoodAPIClient {
return null;
}

/// A username may not exceed 20 characters
static const USER_NAME_MAX_LENGTH = 20;
/// A username may not exceed 60 characters
static const USER_NAME_MAX_LENGTH = 60;

/// A user id may not exceed 40 characters
static const USER_ID_MAX_LENGTH = 40;

/// Creates a new user
///
/// Possible `status.status` responses:
///
/// Returns [Status.status] 201 = complete; 400 = wrong inputs + [Status.error]; 500 = server error;
///
/// User id may not exceed [OpenFoodAPIClient.USER_ID_MAX_LENGTH]
/// [name] may not exceed [OpenFoodAPIClient.USER_NAME_MAX_LENGTH]
///
/// When creating a [producer account](https://world.pro.openfoodfacts.org/) use [orgName] (former requested_org) to name the Producer or brand
Expand Down Expand Up @@ -983,9 +987,15 @@ class OpenFoodAPIClient {
final OpenFoodFactsCountry? country,
final UriProductHelper uriHelper = uriHelperFoodProd,
}) async {
if (user.userId.length > USER_NAME_MAX_LENGTH) {
if (user.userId.length > USER_ID_MAX_LENGTH) {
throw ArgumentError(
'A user id may not exceed $USER_ID_MAX_LENGTH characters!',
);
}

if (name.length > USER_NAME_MAX_LENGTH) {
throw ArgumentError(
'A username may not exceed $USER_NAME_MAX_LENGTH characters!',
'A user name may not exceed $USER_NAME_MAX_LENGTH characters!',
);
}

Expand Down
5 changes: 5 additions & 0 deletions test/user_management_test_prod.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ void main() {
expect(status, isNotNull);
expect(status!.successful, isTrue);
expect(status.userId, TestConstants.PROD_USER.userId);
expect(status.userName, 'Alexander Schacht (TIOLI)');
expect(status.preferredLanguage, OpenFoodFactsLanguage.ENGLISH);
expect(status.country, isNull);
expect(status.isAdmin, isFalse);
expect(status.isModerator, isFalse);
});

group('reset password', () {
Expand Down
8 changes: 4 additions & 4 deletions test/user_management_test_test_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ void main() {
name = 'M. $userId';
email = "$userId@gmail.com";

print(name);

SignUpStatus response = await OpenFoodAPIClient.register(
user: User(userId: userId, password: password),
name: name,
Expand All @@ -49,8 +47,10 @@ void main() {
expect(status!.successful, isTrue);
expect(status.userId, userId);
expect(status.userName, name);
expect(status.userEmail, email);
print('Creating a account and logging in worked in $counter trie(s)');
expect(status.preferredLanguage, isNull);
expect(status.country, isNull);
expect(status.isAdmin, false);
expect(status.isModerator, false);
});

test('Login with invalid credentials', () async {
Expand Down

0 comments on commit a7e195b

Please sign in to comment.