2018-11-18 18:41:05 +01:00
|
|
|
import path from 'path';
|
2020-08-11 07:21:51 +02:00
|
|
|
import request from 'supertest';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
import { HEADERS, API_ERROR, HTTP_STATUS, HEADER_TYPE, DIST_TAGS } from '@verdaccio/dev-commons';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
2020-09-17 06:48:16 +02:00
|
|
|
import {
|
|
|
|
addUser,
|
|
|
|
mockServer,
|
|
|
|
DOMAIN_SERVERS,
|
|
|
|
configExample,
|
|
|
|
generateRamdonStorage,
|
|
|
|
} from '@verdaccio/mock';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
import { setup, logger } from '@verdaccio/logger';
|
2020-08-11 07:21:51 +02:00
|
|
|
import endPointAPI from '../../src';
|
|
|
|
import forbiddenPlace from './partials/forbidden-place';
|
|
|
|
import publishMetadata from './partials/publish-api';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
|
|
|
setup([]);
|
2018-11-18 18:41:05 +01:00
|
|
|
|
|
|
|
const credentials = { name: 'user-web', password: 'secretPass' };
|
2020-03-03 23:59:19 +01:00
|
|
|
|
2018-11-18 18:41:05 +01:00
|
|
|
describe('endpoint web unit test', () => {
|
2020-03-03 23:59:19 +01:00
|
|
|
jest.setTimeout(40000);
|
2018-11-18 18:41:05 +01:00
|
|
|
let app;
|
|
|
|
let mockRegistry;
|
|
|
|
|
2020-03-03 23:59:19 +01:00
|
|
|
beforeAll(async (done) => {
|
|
|
|
const store = generateRamdonStorage();
|
|
|
|
const mockServerPort = 55523;
|
2020-08-13 23:27:00 +02:00
|
|
|
const configForTest = configExample(
|
|
|
|
{
|
|
|
|
storage: store,
|
2020-11-08 15:20:02 +01:00
|
|
|
config_path: store,
|
2020-08-13 23:27:00 +02:00
|
|
|
uplinks: {
|
|
|
|
remote: {
|
|
|
|
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`,
|
|
|
|
},
|
|
|
|
},
|
2020-03-03 23:59:19 +01:00
|
|
|
},
|
2020-08-13 23:27:00 +02:00
|
|
|
'web.yaml',
|
|
|
|
__dirname
|
|
|
|
);
|
2020-03-03 23:59:19 +01: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);
|
|
|
|
done();
|
2018-11-18 18:41:05 +01:00
|
|
|
});
|
|
|
|
|
2020-08-13 23:27:00 +02:00
|
|
|
afterAll(function (done) {
|
2020-03-03 23:59:19 +01:00
|
|
|
const [registry, pid] = mockRegistry;
|
|
|
|
registry.stop();
|
|
|
|
logger.info(`registry ${pid} has been stopped`);
|
|
|
|
|
2018-11-18 18:41:05 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Registry WebUI endpoints', () => {
|
2019-02-24 23:20:25 +01:00
|
|
|
beforeAll(async () => {
|
2018-11-18 18:41:05 +01:00
|
|
|
await request(app)
|
2019-07-16 08:40:01 +02:00
|
|
|
.put('/@scope%2fpk1-test')
|
|
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
|
|
.send(JSON.stringify(publishMetadata))
|
|
|
|
.expect(HTTP_STATUS.CREATED);
|
2018-11-18 18:41:05 +01:00
|
|
|
|
2020-09-17 06:48:16 +02:00
|
|
|
await request(app)
|
|
|
|
.put('/forbidden-place')
|
|
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
|
|
.send(JSON.stringify(forbiddenPlace))
|
|
|
|
.expect(HTTP_STATUS.CREATED);
|
2018-11-18 18:41:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Packages', () => {
|
|
|
|
test('should display all packages', (done) => {
|
|
|
|
request(app)
|
2020-08-13 23:27:00 +02:00
|
|
|
.get('/-/verdaccio/packages')
|
2018-11-18 18:41:05 +01:00
|
|
|
.expect(HTTP_STATUS.OK)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2018-11-18 18:41:05 +01:00
|
|
|
expect(res.body).toHaveLength(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test.skip('should display scoped readme', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/package/readme/@scope/pk1-test')
|
|
|
|
.expect(HTTP_STATUS.OK)
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.TEXT_CHARSET)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2018-11-18 18:41:05 +01:00
|
|
|
expect(res.text).toMatch('<h1 id="test">test</h1>\n');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-23 09:29:27 +01:00
|
|
|
// FIXME: disabled, we need to inspect why fails randomly
|
2018-11-18 18:41:05 +01:00
|
|
|
test.skip('should display scoped readme 404', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/package/readme/@scope/404')
|
|
|
|
.expect(HTTP_STATUS.OK)
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.TEXT_CHARSET)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2018-11-18 18:41:05 +01:00
|
|
|
expect(res.body.error).toMatch(API_ERROR.NO_PACKAGE);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should display sidebar info', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/sidebar/@scope/pk1-test')
|
|
|
|
.expect(HTTP_STATUS.OK)
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2020-03-03 23:59:19 +01:00
|
|
|
// console.log("-->", res);
|
|
|
|
// expect(err).toBeNull();
|
|
|
|
|
2018-11-18 18:41:05 +01:00
|
|
|
const sideBarInfo = res.body;
|
|
|
|
const latestVersion = publishMetadata.versions[publishMetadata[DIST_TAGS].latest];
|
|
|
|
|
|
|
|
expect(sideBarInfo.latest.author).toBeDefined();
|
|
|
|
expect(sideBarInfo.latest.author.avatar).toMatch(/www.gravatar.com/);
|
|
|
|
expect(sideBarInfo.latest.author.name).toBe(latestVersion.author.name);
|
|
|
|
expect(sideBarInfo.latest.author.email).toBe(latestVersion.author.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-05 21:12:10 +02:00
|
|
|
test('should display sidebar info by version', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/sidebar/@scope/pk1-test?v=1.0.6')
|
|
|
|
.expect(HTTP_STATUS.OK)
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2019-09-05 21:12:10 +02:00
|
|
|
const sideBarInfo = res.body;
|
|
|
|
const latestVersion = publishMetadata.versions[publishMetadata[DIST_TAGS].latest];
|
|
|
|
|
|
|
|
expect(sideBarInfo.latest.author).toBeDefined();
|
|
|
|
expect(sideBarInfo.latest.author.avatar).toMatch(/www.gravatar.com/);
|
|
|
|
expect(sideBarInfo.latest.author.name).toBe(latestVersion.author.name);
|
|
|
|
expect(sideBarInfo.latest.author.email).toBe(latestVersion.author.email);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-18 18:41:05 +01:00
|
|
|
test('should display sidebar info 404', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/sidebar/@scope/404')
|
|
|
|
.expect(HTTP_STATUS.NOT_FOUND)
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function () {
|
2018-11-18 18:41:05 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2019-09-05 21:12:10 +02:00
|
|
|
|
|
|
|
test('should display sidebar info 404 with version', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/sidebar/@scope/pk1-test?v=0.0.0-not-found')
|
|
|
|
.expect(HTTP_STATUS.NOT_FOUND)
|
|
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function () {
|
2019-09-05 21:12:10 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-11-18 18:41:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Search', () => {
|
|
|
|
test('should search pk1-test', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/search/scope')
|
|
|
|
.expect(HTTP_STATUS.OK)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2018-11-18 18:41:05 +01:00
|
|
|
expect(res.body).toHaveLength(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should search with 404', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/search/@')
|
|
|
|
.expect(HTTP_STATUS.OK)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2018-11-18 18:41:05 +01:00
|
|
|
// in a normal world, the output would be 1
|
|
|
|
// https://github.com/verdaccio/verdaccio/issues/345
|
|
|
|
// should fix this
|
|
|
|
expect(res.body).toHaveLength(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not find forbidden-place', (done) => {
|
|
|
|
request(app)
|
|
|
|
.get('/-/verdaccio/search/forbidden-place')
|
|
|
|
.expect(HTTP_STATUS.OK)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2019-12-23 09:29:27 +01:00
|
|
|
// this is expected since we are not logged
|
2018-11-18 18:41:05 +01:00
|
|
|
// and forbidden-place is allow_access: 'nobody'
|
|
|
|
expect(res.body).toHaveLength(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('User', () => {
|
|
|
|
beforeAll(async () => {
|
2019-07-16 08:40:01 +02:00
|
|
|
await addUser(request(app), credentials.name, credentials);
|
2018-11-18 18:41:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('login webui', () => {
|
|
|
|
test('should log successfully', (done) => {
|
|
|
|
request(app)
|
|
|
|
.post('/-/verdaccio/login')
|
|
|
|
.send({
|
|
|
|
username: credentials.name,
|
2020-08-13 23:27:00 +02:00
|
|
|
password: credentials.password,
|
2018-11-18 18:41:05 +01:00
|
|
|
})
|
|
|
|
.expect(HTTP_STATUS.OK)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2020-03-03 23:59:19 +01:00
|
|
|
expect(err).toBeNull();
|
2018-11-18 18:41:05 +01:00
|
|
|
expect(res.body.error).toBeUndefined();
|
|
|
|
expect(res.body.token).toBeDefined();
|
|
|
|
expect(res.body.token).toBeTruthy();
|
|
|
|
expect(res.body.username).toMatch(credentials.name);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fails on log unvalid user', (done) => {
|
|
|
|
request(app)
|
|
|
|
.post('/-/verdaccio/login')
|
2020-08-13 23:27:00 +02:00
|
|
|
.send(
|
|
|
|
JSON.stringify({
|
|
|
|
username: 'fake',
|
|
|
|
password: 'fake',
|
|
|
|
})
|
|
|
|
)
|
2018-11-18 18:41:05 +01:00
|
|
|
// FIXME: there should be 401
|
|
|
|
.expect(HTTP_STATUS.OK)
|
2020-08-13 23:27:00 +02:00
|
|
|
.end(function (err, res) {
|
2018-11-18 18:41:05 +01:00
|
|
|
expect(res.body.error).toMatch(/bad username\/password, access denied/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|