Skip to content

Commit bb32a36

Browse files
committed
refactor(user): use Role model for user role
- Updated User model to use Role - Updated tests to use Role model
1 parent 9a143a0 commit bb32a36

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

lib/src/models/user.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:equatable/equatable.dart';
2+
import 'package:ht_shared/src/models/role.dart';
23
import 'package:json_annotation/json_annotation.dart';
34

45
part 'user.g.dart';
@@ -27,8 +28,8 @@ class User extends Equatable {
2728
/// This will be `null` for users who haven't associated an email yet.
2829
final String? email;
2930

30-
/// The role of the user (e.g., 'admin', 'standard_user', 'guest_user').
31-
final String role;
31+
/// The role of the user.
32+
final Role role;
3233

3334
/// Converts this User instance to JSON data.
3435
Map<String, dynamic> toJson() => _$UserToJson(this);

lib/src/models/user.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/src/models/auth_success_response_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ void main() {
77
const testUser = User(
88
id: 'user-123',
99
email: 'test@example.com',
10-
role: 'standard_user',
10+
role: Role(name: 'standard_user'),
1111
);
1212
const testToken = 'sample-jwt-token';
1313

@@ -94,7 +94,7 @@ void main() {
9494
const updatedUser = User(
9595
id: 'user-456',
9696
email: 'updated@example.com',
97-
role: 'guest_user',
97+
role: Role(name: 'guest_user'), // Use Role object
9898
);
9999
final copiedResponse = authSuccessResponse.copyWith(user: updatedUser);
100100

@@ -115,7 +115,7 @@ void main() {
115115
});
116116

117117
test('should create a copy with both user and token updated', () {
118-
const updatedUser = User(id: 'user-789', role: 'guest_user');
118+
const updatedUser = User(id: 'user-789', role: Role(name: 'guest_user')); // Use Role object
119119
const updatedToken = 'another-token-xyz';
120120
final copiedResponse = authSuccessResponse.copyWith(
121121
user: updatedUser,
@@ -137,7 +137,7 @@ void main() {
137137
});
138138

139139
test('should not equate instances with different users', () {
140-
const differentUser = User(id: 'diff-user', role: 'admin');
140+
const differentUser = User(id: 'diff-user', role: Role(name: 'admin')); // Use Role object
141141
const response1 = AuthSuccessResponse(user: testUser, token: testToken);
142142
const response2 = AuthSuccessResponse(
143143
user: differentUser,

test/src/models/user_test.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,78 @@
1+
import 'package:ht_shared/src/models/role.dart';
12
import 'package:ht_shared/src/models/user.dart';
23
import 'package:test/test.dart';
34

45
void main() {
56
group('User Model', () {
67
const id = 'test-id';
78
const email = 'test@example.com';
9+
const standardRole = Role(name: 'standard_user');
10+
const guestRole = Role(name: 'guest_user');
11+
const adminRole = Role(name: 'admin');
812

913
test('supports value equality', () {
1014
expect(
11-
const User(id: id, email: email, role: 'standard_user'),
12-
equals(const User(id: id, email: email, role: 'standard_user')),
15+
const User(id: id, email: email, role: standardRole),
16+
equals(const User(id: id, email: email, role: standardRole)),
1317
);
1418
expect(
15-
const User(id: id, email: email, role: 'standard_user'),
19+
const User(id: id, email: email, role: standardRole),
1620
isNot(
1721
equals(
18-
const User(id: 'other-id', email: email, role: 'standard_user'),
22+
const User(id: 'other-id', email: email, role: standardRole),
1923
),
2024
),
2125
);
2226
expect(
23-
const User(id: id, email: email, role: 'standard_user'),
27+
const User(id: id, email: email, role: standardRole),
2428
isNot(
2529
equals(
2630
const User(
2731
id: id,
2832
email: 'other@example.com',
29-
role: 'standard_user',
33+
role: standardRole,
3034
),
3135
),
3236
),
3337
);
3438
expect(
35-
const User(id: id, email: email, role: 'standard_user'),
36-
isNot(equals(const User(id: id, email: email, role: 'guest_user'))),
39+
const User(id: id, email: email, role: standardRole),
40+
isNot(equals(const User(id: id, email: email, role: guestRole))),
3741
);
3842
expect(
39-
const User(id: id, email: email, role: 'standard_user'),
40-
isNot(equals(const User(id: id, email: email, role: 'admin'))),
43+
const User(id: id, email: email, role: standardRole),
44+
isNot(equals(const User(id: id, email: email, role: adminRole))),
4145
);
4246
});
4347

4448
test('has correct toString', () {
4549
expect(
46-
const User(id: id, email: email, role: 'standard_user').toString(),
47-
equals('User(id: $id, email: $email, role: standard_user)'),
50+
const User(id: id, email: email, role: standardRole).toString(),
51+
equals('User(id: $id, email: $email, role: ${standardRole.toString()})'),
4852
);
4953
expect(
50-
const User(id: id, role: 'guest_user').toString(),
51-
equals('User(id: $id, email: null, role: guest_user)'),
54+
const User(id: id, role: guestRole).toString(),
55+
equals('User(id: $id, email: null, role: ${guestRole.toString()})'),
5256
);
5357
expect(
54-
const User(id: id, role: 'admin').toString(),
55-
equals('User(id: $id, email: null, role: admin)'),
58+
const User(id: id, role: adminRole).toString(),
59+
equals('User(id: $id, email: null, role: ${adminRole.toString()})'),
5660
);
5761
});
5862

5963
// Basic test for JSON serialization - assumes build_runner generated correctly
6064
test('can be serialized and deserialized', () {
61-
const user = User(id: id, email: email, role: 'standard_user');
65+
const user = User(id: id, email: email, role: standardRole);
6266
final json = user.toJson();
6367
final deserializedUser = User.fromJson(json);
6468
expect(deserializedUser, equals(user));
6569

66-
const anonUser = User(id: id, role: 'guest_user');
70+
const anonUser = User(id: id, role: guestRole);
6771
final anonJson = anonUser.toJson();
6872
final deserializedAnonUser = User.fromJson(anonJson);
6973
expect(deserializedAnonUser, equals(anonUser));
7074

71-
const adminUser = User(id: id, email: email, role: 'admin');
75+
const adminUser = User(id: id, email: email, role: adminRole);
7276
final adminJson = adminUser.toJson();
7377
final deserializedAdminUser = User.fromJson(adminJson);
7478
expect(deserializedAdminUser, equals(adminUser));

0 commit comments

Comments
 (0)