Same model as DriverVault, with organizations above workspaces: - users get a role (user, admin, superadmin) and at most one organization; existing instance admins become superadmins - rights cascade: superadmins own everything, org admins own their organization's workspaces, others get their workspace role (owner > admin > member > guest) while they stay in the org - nobody grants a role above their own, acts on someone ranked above them, or changes their own role; rules live in internal/access as pure, tested functions - self-service organizations: an org-less user creates one and becomes its admin; the last admin can delete it and is demoted - endpoints for users, orgs, workspaces and members; token cache is dropped when a user's rights change - admin panel: Users and Organizations pages scoped to the caller - web app: organization page, workspace list, workspace settings with member management (EN/PL) - fix: organization name missing after login/refresh, and stale tokens redirecting public pages to sign-in Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
2.3 KiB
Dart
71 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:docwell_mobile/api/docwell_api.dart';
|
|
import 'package:docwell_mobile/main.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';
|
|
|
|
const ann = {
|
|
'id': 'u1',
|
|
'email': 'ann@docwell.test',
|
|
'name': 'Ann',
|
|
'role': 'admin',
|
|
'organization': 'o1',
|
|
'organizationName': 'Acme',
|
|
};
|
|
|
|
http.Response json(int status, Object body) => http.Response(
|
|
jsonEncode(body),
|
|
status,
|
|
headers: {'content-type': 'application/json'},
|
|
);
|
|
|
|
DocwellApi fakeApi() => DocwellApi(
|
|
'http://server.test/',
|
|
client: MockClient((req) async {
|
|
switch (req.url.path) {
|
|
case '/api/v1/auth/login':
|
|
final body = jsonDecode(req.body) as Map<String, dynamic>;
|
|
if (body['password'] == 'password123') {
|
|
return json(200, {'token': 'tok', 'user': ann});
|
|
}
|
|
return json(401, {'error': 'invalid_credentials'});
|
|
case '/api/v1/version':
|
|
return req.headers['Authorization'] == 'Bearer tok'
|
|
? json(200, {'name': 'docwell-server', 'version': '1.0.0'})
|
|
: json(401, {'error': 'unauthorized'});
|
|
}
|
|
return json(404, {'error': 'not_found'});
|
|
}),
|
|
);
|
|
|
|
Future<void> signIn(WidgetTester tester, String password) async {
|
|
await tester.enterText(find.byKey(const Key('email')), 'ann@docwell.test');
|
|
await tester.enterText(find.byKey(const Key('password')), password);
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Sign in'));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
void main() {
|
|
testWidgets('signs in, shows the user, and signs out', (tester) async {
|
|
await tester.pumpWidget(DocwellApp(api: fakeApi()));
|
|
await signIn(tester, 'password123');
|
|
|
|
expect(find.text('Hello, Ann'), findsOneWidget);
|
|
expect(find.text('Admin · Acme'), findsOneWidget);
|
|
expect(find.text('Connected · v1.0.0'), findsOneWidget);
|
|
|
|
await tester.tap(find.text('Sign out'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Sign in to Docwell'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('shows an error for a wrong password', (tester) async {
|
|
await tester.pumpWidget(DocwellApp(api: fakeApi()));
|
|
await signIn(tester, 'wrong');
|
|
expect(find.text('Wrong email or password.'), findsOneWidget);
|
|
});
|
|
}
|