|
| 1 | +import { RdiUrl } from 'src/modules/rdi/constants'; |
| 2 | +import { sign } from 'jsonwebtoken'; |
| 3 | +import { |
| 4 | + describe, expect, deps, getMainCheckFn, generateInvalidDataTestCases, |
| 5 | + validateInvalidDataTestCase, |
| 6 | +} from '../deps'; |
| 7 | +import { Joi, nock } from '../../helpers/test'; |
| 8 | + |
| 9 | +const { |
| 10 | + localDb, request, server, constants, |
| 11 | +} = deps; |
| 12 | + |
| 13 | +const testRdiId = 'someTestId'; |
| 14 | +const testRdiUrl = 'http://rdilocal.test'; |
| 15 | +const testRdiName = 'Test Rdi Name'; |
| 16 | +const testRdiBase = { id: testRdiId, name: testRdiName, url: testRdiUrl }; |
| 17 | + |
| 18 | +const endpoint = (id) => request(server).patch(`/${constants.API.RDI}/${id || testRdiId}`); |
| 19 | + |
| 20 | +const dataSchema = Joi.object().keys({ |
| 21 | + name: Joi.string().max(500).allow(null), |
| 22 | + username: Joi.string().allow(null), |
| 23 | + password: Joi.string().allow(null), |
| 24 | +}).messages({ 'any.required': '{#label} should not be empty' }).strict(true); |
| 25 | + |
| 26 | +const validInputData = { |
| 27 | + name: 'Updated Rdi', |
| 28 | + username: 'rdiUsername Updated', |
| 29 | + password: constants.TEST_KEYTAR_PASSWORD, |
| 30 | +}; |
| 31 | + |
| 32 | +const responseSchema = Joi.object().keys({ |
| 33 | + id: Joi.string().required(), |
| 34 | + url: Joi.string().required(), |
| 35 | + name: Joi.string().max(500).required(), |
| 36 | + username: Joi.string().required(), |
| 37 | + password: Joi.string().required(), |
| 38 | + lastConnection: Joi.string().isoDate().required(), |
| 39 | + version: Joi.string().required(), |
| 40 | +}).required().strict(true); |
| 41 | + |
| 42 | +const mockedAccessToken = sign({ exp: Math.trunc(Date.now() / 1000) + 3600 }, 'test'); |
| 43 | + |
| 44 | +const mainCheckFn = getMainCheckFn(endpoint); |
| 45 | + |
| 46 | +describe('PATCH /rdi/:id', () => { |
| 47 | + describe('Validation', () => { |
| 48 | + generateInvalidDataTestCases(dataSchema, validInputData).forEach( |
| 49 | + validateInvalidDataTestCase(endpoint, dataSchema), |
| 50 | + ); |
| 51 | + }); |
| 52 | + describe('Common', () => { |
| 53 | + [ |
| 54 | + { |
| 55 | + name: 'Should update rdi name', |
| 56 | + responseSchema, |
| 57 | + data: { name: validInputData.name }, |
| 58 | + statusCode: 200, |
| 59 | + checkFn: ({ body }) => { |
| 60 | + expect(body.name).to.eql(validInputData.name); |
| 61 | + expect(body.id).to.eql(testRdiId); |
| 62 | + }, |
| 63 | + before: async () => { |
| 64 | + await localDb.generateRdis(testRdiBase, 1); |
| 65 | + nock(testRdiUrl).post(`/${RdiUrl.Login}`).query(true).reply(200, { |
| 66 | + access_token: mockedAccessToken, |
| 67 | + }); |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: 'Should update rdi username', |
| 72 | + responseSchema, |
| 73 | + data: { username: validInputData.username }, |
| 74 | + statusCode: 200, |
| 75 | + checkFn: ({ body }) => { |
| 76 | + expect(body.name).to.eql(testRdiName); |
| 77 | + expect(body.username).to.eql(validInputData.username); |
| 78 | + }, |
| 79 | + before: async () => { |
| 80 | + await localDb.generateRdis(testRdiBase, 1); |
| 81 | + nock(testRdiUrl).post(`/${RdiUrl.Login}`).query(true).reply(200, { |
| 82 | + access_token: mockedAccessToken, |
| 83 | + }); |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'Should throw error if rdiClient was not connected', |
| 88 | + statusCode: 401, |
| 89 | + data: validInputData, |
| 90 | + before: () => { |
| 91 | + nock(testRdiUrl).post(`/${RdiUrl.Login}`).query(true).reply(401, { |
| 92 | + message: 'Unauthorized', |
| 93 | + }); |
| 94 | + }, |
| 95 | + responseBody: { |
| 96 | + message: 'Authorization failed', |
| 97 | + statusCode: 401, |
| 98 | + error: 'RdiUnauthorized', |
| 99 | + errorCode: 11402, |
| 100 | + }, |
| 101 | + }, |
| 102 | + ].forEach(mainCheckFn); |
| 103 | + }); |
| 104 | +}); |
0 commit comments