|
| 1 | +// Copyright 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 | +import { Helper, newModel } from '../../src'; |
| 16 | + |
| 17 | +test('test Helper.loadPolicyLine', async () => { |
| 18 | + const m = newModel(); |
| 19 | + m.loadModelFromText(` |
| 20 | +[request_definition] |
| 21 | +r = sub, obj, act |
| 22 | +
|
| 23 | +[policy_definition] |
| 24 | +p = sub, obj, act |
| 25 | +
|
| 26 | +[policy_effect] |
| 27 | +e = some(where (p.eft == allow)) |
| 28 | +
|
| 29 | +[matchers] |
| 30 | +m = r.sub == p.sub && r.obj == p.obj && r.act == p.act |
| 31 | + `); |
| 32 | + |
| 33 | + const testdata = [ |
| 34 | + 'p, admin, /, GET', |
| 35 | + '# test comment 1', |
| 36 | + ' # test comment 2', |
| 37 | + `p, "admin", /, POST`, |
| 38 | + `"p", "admin", "/", "PUT"`, |
| 39 | + `"p","admin",/, "DELETE"`, |
| 40 | + `p, " admin","/ ", "PATCH"`, |
| 41 | + ]; |
| 42 | + |
| 43 | + const expectedPolicy = [ |
| 44 | + ['admin', '/', 'GET'], |
| 45 | + ['admin', '/', 'POST'], |
| 46 | + ['admin', '/', 'PUT'], |
| 47 | + ['admin', '/', 'DELETE'], |
| 48 | + [' admin', '/ ', 'PATCH'], |
| 49 | + ]; |
| 50 | + |
| 51 | + testdata.forEach((n) => { |
| 52 | + Helper.loadPolicyLine(n, m); |
| 53 | + }); |
| 54 | + |
| 55 | + const ast = m.model.get('p')?.get('p'); |
| 56 | + expect(ast !== null && ast !== undefined).toBe(true); |
| 57 | + expect(ast?.policy?.length === expectedPolicy.length).toBe(true); |
| 58 | + expect(ast?.policy).toEqual(expectedPolicy); |
| 59 | +}); |
0 commit comments