From d07f80dba3888a0632702dfa4d23b28aaac4adc1 Mon Sep 17 00:00:00 2001 From: Kalil BARRY Date: Tue, 22 Oct 2024 23:46:35 +0200 Subject: [PATCH 1/3] fix: Add equality to user attributes classes * Both and . * Useful when verifying if a mock has been called with the correct parameter. --- .../gotrue/lib/src/types/user_attributes.dart | 44 ++++++ .../test/src/types/user_attributes_test.dart | 128 ++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 packages/gotrue/test/src/types/user_attributes_test.dart diff --git a/packages/gotrue/lib/src/types/user_attributes.dart b/packages/gotrue/lib/src/types/user_attributes.dart index 60fd12d2..55e838fa 100644 --- a/packages/gotrue/lib/src/types/user_attributes.dart +++ b/packages/gotrue/lib/src/types/user_attributes.dart @@ -1,3 +1,6 @@ +// ignore_for_file: public_member_api_docs, sort_constructors_first +import 'package:collection/collection.dart'; + class UserAttributes { /// The user's email. String? email; @@ -35,6 +38,26 @@ class UserAttributes { if (data != null) 'data': data, }; } + + @override + bool operator ==(covariant UserAttributes other) { + if (identical(this, other)) return true; + + return other.email == email && + other.phone == phone && + other.password == password && + other.nonce == nonce && + other.data == data; + } + + @override + int get hashCode { + return email.hashCode ^ + phone.hashCode ^ + password.hashCode ^ + nonce.hashCode ^ + data.hashCode; + } } class AdminUserAttributes extends UserAttributes { @@ -102,4 +125,25 @@ class AdminUserAttributes extends UserAttributes { if (banDuration != null) 'ban_duration': banDuration, }; } + + @override + bool operator ==(covariant AdminUserAttributes other) { + if (identical(this, other)) return true; + 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 userMetadata.hashCode ^ + appMetadata.hashCode ^ + emailConfirm.hashCode ^ + phoneConfirm.hashCode ^ + banDuration.hashCode; + } } diff --git a/packages/gotrue/test/src/types/user_attributes_test.dart b/packages/gotrue/test/src/types/user_attributes_test.dart new file mode 100644 index 00000000..e158e026 --- /dev/null +++ b/packages/gotrue/test/src/types/user_attributes_test.dart @@ -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 data; + + setUp(() { + email = 'john@supabase.com'; + 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 userMetadata; + late Map 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))); + }); + }); +} From 7041a84ecfd630be343481c0bb5188af2f27c359 Mon Sep 17 00:00:00 2001 From: Kalil BARRY Date: Wed, 23 Oct 2024 11:50:40 +0200 Subject: [PATCH 2/3] refactor: fix requested changes --- packages/gotrue/lib/src/types/user_attributes.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/gotrue/lib/src/types/user_attributes.dart b/packages/gotrue/lib/src/types/user_attributes.dart index 55e838fa..521c4fdf 100644 --- a/packages/gotrue/lib/src/types/user_attributes.dart +++ b/packages/gotrue/lib/src/types/user_attributes.dart @@ -40,14 +40,17 @@ class UserAttributes { } @override - bool operator ==(covariant UserAttributes other) { + 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 && - other.data == data; + mapEquals(other.data, data); } @override @@ -127,8 +130,10 @@ class AdminUserAttributes extends UserAttributes { } @override - bool operator ==(covariant AdminUserAttributes other) { + 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) && @@ -140,7 +145,8 @@ class AdminUserAttributes extends UserAttributes { @override int get hashCode { - return userMetadata.hashCode ^ + return super.hashCode ^ + userMetadata.hashCode ^ appMetadata.hashCode ^ emailConfirm.hashCode ^ phoneConfirm.hashCode ^ From a905d2360af68288656ca646ef15032570402c0d Mon Sep 17 00:00:00 2001 From: Kalil <62212125+bkalil@users.noreply.github.com> Date: Wed, 6 Nov 2024 23:01:30 +0100 Subject: [PATCH 3/3] refactor: Remove ignore comment --- packages/gotrue/lib/src/types/user_attributes.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/gotrue/lib/src/types/user_attributes.dart b/packages/gotrue/lib/src/types/user_attributes.dart index 521c4fdf..25da49b4 100644 --- a/packages/gotrue/lib/src/types/user_attributes.dart +++ b/packages/gotrue/lib/src/types/user_attributes.dart @@ -1,4 +1,3 @@ -// ignore_for_file: public_member_api_docs, sort_constructors_first import 'package:collection/collection.dart'; class UserAttributes {