Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 893 - 4 new fields for user #903

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 37 additions & 14 deletions lib/src/model/login_status.dart
@@ -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,57 @@ 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",
/// "country":"be"
/// }
/// }
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 +77,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
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
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, isNull);
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
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