Skip to content

Commit 4ab798e

Browse files
committed
test: auth repository token persistence
- Added storage interaction tests - Improved exception handling tests
1 parent 728d343 commit 4ab798e

File tree

2 files changed

+180
-37
lines changed

2 files changed

+180
-37
lines changed

coverage/lcov.info

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,35 @@ DA:60,2
99
DA:61,1
1010
DA:72,1
1111
DA:74,2
12-
DA:75,1
13-
DA:86,1
14-
DA:88,2
15-
DA:89,1
12+
DA:76,1
13+
DA:77,1
14+
DA:78,1
15+
DA:80,1
16+
DA:82,1
17+
DA:95,1
18+
DA:97,2
19+
DA:99,1
1620
DA:100,1
17-
DA:102,2
21+
DA:101,1
1822
DA:103,1
19-
DA:111,1
20-
DA:113,2
21-
DA:114,1
22-
DA:117,1
23-
DA:127,1
24-
DA:129,2
25-
DA:130,1
23+
DA:105,1
24+
DA:118,1
25+
DA:120,2
26+
DA:121,1
27+
DA:122,1
28+
DA:124,1
2629
DA:132,1
27-
DA:134,1
28-
DA:142,1
29-
DA:144,3
30-
DA:145,1
31-
LF:29
32-
LH:29
30+
DA:134,2
31+
DA:135,1
32+
DA:138,1
33+
DA:148,1
34+
DA:150,2
35+
DA:151,1
36+
DA:153,1
37+
DA:155,1
38+
DA:163,1
39+
DA:165,3
40+
DA:166,1
41+
LF:39
42+
LH:39
3343
end_of_record

test/src/ht_auth_repository_test.dart

Lines changed: 152 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,43 @@ void main() {
105105
});
106106

