diff --git a/lib/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem.dart b/lib/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem.dart index 43ba8f6bf..f622465b9 100644 --- a/lib/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem.dart +++ b/lib/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem.dart @@ -13,10 +13,28 @@ class SentryConfigLinagoraEcosystem extends LinagoraEcosystemProperties { final String? dsn; final String? environment; - SentryConfigLinagoraEcosystem({this.enabled, this.dsn, this.environment}); + /// Starting position of the per-user reporting toggle, not a master switch: + /// on-prem serves `false` so nothing is sent until a user opts in. + /// Falls back to [enabled] so deployments without this key are unaffected. + @JsonKey(fromJson: _parseBool) + final bool? userOptInByDefault; - factory SentryConfigLinagoraEcosystem.fromJson(Map json) => - _$SentryConfigLinagoraEcosystemFromJson(json); + SentryConfigLinagoraEcosystem({ + this.enabled, + this.dsn, + this.environment, + this.userOptInByDefault, + }); + + factory SentryConfigLinagoraEcosystem.fromJson(Map json) { + final generated = _$SentryConfigLinagoraEcosystemFromJson(json); + return SentryConfigLinagoraEcosystem( + enabled: generated.enabled, + dsn: generated.dsn, + environment: generated.environment, + userOptInByDefault: _parseBool(json['userOptInByDefault']), + ); + } static bool? _parseBool(dynamic value) { if (value == null) return null; @@ -25,7 +43,13 @@ class SentryConfigLinagoraEcosystem extends LinagoraEcosystemProperties { return null; } - Map toJson() => _$SentryConfigLinagoraEcosystemToJson(this); + Map toJson() { + final json = _$SentryConfigLinagoraEcosystemToJson(this); + if (userOptInByDefault != null) { + json['userOptInByDefault'] = userOptInByDefault; + } + return json; + } static LinagoraEcosystemProperties? deserialize(dynamic json) { if (json is Map) { @@ -35,12 +59,16 @@ class SentryConfigLinagoraEcosystem extends LinagoraEcosystemProperties { } } + bool get isUserOptedInByDefault => userOptInByDefault ?? enabled ?? false; + @override - List get props => [enabled, dsn, environment]; + List get props => [enabled, dsn, environment, userOptInByDefault]; } extension SentryConfigLinagoraEcosystemExtension on SentryConfigLinagoraEcosystem { - Future toSentryConfig() async { + Future toSentryConfig({ + bool isReportingAllowed = false, + }) async { const dartDefineRelease = String.fromEnvironment('SENTRY_RELEASE'); final release = dartDefineRelease.isNotEmpty ? dartDefineRelease @@ -51,6 +79,7 @@ extension SentryConfigLinagoraEcosystemExtension on SentryConfigLinagoraEcosyste environment: environment ?? '', release: release, isAvailable: enabled ?? false, + isReportingAllowed: isReportingAllowed, ); } -} \ No newline at end of file +} diff --git a/lib/features/mailbox_dashboard/presentation/sentry_ecosystem.dart b/lib/features/mailbox_dashboard/presentation/sentry_ecosystem.dart index 07d4b4373..3fe78370b 100644 --- a/lib/features/mailbox_dashboard/presentation/sentry_ecosystem.dart +++ b/lib/features/mailbox_dashboard/presentation/sentry_ecosystem.dart @@ -65,12 +65,24 @@ class SentryEcosystem implements SentrySessionCleanup { } final sentryUser = _sentryUser; - final sentryConfig = await ecosystemConfig.toSentryConfig(); + SentryManager.instance + .setSentryReportingDefault(ecosystemConfig.isUserOptedInByDefault); + + final sentryConfig = await ecosystemConfig.toSentryConfig( + isReportingAllowed: + SentryManager.instance.isSentryReportingAllowed, + ); if (!_isCurrentConfiguration(configurationGeneration)) return; await _initializeSentry(sentryConfig); if (!_isCurrentConfiguration(configurationGeneration)) return; + // The SDK initializer receives the effective consent so it can start or + // stay stopped, but that value must not replace the instance default. + // Otherwise clearing an explicit user choice cannot restore the default. + SentryManager.instance + .setSentryReportingDefault(ecosystemConfig.isUserOptedInByDefault); + _applyUser(sentryUser); final configToPersist = sentryConfig.withReportingAllowed( diff --git a/test/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem_test.dart b/test/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem_test.dart new file mode 100644 index 000000000..b550b3730 --- /dev/null +++ b/test/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem_test.dart @@ -0,0 +1,54 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:tmail_ui_user/features/mailbox_dashboard/domain/linagora_ecosystem/sentry_config_linagora_ecosystem.dart'; + +void main() { + group('SentryConfigLinagoraEcosystem.isUserOptedInByDefault', () { + test('uses userOptInByDefault when the instance serves it', () { + final config = SentryConfigLinagoraEcosystem.fromJson({ + 'enabled': 'true', + 'userOptInByDefault': 'false', + }); + + expect(config.isUserOptedInByDefault, isFalse); + }); + + test('opts users in when the instance serves userOptInByDefault true', () { + final config = SentryConfigLinagoraEcosystem.fromJson({ + 'enabled': 'true', + 'userOptInByDefault': 'true', + }); + + expect(config.isUserOptedInByDefault, isTrue); + }); + + test('falls back to enabled so deployments without the key are unaffected', () { + final config = SentryConfigLinagoraEcosystem.fromJson({'enabled': 'true'}); + + expect(config.isUserOptedInByDefault, isTrue); + }); + + test('defaults to opted out when neither key is served', () { + final config = SentryConfigLinagoraEcosystem.fromJson({}); + + expect(config.isUserOptedInByDefault, isFalse); + }); + + test('accepts booleans as well as strings', () { + final config = SentryConfigLinagoraEcosystem.fromJson({ + 'enabled': true, + 'userOptInByDefault': false, + }); + + expect(config.isUserOptedInByDefault, isFalse); + }); + + test('serializes userOptInByDefault when it is served', () { + final config = SentryConfigLinagoraEcosystem( + enabled: true, + userOptInByDefault: false, + ); + + expect(config.toJson()['userOptInByDefault'], isFalse); + }); + }); +} diff --git a/test/features/mailbox_dashboard/presentation/sentry_ecosystem_test.dart b/test/features/mailbox_dashboard/presentation/sentry_ecosystem_test.dart index 5de0010fe..d9e8c72cc 100644 --- a/test/features/mailbox_dashboard/presentation/sentry_ecosystem_test.dart +++ b/test/features/mailbox_dashboard/presentation/sentry_ecosystem_test.dart @@ -147,6 +147,7 @@ void main() { enabled: true, dsn: 'https://test@sentry.io/123', environment: 'test', + userOptInByDefault: false, ); setUp(() { @@ -162,6 +163,50 @@ void main() { }); test('persists the latest consent when it changes while Sentry initializes', () async { + final optedInByDefaultConfig = SentryConfigLinagoraEcosystem( + enabled: true, + dsn: 'https://test@sentry.io/123', + environment: 'test', + userOptInByDefault: true, + ); + final cacheManager = _FakeCacheManager( + configuration: _configuration(isReportingAllowed: false), + user: _user('account-a'), + ); + SentryConfig? initializedConfig; + final initializeStarted = Completer(); + final finishInitialize = Completer(); + final ecosystem = SentryEcosystem( + cacheManager, + null, + initializeSentry: (config) async { + initializedConfig = config; + initializeStarted.complete(); + await finishInitialize.future; + }, + )..initUser(SentryUser(id: 'account-b')); + + final setUp = ecosystem.setUp(optedInByDefaultConfig); + await initializeStarted.future; + await ecosystem.updateReportingConsent(false); + + expect(cacheManager.configuration.isReportingAllowed, isFalse); + expect(cacheManager.user.id, 'account-a'); + + finishInitialize.complete(); + await setUp; + + expect(initializedConfig?.isReportingAllowed, isTrue); + expect(sentryManager.isSentryReportingAllowed, isFalse); + expect(cacheManager.user.id, 'account-b'); + expect(cacheManager.configuration.isReportingAllowed, isFalse); + expect(cacheManager.writes, [ + 'config:false', + 'user:account-b', + ]); + }); + + test('persists an opt-in that arrives while opted-out Sentry initializes', () async { final cacheManager = _FakeCacheManager( configuration: _configuration(isReportingAllowed: false), user: _user('account-a'), @@ -181,7 +226,7 @@ void main() { final setUp = ecosystem.setUp(ecosystemConfig); await initializeStarted.future; - await ecosystem.updateReportingConsent(false); + await ecosystem.updateReportingConsent(true); expect(cacheManager.configuration.isReportingAllowed, isFalse); expect(cacheManager.user.id, 'account-a'); @@ -189,13 +234,14 @@ void main() { finishInitialize.complete(); await setUp; - expect(initializedConfig?.isReportingAllowed, isTrue); - expect(sentryManager.isSentryReportingAllowed, isFalse); + expect(initializedConfig?.isReportingAllowed, isFalse); + expect(sentryManager.isSentryReportingAllowed, isTrue); expect(cacheManager.user.id, 'account-b'); - expect(cacheManager.configuration.isReportingAllowed, isFalse); + expect(cacheManager.configuration.isReportingAllowed, isTrue); expect(cacheManager.writes, [ 'config:false', 'user:account-b', + 'config:true', ]); }); @@ -269,6 +315,12 @@ void main() { }); test('restores the ecosystem default when explicit consent is cleared', () async { + final optedInByDefaultConfig = SentryConfigLinagoraEcosystem( + enabled: true, + dsn: 'https://test@sentry.io/123', + environment: 'test', + userOptInByDefault: true, + ); final cacheManager = _FakeCacheManager( configuration: _configuration(isReportingAllowed: false), user: _user('account-a'), @@ -284,9 +336,9 @@ void main() { ); sentryManager.setSentryReportingConsent(false); - await ecosystem.setUp(ecosystemConfig); + await ecosystem.setUp(optedInByDefaultConfig); - expect(initializedConfig?.isReportingAllowed, isTrue); + expect(initializedConfig?.isReportingAllowed, isFalse); expect(sentryManager.isSentryReportingAllowed, isFalse); expect(cacheManager.configuration.isReportingAllowed, isFalse); @@ -362,8 +414,32 @@ void main() { expect(cacheManager.writes, ['config:false']); }); + test('uses the ecosystem default while the user has not chosen', () async { + final cacheManager = _FakeCacheManager( + configuration: _configuration(isReportingAllowed: true), + user: _user('account-b'), + ); + SentryConfig? initializedConfig; + final ecosystem = SentryEcosystem( + cacheManager, + null, + initializeSentry: (config) async => initializedConfig = config, + )..initUser(SentryUser(id: 'account-b')); + + await ecosystem.setUp(ecosystemConfig); + + expect(sentryManager.isSentryReportingAllowed, isFalse); + expect(initializedConfig?.isReportingAllowed, isFalse); + }); + test('updates iOS Keychain when reporting consent changes', () async { debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + final optedInByDefaultConfig = SentryConfigLinagoraEcosystem( + enabled: true, + dsn: 'https://test@sentry.io/123', + environment: 'test', + userOptInByDefault: true, + ); final cacheManager = _FakeCacheManager( configuration: _configuration(isReportingAllowed: true), user: _user('account-a'), @@ -375,7 +451,7 @@ void main() { initializeSentry: (_) async {}, ); - await ecosystem.setUp(ecosystemConfig); + await ecosystem.setUp(optedInByDefaultConfig); await ecosystem.updateReportingConsent(false); final savedConfigs = verify(