2018-10-12 04:07:55 -05:00
|
|
|
import request from 'supertest';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import path from 'path';
|
|
|
|
import rimraf from 'rimraf';
|
|
|
|
|
2019-05-19 15:23:12 -05:00
|
|
|
import endPointAPI from '../../../../src/api';
|
2021-03-14 02:42:46 -05:00
|
|
|
import { mockServer } from '../../__helper/mock';
|
|
|
|
import { parseConfigFile } from '../../../../src/lib/utils';
|
|
|
|
import { parseConfigurationFile } from '../../__helper';
|
|
|
|
import { getNewToken, getProfile, postProfile } from '../../__helper/api';
|
|
|
|
import { setup } from '../../../../src/lib/logger';
|
|
|
|
import { API_ERROR, HTTP_STATUS, SUPPORT_ERRORS } from '../../../../src/lib/constants';
|
2018-10-12 04:07:55 -05:00
|
|
|
|
|
|
|
setup([]);
|
|
|
|
|
|
|
|
const parseConfigurationProfile = () => {
|
|
|
|
return parseConfigurationFile(`profile/profile`);
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('endpoint user profile', () => {
|
|
|
|
let app;
|
|
|
|
let mockRegistry;
|
2019-07-27 00:20:30 -05:00
|
|
|
jest.setTimeout(20000);
|
2018-10-12 04:07:55 -05:00
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
beforeAll(function (done) {
|
2019-05-19 15:23:12 -05:00
|
|
|
const store = path.join(__dirname, '../../partials/store/test-profile-storage');
|
2018-10-12 04:07:55 -05:00
|
|
|
const mockServerPort = 55544;
|
|
|
|
rimraf(store, async () => {
|
|
|
|
const parsedConfig = parseConfigFile(parseConfigurationProfile());
|
2019-02-24 17:20:25 -05:00
|
|
|
const configForTest = _.assign({}, _.cloneDeep(parsedConfig), {
|
|
|
|
storage: store,
|
|
|
|
auth: {
|
|
|
|
htpasswd: {
|
2019-05-19 15:23:12 -05:00
|
|
|
file: './test-profile-storage/.htpasswd-auth-profile'
|
2019-02-24 17:20:25 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
self_path: store
|
|
|
|
});
|
|
|
|
app = await endPointAPI(configForTest);
|
2018-10-12 04:07:55 -05:00
|
|
|
mockRegistry = await mockServer(mockServerPort).init();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-14 02:42:46 -05:00
|
|
|
afterAll(function (done) {
|
2018-10-12 04:07:55 -05:00
|
|
|
mockRegistry[0].stop();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fetch a profile of logged user', async (done) => {
|
|
|
|
const credentials = { name: 'JotaJWT', password: 'secretPass' };
|
|
|
|
const token = await getNewToken(request(app), credentials);
|
|
|
|
const [err1, res1] = await getProfile(request(app), token);
|
|
|
|
|
|
|
|
expect(err1).toBeNull();
|
|
|
|
expect(res1.body.name).toBe(credentials.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('change password', () => {
|
|
|
|
test('should change password successfully', async (done) => {
|
|
|
|
const credentials = { name: 'userTest2000', password: 'secretPass000' };
|
|
|
|
const body = {
|
|
|
|
password: {
|
|
|
|
new: '12345678',
|
2021-03-14 02:42:46 -05:00
|
|
|
old: credentials.password
|
2018-10-12 04:07:55 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const token = await getNewToken(request(app), credentials);
|
|
|
|
const [err1, res1] = await postProfile(request(app), body, token);
|
|
|
|
|
|
|
|
expect(err1).toBeNull();
|
|
|
|
expect(res1.body.name).toBe(credentials.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should change password is too short', async (done) => {
|
|
|
|
const credentials = { name: 'userTest2001', password: 'secretPass001' };
|
|
|
|
const body = {
|
|
|
|
password: {
|
|
|
|
new: 'p1',
|
2021-03-14 02:42:46 -05:00
|
|
|
old: credentials.password
|
2018-10-12 04:07:55 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const token = await getNewToken(request(app), credentials);
|
|
|
|
const [, resp] = await postProfile(request(app), body, token, HTTP_STATUS.UNAUTHORIZED);
|
|
|
|
|
|
|
|
expect(resp.error).not.toBeNull();
|
2019-12-23 03:29:27 -05:00
|
|
|
/* eslint new-cap: 0 */
|
2018-10-12 04:07:55 -05:00
|
|
|
expect(resp.error.text).toMatch(API_ERROR.PASSWORD_SHORT());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('change tfa', () => {
|
|
|
|
test('should report TFA is disabled', async (done) => {
|
|
|
|
const credentials = { name: 'userTest2002', password: 'secretPass002' };
|
|
|
|
const body = {
|
|
|
|
tfa: {}
|
|
|
|
};
|
|
|
|
const token = await getNewToken(request(app), credentials);
|
2021-03-14 02:42:46 -05:00
|
|
|
const [, resp] = await postProfile(
|
|
|
|
request(app),
|
|
|
|
body,
|
|
|
|
token,
|
|
|
|
HTTP_STATUS.SERVICE_UNAVAILABLE
|
|
|
|
);
|
2018-10-12 04:07:55 -05:00
|
|
|
|
|
|
|
expect(resp.error).not.toBeNull();
|
|
|
|
expect(resp.error.text).toMatch(SUPPORT_ERRORS.TFA_DISABLED);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('error handling', () => {
|
|
|
|
test('should forbid to fetch a profile with invalid token', async (done) => {
|
|
|
|
const [, resp] = await getProfile(request(app), `fakeToken`, HTTP_STATUS.UNAUTHORIZED);
|
|
|
|
|
|
|
|
expect(resp.error).not.toBeNull();
|
|
|
|
expect(resp.error.text).toMatch(API_ERROR.MUST_BE_LOGGED);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should forbid to update a profile with invalid token', async (done) => {
|
|
|
|
const [, resp] = await postProfile(request(app), {}, `fakeToken`, HTTP_STATUS.UNAUTHORIZED);
|
|
|
|
|
|
|
|
expect(resp.error).not.toBeNull();
|
|
|
|
expect(resp.error.text).toMatch(API_ERROR.MUST_BE_LOGGED);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|