|
| 1 | +// Copyright 2018-2021 The Casbin Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/// This test validates the exact scenario described in the GitHub issue |
| 16 | +/// to ensure the fix works as expected for the user's use case. |
| 17 | +
|
| 18 | +import 'package:casbin/casbin.dart'; |
| 19 | +import 'package:test/test.dart'; |
| 20 | +import 'dart:convert'; |
| 21 | + |
| 22 | +void main() { |
| 23 | + test('GitHub issue scenario - ABAC with JSON should work exactly as described', () { |
| 24 | + // Exact model from the GitHub issue |
| 25 | + final model = Model()..loadModelFromText(''' |
| 26 | +[request_definition] |
| 27 | +r = sub, obj, act |
| 28 | +
|
| 29 | +[policy_definition] |
| 30 | +p = sub, obj, act, condition |
| 31 | +
|
| 32 | +[policy_effect] |
| 33 | +e = some(where (p.eft == allow)) |
| 34 | +
|
| 35 | +[matchers] |
| 36 | +m = eval(p.condition) && r.obj == p.obj && r.act == p.act |
| 37 | +'''); |
| 38 | + |
| 39 | + final enforcer = Enforcer.initWithModelAndAdapter(model); |
| 40 | + |
| 41 | + // Exact policy from the GitHub issue |
| 42 | + final policyLine = 'p, {"age": 18}, /data1, read, r.sub.age >= 18 && r.sub.age < 60'; |
| 43 | + final parts = policyLine.split(',').map((e) => e.trim()).toList(); |
| 44 | + |
| 45 | + // Remove the 'p' prefix and add the policy |
| 46 | + enforcer.addPolicy([parts[1], parts[2], parts[3], parts[4]]); |
| 47 | + |
| 48 | + // Test with age 25 (should be allowed) |
| 49 | + String subJson = jsonEncode({"age": 25}); |
| 50 | + bool result = enforcer.enforce([subJson, '/data1', 'read']); |
| 51 | + |
| 52 | + expect(result, isTrue, reason: 'Age 25 should be allowed (18 <= 25 < 60)'); |
| 53 | + |
| 54 | + // Test with age 18 (boundary, should be allowed) |
| 55 | + subJson = jsonEncode({"age": 18}); |
| 56 | + result = enforcer.enforce([subJson, '/data1', 'read']); |
| 57 | + expect(result, isTrue, reason: 'Age 18 should be allowed (boundary condition)'); |
| 58 | + |
| 59 | + // Test with age 17 (should be denied) |
| 60 | + subJson = jsonEncode({"age": 17}); |
| 61 | + result = enforcer.enforce([subJson, '/data1', 'read']); |
| 62 | + expect(result, isFalse, reason: 'Age 17 should be denied (< 18)'); |
| 63 | + |
| 64 | + // Test with age 60 (should be denied) |
| 65 | + subJson = jsonEncode({"age": 60}); |
| 66 | + result = enforcer.enforce([subJson, '/data1', 'read']); |
| 67 | + expect(result, isFalse, reason: 'Age 60 should be denied (>= 60)'); |
| 68 | + |
| 69 | + // Test with age 59 (boundary, should be allowed) |
| 70 | + subJson = jsonEncode({"age": 59}); |
| 71 | + result = enforcer.enforce([subJson, '/data1', 'read']); |
| 72 | + expect(result, isTrue, reason: 'Age 59 should be allowed (18 <= 59 < 60)'); |
| 73 | + }); |
| 74 | +} |
0 commit comments