improve tests and minor fixes

This commit is contained in:
Dieter Stinglhamber 2021-08-30 09:05:39 +02:00
parent 305547b9bb
commit bfcebc9bae
3 changed files with 17 additions and 10 deletions

View File

@ -1,7 +1,5 @@
'use strict'; 'use strict';
const { env } = require('../../../utils/lib'); module.exports = ({ env }) => ({
module.exports = {
salt: env('API_TOKEN_SALT'), salt: env('API_TOKEN_SALT'),
}; });

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const { trim } = require('lodash/fp');
const { getService } = require('../utils'); const { getService } = require('../utils');
const { validateApiTokenCreationInput } = require('../validation/api-tokens'); const { validateApiTokenCreationInput } = require('../validation/api-tokens');
@ -13,10 +14,8 @@ module.exports = {
* - having a space at the end or start of the value. * - having a space at the end or start of the value.
* - having only spaces as value; * - having only spaces as value;
*/ */
attributes.name = attributes.name.trim(); attributes.name = trim(attributes.name);
if (attributes.description) { attributes.description = trim(attributes.description);
attributes.description = attributes.description.trim();
}
try { try {
await validateApiTokenCreationInput(attributes); await validateApiTokenCreationInput(attributes);

View File

@ -73,7 +73,14 @@ describe('API Token', () => {
expect(mockedConfigSet).not.toHaveBeenCalled(); expect(mockedConfigSet).not.toHaveBeenCalled();
}); });
test('It creates a new salt, appendit to the .env file and sets it in the configuration', () => { test('It creates a new salt, appends it to the .env file and sets it in the configuration', () => {
const mockedApiToken = {
randomBytes: 'api-token_test-random-bytes',
hexedString: '6170692d746f6b656e5f746573742d72616e646f6d2d6279746573',
};
crypto.randomBytes = jest.fn(() => Buffer.from(mockedApiToken.randomBytes));
const mockedAppendFile = jest.fn(); const mockedAppendFile = jest.fn();
const mockedConfigSet = jest.fn(); const mockedConfigSet = jest.fn();
@ -88,7 +95,10 @@ describe('API Token', () => {
apiTokenService.createSaltIfNotDefined(); apiTokenService.createSaltIfNotDefined();
expect(mockedAppendFile).toHaveBeenCalled(); expect(mockedAppendFile).toHaveBeenCalled();
expect(mockedConfigSet).toHaveBeenCalled(); expect(mockedConfigSet).toHaveBeenCalledWith(
'server.admin.api-token.salt',
mockedApiToken.hexedString
);
}); });
test('It throws an error if the env variable used in the config file has been changed and is empty', () => { test('It throws an error if the env variable used in the config file has been changed and is empty', () => {