mirror of
https://github.com/linagora/tmail-flutter.git
synced 2026-12-06 16:04:20 +00:00
TF-4809 Part 3: Configure Sentry reporting default
This commit is contained in:
1 parent
68fbf7144c
commit
1e783fbc38
4 files changed
+186
-15
No files matched your search
+36
-7
@@ -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<String, dynamic> json) =>
|
||||
_$SentryConfigLinagoraEcosystemFromJson(json);
|
||||
SentryConfigLinagoraEcosystem({
|
||||
this.enabled,
|
||||
this.dsn,
|
||||
this.environment,
|
||||
this.userOptInByDefault,
|
||||
});
|
||||
|
||||
factory SentryConfigLinagoraEcosystem.fromJson(Map<String, dynamic> 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<String, dynamic> toJson() => _$SentryConfigLinagoraEcosystemToJson(this);
|
||||
Map<String, dynamic> toJson() {
|
||||
final json = _$SentryConfigLinagoraEcosystemToJson(this);
|
||||
if (userOptInByDefault != null) {
|
||||
json['userOptInByDefault'] = userOptInByDefault;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
static LinagoraEcosystemProperties? deserialize(dynamic json) {
|
||||
if (json is Map<String, dynamic>) {
|
||||
@@ -35,12 +59,16 @@ class SentryConfigLinagoraEcosystem extends LinagoraEcosystemProperties {
|
||||
}
|
||||
}
|
||||
|
||||
bool get isUserOptedInByDefault => userOptInByDefault ?? enabled ?? false;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [enabled, dsn, environment];
|
||||
List<Object?> get props => [enabled, dsn, environment, userOptInByDefault];
|
||||
}
|
||||
|
||||
extension SentryConfigLinagoraEcosystemExtension on SentryConfigLinagoraEcosystem {
|
||||
Future<SentryConfig> toSentryConfig() async {
|
||||
Future<SentryConfig> 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
+54
@@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -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<void>();
|
||||
final finishInitialize = Completer<void>();
|
||||
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(
|
||||
|
||||
Reference in new issue
Block a user