2021-10-29 10:33:05 -05:00
|
|
|
import _ from 'lodash';
|
2019-09-07 17:46:50 -05:00
|
|
|
import path from 'path';
|
2020-08-11 00:21:51 -05:00
|
|
|
import request from 'supertest';
|
2019-09-07 17:46:50 -05:00
|
|
|
|
2020-09-16 23:48:16 -05:00
|
|
|
import {
|
2021-10-29 10:33:05 -05:00
|
|
|
API_ERROR,
|
2020-09-16 23:48:16 -05:00
|
|
|
HEADERS,
|
|
|
|
HEADER_TYPE,
|
2021-10-29 10:33:05 -05:00
|
|
|
HTTP_STATUS,
|
2020-09-16 23:48:16 -05:00
|
|
|
SUPPORT_ERRORS,
|
2021-10-29 10:33:05 -05:00
|
|
|
TOKEN_BEARER,
|
2021-09-25 17:08:00 -05:00
|
|
|
} from '@verdaccio/core';
|
2021-10-29 10:33:05 -05:00
|
|
|
import { logger, setup } from '@verdaccio/logger';
|
2020-09-16 23:48:16 -05:00
|
|
|
import {
|
|
|
|
DOMAIN_SERVERS,
|
|
|
|
configExample,
|
2021-10-29 10:33:05 -05:00
|
|
|
generateRamdonStorage,
|
|
|
|
getNewToken,
|
|
|
|
mockServer,
|
2020-09-16 23:48:16 -05:00
|
|
|
} from '@verdaccio/mock';
|
2021-10-29 10:33:05 -05:00
|
|
|
import { buildToken } from '@verdaccio/utils';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2020-08-11 00:21:51 -05:00
|
|
|
import endPointAPI from '../../src';
|
|
|
|
|
2020-03-03 17:59:19 -05:00
|
|
|
setup([]);
|
2019-09-07 17:46:50 -05:00
|
|
|
|
|
|
|
const credentials = { name: 'jota_token', password: 'secretPass' };
|
|
|
|
|
|
|
|
const generateTokenCLI = async (app, token, payload): Promise<any> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request(app)
|
|
|
|
.post('/-/npm/v1/tokens')
|
|
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
|
|
.send(JSON.stringify(payload))
|
|
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
2020-08-13 16:27:00 -05:00
|
|
|
.end(function (err, resp) {
|
2019-09-07 17:46:50 -05:00
|
|
|
if (err) {
|
2020-10-27 04:18:59 -05:00
|
|
|
// eslint-disable-next-line prefer-promise-reject-errors
|
2019-09-07 17:46:50 -05:00
|
|
|
return reject([err, resp]);
|
|
|
|
}
|
|
|
|
resolve([err, resp]);
|
|
|
|
});
|
2020-08-13 16:27:00 -05:00
|
|
|
});
|
2019-09-07 17:46:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const deleteTokenCLI = async (app, token, tokenToDelete): Promise<any> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request(app)
|
|
|
|
.delete(`/-/npm/v1/tokens/token/${tokenToDelete}`)
|
|
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
2020-08-13 16:27:00 -05:00
|
|
|
.end(function (err, resp) {
|
2019-09-07 17:46:50 -05:00
|
|
|
if (err) {
|
2020-10-27 04:18:59 -05:00
|
|
|
// eslint-disable-next-line prefer-promise-reject-errors
|
2019-09-07 17:46:50 -05:00
|
|
|
return reject([err, resp]);
|
|
|
|
}
|
|
|
|
resolve([err, resp]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('endpoint unit test', () => {
|
|
|
|
let app;
|
|
|
|
let mockRegistry;
|
|
|
|
let token;
|
|
|
|
|
2021-06-13 02:14:04 -05:00
|
|
|
beforeAll(async function () {
|
2020-03-03 17:59:19 -05:00
|
|
|
const store = generateRamdonStorage();
|
2019-09-07 17:46:50 -05:00
|
|
|
const mockServerPort = 55543;
|
2020-08-13 16:27:00 -05:00
|
|
|
const configForTest = configExample(
|
|
|
|
{
|
|
|
|
storage: store,
|
2020-11-08 09:20:02 -05:00
|
|
|
config_path: store,
|
2020-08-13 16:27:00 -05:00
|
|
|
uplinks: {
|
|
|
|
npmjs: {
|
|
|
|
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'token.spec.yaml',
|
|
|
|
__dirname
|
|
|
|
);
|
2020-03-03 17:59:19 -05:00
|
|
|
|
|
|
|
app = await endPointAPI(configForTest);
|
|
|
|
const binPath = require.resolve('verdaccio/bin/verdaccio');
|
|
|
|
const storePath = path.join(__dirname, '/mock/store');
|
|
|
|
mockRegistry = await mockServer(mockServerPort, { storePath, silence: true }).init(binPath);
|
|
|
|
token = await getNewToken(request(app), credentials);
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
2021-06-13 02:14:04 -05:00
|
|
|
afterAll(function () {
|
2020-03-03 17:59:19 -05:00
|
|
|
const [registry, pid] = mockRegistry;
|
|
|
|
registry.stop();
|
|
|
|
logger.info(`registry ${pid} has been stopped`);
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Registry Token Endpoints', () => {
|
2021-06-13 02:14:04 -05:00
|
|
|
test('should list empty tokens', async () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/npm/v1/tokens')
|
|
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
|
|
|
.expect(HTTP_STATUS.OK)
|
|
|
|
.end(function (err, resp) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { objects, urls } = resp.body;
|
|
|
|
expect(objects).toHaveLength(0);
|
|
|
|
expect(urls.next).toEqual('');
|
|
|
|
resolve(urls);
|
|
|
|
});
|
|
|
|
});
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
2021-06-13 02:14:04 -05:00
|
|
|
test('should generate one token', async () => {
|
2019-09-07 17:46:50 -05:00
|
|
|
await generateTokenCLI(app, token, {
|
|
|
|
password: credentials.password,
|
|
|
|
readonly: false,
|
2020-08-13 16:27:00 -05:00
|
|
|
cidr_whitelist: [],
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
2021-06-13 02:14:04 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/npm/v1/tokens')
|
|
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
|
|
|
.expect(HTTP_STATUS.OK)
|
|
|
|
.end(function (err, resp) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { objects, urls } = resp.body;
|
|
|
|
|
|
|
|
expect(objects).toHaveLength(1);
|
|
|
|
const [tokenGenerated] = objects;
|
|
|
|
expect(tokenGenerated.user).toEqual(credentials.name);
|
|
|
|
expect(tokenGenerated.readonly).toBeFalsy();
|
|
|
|
expect(tokenGenerated.token).toMatch(/.../);
|
|
|
|
expect(_.isString(tokenGenerated.created)).toBeTruthy();
|
|
|
|
|
|
|
|
// we don't support pagination yet
|
|
|
|
expect(urls.next).toEqual('');
|
|
|
|
resolve(urls);
|
|
|
|
});
|
|
|
|
});
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
2021-06-13 02:14:04 -05:00
|
|
|
test('should delete a token', async () => {
|
2019-09-07 17:46:50 -05:00
|
|
|
const res = await generateTokenCLI(app, token, {
|
|
|
|
password: credentials.password,
|
|
|
|
readonly: false,
|
2020-08-13 16:27:00 -05:00
|
|
|
cidr_whitelist: [],
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const t = res[1].body.token;
|
|
|
|
|
|
|
|
await deleteTokenCLI(app, token, t);
|
2021-06-13 02:14:04 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/npm/v1/tokens')
|
|
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
|
|
|
.expect(HTTP_STATUS.OK)
|
|
|
|
.end(function (err) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: enable these checks
|
|
|
|
// const { objects } = resp.body;
|
|
|
|
// expect(objects).toHaveLength(0);
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
});
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('handle errors', () => {
|
2021-06-13 02:14:04 -05:00
|
|
|
test('should fail with wrong credentials', async () => {
|
2019-09-07 17:46:50 -05:00
|
|
|
try {
|
|
|
|
await generateTokenCLI(app, token, {
|
|
|
|
password: 'wrongPassword',
|
|
|
|
readonly: false,
|
2020-08-13 16:27:00 -05:00
|
|
|
cidr_whitelist: [],
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
2021-08-30 01:19:08 -05:00
|
|
|
} catch (e: any) {
|
2019-09-07 17:46:50 -05:00
|
|
|
const [err, body] = e;
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(body.error).toEqual(API_ERROR.BAD_USERNAME_PASSWORD);
|
|
|
|
expect(body.status).toEqual(HTTP_STATUS.UNAUTHORIZED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-13 02:14:04 -05:00
|
|
|
test('should fail if readonly is missing', async () => {
|
2019-09-07 17:46:50 -05:00
|
|
|
try {
|
|
|
|
const res = await generateTokenCLI(app, token, {
|
|
|
|
password: credentials.password,
|
2020-08-13 16:27:00 -05:00
|
|
|
cidr_whitelist: [],
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res[0]).toBeNull();
|
|
|
|
expect(res[1].body.error).toEqual(SUPPORT_ERRORS.PARAMETERS_NOT_VALID);
|
2021-08-30 01:19:08 -05:00
|
|
|
} catch (e: any) {
|
2021-06-13 02:14:04 -05:00
|
|
|
return Promise.reject(e);
|
2019-09-07 17:46:50 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-13 02:14:04 -05:00
|
|
|
test('should fail if cidr_whitelist is missing', async () => {
|
2019-09-07 17:46:50 -05:00
|
|
|
try {
|
|
|
|
const res = await generateTokenCLI(app, token, {
|
|
|
|
password: credentials.password,
|
2020-03-03 17:59:19 -05:00
|
|
|
readonly: false,
|
2019-09-07 17:46:50 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res[0]).toBeNull();
|
|
|
|
expect(res[1].body.error).toEqual(SUPPORT_ERRORS.PARAMETERS_NOT_VALID);
|
2021-08-30 01:19:08 -05:00
|
|
|
} catch (e: any) {
|
2021-06-13 02:14:04 -05:00
|
|
|
return Promise.reject(e);
|
2019-09-07 17:46:50 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|