|
1 | 1 | const test = require('ava'); |
| 2 | +const goodSchema = require('./exampleSchemas/goodSchema'); |
| 3 | +const bareBonesSchema = require('./exampleSchemas/bareBonesSchema'); |
| 4 | +const getAuthorizedFields = require('../src/getAuthorizedFields'); |
2 | 5 |
|
3 | | -test.todo('Migrate tests for getAuthorizedFields'); |
| 6 | +test('No authorized fields', (t) => { |
| 7 | + t.deepEqual( |
| 8 | + getAuthorizedFields(bareBonesSchema, 'foobar', 'read'), |
| 9 | + [], |
| 10 | + ); |
| 11 | +}); |
| 12 | + |
| 13 | +test('Handles non-existent authLevels', (t) => { |
| 14 | + t.deepEqual( |
| 15 | + getAuthorizedFields(goodSchema, ['defaults', 'foobar'], 'read').sort(), |
| 16 | + ['_id', 'name'].sort(), |
| 17 | + ); |
| 18 | +}); |
| 19 | + |
| 20 | +test('Combines basic authLevel permissions', (t) => { |
| 21 | + t.deepEqual( |
| 22 | + getAuthorizedFields(goodSchema, ['defaults', 'admin'], 'read').sort(), |
| 23 | + ['_id', 'name', 'address', 'phone', 'birthday'].sort(), |
| 24 | + ); |
| 25 | +}); |
| 26 | + |
| 27 | +test('Handles authLevels that have no permissions specified', (t) => { |
| 28 | + t.deepEqual( |
| 29 | + getAuthorizedFields(goodSchema, ['defaults', 'stranger'], 'read').sort(), |
| 30 | + ['_id', 'name'].sort(), |
| 31 | + ); |
| 32 | +}); |
| 33 | + |
| 34 | +test('Correctly deduping fields that are mentioned in multiple authLevels', (t) => { |
| 35 | + t.deepEqual( |
| 36 | + getAuthorizedFields(goodSchema, ['defaults', 'self', 'admin'], 'read').sort(), |
| 37 | + ['_id', 'name', 'address', 'phone', 'birthday'].sort(), |
| 38 | + ); |
| 39 | +}); |
| 40 | + |
| 41 | +test('Handles basic write permissions', (t) => { |
| 42 | + t.deepEqual( |
| 43 | + getAuthorizedFields(goodSchema, ['defaults', 'self'], 'write').sort(), |
| 44 | + ['address', 'phone'].sort(), |
| 45 | + ); |
| 46 | +}); |
| 47 | + |
| 48 | +test('Handles authorized fields that are not in the schema', (t) => { |
| 49 | + t.deepEqual( |
| 50 | + getAuthorizedFields(bareBonesSchema, 'admin', 'write'), |
| 51 | + [], |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +test('Virtual fields are correctly returned', (t) => { |
| 56 | + t.deepEqual( |
| 57 | + getAuthorizedFields(goodSchema, ['defaults', 'hasVirtuals'], 'read').sort(), |
| 58 | + ['_id', 'name', 'virtual_name'].sort(), |
| 59 | + ); |
| 60 | +}); |
| 61 | + |
| 62 | +test('Top level nested field should be ok as authorized field', (t) => { |
| 63 | + t.deepEqual( |
| 64 | + getAuthorizedFields(goodSchema, ['defaults', 'nested_top'], 'read').sort(), |
| 65 | + ['_id', 'name', 'nested'].sort(), |
| 66 | + 'top level nested field should be ok as authorized field', |
| 67 | + ); |
| 68 | +}); |
| 69 | +test('Deeply nested field should be ok as authorized field', (t) => { |
| 70 | + t.deepEqual( |
| 71 | + getAuthorizedFields(goodSchema, ['defaults', 'nested_deep'], 'read').sort(), |
| 72 | + ['_id', 'name', 'nested.thing'].sort(), |
| 73 | + 'deeply nested field should be ok as authorized field', |
| 74 | + ); |
| 75 | +}); |
0 commit comments