cyberhybridhub/test/admin/widgets/admin_app_bar_action_test.dart
2026-05-31 11:17:12 -05:00

86 lines
2.5 KiB
Dart

import 'package:cyberhybridhub/admin/services/admin_access_service.dart';
import 'package:cyberhybridhub/admin/services/market_history_admin_api.dart';
import 'package:cyberhybridhub/admin/widgets/admin_app_bar_action.dart';
import 'package:cyberhybridhub/theme/app_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
MarketHistoryAdminApi _authorizedApi() {
return MarketHistoryAdminApi(
tokenProvider: () async => 'token',
client: MockClient(
(http.Request request) async => http.Response('{"runs":[],"pinned":[]}', 200),
),
);
}
MarketHistoryAdminApi _forbiddenApi() {
return MarketHistoryAdminApi(
tokenProvider: () async => 'token',
client: MockClient(
(http.Request request) async => http.Response('forbidden', 403),
),
);
}
void main() {
setUp(AdminAccessService.instance.reset);
testWidgets('shows admin icon when allowlisted user probe succeeds', (
WidgetTester tester,
) async {
await tester.pumpWidget(
MaterialApp(
theme: buildAppTheme(),
home: Scaffold(
appBar: AppBar(actions: <Widget>[AdminAppBarAction(api: _authorizedApi())]),
),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('admin-app-bar-action')), findsOneWidget);
expect(find.byIcon(Icons.admin_panel_settings_outlined), findsOneWidget);
});
testWidgets('hides admin icon when user is not on allowlist', (
WidgetTester tester,
) async {
await tester.pumpWidget(
MaterialApp(
theme: buildAppTheme(),
home: Scaffold(
appBar: AppBar(actions: <Widget>[AdminAppBarAction(api: _forbiddenApi())]),
),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('admin-app-bar-action')), findsNothing);
});
testWidgets('navigates to admin portal when icon is tapped', (
WidgetTester tester,
) async {
await tester.pumpWidget(
MaterialApp(
theme: buildAppTheme(),
routes: <String, WidgetBuilder>{
'/admin/market-history': (_) => const Scaffold(body: Text('admin log')),
},
home: Scaffold(
appBar: AppBar(actions: <Widget>[AdminAppBarAction(api: _authorizedApi())]),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('admin-app-bar-action')));
await tester.pumpAndSettle();
expect(find.text('admin log'), findsOneWidget);
});
}