|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { httpServiceMock } from '../../../../core/server/mocks'; |
| 7 | +import { loggerMock } from '@osd/logging/target/mocks'; |
| 8 | +import { |
| 9 | + defineRoutes, |
| 10 | + handleDeleteEntityConfig, |
| 11 | + handleGetConfig, |
| 12 | + handleGetEntityConfig, |
| 13 | + handleUpdateEntityConfig, |
| 14 | +} from '.'; |
| 15 | + |
| 16 | +const ERROR_MESSAGE = 'Service unavailable'; |
| 17 | + |
| 18 | +const ERROR_RESPONSE = { |
| 19 | + statusCode: 500, |
| 20 | +}; |
| 21 | + |
| 22 | +const ENTITY_NAME = 'config1'; |
| 23 | +const ENTITY_VALUE = 'value1'; |
| 24 | +const ENTITY_NEW_VALUE = 'newValue1'; |
| 25 | + |
| 26 | +describe('application config routes', () => { |
| 27 | + describe('defineRoutes', () => { |
| 28 | + it('check route paths are defined', () => { |
| 29 | + const router = httpServiceMock.createRouter(); |
| 30 | + const configurationClient = { |
| 31 | + existsCspRules: jest.fn().mockReturnValue(true), |
| 32 | + getCspRules: jest.fn().mockReturnValue(''), |
| 33 | + }; |
| 34 | + |
| 35 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 36 | + |
| 37 | + const logger = loggerMock.create(); |
| 38 | + |
| 39 | + defineRoutes(router, getConfigurationClient, logger); |
| 40 | + |
| 41 | + expect(router.get).toHaveBeenCalledWith( |
| 42 | + expect.objectContaining({ |
| 43 | + path: '/api/appconfig', |
| 44 | + }), |
| 45 | + expect.any(Function) |
| 46 | + ); |
| 47 | + |
| 48 | + expect(router.get).toHaveBeenCalledWith( |
| 49 | + expect.objectContaining({ |
| 50 | + path: '/api/appconfig/{entity}', |
| 51 | + }), |
| 52 | + expect.any(Function) |
| 53 | + ); |
| 54 | + |
| 55 | + expect(router.post).toHaveBeenCalledWith( |
| 56 | + expect.objectContaining({ |
| 57 | + path: '/api/appconfig/{entity}', |
| 58 | + }), |
| 59 | + expect.any(Function) |
| 60 | + ); |
| 61 | + |
| 62 | + expect(router.delete).toHaveBeenCalledWith( |
| 63 | + expect.objectContaining({ |
| 64 | + path: '/api/appconfig/{entity}', |
| 65 | + }), |
| 66 | + expect.any(Function) |
| 67 | + ); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('handleGetConfig', () => { |
| 72 | + it('returns configurations when client returns', async () => { |
| 73 | + const configurations = { |
| 74 | + config1: 'value1', |
| 75 | + config2: 'value2', |
| 76 | + }; |
| 77 | + |
| 78 | + const client = { |
| 79 | + getConfig: jest.fn().mockReturnValue(configurations), |
| 80 | + }; |
| 81 | + |
| 82 | + const okResponse = { |
| 83 | + statusCode: 200, |
| 84 | + }; |
| 85 | + |
| 86 | + const response = { |
| 87 | + ok: jest.fn().mockReturnValue(okResponse), |
| 88 | + }; |
| 89 | + |
| 90 | + const logger = loggerMock.create(); |
| 91 | + |
| 92 | + const returnedResponse = await handleGetConfig(client, response, logger); |
| 93 | + |
| 94 | + expect(returnedResponse).toBe(okResponse); |
| 95 | + |
| 96 | + expect(response.ok).toBeCalledWith({ |
| 97 | + body: { |
| 98 | + value: configurations, |
| 99 | + }, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('return error response when client throws error', async () => { |
| 104 | + const error = new Error(ERROR_MESSAGE); |
| 105 | + |
| 106 | + const client = { |
| 107 | + getConfig: jest.fn().mockImplementation(() => { |
| 108 | + throw error; |
| 109 | + }), |
| 110 | + }; |
| 111 | + |
| 112 | + const response = { |
| 113 | + customError: jest.fn().mockReturnValue(ERROR_RESPONSE), |
| 114 | + }; |
| 115 | + |
| 116 | + const logger = loggerMock.create(); |
| 117 | + |
| 118 | + const returnedResponse = await handleGetConfig(client, response, logger); |
| 119 | + |
| 120 | + expect(returnedResponse).toBe(ERROR_RESPONSE); |
| 121 | + |
| 122 | + expect(client.getConfig).toBeCalledTimes(1); |
| 123 | + |
| 124 | + expect(response.customError).toBeCalledWith({ |
| 125 | + body: error, |
| 126 | + statusCode: 500, |
| 127 | + }); |
| 128 | + |
| 129 | + expect(logger.error).toBeCalledWith(error); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('handleGetEntityConfig', () => { |
| 134 | + it('returns value when client returns value', async () => { |
| 135 | + const client = { |
| 136 | + getEntityConfig: jest.fn().mockReturnValue(ENTITY_VALUE), |
| 137 | + }; |
| 138 | + |
| 139 | + const okResponse = { |
| 140 | + statusCode: 200, |
| 141 | + }; |
| 142 | + |
| 143 | + const request = { |
| 144 | + params: { |
| 145 | + entity: ENTITY_NAME, |
| 146 | + }, |
| 147 | + }; |
| 148 | + |
| 149 | + const response = { |
| 150 | + ok: jest.fn().mockReturnValue(okResponse), |
| 151 | + }; |
| 152 | + |
| 153 | + const logger = loggerMock.create(); |
| 154 | + |
| 155 | + const returnedResponse = await handleGetEntityConfig(client, request, response, logger); |
| 156 | + |
| 157 | + expect(returnedResponse).toBe(okResponse); |
| 158 | + |
| 159 | + expect(response.ok).toBeCalledWith({ |
| 160 | + body: { |
| 161 | + value: ENTITY_VALUE, |
| 162 | + }, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('return error response when client throws error', async () => { |
| 167 | + const error = new Error(ERROR_MESSAGE); |
| 168 | + |
| 169 | + const client = { |
| 170 | + getEntityConfig: jest.fn().mockImplementation(() => { |
| 171 | + throw error; |
| 172 | + }), |
| 173 | + }; |
| 174 | + |
| 175 | + const request = { |
| 176 | + params: { |
| 177 | + entity: ENTITY_NAME, |
| 178 | + }, |
| 179 | + }; |
| 180 | + |
| 181 | + const response = { |
| 182 | + customError: jest.fn().mockReturnValue(ERROR_RESPONSE), |
| 183 | + }; |
| 184 | + |
| 185 | + const logger = loggerMock.create(); |
| 186 | + |
| 187 | + const returnedResponse = await handleGetEntityConfig(client, request, response, logger); |
| 188 | + |
| 189 | + expect(returnedResponse).toBe(ERROR_RESPONSE); |
| 190 | + |
| 191 | + expect(client.getEntityConfig).toBeCalledTimes(1); |
| 192 | + |
| 193 | + expect(response.customError).toBeCalledWith({ |
| 194 | + body: error, |
| 195 | + statusCode: 500, |
| 196 | + }); |
| 197 | + |
| 198 | + expect(logger.error).toBeCalledWith(error); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('handleUpdateEntityConfig', () => { |
| 203 | + it('return success when client succeeds', async () => { |
| 204 | + const client = { |
| 205 | + updateEntityConfig: jest.fn().mockReturnValue(ENTITY_NEW_VALUE), |
| 206 | + }; |
| 207 | + |
| 208 | + const okResponse = { |
| 209 | + statusCode: 200, |
| 210 | + }; |
| 211 | + |
| 212 | + const request = { |
| 213 | + params: { |
| 214 | + entity: ENTITY_NAME, |
| 215 | + }, |
| 216 | + body: { |
| 217 | + newValue: ENTITY_NEW_VALUE, |
| 218 | + }, |
| 219 | + }; |
| 220 | + |
| 221 | + const response = { |
| 222 | + ok: jest.fn().mockReturnValue(okResponse), |
| 223 | + }; |
| 224 | + |
| 225 | + const logger = loggerMock.create(); |
| 226 | + |
| 227 | + const returnedResponse = await handleUpdateEntityConfig(client, request, response, logger); |
| 228 | + |
| 229 | + expect(returnedResponse).toBe(okResponse); |
| 230 | + |
| 231 | + expect(client.updateEntityConfig).toBeCalledTimes(1); |
| 232 | + |
| 233 | + expect(response.ok).toBeCalledWith({ |
| 234 | + body: { |
| 235 | + newValue: ENTITY_NEW_VALUE, |
| 236 | + }, |
| 237 | + }); |
| 238 | + |
| 239 | + expect(logger.error).not.toBeCalled(); |
| 240 | + }); |
| 241 | + |
| 242 | + it('return error response when client fails', async () => { |
| 243 | + const error = new Error(ERROR_MESSAGE); |
| 244 | + |
| 245 | + const client = { |
| 246 | + updateEntityConfig: jest.fn().mockImplementation(() => { |
| 247 | + throw error; |
| 248 | + }), |
| 249 | + }; |
| 250 | + |
| 251 | + const request = { |
| 252 | + params: { |
| 253 | + entity: ENTITY_NAME, |
| 254 | + }, |
| 255 | + body: { |
| 256 | + newValue: ENTITY_NEW_VALUE, |
| 257 | + }, |
| 258 | + }; |
| 259 | + |
| 260 | + const response = { |
| 261 | + customError: jest.fn().mockReturnValue(ERROR_RESPONSE), |
| 262 | + }; |
| 263 | + |
| 264 | + const logger = loggerMock.create(); |
| 265 | + |
| 266 | + const returnedResponse = await handleUpdateEntityConfig(client, request, response, logger); |
| 267 | + |
| 268 | + expect(returnedResponse).toBe(ERROR_RESPONSE); |
| 269 | + |
| 270 | + expect(client.updateEntityConfig).toBeCalledTimes(1); |
| 271 | + |
| 272 | + expect(response.customError).toBeCalledWith({ |
| 273 | + body: error, |
| 274 | + statusCode: 500, |
| 275 | + }); |
| 276 | + |
| 277 | + expect(logger.error).toBeCalledWith(error); |
| 278 | + }); |
| 279 | + }); |
| 280 | + |
| 281 | + describe('handleDeleteEntityConfig', () => { |
| 282 | + it('returns successful response when client succeeds', async () => { |
| 283 | + const client = { |
| 284 | + deleteEntityConfig: jest.fn().mockReturnValue(ENTITY_NAME), |
| 285 | + }; |
| 286 | + |
| 287 | + const okResponse = { |
| 288 | + statusCode: 200, |
| 289 | + }; |
| 290 | + |
| 291 | + const request = { |
| 292 | + params: { |
| 293 | + entity: ENTITY_NAME, |
| 294 | + }, |
| 295 | + }; |
| 296 | + |
| 297 | + const response = { |
| 298 | + ok: jest.fn().mockReturnValue(okResponse), |
| 299 | + }; |
| 300 | + |
| 301 | + const logger = loggerMock.create(); |
| 302 | + |
| 303 | + const returnedResponse = await handleDeleteEntityConfig(client, request, response, logger); |
| 304 | + |
| 305 | + expect(returnedResponse).toBe(okResponse); |
| 306 | + |
| 307 | + expect(client.deleteEntityConfig).toBeCalledTimes(1); |
| 308 | + |
| 309 | + expect(response.ok).toBeCalledWith({ |
| 310 | + body: { |
| 311 | + deletedEntity: ENTITY_NAME, |
| 312 | + }, |
| 313 | + }); |
| 314 | + |
| 315 | + expect(logger.error).not.toBeCalled(); |
| 316 | + }); |
| 317 | + |
| 318 | + it('return error response when client fails', async () => { |
| 319 | + const error = new Error(ERROR_MESSAGE); |
| 320 | + |
| 321 | + const client = { |
| 322 | + deleteEntityConfig: jest.fn().mockImplementation(() => { |
| 323 | + throw error; |
| 324 | + }), |
| 325 | + }; |
| 326 | + |
| 327 | + const request = { |
| 328 | + params: { |
| 329 | + entity: ENTITY_NAME, |
| 330 | + }, |
| 331 | + }; |
| 332 | + |
| 333 | + const response = { |
| 334 | + customError: jest.fn().mockReturnValue(ERROR_RESPONSE), |
| 335 | + }; |
| 336 | + |
| 337 | + const logger = loggerMock.create(); |
| 338 | + |
| 339 | + const returnedResponse = await handleDeleteEntityConfig(client, request, response, logger); |
| 340 | + |
| 341 | + expect(returnedResponse).toBe(ERROR_RESPONSE); |
| 342 | + |
| 343 | + expect(client.deleteEntityConfig).toBeCalledTimes(1); |
| 344 | + |
| 345 | + expect(response.customError).toBeCalledWith({ |
| 346 | + body: error, |
| 347 | + statusCode: 500, |
| 348 | + }); |
| 349 | + |
| 350 | + expect(logger.error).toBeCalledWith(error); |
| 351 | + }); |
| 352 | + }); |
| 353 | +}); |
0 commit comments