import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/servers/machine_access.dart'; import 'package:pipelines_app/core/servers/server_catalog.dart'; import 'package:pipelines_app/core/servers/server_catalog_store.dart'; import 'package:pipelines_app/features/settings/saved_server_editor.dart'; import 'package:pipelines_app/features/settings/saved_server_row.dart'; import 'package:pipelines_app/features/settings/saved_servers_section.dart'; import 'package:pipelines_app/features/settings/server_connections.dart'; /// A machine authenticator the test scripts: what each probe answers and /// which password each machine accepts. class FakeAuthenticator extends ChangeNotifier implements MachineAuthenticator { final verdicts = {}; final passwords = {}; final probes = []; final signIns = <(String, String)>[]; @override MachineAccessResult? status(String id) => verdicts[id]; @override Future probe(SavedServer server) async { probes.add(server.id); return verdicts[server.id] ?? const MachineAccessResult(MachineAccess.connected); } @override Future authenticate(SavedServer server, String password) async { signIns.add((server.id, password)); if (passwords[server.id] != password) return 'Wrong password.'; verdicts[server.id] = const MachineAccessResult(MachineAccess.connected); notifyListeners(); return null; } } Future revealSave(WidgetTester tester) async { await tester.scrollUntilVisible( find.text('Save server'), 120, scrollable: find.byType(Scrollable).last, ); await tester.pumpAndSettle(); } Future addServer( WidgetTester tester, { required String name, required String address, }) async { await tester.tap(find.text('Add server')); await tester.pumpAndSettle(); await tester.enterText(find.byKey(const Key('server-profile-name')), name); await tester.enterText( find.byKey(const Key('server-profile-addresses')), address, ); await revealSave(tester); await tester.tap(find.text('Save server')); await tester.pumpAndSettle(); } void main() { testWidgets('a new machine asks for its own password', (tester) async { final connections = ServerConnections( store: MemoryServerCatalogStore( catalog: ServerCatalog( servers: [ SavedServer.validated( id: 'mac', name: 'Mac', endpoints: ['ws://10.0.0.75:8083/connect'], ), ], selectedId: 'mac', ), ), onSelected: (_) async {}, ); addTearDown(connections.dispose); await connections.load(); final auth = FakeAuthenticator() ..verdicts['mac'] = const MachineAccessResult(MachineAccess.connected); connections.authenticator = auth; final body = SingleChildScrollView( child: SavedServersSection(connections: connections), ); await tester.pumpWidget(MaterialApp(home: Scaffold(body: body))); expect(find.text('Connected'), findsOneWidget); // The new machine is unknown to the authenticator until saved; a // password verdict arrives for whatever id the editor assigned. auth.verdicts.addAll({ for (final id in ['pending']) id: const MachineAccessResult(MachineAccess.needsPassword), }); await tester.tap(find.text('Add server')); await tester.pumpAndSettle(); await tester.enterText( find.byKey(const Key('server-profile-name')), 'tank', ); await tester.enterText( find.byKey(const Key('server-profile-addresses')), 'ws://10.0.0.100:8082/connect', ); // Whatever id the editor chose, that machine wants a password. connections.addListener(() { for (final server in connections.catalog.servers) { auth.verdicts.putIfAbsent( server.id, () => const MachineAccessResult(MachineAccess.needsPassword), ); auth.passwords.putIfAbsent(server.id, () => 'tank-secret'); } }); await revealSave(tester); await tester.tap(find.text('Save server')); await tester.pumpAndSettle(); final tank = connections.catalog.servers.firstWhere( (s) => s.name == 'tank', ); expect(auth.probes, [tank.id]); // Saved, and the editor now asks this machine's password; the first // machine's sign-in is not reused. expect(find.byKey(const Key('server-profile-password')), findsOneWidget); expect(find.textContaining('asks for its own password'), findsOneWidget); await tester.enterText( find.byKey(const Key('server-profile-password')), 'wrong', ); await tester.tap(find.byKey(const Key('server-profile-sign-in'))); await tester.pumpAndSettle(); expect(find.text('Wrong password.'), findsOneWidget); expect(find.byKey(const Key('server-profile-password')), findsOneWidget); await tester.enterText( find.byKey(const Key('server-profile-password')), 'tank-secret', ); await tester.tap(find.byKey(const Key('server-profile-sign-in'))); await tester.pumpAndSettle(); expect(auth.signIns, [(tank.id, 'wrong'), (tank.id, 'tank-secret')]); // Back on the list: both machines connected, each on its own sign-in. expect(find.byKey(const Key('server-profile-password')), findsNothing); expect(find.text('Connected'), findsNWidgets(2)); expect(find.text('Address: ws://10.0.0.100:8082/connect'), findsOneWidget); expect(connections.catalog.active?.id, 'mac'); }); testWidgets('an unreachable machine stays saved with the reason shown', ( tester, ) async { final connections = ServerConnections( store: MemoryServerCatalogStore(), onSelected: (_) async {}, ); addTearDown(connections.dispose); await connections.load(); final auth = FakeAuthenticator(); connections.authenticator = auth; connections.addListener(() { for (final server in connections.catalog.servers) { auth.verdicts.putIfAbsent( server.id, () => const MachineAccessResult( MachineAccess.unreachable, detail: 'The HTTPS certificate of 100.85.25.33:8443 does not ' 'cover an IP address.', ), ); } }); await tester.pumpWidget( MaterialApp( home: Scaffold( body: SingleChildScrollView( child: SavedServersSection(connections: connections), ), ), ), ); await addServer( tester, name: 'Mac by IP', address: 'wss://100.85.25.33:8443/connect', ); expect(find.textContaining('could not be reached'), findsOneWidget); expect(find.textContaining('does not cover an IP address'), findsOneWidget); expect(find.byKey(const Key('server-profile-password')), findsNothing); await tester.tap(find.byKey(const Key('server-sign-in-later'))); await tester.pumpAndSettle(); expect(connections.catalog.servers.single.name, 'Mac by IP'); expect( find.textContaining('Unreachable: The HTTPS certificate'), findsOneWidget, ); }); testWidgets('a saved machine that wants a password offers Sign in', ( tester, ) async { final tank = SavedServer.validated( id: 'tank', name: 'tank', endpoints: ['ws://10.0.0.100:8082/connect'], ); final connections = ServerConnections( store: MemoryServerCatalogStore( catalog: ServerCatalog(servers: [tank], selectedId: 'tank'), ), onSelected: (_) async {}, ); addTearDown(connections.dispose); await connections.load(); final auth = FakeAuthenticator() ..verdicts['tank'] = const MachineAccessResult( MachineAccess.needsPassword, ) ..passwords['tank'] = 'tank-secret'; connections.authenticator = auth; await tester.pumpWidget( MaterialApp( home: Scaffold(body: SavedServersSection(connections: connections)), ), ); expect(find.text('Sign in required'), findsOneWidget); await tester.tap(find.byKey(savedServerMenuKey('tank'))); await tester.pumpAndSettle(); await tester.tap(find.text('Sign in to tank')); await tester.pumpAndSettle(); await tester.enterText( find.byKey(const Key('machine-password')), 'tank-secret', ); await tester.tap(find.byKey(const Key('machine-sign-in'))); await tester.pumpAndSettle(); expect(auth.signIns, [('tank', 'tank-secret')]); expect(find.text('Connected'), findsOneWidget); expect(find.text('Sign in required'), findsNothing); }); testWidgets('editing a name alone does not re-ask the password', ( tester, ) async { final mac = SavedServer.validated( id: 'mac', name: 'Mac', endpoints: ['ws://10.0.0.75:8083/connect'], ); final auth = FakeAuthenticator() ..verdicts['mac'] = const MachineAccessResult( MachineAccess.needsPassword, ); SavedServer? saved; await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) => Scaffold( body: TextButton( onPressed: () => editSavedServer( context, server: mac, authenticator: auth, onSave: (value) async => saved = value, ), child: const Text('Edit'), ), ), ), ), ); await tester.tap(find.text('Edit')); await tester.pumpAndSettle(); await tester.enterText( find.byKey(const Key('server-profile-name')), 'Studio', ); await revealSave(tester); await tester.tap(find.text('Save server')); await tester.pumpAndSettle(); expect(saved?.name, 'Studio'); expect(auth.probes, isEmpty); expect(find.text('Edit'), findsOneWidget); }); }