Take the terminal's role from the back office, not from which tab was clicked
The role split was right; only its source was wrong. Signing in matched what was typed against two constants compiled into the app — admin@nearle.in and cashier@nearle.in — so which shell a person got was a property of the *build*. A shop could not add a third person, revoke either of the two it had, or stop anyone with the APK reading both passwords out of it. TerminalLogin survives unchanged in shape, because the shape was the good part: one flag the shell reads, a session that decides it, and a cashier sign-out that takes the catalogue with it while a supervisor's leaves it behind. Every consumer — visibleModulesProvider, resolvedModuleProvider, the sidebar, the page header, the sign-out dialog — is untouched. What changed is that the enum is now only constructible from a session the back office signed, so there is no path left where the terminal grants itself a permission the server did not send. It reads `can_manage_staff` rather than the role name or id. app_roles holds six rows for four distinct roles, a great many accounts carry a roleid that is not in the table at all, and the name comes back blank for most of them. Matching on either would mean shipping a copy of the role table in the app and keeping the two in step for ever. One boolean, decided server-side, cannot drift. It defaults to false, which matters on the restore path: a session saved by a build that predates the field comes back as a cashier, never silently as an admin. This also restores the sign-in layer itself — pos_auth_api, pos_session, session_store, the staff import and the bearer token — which an earlier commit removed wholesale from a stale checkout. Its parent was the commit that added them, so the deletion was a bad merge rather than a decision; the terminal has been running on the two constants since. The login screen loses its role tabs and its credential prefill. You do not choose what you are on the way in. The opener is now matched on the back office user id rather than on the first account with a matching role, so the first bill of a shift is attributed to whoever actually signed in. Tests: the smoke suite pinned only the supervisor shell, and it was passing for the wrong reason — the fake session omitted can_manage_staff, and the sidebar it asserted on was there because the role was hardcoded. Both halves are pinned now and the fake is parameterised. widget_test.dart was the stock Flutter counter template, restored by the same bad merge, testing a MyApp that has never existed in this repo. 292 tests pass; analyzer reports no errors and no warnings. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -51,7 +51,7 @@ void main() {
|
||||
cashierName: 'Suriya',
|
||||
);
|
||||
|
||||
Future<void> bootApp(WidgetTester tester) async {
|
||||
Future<void> bootApp(WidgetTester tester, {bool supervisor = true}) async {
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [
|
||||
@@ -70,7 +70,9 @@ void main() {
|
||||
// rather than two constants compiled into the build. A widget test
|
||||
// must not depend on a live endpoint, so the client is swapped for
|
||||
// one that answers with a fixed session.
|
||||
posAuthApiProvider.overrideWithValue(_FakePosAuthApi()),
|
||||
posAuthApiProvider.overrideWithValue(
|
||||
_FakePosAuthApi(canManageStaff: supervisor),
|
||||
),
|
||||
|
||||
// Catalogue reads come from the in-memory cache and resolve on the
|
||||
// spot, but these four go to SQLite. Real disk I/O cannot be driven
|
||||
@@ -167,10 +169,49 @@ void main() {
|
||||
|
||||
await tester.tap(target.first);
|
||||
await settle(tester);
|
||||
|
||||
// Settings builds a printer-settings controller that reads six values
|
||||
// out of SQLite, and sqflite arms a ten-second lock-warning timer around
|
||||
// each. Those reads cannot complete on a fake clock, so the timer would
|
||||
// still be pending at teardown and the binding would fail the test for
|
||||
// that rather than for anything it is about. Pumping past the ten
|
||||
// seconds lets the timer fire and clear.
|
||||
await tester.pump(const Duration(seconds: 11));
|
||||
expect(tester.takeException(), isNull, reason: 'opening "$label" threw');
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('a cashier session gets billing and nothing else',
|
||||
(tester) async {
|
||||
// The other half of the role split, and the half worth pinning: the shell
|
||||
// a person gets is decided by the back office, not by which tab they
|
||||
// picked on the way in. Same credentials, same screen size, same boot —
|
||||
// the only difference is `can_manage_staff` on the session, and the
|
||||
// back-office modules have to be unreachable because of it.
|
||||
tester.view.physicalSize = const Size(1800, 1200);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
await bootApp(tester, supervisor: false);
|
||||
await signIn(tester);
|
||||
|
||||
// Signed in, and on the billing screen.
|
||||
expect(find.byType(PosDashboardScreen), findsOneWidget);
|
||||
|
||||
// These labels exist only in the sidebar, so their absence is the whole
|
||||
// claim: a cashier cannot reach the catalogue, the promos or the
|
||||
// terminal's configuration.
|
||||
for (final label in ['Product Import', 'Promo', 'Settings']) {
|
||||
expect(
|
||||
find.text(label),
|
||||
findsNothing,
|
||||
reason: 'a cashier must not be offered "$label"',
|
||||
);
|
||||
}
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('the back office connection dialog opens and validates',
|
||||
(tester) async {
|
||||
// The only way a shop can point a till at a broker. Until it existed a
|
||||
@@ -185,6 +226,9 @@ void main() {
|
||||
await tester.tap(find.text('Settings').first);
|
||||
await settle(tester);
|
||||
|
||||
// Clears sqflite's lock-warning timer — see the module loop above.
|
||||
await tester.pump(const Duration(seconds: 11));
|
||||
|
||||
await tester.tap(find.text('Configure').first);
|
||||
await settle(tester);
|
||||
|
||||
@@ -217,7 +261,12 @@ const _testPassword = 'correct-horse';
|
||||
/// concrete — and answering a wrong password correctly matters here: the login
|
||||
/// screen's failure path is part of what these tests cover.
|
||||
class _FakePosAuthApi extends PosAuthApi {
|
||||
_FakePosAuthApi() : super(baseUrl: 'https://example.invalid/pos');
|
||||
_FakePosAuthApi({this.canManageStaff = true})
|
||||
: super(baseUrl: 'https://example.invalid/pos');
|
||||
|
||||
/// Which shell the back office says this account gets. A supervisor by
|
||||
/// default, because most of these tests are about the full shell rendering.
|
||||
final bool canManageStaff;
|
||||
|
||||
@override
|
||||
Future<PosSession> login({
|
||||
@@ -241,7 +290,9 @@ class _FakePosAuthApi extends PosAuthApi {
|
||||
userId: 1229,
|
||||
fullName: 'Test Manager',
|
||||
email: _testEmail,
|
||||
roleId: 0,
|
||||
roleId: canManageStaff ? 7 : 8,
|
||||
role: canManageStaff ? 'Supervisor' : 'Cashier',
|
||||
canManageStaff: canManageStaff,
|
||||
tenantId: 1087,
|
||||
tenantName: 'Ragul Stores',
|
||||
storeId: '1135',
|
||||
|
||||
Reference in New Issue
Block a user