|
| 1 | +import { |
| 2 | + expect, |
| 3 | + describe, |
| 4 | + it, |
| 5 | + before, |
| 6 | + deps, |
| 7 | + Joi, |
| 8 | + requirements, |
| 9 | + generateInvalidDataTestCases, |
| 10 | + validateInvalidDataTestCase, |
| 11 | + validateApiCall |
| 12 | +} from '../deps'; |
| 13 | +const { server, request, constants, rte } = deps; |
| 14 | + |
| 15 | +// endpoint to test |
| 16 | +const endpoint = (instanceId = constants.TEST_INSTANCE_ID) => |
| 17 | + request(server).delete(`/instance/${instanceId}/streams/consumer-groups/consumers`); |
| 18 | + |
| 19 | +const dataSchema = Joi.object({ |
| 20 | + keyName: Joi.string().allow('').required(), |
| 21 | + groupName: Joi.string().required().messages({ |
| 22 | + 'any.required': '{#label} should not be empty', |
| 23 | + }), |
| 24 | + consumerNames: Joi.array().items(Joi.string().required().label('consumerNames').messages({ |
| 25 | + 'any.required': '{#label} should not be empty', |
| 26 | + })).min(1).required().messages({ |
| 27 | + 'array.sparse': 'each value in consumerNames must be a string', |
| 28 | + }), |
| 29 | +}).strict(); |
| 30 | + |
| 31 | +const validInputData = { |
| 32 | + keyName: constants.TEST_STREAM_KEY_1, |
| 33 | + groupName: constants.TEST_STREAM_GROUP_1, |
| 34 | + consumerNames: [constants.TEST_STREAM_GROUP_1], |
| 35 | +}; |
| 36 | + |
| 37 | + |
| 38 | +const mainCheckFn = async (testCase) => { |
| 39 | + it(testCase.name, async () => { |
| 40 | + // additional checks before test run |
| 41 | + if (testCase.before) { |
| 42 | + await testCase.before(); |
| 43 | + } |
| 44 | + |
| 45 | + await validateApiCall({ |
| 46 | + endpoint, |
| 47 | + ...testCase, |
| 48 | + }); |
| 49 | + |
| 50 | + // additional checks after test pass |
| 51 | + if (testCase.after) { |
| 52 | + await testCase.after(); |
| 53 | + } |
| 54 | + }); |
| 55 | +}; |
| 56 | + |
| 57 | +describe('DELETE /instance/:instanceId/streams/consumer-groups/consumers', () => { |
| 58 | + before(async () => await rte.data.generateKeys(true)); |
| 59 | + |
| 60 | + describe('Validation', () => { |
| 61 | + generateInvalidDataTestCases(dataSchema, validInputData).map( |
| 62 | + validateInvalidDataTestCase(endpoint, dataSchema), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('Common', () => { |
| 67 | + beforeEach(async () => { |
| 68 | + await rte.data.sendCommand('xreadgroup', [ |
| 69 | + 'GROUP', |
| 70 | + constants.TEST_STREAM_GROUP_1, |
| 71 | + constants.TEST_STREAM_CONSUMER_1, |
| 72 | + 'STREAMS', |
| 73 | + constants.TEST_STREAM_KEY_1, |
| 74 | + constants.TEST_STREAM_ID_1, |
| 75 | + ]); |
| 76 | + await rte.data.sendCommand('xreadgroup', [ |
| 77 | + 'GROUP', |
| 78 | + constants.TEST_STREAM_GROUP_1, |
| 79 | + constants.TEST_STREAM_CONSUMER_2, |
| 80 | + 'STREAMS', |
| 81 | + constants.TEST_STREAM_KEY_1, |
| 82 | + constants.TEST_STREAM_ID_2, |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + [ |
| 87 | + { |
| 88 | + name: 'Should remove single consumer', |
| 89 | + data: { |
| 90 | + keyName: constants.TEST_STREAM_KEY_1, |
| 91 | + groupName: constants.TEST_STREAM_GROUP_1, |
| 92 | + consumerNames: [constants.TEST_STREAM_CONSUMER_1], |
| 93 | + }, |
| 94 | + before: async () => { |
| 95 | + const consumers = await rte.data.sendCommand('xinfo', ['consumers', constants.TEST_STREAM_KEY_1, constants.TEST_STREAM_GROUP_1]); |
| 96 | + expect(consumers.length).to.eq(2); |
| 97 | + }, |
| 98 | + after: async () => { |
| 99 | + const consumers = await rte.data.sendCommand('xinfo', ['consumers', constants.TEST_STREAM_KEY_1, constants.TEST_STREAM_GROUP_1]); |
| 100 | + expect(consumers.length).to.eq(1); |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: 'Should remove multiple consumers', |
| 105 | + data: { |
| 106 | + keyName: constants.TEST_STREAM_KEY_1, |
| 107 | + groupName: constants.TEST_STREAM_GROUP_1, |
| 108 | + consumerNames: [constants.TEST_STREAM_CONSUMER_1, constants.TEST_STREAM_CONSUMER_2], |
| 109 | + }, |
| 110 | + before: async () => { |
| 111 | + const consumers = await rte.data.sendCommand('xinfo', ['consumers', constants.TEST_STREAM_KEY_1, constants.TEST_STREAM_GROUP_1]); |
| 112 | + expect(consumers.length).to.eq(2); |
| 113 | + }, |
| 114 | + after: async () => { |
| 115 | + const consumers = await rte.data.sendCommand('xinfo', ['consumers', constants.TEST_STREAM_KEY_1, constants.TEST_STREAM_GROUP_1]); |
| 116 | + expect(consumers.length).to.eq(0); |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: 'Should remove single consumers and skip not existing consumers', |
| 121 | + data: { |
| 122 | + keyName: constants.TEST_STREAM_KEY_1, |
| 123 | + groupName: constants.TEST_STREAM_GROUP_1, |
| 124 | + consumerNames: [constants.TEST_STREAM_CONSUMER_1, constants.getRandomString(), constants.getRandomString()], |
| 125 | + }, |
| 126 | + before: async () => { |
| 127 | + const consumers = await rte.data.sendCommand('xinfo', ['consumers', constants.TEST_STREAM_KEY_1, constants.TEST_STREAM_GROUP_1]); |
| 128 | + console.log('_c', consumers) |
| 129 | + expect(consumers.length).to.eq(2); |
| 130 | + }, |
| 131 | + after: async () => { |
| 132 | + const consumers = await rte.data.sendCommand('xinfo', ['consumers', constants.TEST_STREAM_KEY_1, constants.TEST_STREAM_GROUP_1]); |
| 133 | + expect(consumers.length).to.eq(1); |
| 134 | + }, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: 'Should return BadRequest error if key has another type', |
| 138 | + data: { |
| 139 | + ...validInputData, |
| 140 | + keyName: constants.TEST_STRING_KEY_1, |
| 141 | + }, |
| 142 | + statusCode: 400, |
| 143 | + responseBody: { |
| 144 | + statusCode: 400, |
| 145 | + error: 'Bad Request', |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: 'Should return NotFound error if group does not exists', |
| 150 | + data: { |
| 151 | + ...validInputData, |
| 152 | + groupName: constants.getRandomString(), |
| 153 | + }, |
| 154 | + statusCode: 404, |
| 155 | + responseBody: { |
| 156 | + statusCode: 404, |
| 157 | + error: 'Not Found', |
| 158 | + message: 'Consumer Group with such name was not found.', |
| 159 | + }, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: 'Should return NotFound error if key does not exists', |
| 163 | + data: { |
| 164 | + ...validInputData, |
| 165 | + keyName: constants.getRandomString(), |
| 166 | + }, |
| 167 | + statusCode: 404, |
| 168 | + responseBody: { |
| 169 | + statusCode: 404, |
| 170 | + error: 'Not Found', |
| 171 | + message: 'Key with this name does not exist.', |
| 172 | + }, |
| 173 | + }, |
| 174 | + { |
| 175 | + name: 'Should return NotFound error if instance id does not exists', |
| 176 | + endpoint: () => endpoint(constants.TEST_NOT_EXISTED_INSTANCE_ID), |
| 177 | + data: { |
| 178 | + ...validInputData, |
| 179 | + }, |
| 180 | + statusCode: 404, |
| 181 | + responseBody: { |
| 182 | + statusCode: 404, |
| 183 | + error: 'Not Found', |
| 184 | + message: 'Invalid database instance id.', |
| 185 | + }, |
| 186 | + }, |
| 187 | + ].map(mainCheckFn); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('ACL', () => { |
| 191 | + requirements('rte.acl'); |
| 192 | + |
| 193 | + before(async () => await rte.data.generateKeys(true)); |
| 194 | + before(async () => rte.data.setAclUserRules('~* +@all')); |
| 195 | + |
| 196 | + [ |
| 197 | + { |
| 198 | + name: 'Should create consumer group', |
| 199 | + endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID), |
| 200 | + data: { |
| 201 | + ...validInputData, |
| 202 | + }, |
| 203 | + }, |
| 204 | + { |
| 205 | + name: 'Should throw error if no permissions for "exists" command', |
| 206 | + endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID), |
| 207 | + data: { |
| 208 | + ...validInputData, |
| 209 | + }, |
| 210 | + statusCode: 403, |
| 211 | + responseBody: { |
| 212 | + statusCode: 403, |
| 213 | + error: 'Forbidden', |
| 214 | + }, |
| 215 | + before: () => rte.data.setAclUserRules('~* +@all -exists') |
| 216 | + }, |
| 217 | + { |
| 218 | + name: 'Should throw error if no permissions for "xgroup)" command', |
| 219 | + endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID), |
| 220 | + data: { |
| 221 | + ...validInputData, |
| 222 | + }, |
| 223 | + statusCode: 403, |
| 224 | + responseBody: { |
| 225 | + statusCode: 403, |
| 226 | + error: 'Forbidden', |
| 227 | + }, |
| 228 | + before: () => rte.data.setAclUserRules('~* +@all -xgroup') |
| 229 | + }, |
| 230 | + ].map(mainCheckFn); |
| 231 | + }); |
| 232 | +}); |
0 commit comments