Skip to content

Commit

Permalink
fix: Add equality to user attributes classes (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkalil authored Nov 6, 2024
1 parent be998fd commit 7e7bc0c
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
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) &&
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)));
});
});
}

0 comments on commit 7e7bc0c

Please sign in to comment.