From 7cd6d54e16c97357a88c55f0f07c5fe70fc9bf50 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 3 Dec 2026 20:15:45 +0100 Subject: [PATCH] Test the AppLinkRouter itself, not only its helpers --- test/core/app_link_router_test.dart | 163 ++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git a/test/core/app_link_router_test.dart b/test/core/app_link_router_test.dart index 03e1fbb57..3cca2e9e1 100644 --- a/test/core/app_link_router_test.dart +++ b/test/core/app_link_router_test.dart @@ -16,11 +16,62 @@ * along with this program. If not, see . */ +import 'dart:async'; + +import 'package:app_links/app_links.dart'; import 'package:clock/clock.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; import 'package:wger/core/app_link_router.dart'; +import 'package:wger/core/network/auth_notifier.dart'; +import 'package:wger/core/network/auth_state.dart'; + +import '../helpers/fake_auth_notifier.dart'; + +/// Hands the router the links a test pushes, in place of the platform plugin +class _FakeAppLinks implements AppLinks { + Uri? initialLink; + Object? initialLinkError; + final links = StreamController(); + + @override + Future getInitialLink() async { + if (initialLinkError != null) { + throw initialLinkError!; + } + return initialLink; + } + + @override + Stream get uriLinkStream => links.stream; + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} + +/// Records the logins the router starts instead of talking to a server +class _RecordingAuthNotifier extends FakeAuthNotifier { + _RecordingAuthNotifier() : super(const AuthState()); + + final logins = <({String serverUrl, String? token})>[]; + Object? loginError; + + @override + Future login( + String username, + String password, + String serverUrl, + String? refreshToken, + ) async { + logins.add((serverUrl: serverUrl, token: refreshToken)); + if (loginError != null) { + throw loginError!; + } + return LoginActions.proceed; + } +} void main() { group('AppLinkRouter.tokenFromUri', () { @@ -153,4 +204,116 @@ void main() { expect(await consumeAppAuthState(first), isNull); }); }); + + group('AppLinkRouter', () { + const server = 'https://my.wger.example'; + + late _FakeAppLinks appLinks; + late _RecordingAuthNotifier auth; + late AppLinkRouter router; + + setUp(() { + SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty(); + appLinks = _FakeAppLinks(); + addTearDown(appLinks.links.close); + auth = _RecordingAuthNotifier(); + final container = ProviderContainer.test( + overrides: [authProvider.overrideWith(() => auth)], + ); + // The router needs a Ref, the provider hands it one + router = container.read(Provider((ref) => AppLinkRouter(ref, appLinks: appLinks))); + addTearDown(router.stop); + }); + + Uri handoff({String token = 'refresh.jwt', String? state}) => Uri.parse( + 'wger://app-auth#token=$token${state == null ? '' : '&state=$state'}', + ); + + Future receive(Uri uri) async { + appLinks.links.add(uri); + await pumpEventQueue(); + } + + test('a handoff the app started logs in on the server it was started for', () async { + await router.start(); + final state = await issueAppAuthState(server); + + await receive(handoff(state: state)); + + expect(auth.logins, [(serverUrl: server, token: 'refresh.jwt')]); + }); + + test('a link the app did not ask for does not log in', () async { + // Login CSRF: a link from elsewhere would sign the user into an + // account of the sender's choosing + await router.start(); + await issueAppAuthState(server); + + await receive(handoff()); + await receive(handoff(state: 'forged')); + + expect(auth.logins, isEmpty); + }); + + test('a forged state burns the pending one, the real link then fails too', () async { + await router.start(); + final state = await issueAppAuthState(server); + + await receive(handoff(state: 'forged')); + await receive(handoff(state: state)); + + expect(auth.logins, isEmpty); + }); + + test('a link that is not a handoff leaves the pending state alone', () async { + await router.start(); + final state = await issueAppAuthState(server); + + await receive(Uri.parse('wger://somewhere-else#token=x&state=$state')); + await receive(handoff(state: state)); + + expect(auth.logins, [(serverUrl: server, token: 'refresh.jwt')]); + }); + + test('a link that started the app is handled as well', () async { + final state = await issueAppAuthState(server); + appLinks.initialLink = handoff(state: state); + + await router.start(); + await pumpEventQueue(); + + expect(auth.logins, [(serverUrl: server, token: 'refresh.jwt')]); + }); + + test('a failing cold-start lookup still listens for later links', () async { + appLinks.initialLinkError = Exception('no plugin'); + await router.start(); + final state = await issueAppAuthState(server); + + await receive(handoff(state: state)); + + expect(auth.logins, hasLength(1)); + }); + + test('a failed login does not stop the next handoff', () async { + await router.start(); + auth.loginError = Exception('server down'); + await receive(handoff(state: await issueAppAuthState(server))); + + auth.loginError = null; + await receive(handoff(token: 'second.jwt', state: await issueAppAuthState(server))); + + expect(auth.logins.map((login) => login.token), ['refresh.jwt', 'second.jwt']); + }); + + test('after stop no link logs in', () async { + await router.start(); + final state = await issueAppAuthState(server); + + await router.stop(); + await receive(handoff(state: state)); + + expect(auth.logins, isEmpty); + }); + }); }