Skip to content

Commit 2c96f16

Browse files
feat(policy_manager): add tests for handling non-Map policy values in JSON assets
1 parent e95a8d1 commit 2c96f16

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

test/core/policy_manager_test.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,82 @@ void main() {
570570
.setMockMessageHandler('flutter/assets', null);
571571
});
572572

573+
test('should handle non-Map policy values in JSON asset', () async {
574+
// Mock the rootBundle to return JSON with non-Map policy values
575+
const nonMapJson = '''
576+
{
577+
"admin": "not_a_map",
578+
"user": 123,
579+
"guest": ["read", "write"],
580+
"valid_role": {
581+
"allowedContent": ["read"]
582+
}
583+
}
584+
''';
585+
586+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
587+
.setMockMessageHandler('flutter/assets', (ByteData? message) async {
588+
return ByteData.view(
589+
Uint8List.fromList(utf8.encode(nonMapJson)).buffer);
590+
});
591+
592+
await policyManager
593+
.initializeFromJsonAssets('assets/policies/non_map.json');
594+
595+
// Should initialize with only valid policies
596+
expect(policyManager.isInitialized, isTrue);
597+
expect(policyManager.roles.length, equals(1));
598+
expect(policyManager.roles['valid_role'], isNotNull);
599+
expect(policyManager.roles['valid_role']!.name, equals('valid_role'));
600+
expect(policyManager.roles['valid_role']!.allowedContent,
601+
contains('read'));
602+
expect(policyManager.roles['admin'], isNull);
603+
expect(policyManager.roles['user'], isNull);
604+
expect(policyManager.roles['guest'], isNull);
605+
606+
// Clean up mock message handler
607+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
608+
.setMockMessageHandler('flutter/assets', null);
609+
});
610+
611+
test('should execute warning log for non-Map values', () async {
612+
// Mock the rootBundle to return JSON with non-Map policy values
613+
const nonMapJson = '''
614+
{
615+
"admin": "not_a_map",
616+
"user": 123,
617+
"guest": ["read", "write"],
618+
"valid_role": {
619+
"allowedContent": ["read"]
620+
}
621+
}
622+
''';
623+
624+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
625+
.setMockMessageHandler('flutter/assets', (ByteData? message) async {
626+
return ByteData.view(
627+
Uint8List.fromList(utf8.encode(nonMapJson)).buffer);
628+
});
629+
630+
await policyManager
631+
.initializeFromJsonAssets('assets/policies/non_map.json');
632+
633+
// Should initialize with only valid policies
634+
expect(policyManager.isInitialized, isTrue);
635+
expect(policyManager.roles.length, equals(1));
636+
expect(policyManager.roles['valid_role'], isNotNull);
637+
expect(policyManager.roles['valid_role']!.name, equals('valid_role'));
638+
expect(policyManager.roles['valid_role']!.allowedContent,
639+
contains('read'));
640+
expect(policyManager.roles['admin'], isNull);
641+
expect(policyManager.roles['user'], isNull);
642+
expect(policyManager.roles['guest'], isNull);
643+
644+
// Clean up mock message handler
645+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
646+
.setMockMessageHandler('flutter/assets', null);
647+
});
648+
573649
test('should handle empty JSON object in asset', () async {
574650
// Mock the rootBundle to return empty JSON object
575651
const emptyJson = '{}';

0 commit comments

Comments
 (0)