|
| 1 | +// Copyright 2024 The Casdoor 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 { SDK } from '../src' |
| 16 | +import * as util from './util' |
| 17 | +import { Config } from '../src/config' |
| 18 | +import { Policy } from '../src/policy' |
| 19 | +import { Enforcer } from '../src/enforcer' |
| 20 | + |
| 21 | +test('TestPolicy', async () => { |
| 22 | + const testConfig: Config = { |
| 23 | + endpoint: util.TestCasdoorEndpoint, |
| 24 | + clientId: util.TestClientId, |
| 25 | + clientSecret: util.TestClientSecret, |
| 26 | + certificate: util.TestJwtPublicKey, |
| 27 | + orgName: util.TestCasdoorOrganization, |
| 28 | + appName: util.TestCasdoorApplication, |
| 29 | + } |
| 30 | + const sdk = new SDK(testConfig) |
| 31 | + |
| 32 | + const name = util.getRandomName('policy') |
| 33 | + |
| 34 | + const enforcer: Enforcer = { |
| 35 | + owner: 'admin', |
| 36 | + name: name, |
| 37 | + createdTime: new Date().toISOString(), |
| 38 | + displayName: name, |
| 39 | + model: 'built-in/user-model-built-in', |
| 40 | + adapter: 'built-in/user-adapter-built-in', |
| 41 | + description: 'Casdoor Website', |
| 42 | + } |
| 43 | + |
| 44 | + // Add a new object |
| 45 | + const policy: Policy = { |
| 46 | + Id: 0, |
| 47 | + Ptype: 'p', |
| 48 | + V0: '1', |
| 49 | + V1: '2', |
| 50 | + V2: '4', |
| 51 | + } |
| 52 | + |
| 53 | + const newPolicy: Policy = { |
| 54 | + Id: 0, |
| 55 | + Ptype: 'p', |
| 56 | + V0: '1', |
| 57 | + V1: '2', |
| 58 | + V2: '5', |
| 59 | + } |
| 60 | + |
| 61 | + const { data: enforcerAddResponse } = await sdk.addEnforcer(enforcer) |
| 62 | + if (enforcerAddResponse.data !== 'Affected') { |
| 63 | + throw new Error('Failed to add enforcer') |
| 64 | + } |
| 65 | + |
| 66 | + const { data: addResponse } = await sdk.addPolicy(enforcer, policy) |
| 67 | + if (addResponse.data !== 'Affected') { |
| 68 | + throw new Error('Failed to add object') |
| 69 | + } |
| 70 | + |
| 71 | + // Get all objects and check if our added object is in the list |
| 72 | + const { |
| 73 | + data: { data: policies }, |
| 74 | + } = await sdk.getPolicies(enforcer.name) |
| 75 | + const found = policies.some((item) => item.Id === 0 && item.V2 === '4') |
| 76 | + if (!found) { |
| 77 | + throw new Error('Added object not found in list') |
| 78 | + } |
| 79 | + |
| 80 | + // Update the object |
| 81 | + const { data: updateResponse } = await sdk.updatePolicy( |
| 82 | + enforcer, |
| 83 | + policy, |
| 84 | + newPolicy, |
| 85 | + ) |
| 86 | + if (updateResponse.data !== 'Affected') { |
| 87 | + throw new Error('Failed to update object') |
| 88 | + } |
| 89 | + |
| 90 | + // ValIdate the update |
| 91 | + const { |
| 92 | + data: { data: updatedPolicies }, |
| 93 | + } = await sdk.getPolicies(name) |
| 94 | + const updatedfound = updatedPolicies.some( |
| 95 | + (item) => item.Id === 0 && item.V2 === '5', |
| 96 | + ) |
| 97 | + if (!updatedfound) { |
| 98 | + throw new Error( |
| 99 | + `Failed to update object, description mismatch: ${policy.V2} != ${newPolicy.V2}`, |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + // Delete the object |
| 104 | + const { data: deleteResponse } = await sdk.deletePolicy(enforcer, newPolicy) |
| 105 | + if (deleteResponse.data !== 'Affected') { |
| 106 | + throw new Error('Failed to delete object') |
| 107 | + } |
| 108 | + |
| 109 | + // ValIdate the deletion |
| 110 | + const { |
| 111 | + data: { data: deletedPolicies }, |
| 112 | + } = await sdk.getPolicies(name) |
| 113 | + const deletedfound = deletedPolicies.some((item) => item === newPolicy) |
| 114 | + if (deletedfound) { |
| 115 | + throw new Error( |
| 116 | + `Failed to delete object, it's still retrievable ${JSON.stringify( |
| 117 | + deletedPolicies, |
| 118 | + )}`, |
| 119 | + ) |
| 120 | + } |
| 121 | +}) |
0 commit comments