|
| 1 | +import assert from 'assert'; |
| 2 | +import * as PermifyApi from '../src/index'; |
| 3 | + |
| 4 | +const PERMIFY_BASE_URL = process.env.PERMIFY_BASE_URL || 'http://127.0.0.1:3476'; |
| 5 | +const TENANT_ID = 't1'; |
| 6 | +const RUN_ID = Date.now().toString(36); |
| 7 | + |
| 8 | +function createApis() { |
| 9 | + const apiClient = new PermifyApi.ApiClient(PERMIFY_BASE_URL); |
| 10 | + apiClient.timeout = 10000; |
| 11 | + |
| 12 | + return { |
| 13 | + schema: new PermifyApi.SchemaApi(apiClient), |
| 14 | + data: new PermifyApi.DataApi(apiClient), |
| 15 | + permission: new PermifyApi.PermissionApi(apiClient), |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +describe('Permify live REST tests', function() { |
| 20 | + this.timeout(20000); |
| 21 | + |
| 22 | + it('denies a permission check when no relationship exists', async function() { |
| 23 | + const apis = createApis(); |
| 24 | + const documentId = `denieddoc${RUN_ID}`; |
| 25 | + const subjectId = `denieduser${RUN_ID}`; |
| 26 | + const schema = ` |
| 27 | + entity user {} |
| 28 | +
|
| 29 | + entity document { |
| 30 | + relation viewer @user |
| 31 | +
|
| 32 | + action view = viewer |
| 33 | + } |
| 34 | + `; |
| 35 | + |
| 36 | + const schemaWrite = await apis.schema.schemasWrite(TENANT_ID, { |
| 37 | + schema, |
| 38 | + }); |
| 39 | + |
| 40 | + const check = await apis.permission.permissionsCheck(TENANT_ID, { |
| 41 | + metadata: { |
| 42 | + snap_token: '', |
| 43 | + schema_version: schemaWrite.schema_version, |
| 44 | + depth: 20, |
| 45 | + }, |
| 46 | + entity: { |
| 47 | + type: 'document', |
| 48 | + id: documentId, |
| 49 | + }, |
| 50 | + permission: 'view', |
| 51 | + subject: { |
| 52 | + type: 'user', |
| 53 | + id: subjectId, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + assert.strictEqual(check.can, 'CHECK_RESULT_DENIED'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('looks up entities a subject can view', async function() { |
| 61 | + const apis = createApis(); |
| 62 | + const subjectId = `lookupuser${RUN_ID}`; |
| 63 | + const expectedEntityIds = [ |
| 64 | + `lookupdoca${RUN_ID}`, |
| 65 | + `lookupdocb${RUN_ID}`, |
| 66 | + `lookupdocc${RUN_ID}`, |
| 67 | + ]; |
| 68 | + const schema = ` |
| 69 | + entity user {} |
| 70 | +
|
| 71 | + entity document { |
| 72 | + relation viewer @user |
| 73 | +
|
| 74 | + action view = viewer |
| 75 | + } |
| 76 | + `; |
| 77 | + |
| 78 | + const schemaWrite = await apis.schema.schemasWrite(TENANT_ID, { |
| 79 | + schema, |
| 80 | + }); |
| 81 | + |
| 82 | + const dataWrite = await apis.data.dataWrite(TENANT_ID, { |
| 83 | + metadata: { |
| 84 | + schema_version: schemaWrite.schema_version, |
| 85 | + }, |
| 86 | + tuples: [ |
| 87 | + { |
| 88 | + entity: { |
| 89 | + type: 'document', |
| 90 | + id: expectedEntityIds[0], |
| 91 | + }, |
| 92 | + relation: 'viewer', |
| 93 | + subject: { |
| 94 | + type: 'user', |
| 95 | + id: subjectId, |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + entity: { |
| 100 | + type: 'document', |
| 101 | + id: expectedEntityIds[1], |
| 102 | + }, |
| 103 | + relation: 'viewer', |
| 104 | + subject: { |
| 105 | + type: 'user', |
| 106 | + id: subjectId, |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + entity: { |
| 111 | + type: 'document', |
| 112 | + id: expectedEntityIds[2], |
| 113 | + }, |
| 114 | + relation: 'viewer', |
| 115 | + subject: { |
| 116 | + type: 'user', |
| 117 | + id: subjectId, |
| 118 | + }, |
| 119 | + }, |
| 120 | + ], |
| 121 | + }); |
| 122 | + |
| 123 | + const lookup = await apis.permission.permissionsLookupEntity(TENANT_ID, { |
| 124 | + metadata: { |
| 125 | + snap_token: dataWrite.snap_token, |
| 126 | + schema_version: schemaWrite.schema_version, |
| 127 | + depth: 20, |
| 128 | + }, |
| 129 | + entity_type: 'document', |
| 130 | + permission: 'view', |
| 131 | + subject: { |
| 132 | + type: 'user', |
| 133 | + id: subjectId, |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + assert.deepStrictEqual(lookup.entity_ids.sort(), expectedEntityIds.sort()); |
| 138 | + }); |
| 139 | +}); |
0 commit comments