|
| 1 | +import 'package:dio/dio.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:mobile/core/network/interceptors/auth_interceptor.dart'; |
| 4 | +import 'package:mocktail/mocktail.dart'; |
| 5 | + |
| 6 | +// --------------------------------------------------------------------------- |
| 7 | +// Minimal handler doubles |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | + |
| 10 | +class _FakeRequestOptions extends Fake implements RequestOptions {} |
| 11 | + |
| 12 | +class _MockRequestInterceptorHandler extends Mock |
| 13 | + implements RequestInterceptorHandler {} |
| 14 | + |
| 15 | +class _MockErrorInterceptorHandler extends Mock |
| 16 | + implements ErrorInterceptorHandler {} |
| 17 | + |
| 18 | +// --------------------------------------------------------------------------- |
| 19 | +// Helpers |
| 20 | +// --------------------------------------------------------------------------- |
| 21 | + |
| 22 | +DioException _buildDioError({ |
| 23 | + required RequestOptions options, |
| 24 | + int? statusCode, |
| 25 | +}) { |
| 26 | + final response = statusCode != null |
| 27 | + ? Response<dynamic>( |
| 28 | + requestOptions: options, |
| 29 | + statusCode: statusCode, |
| 30 | + ) |
| 31 | + : null; |
| 32 | + return DioException( |
| 33 | + requestOptions: options, |
| 34 | + response: response, |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +void main() { |
| 39 | + setUpAll(() { |
| 40 | + registerFallbackValue(_FakeRequestOptions()); |
| 41 | + registerFallbackValue( |
| 42 | + DioException(requestOptions: RequestOptions()), |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + group('AuthInterceptor', () { |
| 47 | + group('onRequest', () { |
| 48 | + test('adds Authorization header when token is non-empty', () { |
| 49 | + final handler = _MockRequestInterceptorHandler(); |
| 50 | + final interceptor = AuthInterceptor( |
| 51 | + tokenGetter: () => 'my_access_token', |
| 52 | + tokenRefresher: () async => null, |
| 53 | + onLogout: () async {}, |
| 54 | + ); |
| 55 | + |
| 56 | + final options = RequestOptions(path: '/api/v1/wellness'); |
| 57 | + interceptor.onRequest(options, handler); |
| 58 | + |
| 59 | + expect( |
| 60 | + options.headers['Authorization'], |
| 61 | + 'Bearer my_access_token', |
| 62 | + ); |
| 63 | + verify(() => handler.next(options)).called(1); |
| 64 | + }); |
| 65 | + |
| 66 | + test('does not add Authorization header when token is empty', () { |
| 67 | + final handler = _MockRequestInterceptorHandler(); |
| 68 | + final interceptor = AuthInterceptor( |
| 69 | + tokenGetter: () => '', |
| 70 | + tokenRefresher: () async => null, |
| 71 | + onLogout: () async {}, |
| 72 | + ); |
| 73 | + |
| 74 | + final options = RequestOptions(path: '/api/v1/wellness'); |
| 75 | + interceptor.onRequest(options, handler); |
| 76 | + |
| 77 | + expect(options.headers.containsKey('Authorization'), isFalse); |
| 78 | + verify(() => handler.next(options)).called(1); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + group('onError — non-401', () { |
| 83 | + test('passes through errors that are not 401', () async { |
| 84 | + final handler = _MockErrorInterceptorHandler(); |
| 85 | + final interceptor = AuthInterceptor( |
| 86 | + tokenGetter: () => 'token', |
| 87 | + tokenRefresher: () async => null, |
| 88 | + onLogout: () async {}, |
| 89 | + ); |
| 90 | + |
| 91 | + final options = RequestOptions(path: '/api/v1/wellness'); |
| 92 | + final err = _buildDioError(options: options, statusCode: 500); |
| 93 | + |
| 94 | + await interceptor.onError(err, handler); |
| 95 | + |
| 96 | + verify(() => handler.next(err)).called(1); |
| 97 | + }); |
| 98 | + |
| 99 | + test('passes through errors with null status code', () async { |
| 100 | + final handler = _MockErrorInterceptorHandler(); |
| 101 | + final interceptor = AuthInterceptor( |
| 102 | + tokenGetter: () => 'token', |
| 103 | + tokenRefresher: () async => null, |
| 104 | + onLogout: () async {}, |
| 105 | + ); |
| 106 | + |
| 107 | + final options = RequestOptions(path: '/api/v1/wellness'); |
| 108 | + final err = _buildDioError(options: options); |
| 109 | + |
| 110 | + await interceptor.onError(err, handler); |
| 111 | + |
| 112 | + verify(() => handler.next(err)).called(1); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + group('onError — 401 on auth endpoints (skip refresh)', () { |
| 117 | + test('skips refresh for /auth/login endpoint', () async { |
| 118 | + final handler = _MockErrorInterceptorHandler(); |
| 119 | + var refreshCalled = false; |
| 120 | + final interceptor = AuthInterceptor( |
| 121 | + tokenGetter: () => 'token', |
| 122 | + tokenRefresher: () async { |
| 123 | + refreshCalled = true; |
| 124 | + return 'new_token'; |
| 125 | + }, |
| 126 | + onLogout: () async {}, |
| 127 | + ); |
| 128 | + |
| 129 | + final options = RequestOptions(path: '/api/v1/auth/login'); |
| 130 | + final err = _buildDioError(options: options, statusCode: 401); |
| 131 | + |
| 132 | + await interceptor.onError(err, handler); |
| 133 | + |
| 134 | + expect(refreshCalled, isFalse); |
| 135 | + verify(() => handler.next(err)).called(1); |
| 136 | + }); |
| 137 | + |
| 138 | + test('skips refresh for /auth/refresh endpoint', () async { |
| 139 | + final handler = _MockErrorInterceptorHandler(); |
| 140 | + var refreshCalled = false; |
| 141 | + final interceptor = AuthInterceptor( |
| 142 | + tokenGetter: () => 'token', |
| 143 | + tokenRefresher: () async { |
| 144 | + refreshCalled = true; |
| 145 | + return 'new_token'; |
| 146 | + }, |
| 147 | + onLogout: () async {}, |
| 148 | + ); |
| 149 | + |
| 150 | + final options = RequestOptions(path: '/api/v1/auth/refresh'); |
| 151 | + final err = _buildDioError(options: options, statusCode: 401); |
| 152 | + |
| 153 | + await interceptor.onError(err, handler); |
| 154 | + |
| 155 | + expect(refreshCalled, isFalse); |
| 156 | + verify(() => handler.next(err)).called(1); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + group('onError — 401 with null/empty new token', () { |
| 161 | + test( |
| 162 | + 'calls onLogout and passes error when refresher returns null', |
| 163 | + () async { |
| 164 | + final handler = _MockErrorInterceptorHandler(); |
| 165 | + var logoutCalled = false; |
| 166 | + final interceptor = AuthInterceptor( |
| 167 | + tokenGetter: () => 'old_token', |
| 168 | + tokenRefresher: () async => null, |
| 169 | + onLogout: () async { |
| 170 | + logoutCalled = true; |
| 171 | + }, |
| 172 | + ); |
| 173 | + |
| 174 | + final options = RequestOptions(path: '/api/v1/wellness'); |
| 175 | + final err = _buildDioError(options: options, statusCode: 401); |
| 176 | + |
| 177 | + await interceptor.onError(err, handler); |
| 178 | + |
| 179 | + expect(logoutCalled, isTrue); |
| 180 | + verify(() => handler.next(err)).called(1); |
| 181 | + }, |
| 182 | + ); |
| 183 | + |
| 184 | + test( |
| 185 | + 'calls onLogout and passes error when refresher returns empty string', |
| 186 | + () async { |
| 187 | + final handler = _MockErrorInterceptorHandler(); |
| 188 | + var logoutCalled = false; |
| 189 | + final interceptor = AuthInterceptor( |
| 190 | + tokenGetter: () => 'old_token', |
| 191 | + tokenRefresher: () async => '', |
| 192 | + onLogout: () async { |
| 193 | + logoutCalled = true; |
| 194 | + }, |
| 195 | + ); |
| 196 | + |
| 197 | + final options = RequestOptions(path: '/api/v1/wellness'); |
| 198 | + final err = _buildDioError(options: options, statusCode: 401); |
| 199 | + |
| 200 | + await interceptor.onError(err, handler); |
| 201 | + |
| 202 | + expect(logoutCalled, isTrue); |
| 203 | + verify(() => handler.next(err)).called(1); |
| 204 | + }, |
| 205 | + ); |
| 206 | + }); |
| 207 | + |
| 208 | + group('onError — 401 with refresher exception', () { |
| 209 | + test( |
| 210 | + 'calls onLogout when refresher throws a generic exception', |
| 211 | + () async { |
| 212 | + final handler = _MockErrorInterceptorHandler(); |
| 213 | + var logoutCalled = false; |
| 214 | + final interceptor = AuthInterceptor( |
| 215 | + tokenGetter: () => 'old_token', |
| 216 | + tokenRefresher: () async { |
| 217 | + throw Exception('network failure'); |
| 218 | + }, |
| 219 | + onLogout: () async { |
| 220 | + logoutCalled = true; |
| 221 | + }, |
| 222 | + ); |
| 223 | + |
| 224 | + final options = RequestOptions(path: '/api/v1/wellness'); |
| 225 | + final err = _buildDioError(options: options, statusCode: 401); |
| 226 | + |
| 227 | + await interceptor.onError(err, handler); |
| 228 | + |
| 229 | + expect(logoutCalled, isTrue); |
| 230 | + verify(() => handler.next(err)).called(1); |
| 231 | + }, |
| 232 | + ); |
| 233 | + }); |
| 234 | + }); |
| 235 | +} |
0 commit comments