|
| 1 | +import { |
| 2 | + describe, |
| 3 | + before, |
| 4 | + Joi, |
| 5 | + deps, |
| 6 | + requirements, |
| 7 | + generateInvalidDataTestCases, |
| 8 | + validateInvalidDataTestCase, |
| 9 | + getMainCheckFn |
| 10 | +} from '../deps'; |
| 11 | +const { server, request, constants, rte } = deps; |
| 12 | + |
| 13 | +// endpoint to test |
| 14 | +const endpoint = (instanceId = constants.TEST_INSTANCE_ID) => |
| 15 | + request(server).post(`/${constants.API.DATABASES}/${instanceId}/string/download-value`); |
| 16 | + |
| 17 | +// input data schema |
| 18 | +const dataSchema = Joi.object({ |
| 19 | + keyName: Joi.string().allow('').required(), |
| 20 | +}).strict(); |
| 21 | + |
| 22 | +const validInputData = { |
| 23 | + keyName: constants.TEST_STRING_KEY_1, |
| 24 | +}; |
| 25 | + |
| 26 | +const mainCheckFn = getMainCheckFn(endpoint); |
| 27 | + |
| 28 | +describe('POST /databases/:instanceId/string/download-value', () => { |
| 29 | + describe('Main', () => { |
| 30 | + before(() => rte.data.generateKeys(true)); |
| 31 | + |
| 32 | + describe('Validation', () => { |
| 33 | + generateInvalidDataTestCases(dataSchema, validInputData).map( |
| 34 | + validateInvalidDataTestCase(endpoint, dataSchema), |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('Common', () => { |
| 39 | + [ |
| 40 | + { |
| 41 | + name: 'Should download value', |
| 42 | + data: { |
| 43 | + keyName: constants.TEST_STRING_KEY_1, |
| 44 | + }, |
| 45 | + responseHeaders: { |
| 46 | + 'content-type': 'application/octet-stream', |
| 47 | + 'content-disposition': 'attachment;filename="string_value.txt"', |
| 48 | + 'access-control-expose-headers': 'Content-Disposition', |
| 49 | + }, |
| 50 | + responseBody: Buffer.from(`"${constants.TEST_STRING_VALUE_1}"`), |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'Should return an error when incorrect type', |
| 54 | + data: { |
| 55 | + keyName: constants.TEST_LIST_KEY_1, |
| 56 | + }, |
| 57 | + statusCode: 400, |
| 58 | + responseBody: { |
| 59 | + statusCode: 400, |
| 60 | + error: 'Bad Request', |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: 'Should return NotFound error if instance id does not exists', |
| 65 | + endpoint: () => endpoint(constants.TEST_NOT_EXISTED_INSTANCE_ID), |
| 66 | + data: { |
| 67 | + keyName: constants.TEST_STRING_KEY_1, |
| 68 | + }, |
| 69 | + statusCode: 404, |
| 70 | + responseBody: { |
| 71 | + statusCode: 404, |
| 72 | + error: 'Not Found', |
| 73 | + message: 'Invalid database instance id.', |
| 74 | + }, |
| 75 | + }, |
| 76 | + ].map(mainCheckFn); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('ACL', () => { |
| 80 | + requirements('rte.acl'); |
| 81 | + before(async () => rte.data.setAclUserRules('~* +@all')); |
| 82 | + |
| 83 | + [ |
| 84 | + { |
| 85 | + name: 'Should download value', |
| 86 | + endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID), |
| 87 | + data: { |
| 88 | + keyName: constants.TEST_STRING_KEY_1, |
| 89 | + }, |
| 90 | + responseHeaders: { |
| 91 | + 'content-type': 'application/octet-stream', |
| 92 | + 'content-disposition': 'attachment;filename="string_value.txt"', |
| 93 | + 'access-control-expose-headers': 'Content-Disposition', |
| 94 | + }, |
| 95 | + responseBody: Buffer.from(`"${constants.TEST_STRING_VALUE_1}"`), |
| 96 | + }, |
| 97 | + { |
| 98 | + name: 'Should throw error if no permissions for "set" command', |
| 99 | + endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID), |
| 100 | + data: { |
| 101 | + keyName: constants.getRandomString(), |
| 102 | + value: constants.getRandomString(), |
| 103 | + }, |
| 104 | + statusCode: 403, |
| 105 | + responseBody: { |
| 106 | + statusCode: 403, |
| 107 | + error: 'Forbidden', |
| 108 | + }, |
| 109 | + before: () => rte.data.setAclUserRules('~* +@all -get') |
| 110 | + }, |
| 111 | + ].map(mainCheckFn); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments