|
| 1 | +// Copyright 2024 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 { newEnforcer, Enforcer, Model } from '../src'; |
| 16 | +import { Adapter } from '../src'; |
| 17 | + |
| 18 | +// Mock adapter that tracks method calls |
| 19 | +class MockAdapter implements Adapter { |
| 20 | + public addPolicyCalled = false; |
| 21 | + public removePolicyCalled = false; |
| 22 | + public addPoliciesCalled = false; |
| 23 | + public removePoliciesCalled = false; |
| 24 | + |
| 25 | + async loadPolicy(model: Model): Promise<void> { |
| 26 | + // No-op |
| 27 | + } |
| 28 | + |
| 29 | + async savePolicy(model: Model): Promise<boolean> { |
| 30 | + throw new Error('not implemented'); |
| 31 | + } |
| 32 | + |
| 33 | + async addPolicy(sec: string, ptype: string, rule: string[]): Promise<void> { |
| 34 | + this.addPolicyCalled = true; |
| 35 | + } |
| 36 | + |
| 37 | + async removePolicy(sec: string, ptype: string, rule: string[]): Promise<void> { |
| 38 | + this.removePolicyCalled = true; |
| 39 | + } |
| 40 | + |
| 41 | + async removeFilteredPolicy(sec: string, ptype: string, fieldIndex: number, ...fieldValues: string[]): Promise<void> { |
| 42 | + throw new Error('not implemented'); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Mock adapter that also implements BatchAdapter |
| 47 | +class MockBatchAdapter extends MockAdapter { |
| 48 | + async addPolicies(sec: string, ptype: string, rules: string[][]): Promise<void> { |
| 49 | + this.addPoliciesCalled = true; |
| 50 | + } |
| 51 | + |
| 52 | + async removePolicies(sec: string, ptype: string, rules: string[][]): Promise<void> { |
| 53 | + this.removePoliciesCalled = true; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +describe('Self* methods should not call adapter', () => { |
| 58 | + let e: Enforcer; |
| 59 | + let adapter: MockBatchAdapter; |
| 60 | + |
| 61 | + beforeEach(async () => { |
| 62 | + adapter = new MockBatchAdapter(); |
| 63 | + e = await newEnforcer('examples/rbac_model.conf', adapter); |
| 64 | + |
| 65 | + // Load initial policy manually |
| 66 | + await e.addPolicy('alice', 'data1', 'read'); |
| 67 | + await e.addPolicy('bob', 'data2', 'write'); |
| 68 | + |
| 69 | + // Reset flags after setup |
| 70 | + adapter.addPolicyCalled = false; |
| 71 | + adapter.removePolicyCalled = false; |
| 72 | + adapter.addPoliciesCalled = false; |
| 73 | + adapter.removePoliciesCalled = false; |
| 74 | + }); |
| 75 | + |
| 76 | + test('selfAddPolicy should not call adapter', async () => { |
| 77 | + const result = await e.selfAddPolicy('p', 'p', ['charlie', 'data3', 'read']); |
| 78 | + |
| 79 | + expect(result).toBe(true); |
| 80 | + expect(adapter.addPolicyCalled).toBe(false); |
| 81 | + |
| 82 | + // Verify the policy was added to the model |
| 83 | + const hasPolicy = await e.hasPolicy('charlie', 'data3', 'read'); |
| 84 | + expect(hasPolicy).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + test('selfRemovePolicy should not call adapter', async () => { |
| 88 | + const result = await e.selfRemovePolicy('p', 'p', ['alice', 'data1', 'read']); |
| 89 | + |
| 90 | + expect(result).toBe(true); |
| 91 | + expect(adapter.removePolicyCalled).toBe(false); |
| 92 | + |
| 93 | + // Verify the policy was removed from the model |
| 94 | + const hasPolicy = await e.hasPolicy('alice', 'data1', 'read'); |
| 95 | + expect(hasPolicy).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + test('selfAddPolicies should not call adapter', async () => { |
| 99 | + const rules = [ |
| 100 | + ['charlie', 'data3', 'read'], |
| 101 | + ['david', 'data4', 'write'], |
| 102 | + ]; |
| 103 | + const result = await e.selfAddPolicies('p', 'p', rules); |
| 104 | + |
| 105 | + expect(result).toBe(true); |
| 106 | + expect(adapter.addPoliciesCalled).toBe(false); |
| 107 | + |
| 108 | + // Verify the policies were added to the model |
| 109 | + expect(await e.hasPolicy('charlie', 'data3', 'read')).toBe(true); |
| 110 | + expect(await e.hasPolicy('david', 'data4', 'write')).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + test('selfRemovePolicies should not call adapter', async () => { |
| 114 | + const rules = [ |
| 115 | + ['alice', 'data1', 'read'], |
| 116 | + ['bob', 'data2', 'write'], |
| 117 | + ]; |
| 118 | + const result = await e.selfRemovePolicies('p', 'p', rules); |
| 119 | + |
| 120 | + expect(result).toBe(true); |
| 121 | + expect(adapter.removePoliciesCalled).toBe(false); |
| 122 | + |
| 123 | + // Verify the policies were removed from the model |
| 124 | + expect(await e.hasPolicy('alice', 'data1', 'read')).toBe(false); |
| 125 | + expect(await e.hasPolicy('bob', 'data2', 'write')).toBe(false); |
| 126 | + }); |
| 127 | + |
| 128 | + test('selfUpdatePolicy should not call adapter', async () => { |
| 129 | + const result = await e.selfUpdatePolicy('p', 'p', ['alice', 'data1', 'read'], ['alice', 'data1', 'write']); |
| 130 | + |
| 131 | + expect(result).toBe(true); |
| 132 | + // Note: updatePolicy is not in our basic MockAdapter, but we verify it doesn't call addPolicy or removePolicy |
| 133 | + expect(adapter.addPolicyCalled).toBe(false); |
| 134 | + expect(adapter.removePolicyCalled).toBe(false); |
| 135 | + |
| 136 | + // Verify the policy was updated in the model |
| 137 | + expect(await e.hasPolicy('alice', 'data1', 'read')).toBe(false); |
| 138 | + expect(await e.hasPolicy('alice', 'data1', 'write')).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + test('selfRemoveFilteredPolicy should not call adapter', async () => { |
| 142 | + await e.addPolicy('alice', 'data2', 'read'); |
| 143 | + adapter.addPolicyCalled = false; |
| 144 | + |
| 145 | + const result = await e.selfRemoveFilteredPolicy('p', 'p', 0, ['alice']); |
| 146 | + |
| 147 | + expect(result).toBe(true); |
| 148 | + expect(adapter.removePolicyCalled).toBe(false); |
| 149 | + |
| 150 | + // Verify the filtered policies were removed from the model |
| 151 | + expect(await e.hasPolicy('alice', 'data1', 'read')).toBe(false); |
| 152 | + expect(await e.hasPolicy('alice', 'data2', 'read')).toBe(false); |
| 153 | + }); |
| 154 | + |
| 155 | + test('regular addPolicy should call adapter with autoSave enabled', async () => { |
| 156 | + e.enableAutoSave(true); |
| 157 | + const result = await e.addPolicy('charlie', 'data3', 'read'); |
| 158 | + |
| 159 | + expect(result).toBe(true); |
| 160 | + expect(adapter.addPolicyCalled).toBe(true); |
| 161 | + }); |
| 162 | + |
| 163 | + test('regular removePolicy should call adapter with autoSave enabled', async () => { |
| 164 | + e.enableAutoSave(true); |
| 165 | + const result = await e.removePolicy('alice', 'data1', 'read'); |
| 166 | + |
| 167 | + expect(result).toBe(true); |
| 168 | + expect(adapter.removePolicyCalled).toBe(true); |
| 169 | + }); |
| 170 | + |
| 171 | + test('self* methods work correctly even when autoSave is disabled', async () => { |
| 172 | + e.enableAutoSave(false); |
| 173 | + |
| 174 | + const addResult = await e.selfAddPolicy('p', 'p', ['charlie', 'data3', 'read']); |
| 175 | + expect(addResult).toBe(true); |
| 176 | + expect(adapter.addPolicyCalled).toBe(false); |
| 177 | + expect(await e.hasPolicy('charlie', 'data3', 'read')).toBe(true); |
| 178 | + |
| 179 | + const removeResult = await e.selfRemovePolicy('p', 'p', ['charlie', 'data3', 'read']); |
| 180 | + expect(removeResult).toBe(true); |
| 181 | + expect(adapter.removePolicyCalled).toBe(false); |
| 182 | + expect(await e.hasPolicy('charlie', 'data3', 'read')).toBe(false); |
| 183 | + }); |
| 184 | +}); |
| 185 | + |
| 186 | +describe('Self* methods with RBAC', () => { |
| 187 | + let e: Enforcer; |
| 188 | + let adapter: MockBatchAdapter; |
| 189 | + |
| 190 | + beforeEach(async () => { |
| 191 | + adapter = new MockBatchAdapter(); |
| 192 | + e = await newEnforcer('examples/rbac_model.conf', adapter); |
| 193 | + |
| 194 | + // Load initial policy manually |
| 195 | + await e.addGroupingPolicy('alice', 'admin'); |
| 196 | + await e.addPolicy('admin', 'data1', 'read'); |
| 197 | + |
| 198 | + // Reset flags after setup |
| 199 | + adapter.addPolicyCalled = false; |
| 200 | + adapter.removePolicyCalled = false; |
| 201 | + adapter.addPoliciesCalled = false; |
| 202 | + adapter.removePoliciesCalled = false; |
| 203 | + }); |
| 204 | + |
| 205 | + test('selfAddPolicy with roles should not call adapter', async () => { |
| 206 | + const result = await e.selfAddPolicy('g', 'g', ['bob', 'admin']); |
| 207 | + |
| 208 | + expect(result).toBe(true); |
| 209 | + expect(adapter.addPolicyCalled).toBe(false); |
| 210 | + |
| 211 | + // Verify the role was added |
| 212 | + const hasRole = await e.hasGroupingPolicy('bob', 'admin'); |
| 213 | + expect(hasRole).toBe(true); |
| 214 | + |
| 215 | + // Verify role links work (bob should inherit admin permissions) |
| 216 | + const enforce = await e.enforce('bob', 'data1', 'read'); |
| 217 | + expect(enforce).toBe(true); |
| 218 | + }); |
| 219 | + |
| 220 | + test('selfRemovePolicy with roles should not call adapter', async () => { |
| 221 | + const result = await e.selfRemovePolicy('g', 'g', ['alice', 'admin']); |
| 222 | + |
| 223 | + expect(result).toBe(true); |
| 224 | + expect(adapter.removePolicyCalled).toBe(false); |
| 225 | + |
| 226 | + // Verify the role was removed |
| 227 | + const hasRole = await e.hasGroupingPolicy('alice', 'admin'); |
| 228 | + expect(hasRole).toBe(false); |
| 229 | + |
| 230 | + // Verify role links were updated (alice should no longer have admin permissions) |
| 231 | + const enforce = await e.enforce('alice', 'data1', 'read'); |
| 232 | + expect(enforce).toBe(false); |
| 233 | + }); |
| 234 | +}); |
0 commit comments