Skip to content

Commit 7e7bc0c

Browse files
authored
fix: Add equality to user attributes classes (#1070)
1 parent be998fd commit 7e7bc0c

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

packages/gotrue/lib/src/types/user_attributes.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:collection/collection.dart';
2+
13
class UserAttributes {
24
/// The user's email.
35
String? email;
@@ -35,6 +37,29 @@ class UserAttributes {
3537
if (data != null) 'data': data,
3638
};
3739
}
40+
41+
@override
42+
bool operator ==(Object other) {
43+
if (identical(this, other)) return true;
44+
if (other is! UserAttributes) return false;
45+
46+
final mapEquals = const DeepCollectionEquality().equals;
47+
48+
return other.email == email &&
49+
other.phone == phone &&
50+
other.password == password &&
51+
other.nonce == nonce &&
52+
mapEquals(other.data, data);
53+
}
54+
55+
@override
56+
int get hashCode {
57+
return email.hashCode ^
58+
phone.hashCode ^
59+
password.hashCode ^
60+
nonce.hashCode ^
61+
data.hashCode;
62+
}
3863
}
3964

4065
class AdminUserAttributes extends UserAttributes {
@@ -102,4 +127,28 @@ class AdminUserAttributes extends UserAttributes {
102127
if (banDuration != null) 'ban_duration': banDuration,
103128
};
104129
}
130+
131+
@override
132+
bool operator ==(Object other) {
133+
if (identical(this, other)) return true;
134+
if (other is! AdminUserAttributes) return false;
135+
136+
final mapEquals = const DeepCollectionEquality().equals;
137+
138+
return mapEquals(other.userMetadata, userMetadata) &&
139+
mapEquals(other.appMetadata, appMetadata) &&
140+
other.emailConfirm == emailConfirm &&
141+
other.phoneConfirm == phoneConfirm &&
142+
other.banDuration == banDuration;
143+
}
144+
145+
@override
146+
int get hashCode {
147+
return super.hashCode ^
148+
userMetadata.hashCode ^
149+
appMetadata.hashCode ^
150+
emailConfirm.hashCode ^
151+
phoneConfirm.hashCode ^
152+
banDuration.hashCode;
153+
}
105154
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import 'package:gotrue/src/types/user_attributes.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
late String email;
6+
late String phone;
7+
late String password;
8+
late String nonce;
9+
late Map<String, dynamic> data;
10+
11+
setUp(() {
12+
email = 'john@supabase.com';
13+
phone = '+1234567890';
14+
password = 'password';
15+
nonce = 'nonce';
16+
data = {'first_name': 'John', 'last_name': 'Doe'};
17+
});
18+
19+
group('User attributes', () {
20+
late UserAttributes userAttributesOne;
21+
late UserAttributes userAttributesTwo;
22+
23+
setUp(() {
24+
userAttributesOne = UserAttributes(
25+
email: email,
26+
phone: phone,
27+
password: password,
28+
nonce: nonce,
29+
data: data,
30+
);
31+
32+
userAttributesTwo = UserAttributes(
33+
email: email,
34+
phone: phone,
35+
password: password,
36+
nonce: nonce,
37+
data: data,
38+
);
39+
});
40+
41+
test('Attributes are equals', () {
42+
// assert
43+
expect(userAttributesOne, equals(userAttributesTwo));
44+
});
45+
46+
test('Attributes are not equals', () {
47+
// arrange
48+
final userAttributesThree = UserAttributes(
49+
email: 'email',
50+
phone: phone,
51+
password: password,
52+
nonce: nonce,
53+
data: {'first_name': 'Jane', 'last_name': 'Doe'},
54+
);
55+
56+
// assert
57+
expect(userAttributesOne, isNot(equals(userAttributesThree)));
58+
});
59+
});
60+
61+
group('Admin user attributes', () {
62+
late AdminUserAttributes adminUserAttributesOne;
63+
late AdminUserAttributes adminUserAttributesTwo;
64+
65+
late Map<String, dynamic> userMetadata;
66+
late Map<String, dynamic> appMetadata;
67+
late bool emailConfirm;
68+
late bool phoneConfirm;
69+
late String banDuration;
70+
71+
setUp(() {
72+
userMetadata = {'first_name': 'John', 'last_name': 'Doe'};
73+
appMetadata = {
74+
'roles': ['admin']
75+
};
76+
emailConfirm = true;
77+
phoneConfirm = true;
78+
banDuration = '1d';
79+
80+
adminUserAttributesOne = AdminUserAttributes(
81+
email: email,
82+
phone: phone,
83+
password: password,
84+
data: data,
85+
userMetadata: userMetadata,
86+
appMetadata: appMetadata,
87+
emailConfirm: emailConfirm,
88+
phoneConfirm: phoneConfirm,
89+
banDuration: banDuration,
90+
);
91+
92+
adminUserAttributesTwo = AdminUserAttributes(
93+
email: email,
94+
phone: phone,
95+
password: password,
96+
data: data,
97+
userMetadata: userMetadata,
98+
appMetadata: appMetadata,
99+
emailConfirm: emailConfirm,
100+
phoneConfirm: phoneConfirm,
101+
banDuration: banDuration,
102+
);
103+
});
104+
105+
test('Attributes are equals', () {
106+
// assert
107+
expect(adminUserAttributesOne, equals(adminUserAttributesTwo));
108+
});
109+
110+
test('Attributes are not equals', () {
111+
// arrange
112+
final adminUserAttributesThree = AdminUserAttributes(
113+
email: email,
114+
phone: phone,
115+
password: password,
116+
data: data,
117+
userMetadata: {'first_name': 'Jane', 'last_name': 'Doe'},
118+
appMetadata: appMetadata,
119+
emailConfirm: false,
120+
phoneConfirm: false,
121+
banDuration: 'banDuration',
122+
);
123+
124+
// assert
125+
expect(adminUserAttributesOne, isNot(equals(adminUserAttributesThree)));
126+
});
127+
});
128+
}

0 commit comments

Comments
 (0)