107107
group('verifySignInCode', () {
108-
test('delegates to client and returns user on success', () async {
109-
const email = '[email protected]';
110-
const code = '123456';
111-
final mockUser = MockUser();
108+
const email = '[email protected]';
109+
const code = '123456';
110+
const testToken = 'sample_token';
111+
late User mockUser;
112+
late AuthSuccessResponse mockAuthResponse;
113+
114+
setUp(() {
115+
mockUser = MockUser();
116+
mockAuthResponse =
117+
AuthSuccessResponse(user: mockUser, token: testToken);
118+
});
119+
120+
test(
121+
'calls client, saves token, and returns user on successful verification',
122+
() async {
112123
when(() => mockAuthClient.verifySignInCode(email, code))
113-
.thenAnswer((_) async => mockUser);
124+
.thenAnswer((_) async => mockAuthResponse);
125+
when(
126+
() => mockStorageService.writeString(
127+
key: StorageKey.authToken.stringValue,
128+
value: testToken,
129+
),
130+
).thenAnswer((_) async {});
114131

115132
final user = await authRepository.verifySignInCode(email, code);
116133

117134
expect(user, equals(mockUser));
118135
verify(() => mockAuthClient.verifySignInCode(email, code)).called(1);
136+
verify(
137+
() => mockStorageService.writeString(
138+
key: StorageKey.authToken.stringValue,
139+
value: testToken,
140+
),
141+
).called(1);
119142
});
120143

121-
test('delegates to client and re-throws HtHttpException on failure',
122-
() async {
123-
const email = '[email protected]';
124-
const code = '123456';
144+
test('re-throws HtHttpException from client on client failure', () async {
125145
final exception = AuthenticationException('Invalid code');
126146
when(() => mockAuthClient.verifySignInCode(email, code))
127147
.thenThrow(exception);
@@ -131,23 +151,74 @@ void main() {
131151
throwsA(equals(exception)),
132152
);
133153
verify(() => mockAuthClient.verifySignInCode(email, code)).called(1);
154+
verifyNever(
155+
() => mockStorageService.writeString(
156+
key: any(named: 'key'),
157+
value: any(named: 'value'),
158+
),
159+
);
160+
});
161+
162+
test(
163+
're-throws StorageException from storageService on token save failure',
164+
() async {
165+
when(() => mockAuthClient.verifySignInCode(email, code))
166+
.thenAnswer((_) async => mockAuthResponse);
167+
final exception = StorageWriteException(
168+
StorageKey.authToken.stringValue,
169+
testToken,
170+
);
171+
when(
172+
() => mockStorageService.writeString(
173+
key: StorageKey.authToken.stringValue,
174+
value: testToken,
175+
),
176+
).thenThrow(exception);
177+
178+
expect(
179+
() => authRepository.verifySignInCode(email, code),
180+
throwsA(equals(exception)),
181+
);
182+
verify(() => mockAuthClient.verifySignInCode(email, code)).called(1);
183+
// Removed verify for mockStorageService.writeString here
134184
});
135185
});
136186

137187
group('signInAnonymously', () {
138-
test('delegates to client and returns user on success', () async {
139-
final mockUser = MockUser();
188+
const testToken = 'anonymous_token';
189+
late User mockUser;
190+
late AuthSuccessResponse mockAuthResponse;
191+
192+
setUp(() {
193+
mockUser = MockUser();
194+
mockAuthResponse =
195+
AuthSuccessResponse(user: mockUser, token: testToken);
196+
});
197+
198+
test('calls client, saves token, and returns user on successful sign-in',
199+
() async {
140200
when(() => mockAuthClient.signInAnonymously())
141-
.thenAnswer((_) async => mockUser);
201+
.thenAnswer((_) async => mockAuthResponse);
202+
when(
203+
() => mockStorageService.writeString(
204+
key: StorageKey.authToken.stringValue,
205+
value: testToken,
206+
),
207+
).thenAnswer((_) async {});
142208

143209
final user = await authRepository.signInAnonymously();
144210

145211
expect(user, equals(mockUser));
146212
verify(() => mockAuthClient.signInAnonymously()).called(1);
213+
verify(
214+
() => mockStorageService.writeString(
215+
key: StorageKey.authToken.stringValue,
216+
value: testToken,
217+
),
218+
).called(1);
147219
});
148220

149-
test('delegates to client and re-throws HtHttpException on failure',
150-
() async {
221+
test('re-throws HtHttpException from client on client failure', () async {
151222
final exception = ServerException('Server error');
152223
when(() => mockAuthClient.signInAnonymously()).thenThrow(exception);
153224

@@ -156,21 +227,59 @@ void main() {
156227
throwsA(equals(exception)),
157228
);
158229
verify(() => mockAuthClient.signInAnonymously()).called(1);
230+
verifyNever(
231+
() => mockStorageService.writeString(
232+
key: any(named: 'key'),
233+
value: any(named: 'value'),
234+
),
235+
);
236+
});
237+
238+
test(
239+
're-throws StorageException from storageService on token save failure',
240+
() async {
241+
when(() => mockAuthClient.signInAnonymously())
242+
.thenAnswer((_) async => mockAuthResponse);
243+
final exception = StorageWriteException(
244+
StorageKey.authToken.stringValue,
245+
testToken,
246+
);
247+
when(
248+
() => mockStorageService.writeString(
249+
key: StorageKey.authToken.stringValue,
250+
value: testToken,
251+
),
252+
).thenThrow(exception);
253+
254+
expect(
255+
() => authRepository.signInAnonymously(),
256+
throwsA(equals(exception)),
257+
);
258+
verify(() => mockAuthClient.signInAnonymously()).called(1);
259+
// Removed verify for mockStorageService.writeString here
159260
});
160261
});
161262

162263
group('signOut', () {
163-
test('delegates to client on success', () async {
164-
when(() => mockAuthClient.signOut())
165-
.thenAnswer((_) async => Future.value());
264+
test('calls client signOut and clears token on success', () async {
265+
when(() => mockAuthClient.signOut()).thenAnswer((_) async {});
266+
when(
267+
() => mockStorageService.delete(
268+
key: StorageKey.authToken.stringValue,
269+
),
270+
).thenAnswer((_) async {});
166271

167272
await authRepository.signOut();
168273

169274
verify(() => mockAuthClient.signOut()).called(1);
275+
verify(
276+
() => mockStorageService.delete(
277+
key: StorageKey.authToken.stringValue,
278+
),
279+
).called(1);
170280
});
171281

172-
test('delegates to client and re-throws HtHttpException on failure',
173-
() async {
282+
test('re-throws HtHttpException from client on client failure', () async {
174283
final exception = OperationFailedException('Sign out failed');
175284
when(() => mockAuthClient.signOut()).thenThrow(exception);
176285

@@ -179,6 +288,30 @@ void main() {
179288
throwsA(equals(exception)),
180289
);
181290
verify(() => mockAuthClient.signOut()).called(1);
291+
verifyNever(
292+
() => mockStorageService.delete(key: any(named: 'key')),
293+
);
294+
});
295+
296+
test(
297+
're-throws StorageException from storageService on token clear failure',
298+
() async {
299+
when(() => mockAuthClient.signOut()).thenAnswer((_) async {});
300+
final exception = StorageDeleteException(
301+
StorageKey.authToken.stringValue,
302+
);
303+
when(
304+
() => mockStorageService.delete(
305+
key: StorageKey.authToken.stringValue,
306+
),
307+
).thenThrow(exception);
308+
309+
expect(
310+
() => authRepository.signOut(),
311+
throwsA(equals(exception)),
312+
);
313+
verify(() => mockAuthClient.signOut()).called(1);
314+
// Removed verify for mockStorageService.delete here
182315
});
183316
});
184317

0 commit comments

Comments
 (0)