119 lines
2.8 KiB
Dart
119 lines
2.8 KiB
Dart
import 'package:bharat_erp/core/constants/enums.dart';
|
|
import 'package:bharat_erp/core/utils/permission_utils.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
group('Permission security', () {
|
|
test('wildcard grants all module actions', () {
|
|
expect(
|
|
hasPermission(
|
|
userPermissions: const ['*'],
|
|
module: 'assets',
|
|
action: PermissionAction.delete,
|
|
),
|
|
isTrue,
|
|
);
|
|
});
|
|
|
|
test('denies action when permission missing', () {
|
|
expect(
|
|
hasPermission(
|
|
userPermissions: const ['ASSET:read'],
|
|
module: 'assets',
|
|
action: PermissionAction.delete,
|
|
),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
test('grants read via view alias', () {
|
|
expect(
|
|
hasPermission(
|
|
userPermissions: const ['asset:view'],
|
|
module: 'assets',
|
|
action: PermissionAction.read,
|
|
),
|
|
isTrue,
|
|
);
|
|
});
|
|
|
|
test('grants update via edit alias', () {
|
|
expect(
|
|
hasPermission(
|
|
userPermissions: const ['ASSET:edit'],
|
|
module: 'assets',
|
|
action: PermissionAction.update,
|
|
),
|
|
isTrue,
|
|
);
|
|
});
|
|
|
|
test('module aliases map master_data to MASTERS', () {
|
|
expect(normalizePermissionModule('master_data'), 'MASTERS');
|
|
expect(normalizePermissionModule('assets'), 'ASSET');
|
|
});
|
|
|
|
test('super admin can see all menu modules', () {
|
|
expect(
|
|
canSeeMenuModule(
|
|
permissions: const [],
|
|
module: 'assets',
|
|
role: UserRole.superAdmin,
|
|
),
|
|
isTrue,
|
|
);
|
|
});
|
|
|
|
test('employee without permissions cannot see assets menu', () {
|
|
expect(
|
|
canSeeMenuModule(
|
|
permissions: const [],
|
|
module: 'assets',
|
|
role: UserRole.employee,
|
|
),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
test('users menu visible with roles read permission', () {
|
|
expect(
|
|
canSeeMenuModule(
|
|
permissions: const ['ROLES:read'],
|
|
module: 'users',
|
|
role: UserRole.employee,
|
|
),
|
|
isTrue,
|
|
);
|
|
});
|
|
});
|
|
|
|
group('Route preview guard logic', () {
|
|
bool shouldAllowUnauthenticatedPreview({
|
|
required bool isPreview,
|
|
required bool screenPreviewEnabled,
|
|
}) {
|
|
return isPreview && screenPreviewEnabled;
|
|
}
|
|
|
|
test('preview query blocked when screen preview disabled (production)', () {
|
|
expect(
|
|
shouldAllowUnauthenticatedPreview(
|
|
isPreview: true,
|
|
screenPreviewEnabled: false,
|
|
),
|
|
isFalse,
|
|
);
|
|
});
|
|
|
|
test('preview query allowed only in dev preview mode', () {
|
|
expect(
|
|
shouldAllowUnauthenticatedPreview(
|
|
isPreview: true,
|
|
screenPreviewEnabled: true,
|
|
),
|
|
isTrue,
|
|
);
|
|
});
|
|
});
|
|
}
|