@@ -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