2018-08-21 08:05:34 +02:00
|
|
|
import request from 'supertest';
|
|
|
|
import path from 'path';
|
|
|
|
import rimraf from 'rimraf';
|
|
|
|
|
2019-05-19 22:23:12 +02:00
|
|
|
import endPointAPI from '../../../../src/api';
|
2018-08-21 08:05:34 +02:00
|
|
|
|
2021-03-14 08:42:46 +01:00
|
|
|
import {
|
|
|
|
HEADERS,
|
|
|
|
HTTP_STATUS,
|
|
|
|
HEADER_TYPE,
|
|
|
|
TOKEN_BEARER,
|
|
|
|
TOKEN_BASIC,
|
|
|
|
API_ERROR
|
|
|
|
} from '../../../../src/lib/constants';
|
|
|
|
import { mockServer } from '../../__helper/mock';
|
|
|
|
import { DOMAIN_SERVERS } from '../../../functional/config.functional';
|
|
|
|
import { buildToken } from '../../../../src/lib/utils';
|
|
|
|
import { addUser, getPackage, loginUserToken } from '../../__helper/api';
|
|
|
|
import { setup } from '../../../../src/lib/logger';
|
2019-08-10 13:38:06 +02:00
|
|
|
import configDefault from '../../partials/config';
|
2021-03-14 08:42:46 +01:00
|
|
|
import { buildUserBuffer } from '../../../../src/lib/auth-utils';
|
2018-08-21 08:05:34 +02:00
|
|
|
|
|
|
|
setup([]);
|
|
|
|
const credentials = { name: 'JotaJWT', password: 'secretPass' };
|
|
|
|
|
2019-07-16 08:40:01 +02:00
|
|
|
const FORBIDDEN_VUE = 'authorization required to access package vue';
|
2018-08-21 08:05:34 +02:00
|
|
|
|
|
|
|
describe('endpoint user auth JWT unit test', () => {
|
2019-05-20 07:41:12 +02:00
|
|
|
jest.setTimeout(20000);
|
2018-08-21 08:05:34 +02:00
|
|
|
let app;
|
|
|
|
let mockRegistry;
|
2019-05-19 21:37:43 +02:00
|
|
|
const FAKE_TOKEN: string = buildToken(TOKEN_BEARER, 'fake');
|
2018-08-21 08:05:34 +02:00
|
|
|
|
2021-03-14 08:42:46 +01:00
|
|
|
beforeAll(function (done) {
|
2019-05-19 22:23:12 +02:00
|
|
|
const store = path.join(__dirname, '../../partials/store/test-jwt-storage');
|
2018-08-21 08:05:34 +02:00
|
|
|
const mockServerPort = 55546;
|
|
|
|
rimraf(store, async () => {
|
2021-03-14 08:42:46 +01:00
|
|
|
const configForTest = configDefault(
|
|
|
|
{
|
|
|
|
storage: store,
|
|
|
|
uplinks: {
|
|
|
|
npmjs: {
|
|
|
|
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
self_path: store,
|
|
|
|
auth: {
|
|
|
|
htpasswd: {
|
|
|
|
file: './test-jwt-storage/.htpasswd_jwt_auth'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
logs: [{ type: 'stdout', format: 'pretty', level: 'warn' }]
|
2019-02-24 23:20:25 +01:00
|
|
|
},
|
2021-03-14 08:42:46 +01:00
|
|
|
'api-jwt/jwt.yaml'
|
|
|
|
);
|
2019-07-16 08:40:01 +02:00
|
|
|
|
2019-02-24 23:20:25 +01:00
|
|
|
app = await endPointAPI(configForTest);
|
2018-08-21 08:05:34 +02:00
|
|
|
mockRegistry = await mockServer(mockServerPort).init();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-14 08:42:46 +01:00
|
|
|
afterAll(function (done) {
|
2018-08-21 08:05:34 +02:00
|
|
|
mockRegistry[0].stop();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should test add a new user with JWT enabled', async (done) => {
|
|
|
|
const [err, res] = await addUser(request(app), credentials.name, credentials);
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(res.body.ok).toBeDefined();
|
|
|
|
expect(res.body.token).toBeDefined();
|
2019-06-13 06:58:43 +02:00
|
|
|
|
|
|
|
const { token } = res.body;
|
2018-08-21 08:05:34 +02:00
|
|
|
expect(typeof token).toBe('string');
|
|
|
|
expect(res.body.ok).toMatch(`user '${credentials.name}' created`);
|
2019-06-13 06:58:43 +02:00
|
|
|
|
2018-08-21 08:05:34 +02:00
|
|
|
// testing JWT auth headers with token
|
|
|
|
// we need it here, because token is required
|
2019-08-10 13:38:06 +02:00
|
|
|
const [err1, resp1] = await getPackage(request(app), token, 'vue');
|
2018-08-21 08:05:34 +02:00
|
|
|
expect(err1).toBeNull();
|
|
|
|
expect(resp1.body).toBeDefined();
|
|
|
|
expect(resp1.body.name).toMatch('vue');
|
|
|
|
|
2021-03-14 08:42:46 +01:00
|
|
|
const [err2, resp2] = await getPackage(
|
|
|
|
request(app),
|
|
|
|
FAKE_TOKEN,
|
|
|
|
'vue',
|
|
|
|
HTTP_STATUS.UNAUTHORIZED
|
|
|
|
);
|
2018-08-21 08:05:34 +02:00
|
|
|
expect(err2).toBeNull();
|
2018-11-15 21:13:41 +01:00
|
|
|
expect(resp2.statusCode).toBe(HTTP_STATUS.UNAUTHORIZED);
|
2018-08-21 08:05:34 +02:00
|
|
|
expect(resp2.body.error).toMatch(FORBIDDEN_VUE);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should emulate npm login when user already exist', async (done) => {
|
|
|
|
const credentials = { name: 'jwtUser2', password: 'secretPass' };
|
|
|
|
// creates an user
|
|
|
|
await addUser(request(app), credentials.name, credentials);
|
|
|
|
// it should fails conflict 409
|
|
|
|
await addUser(request(app), credentials.name, credentials, HTTP_STATUS.CONFLICT);
|
2019-06-13 06:58:43 +02:00
|
|
|
|
2018-08-21 08:05:34 +02:00
|
|
|
// npm will try to sign in sending credentials via basic auth header
|
|
|
|
const token = buildUserBuffer(credentials.name, credentials.password).toString('base64');
|
2019-07-16 08:40:01 +02:00
|
|
|
// put should exist in request
|
|
|
|
// @ts-ignore
|
2021-03-14 08:42:46 +01:00
|
|
|
request(app)
|
|
|
|
.put(`/-/user/org.couchdb.user:${credentials.name}/-rev/undefined`)
|
2018-08-21 08:05:34 +02:00
|
|
|
.send(credentials)
|
2019-05-19 21:37:43 +02:00
|
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BASIC, token))
|
2018-08-21 08:05:34 +02:00
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
|
|
|
.expect(HTTP_STATUS.CREATED)
|
2021-03-14 08:42:46 +01:00
|
|
|
.end(function (err, res) {
|
2018-08-21 08:05:34 +02:00
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(res.body.ok).toBeDefined();
|
|
|
|
expect(res.body.token).toBeDefined();
|
2019-06-13 06:58:43 +02:00
|
|
|
|
2018-08-21 08:05:34 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails on try to access with corrupted token', async (done) => {
|
2021-03-14 08:42:46 +01:00
|
|
|
const [err2, resp2] = await getPackage(
|
|
|
|
request(app),
|
|
|
|
FAKE_TOKEN,
|
|
|
|
'vue',
|
|
|
|
HTTP_STATUS.UNAUTHORIZED
|
|
|
|
);
|
2018-08-21 08:05:34 +02:00
|
|
|
expect(err2).toBeNull();
|
2018-11-15 21:13:41 +01:00
|
|
|
expect(resp2.statusCode).toBe(HTTP_STATUS.UNAUTHORIZED);
|
2018-08-21 08:05:34 +02:00
|
|
|
expect(resp2.body.error).toMatch(FORBIDDEN_VUE);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2019-06-13 06:58:43 +02:00
|
|
|
test('should fails on login if user credentials are invalid even if jwt valid token is provided', async (done) => {
|
|
|
|
const credentials = { name: 'newFailsUser', password: 'secretPass' };
|
|
|
|
const [err, res] = await addUser(request(app), credentials.name, credentials);
|
|
|
|
expect(err).toBeNull();
|
|
|
|
expect(res.body.ok).toBeDefined();
|
|
|
|
expect(res.body.token).toBeDefined();
|
|
|
|
|
|
|
|
const { token } = res.body;
|
|
|
|
expect(typeof token).toBe('string');
|
|
|
|
expect(res.body.ok).toMatch(`user '${credentials.name}' created`);
|
|
|
|
|
|
|
|
// we login when token is valid
|
|
|
|
const newCredentials = { name: 'newFailsUser', password: 'BAD_PASSWORD' };
|
2021-03-14 08:42:46 +01:00
|
|
|
const [err2, resp2] = await loginUserToken(
|
|
|
|
request(app),
|
|
|
|
newCredentials.name,
|
|
|
|
newCredentials,
|
|
|
|
token,
|
|
|
|
HTTP_STATUS.UNAUTHORIZED
|
|
|
|
);
|
2019-06-13 06:58:43 +02:00
|
|
|
expect(err2).toBeNull();
|
|
|
|
expect(resp2.statusCode).toBe(HTTP_STATUS.UNAUTHORIZED);
|
|
|
|
expect(resp2.body.error).toMatch(API_ERROR.BAD_USERNAME_PASSWORD);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
2018-08-21 08:05:34 +02:00
|
|
|
});
|