mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Gate push toggle on backend notification method
Add a GET /api/config endpoint exposing the front-end-facing backend
configuration (currently the notification delivery method, grouped under
a nested "notifications" object so the shape can grow). The settings
screen fetches it on init and only shows the per-device push toggle when
the backend method is "push" (and the platform supports push, which keeps
it off the browser).
Also fold in related push-notifications cleanups: fix the Android app
label ("frontend" -> "OOTT") so the notification permission dialog reads
correctly, remove the completed push_notifications.md plan, and update
TODO.md.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
bef1a2987d
commit
1d85ec6f83
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:frontend/utils/oott_api.dart';
|
||||
import 'package:http_mock_adapter/http_mock_adapter.dart';
|
||||
|
||||
import '../helpers/backend_test_harness.dart';
|
||||
|
||||
void main() {
|
||||
late DioAdapter adapter;
|
||||
|
||||
setUp(() async {
|
||||
adapter = await setUpBackendForTest();
|
||||
});
|
||||
|
||||
test('getConfig GETs /config and decodes the notification method', () async {
|
||||
adapter.onGet(
|
||||
'/config',
|
||||
(server) => server.reply(200, {
|
||||
'notifications': {'method': 'push'},
|
||||
}),
|
||||
);
|
||||
|
||||
final config = await BackendAPI.instance.getConfig();
|
||||
|
||||
expect(config.notificationMethod, 'push');
|
||||
});
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:frontend/settings/settings.dart';
|
||||
import 'package:frontend/utils/pref_utils.dart';
|
||||
import 'package:frontend/utils/push_service.dart';
|
||||
import 'package:http_mock_adapter/http_mock_adapter.dart';
|
||||
|
||||
import '../helpers/backend_test_harness.dart';
|
||||
import '../helpers/pump_app.dart';
|
||||
@@ -35,8 +36,10 @@ class _FakePushService implements PushService {
|
||||
const _toggleText = 'Push notifications on this device';
|
||||
|
||||
void main() {
|
||||
late DioAdapter adapter;
|
||||
|
||||
setUp(() async {
|
||||
await setUpBackendForTest(
|
||||
adapter = await setUpBackendForTest(
|
||||
prefs: {
|
||||
'base_url': 'http://my.server/api',
|
||||
'api_key': XOR().xorEncode('topsecret'),
|
||||
@@ -48,24 +51,59 @@ void main() {
|
||||
await PrefUtil.setValue('push_enabled', false);
|
||||
});
|
||||
|
||||
testWidgets('shows the push toggle on a supported platform', (tester) async {
|
||||
// Stubs the backend config endpoint the settings screen reads on load.
|
||||
void stubNotificationMethod(String method) {
|
||||
adapter.onGet(
|
||||
'/config',
|
||||
(server) => server.reply(200, {
|
||||
'notifications': {'method': method},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Pumps frames so the async config load (resolved via Dio's zero-duration
|
||||
// timer) settles, for assertions that expect the toggle to be absent.
|
||||
Future<void> settleConfig(WidgetTester tester) async {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 10));
|
||||
}
|
||||
}
|
||||
|
||||
testWidgets('shows the push toggle when the backend method is push', (
|
||||
tester,
|
||||
) async {
|
||||
stubNotificationMethod('push');
|
||||
await pumpScreen(tester, Settings(pushService: _FakePushService()));
|
||||
await pumpUntilFound(tester, find.text(_toggleText));
|
||||
expect(find.text(_toggleText), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('hides the push toggle when push is unsupported', (tester) async {
|
||||
stubNotificationMethod('push');
|
||||
await pumpScreen(
|
||||
tester,
|
||||
Settings(pushService: _FakePushService(supported: false)),
|
||||
);
|
||||
await settleConfig(tester);
|
||||
expect(find.text(_toggleText), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('hides the push toggle when the backend method is not push', (
|
||||
tester,
|
||||
) async {
|
||||
stubNotificationMethod('pushover');
|
||||
await pumpScreen(tester, Settings(pushService: _FakePushService()));
|
||||
await settleConfig(tester);
|
||||
expect(find.text(_toggleText), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('enabling push registers and shows a success message', (
|
||||
tester,
|
||||
) async {
|
||||
stubNotificationMethod('push');
|
||||
final service = _FakePushService(enableResult: true);
|
||||
await pumpScreen(tester, Settings(pushService: service));
|
||||
await pumpUntilFound(tester, find.byType(SwitchListTile));
|
||||
|
||||
await tester.tap(find.byType(SwitchListTile));
|
||||
await pumpUntilFound(
|
||||
@@ -75,31 +113,38 @@ void main() {
|
||||
|
||||
expect(service.enableCalls, 1);
|
||||
expect(PrefUtil.getValue('push_enabled', false), isTrue);
|
||||
expect(tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value, isTrue);
|
||||
expect(
|
||||
tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value,
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a declined permission leaves the toggle off with an error', (
|
||||
tester,
|
||||
) async {
|
||||
stubNotificationMethod('push');
|
||||
final service = _FakePushService(enableResult: false);
|
||||
await pumpScreen(tester, Settings(pushService: service));
|
||||
await pumpUntilFound(tester, find.byType(SwitchListTile));
|
||||
|
||||
await tester.tap(find.byType(SwitchListTile));
|
||||
await pumpUntilFound(
|
||||
tester,
|
||||
find.textContaining('Could not enable push'),
|
||||
);
|
||||
await pumpUntilFound(tester, find.textContaining('Could not enable push'));
|
||||
|
||||
expect(service.enableCalls, 1);
|
||||
expect(tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value, isFalse);
|
||||
expect(
|
||||
tester.widget<SwitchListTile>(find.byType(SwitchListTile)).value,
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('disabling push unregisters and shows a success message', (
|
||||
tester,
|
||||
) async {
|
||||
stubNotificationMethod('push');
|
||||
await PrefUtil.setValue('push_enabled', true);
|
||||
final service = _FakePushService();
|
||||
await pumpScreen(tester, Settings(pushService: service));
|
||||
await pumpUntilFound(tester, find.byType(SwitchListTile));
|
||||
|
||||
// Starts on because the stored intent is enabled.
|
||||
expect(
|
||||
|
||||
@@ -9,17 +9,27 @@ import '../helpers/pump_app.dart';
|
||||
|
||||
void main() {
|
||||
setUp(() async {
|
||||
await setUpBackendForTest(
|
||||
final adapter = await setUpBackendForTest(
|
||||
prefs: {
|
||||
'base_url': 'http://my.server/api',
|
||||
'api_key': XOR().xorEncode('topsecret'),
|
||||
'theme': 'catppuccin_mocha',
|
||||
},
|
||||
);
|
||||
// Settings loads the backend config on init; these tests don't exercise the
|
||||
// push toggle, so report a non-push method to keep it hidden.
|
||||
adapter.onGet(
|
||||
'/config',
|
||||
(server) => server.reply(200, {
|
||||
'notifications': {'method': 'none'},
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('prefills the form from stored preferences', (tester) async {
|
||||
await pumpScreen(tester, const Settings());
|
||||
// Let the on-init config request resolve so its timer doesn't leak.
|
||||
await tester.pump(const Duration(milliseconds: 10));
|
||||
|
||||
expect(find.text('http://my.server/api'), findsOneWidget);
|
||||
expect(find.text('Catppuccin Mocha'), findsOneWidget);
|
||||
@@ -78,6 +88,7 @@ void main() {
|
||||
) async {
|
||||
await PrefUtil.setValue('base_url', '');
|
||||
await pumpScreen(tester, const Settings());
|
||||
await tester.pump(const Duration(milliseconds: 10));
|
||||
|
||||
expect(find.textContaining('Welcome to OOTT'), findsOneWidget);
|
||||
});
|
||||
@@ -87,6 +98,7 @@ void main() {
|
||||
) async {
|
||||
await PrefUtil.setValue('base_url', 'http://my.server/api');
|
||||
await pumpScreen(tester, const Settings());
|
||||
await tester.pump(const Duration(milliseconds: 10));
|
||||
|
||||
expect(find.textContaining('Welcome to OOTT'), findsNothing);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user