|
| 1 | +// Copyright 2018 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 { Enforcer, Util } from 'casbin'; |
| 16 | +import TypeORMAdapter from '../src/index'; |
| 17 | +import { connectionConfig } from './config'; |
| 18 | + |
| 19 | +test( |
| 20 | + 'TestAdapter', |
| 21 | + async () => { |
| 22 | + const a = await TypeORMAdapter.newAdapter(connectionConfig); |
| 23 | + try { |
| 24 | + // Because the DB is empty at first, |
| 25 | + // so we need to load the policy from the file adapter (.CSV) first. |
| 26 | + let e = new Enforcer(); |
| 27 | + |
| 28 | + await e.initWithFile( |
| 29 | + 'examples/acl_model.conf', |
| 30 | + 'examples/acl_policy.csv', |
| 31 | + ); |
| 32 | + |
| 33 | + // This is a trick to save the current policy to the DB. |
| 34 | + // We can't call e.savePolicy() because the adapter in the enforcer is still the file adapter. |
| 35 | + // The current policy means the policy in the Node-Casbin enforcer (aka in memory). |
| 36 | + await a.savePolicy(e.getModel()); |
| 37 | + |
| 38 | + // Clear the current policy. |
| 39 | + e.clearPolicy(); |
| 40 | + expect(await e.getPolicy()).toEqual([]); |
| 41 | + |
| 42 | + // Load the policy from DB. |
| 43 | + await a.loadPolicy(e.getModel()); |
| 44 | + expect(await e.getPolicy()).toEqual([ |
| 45 | + ['alice', 'data1', 'read'], |
| 46 | + ['bob', 'data2', 'write'], |
| 47 | + ]); |
| 48 | + |
| 49 | + // Note: you don't need to look at the above code |
| 50 | + // if you already have a working DB with policy inside. |
| 51 | + |
| 52 | + // Now the DB has policy, so we can provide a normal use case. |
| 53 | + // Create an adapter and an enforcer. |
| 54 | + // newEnforcer() will load the policy automatically. |
| 55 | + e = new Enforcer(); |
| 56 | + await e.initWithAdapter('examples/rbac_model.conf', a); |
| 57 | + expect(await e.getPolicy()).toEqual([ |
| 58 | + ['alice', 'data1', 'read'], |
| 59 | + ['bob', 'data2', 'write'], |
| 60 | + ]); |
| 61 | + |
| 62 | + // load filtered policies |
| 63 | + e.clearPolicy(); |
| 64 | + await a.loadFilteredPolicy(e.getModel(), { ptype: 'p', v0: 'alice' }); |
| 65 | + expect(await e.getFilteredNamedPolicy('p', 0, 'alice')).toEqual([ |
| 66 | + ['alice', 'data1', 'read'], |
| 67 | + ]); |
| 68 | + |
| 69 | + // Add policy to DB |
| 70 | + await a.addPolicy('', 'p', ['role', 'res', 'action']); |
| 71 | + e = new Enforcer(); |
| 72 | + await e.initWithAdapter('examples/rbac_model.conf', a); |
| 73 | + expect(await e.getPolicy()).toEqual([ |
| 74 | + ['alice', 'data1', 'read'], |
| 75 | + ['bob', 'data2', 'write'], |
| 76 | + ['role', 'res', 'action'], |
| 77 | + ]); |
| 78 | + |
| 79 | + await a.addPolicies('', 'p', [ |
| 80 | + ['role1', 'res1', 'action1'], |
| 81 | + ['role2', 'res2', 'action2'], |
| 82 | + ['role3', 'res3', 'action3'], |
| 83 | + ['role4', 'res4', 'action4'], |
| 84 | + ['role5', 'res5', 'action5'], |
| 85 | + ]); |
| 86 | + e = new Enforcer(); |
| 87 | + await e.initWithAdapter('examples/rbac_model.conf', a); |
| 88 | + expect(await e.getPolicy()).toEqual([ |
| 89 | + ['alice', 'data1', 'read'], |
| 90 | + ['bob', 'data2', 'write'], |
| 91 | + ['role', 'res', 'action'], |
| 92 | + ['role1', 'res1', 'action1'], |
| 93 | + ['role2', 'res2', 'action2'], |
| 94 | + ['role3', 'res3', 'action3'], |
| 95 | + ['role4', 'res4', 'action4'], |
| 96 | + ['role5', 'res5', 'action5'], |
| 97 | + ]); |
| 98 | + |
| 99 | + // Remove policy from DB |
| 100 | + await a.removePolicy('', 'p', ['role', 'res', 'action']); |
| 101 | + e = new Enforcer(); |
| 102 | + await e.initWithAdapter('examples/rbac_model.conf', a); |
| 103 | + expect(await e.getPolicy()).toEqual([ |
| 104 | + ['alice', 'data1', 'read'], |
| 105 | + ['bob', 'data2', 'write'], |
| 106 | + ['role1', 'res1', 'action1'], |
| 107 | + ['role2', 'res2', 'action2'], |
| 108 | + ['role3', 'res3', 'action3'], |
| 109 | + ['role4', 'res4', 'action4'], |
| 110 | + ['role5', 'res5', 'action5'], |
| 111 | + ]); |
| 112 | + |
| 113 | + await a.removePolicies('', 'p', [ |
| 114 | + ['role1', 'res1', 'action1'], |
| 115 | + ['role2', 'res2', 'action2'], |
| 116 | + ['role3', 'res3', 'action3'], |
| 117 | + ['role4', 'res4', 'action4'], |
| 118 | + ['role5', 'res5', 'action5'], |
| 119 | + ]); |
| 120 | + e = new Enforcer(); |
| 121 | + await e.initWithAdapter('examples/rbac_model.conf', a); |
| 122 | + expect(await e.getPolicy()).toEqual([ |
| 123 | + ['alice', 'data1', 'read'], |
| 124 | + ['bob', 'data2', 'write'], |
| 125 | + ]); |
| 126 | + } finally { |
| 127 | + a.close(); |
| 128 | + } |
| 129 | + }, |
| 130 | + 60 * 1000, |
| 131 | +); |
0 commit comments