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

fix: Add equality to user attributes classes #1070

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/gotrue/lib/src/types/user_attributes.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:collection/collection.dart';

class UserAttributes {
/// The user's email.
String? email;
Expand Down Expand Up @@ -35,6 +37,29 @@ class UserAttributes {
if (data != null) 'data': data,
};
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! UserAttributes) return false;

final mapEquals = const DeepCollectionEquality().equals;

return other.email == email &&
other.phone == phone &&
other.password == password &&
other.nonce == nonce &&
mapEquals(other.data, data);
}

@override
int get hashCode {
return email.hashCode ^
phone.hashCode ^
password.hashCode ^
nonce.hashCode ^
data.hashCode;
}
}

class AdminUserAttributes extends UserAttributes {
Expand Down Expand Up @@ -102,4 +127,28 @@ class AdminUserAttributes extends UserAttributes {
if (banDuration != null) 'ban_duration': banDuration,
};
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! AdminUserAttributes) return false;

final mapEquals = const DeepCollectionEquality().equals;

return mapEquals(other.userMetadata, userMetadata) &&
bkalil marked this conversation as resolved.
Show resolved Hide resolved
mapEquals(other.appMetadata, appMetadata) &&
other.emailConfirm == emailConfirm &&
other.phoneConfirm == phoneConfirm &&
other.banDuration == banDuration;
}

@override
int get hashCode {
return super.hashCode ^
userMetadata.hashCode ^
appMetadata.hashCode ^
emailConfirm.hashCode ^
phoneConfirm.hashCode ^
banDuration.hashCode;
}
}
128 changes: 128 additions & 0 deletions packages/gotrue/test/src/types/user_attributes_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'package:gotrue/src/types/user_attributes.dart';
import 'package:test/test.dart';

void main() {
late String email;
late String phone;
late String password;
late String nonce;
late Map<String, dynamic> data;

setUp(() {
email = '[email protected]';
phone = '+1234567890';
password = 'password';
nonce = 'nonce';
data = {'first_name': 'John', 'last_name': 'Doe'};
});

group('User attributes', () {
late UserAttributes userAttributesOne;
late UserAttributes userAttributesTwo;

setUp(() {
userAttributesOne = UserAttributes(
email: email,
phone: phone,
password: password,
nonce: nonce,
data: data,
);

userAttributesTwo = UserAttributes(
email: email,
phone: phone,
password: password,
nonce: nonce,
data: data,
);
});

test('Attributes are equals', () {
// assert
expect(userAttributesOne, equals(userAttributesTwo));
});

test('Attributes are not equals', () {
// arrange
final userAttributesThree = UserAttributes(
email: 'email',
phone: phone,
password: password,
nonce: nonce,
data: {'first_name': 'Jane', 'last_name': 'Doe'},
);

// assert
expect(userAttributesOne, isNot(equals(userAttributesThree)));
});
});

group('Admin user attributes', () {
late AdminUserAttributes adminUserAttributesOne;
late AdminUserAttributes adminUserAttributesTwo;

late Map<String, dynamic> userMetadata;
late Map<String, dynamic> appMetadata;
late bool emailConfirm;
late bool phoneConfirm;
late String banDuration;

setUp(() {
userMetadata = {'first_name': 'John', 'last_name': 'Doe'};
appMetadata = {
'roles': ['admin']
};
emailConfirm = true;
phoneConfirm = true;
banDuration = '1d';

adminUserAttributesOne = AdminUserAttributes(
email: email,
phone: phone,
password: password,
data: data,
userMetadata: userMetadata,
appMetadata: appMetadata,
emailConfirm: emailConfirm,
phoneConfirm: phoneConfirm,
banDuration: banDuration,
);

adminUserAttributesTwo = AdminUserAttributes(
email: email,
phone: phone,
password: password,
data: data,
userMetadata: userMetadata,
appMetadata: appMetadata,
emailConfirm: emailConfirm,
phoneConfirm: phoneConfirm,
banDuration: banDuration,
);
});

test('Attributes are equals', () {
// assert
expect(adminUserAttributesOne, equals(adminUserAttributesTwo));
});

test('Attributes are not equals', () {
// arrange
final adminUserAttributesThree = AdminUserAttributes(
email: email,
phone: phone,
password: password,
data: data,
userMetadata: {'first_name': 'Jane', 'last_name': 'Doe'},
appMetadata: appMetadata,
emailConfirm: false,
phoneConfirm: false,
banDuration: 'banDuration',
);

// assert
expect(adminUserAttributesOne, isNot(equals(adminUserAttributesThree)));
});
});
}