Skip to content

fix: Add equality to user attributes classes #1070

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

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions packages/gotrue/lib/src/types/user_attributes.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
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 = '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<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)));
});
});
}