π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Wait for cast token revocation
ashil-pilotc6c8dce3109d
diff --git a/mobile/apps/photos/changes/cast-revocation.md b/mobile/apps/photos/changes/cast-revocation.mdnew file mode 100644index 0000000000..3abfeb10cb--- /dev/null+++ b/mobile/apps/photos/changes/cast-revocation.md@@ -0,0 +1 @@+Wait for cast access to be revoked before stopping or starting a new pairing, and report failures so you can retry.diff --git a/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart b/mobile/apps/photos/lib/gateways/cast/cast_gateway.dartindex eb172516e0..00f28e54b8 100644--- a/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart+++ b/mobile/apps/photos/lib/gateways/cast/cast_gateway.dart@@ -81,11 +81,7 @@ class CastGateway {}Future<void> revokeAllTokens() async {- try {- await _enteDio.delete("/cast/revoke-all-tokens");- } catch (e) {- // swallow error- }+ await _enteDio.delete("/cast/revoke-all-tokens");}Future<void> revokeSession(CastInfo session) async {diff --git a/mobile/apps/photos/lib/ui/cast/cast.dart b/mobile/apps/photos/lib/ui/cast/cast.dartindex aea2ab3990..b708e83d9d 100644--- a/mobile/apps/photos/lib/ui/cast/cast.dart+++ b/mobile/apps/photos/lib/ui/cast/cast.dart@@ -1,5 +1,4 @@-import "dart:async";-+import "package:ente_cast/ente_cast.dart";import "package:ente_components/ente_components.dart";import "package:ente_strings/ente_strings.dart";import "package:flutter/widgets.dart";@@ -12,32 +11,56 @@ import "package:photos/service_locator.dart";import "package:photos/theme/ente_theme.dart";import "package:photos/ui/cast/pair_with_auto.dart";import "package:photos/ui/cast/pair_with_code.dart";+import "package:photos/ui/components/buttons/button_widget.dart";import "package:photos/ui/settings/cast/cast_settings_page.dart";import "package:photos/utils/dialog_util.dart";-Future<void> showCastSheet(BuildContext context, Collection collection) async {+Future<void> showCastSheet(+ BuildContext context,+ Collection collection, {+ CastGateway? gateway,+ CastService? transport,+}) async {final l10n = context.strings;final textStyle = getEnteTextTheme(context);- final gw = CastGateway(NetworkClient.instance.enteDio);- final showAutoPair = castService.isSupported;+ final gw = gateway ?? CastGateway(NetworkClient.instance.enteDio);+ final service = transport ?? castService;+ final showAutoPair = service.isSupported;+ final logger = Logger("showCastSheet");if (!flagService.enableMultiCast) {- if (castService.getActiveSessions().isNotEmpty) {- await showChoiceDialog(+ if (service.getActiveSessions().isNotEmpty) {+ final result = await showChoiceDialog(context,title: l10n.stopCastingTitle,body: l10n.stopCastingBody,firstButtonLabel: l10n.yes,secondButtonLabel: l10n.no,firstButtonOnTap: () async {- unawaited(gw.revokeAllTokens());- await castService.closeActiveCasts();+ await gw.revokeAllTokens();+ await service.closeActiveCasts();},);+ if (result?.action == ButtonAction.error && context.mounted) {+ await showGenericErrorDialog(+ context: context,+ error: result?.exception,+ );+ }+ return;+ }+ final dialog = createProgressDialog(context, l10n.pleaseWait);+ await dialog.show();+ try {+ await gw.revokeAllTokens();+ await dialog.hide();+ } catch (e, s) {+ await dialog.hide();+ logger.severe("Failed to revoke cast tokens before pairing", e, s);+ if (!context.mounted) return;+ await showGenericErrorDialog(context: context, error: e);return;}- unawaited(gw.revokeAllTokens());}- final logger = Logger("showCastSheet");List<CastInfo> sessions = [];if (flagService.enableMultiCast) {try {diff --git a/mobile/apps/photos/test/ui/cast/cast_test.dart b/mobile/apps/photos/test/ui/cast/cast_test.dartnew file mode 100644index 0000000000..497129a01f--- /dev/null+++ b/mobile/apps/photos/test/ui/cast/cast_test.dart@@ -0,0 +1,184 @@+import "package:dio/dio.dart";+import "package:ente_cast/ente_cast.dart";+import "package:ente_strings/ente_strings.dart";+import "package:flutter/material.dart";+import "package:flutter_test/flutter_test.dart";+import "package:package_info_plus/package_info_plus.dart";+import "package:photos/ente_theme_data.dart";+import "package:photos/gateways/cast/cast_gateway.dart";+import "package:photos/models/collection/collection.dart";+import "package:photos/service_locator.dart";+import "package:photos/ui/cast/cast.dart";+import "package:shared_preferences/shared_preferences.dart";++void main() {+ TestWidgetsFlutterBinding.ensureInitialized();++ setUpAll(() async {+ SharedPreferences.setMockInitialValues({});+ final preferences = await SharedPreferences.getInstance();+ ServiceLocator.instance.init(+ preferences,+ Dio(),+ Dio(),+ Dio(),+ PackageInfo(+ appName: "Photos",+ packageName: "photos",+ version: "1.0.0",+ buildNumber: "1",+ ),+ );+ await localSettings.setInternalUserDisabled(true);+ // Initialize the flag refresher outside the widget-test fake clock.+ expect(flagService.enableMultiCast, isFalse);+ });++ testWidgets("stop waits for revocation and a failed request can be retried", (+ tester,+ ) async {+ final requests = _RevocationRequests();+ final transport = _FakeCastService(hasActiveSession: true);+ await _pumpCastButton(tester, requests, transport);++ await tester.tap(find.text("Cast"));+ await tester.pumpAndSettle();+ await tester.tap(find.text("Yes"));+ await tester.pump(const Duration(seconds: 1));++ expect(requests.pending, hasLength(1));+ expect(transport.closeCount, 0);+ expect(find.text("Stop casting"), findsOneWidget);++ requests.fail();+ await tester.pumpAndSettle();+ expect(transport.closeCount, 0);+ expect(find.text("Error"), findsOneWidget);++ await tester.tap(find.text("OK"));+ await tester.pumpAndSettle();+ await tester.tap(find.text("Cast"));+ await tester.pumpAndSettle();+ await tester.tap(find.text("Yes"));+ await tester.pump(const Duration(seconds: 1));+ expect(transport.closeCount, 0);++ requests.succeed();+ await tester.pumpAndSettle();+ expect(transport.closeCount, 1);+ expect(find.text("Stop casting"), findsNothing);+ });++ testWidgets("pairing stays blocked until revocation succeeds after retry", (+ tester,+ ) async {+ final requests = _RevocationRequests();+ final transport = _FakeCastService(hasActiveSession: false);+ await _pumpCastButton(tester, requests, transport);++ await tester.tap(find.text("Cast"));+ await tester.pump(const Duration(seconds: 1));+ expect(requests.pending, hasLength(1));+ expect(find.text("Please wait..."), findsOneWidget);+ expect(find.text("Auto pair"), findsNothing);+ expect(find.text("Pair using code"), findsNothing);++ requests.fail();+ await tester.pumpAndSettle();+ expect(find.text("Error"), findsOneWidget);+ expect(find.text("Pair using code"), findsNothing);++ await tester.tap(find.text("OK"));+ await tester.pumpAndSettle();+ await tester.tap(find.text("Cast"));+ await tester.pump(const Duration(seconds: 1));+ expect(find.text("Pair using code"), findsNothing);++ requests.succeed();+ await tester.pumpAndSettle();+ expect(find.text("Auto pair"), findsOneWidget);+ expect(find.text("Pair using code"), findsOneWidget);+ expect(transport.closeCount, 0);+ });+}++Future<void> _pumpCastButton(+ WidgetTester tester,+ _RevocationRequests requests,+ CastService transport,+) async {+ await tester.pumpWidget(+ MaterialApp(+ theme: lightThemeData,+ localizationsDelegates: StringsLocalizations.localizationsDelegates,+ supportedLocales: StringsLocalizations.supportedLocales,+ home: Scaffold(+ body: Builder(+ builder: (context) => TextButton(+ onPressed: () => showCastSheet(+ context,+ _FakeCollection(),+ gateway: CastGateway(requests.dio),+ transport: transport,+ ),+ child: const Text("Cast"),+ ),+ ),+ ),+ ),+ );+}++class _RevocationRequests {+ final dio = Dio();+ final pending = <(RequestOptions, RequestInterceptorHandler)>[];++ _RevocationRequests() {+ dio.interceptors.add(+ InterceptorsWrapper(+ onRequest: (options, handler) {+ expect(options.method, "DELETE");+ expect(options.path, "/cast/revoke-all-tokens");+ pending.add((options, handler));+ },+ ),+ );+ }++ void fail() {+ final (options, handler) = pending.removeAt(0);+ handler.reject(+ DioException(+ requestOptions: options,+ type: DioExceptionType.connectionError,+ ),+ );+ }++ void succeed() {+ final (options, handler) = pending.removeAt(0);+ handler.resolve(Response<void>(requestOptions: options, statusCode: 200));+ }+}++class _FakeCastService extends Fake implements CastService {+ bool hasActiveSession;+ int closeCount = 0;++ _FakeCastService({required this.hasActiveSession});++ @override+ bool get isSupported => true;++ @override+ Map<String, String> getActiveSessions() =>+ hasActiveSession ? {"device": "connected"} : {};++ @override+ Future<void> closeActiveCasts() async {+ closeCount++;+ hasActiveSession = false;+ }+}++class _FakeCollection extends Fake implements Collection {}