refactor: unit test goes offline

This commit is contained in:
Juan Picado @jotadeveloper 2018-06-24 22:39:09 +02:00
parent e12dc5b477
commit 57f0fa7610
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
13 changed files with 268 additions and 194 deletions

View File

@ -1,3 +1,5 @@
import {DOMAIN_SERVERS as localhost} from '../test.conf';
export const CREDENTIALS = {
user: 'test',
password: 'test'
@ -8,4 +10,5 @@ export const PORT_SERVER_APP = '55550';
export const PORT_SERVER_1 = '55551';
export const PORT_SERVER_2 = '55552';
export const PORT_SERVER_3 = '55553';
export const DOMAIN_SERVERS = 'localhost';
export const DOMAIN_SERVERS = localhost;

View File

@ -77,9 +77,7 @@ class FunctionalEnvironment extends NodeEnvironment {
async teardown() {
await super.teardown();
console.log(chalk.yellow('Teardown Test Environment.'));
// this.global.__VERDACCIO_E2E__.stop();
// this.global.__VERDACCIO__PROTECTED_E2E__.stop();
// close verdaccios
// shutdown verdaccio
for (let server of this.global.__SERVERS_PROCESS__) {
server[0].stop();
}

View File

@ -120,7 +120,5 @@ packages:
access: test $anonymous
publish: test $anonymous
listen: 55551
# expose internal methods
_debug: true

View File

@ -93,7 +93,5 @@ packages:
access: test $anonymous
publish: test $anonymous
listen: 55552
# expose internal methods
_debug: true

View File

@ -38,7 +38,5 @@ packages:
'*':
access: $all
listen: 55553
# expose internal methods
_debug: true

View File

@ -14,65 +14,78 @@ export default class VerdaccioProcess implements IServerProcess {
childFork: any;
isDebug: boolean;
silence: boolean;
cleanStore: boolean;
constructor(config: IVerdaccioConfig, bridge: IServerBridge, silence: boolean = true, isDebug: boolean = false) {
constructor(config: IVerdaccioConfig,
bridge: IServerBridge,
silence: boolean = true,
isDebug: boolean = false,
cleanStore: boolean = true) {
this.config = config;
this.bridge = bridge;
this.silence = silence;
this.isDebug = isDebug;
this.cleanStore = cleanStore;
}
init(): Promise<any> {
init(verdaccioPath: string = '../../bin/verdaccio'): Promise<any> {
return new Promise((resolve, reject) => {
const verdaccioRegisterWrap: string = path.join(__dirname, '../../bin/verdaccio');
rimRaf(this.config.storagePath, (err) => {
if (_.isNil(err) === false) {
reject(err);
}
let childOptions = {
silent: this.silence
};
if (this.isDebug) {
const debugPort = parseInt(this.config.port, 10) + 5;
childOptions = Object.assign({}, childOptions, {
execArgv: [`--inspect=${debugPort}`]
});
}
this.childFork = fork(verdaccioRegisterWrap, ['-c', this.config.configPath], childOptions);
this.childFork.on('message', (msg) => {
if ('verdaccio_started' in msg) {
this.bridge.debug().status(HTTP_STATUS.OK).then((body) => {
this.bridge.auth(CREDENTIALS.user, CREDENTIALS.password)
.status(HTTP_STATUS.CREATED)
.body_ok(new RegExp(CREDENTIALS.user))
.then(() => {
resolve([this, body.pid]);
}, reject)
}, reject);
if(this.cleanStore) {
rimRaf(this.config.storagePath, (err) => {
if (_.isNil(err) === false) {
reject(err);
}
});
this.childFork.on('error', (err) => {
console.log('error process', err);
reject([err, this]);
this._start(verdaccioPath, resolve, reject);
});
} else {
this._start(verdaccioPath, resolve, reject);
}
});
}
this.childFork.on('disconnect', (err) => {
reject([err, this]);
});
_start(verdaccioPath: string, resolve: Function, reject: Function) {
const verdaccioRegisterWrap: string = path.join(__dirname, verdaccioPath);
let childOptions = {
silent: this.silence
};
this.childFork.on('exit', (err) => {
reject([err, this]);
});
if (this.isDebug) {
const debugPort = parseInt(this.config.port, 10) + 5;
childOptions = Object.assign({}, childOptions, {
execArgv: [`--inspect=${debugPort}`]
});
}
const {configPath, port} = this.config;
// $FlowFixMe
this.childFork = fork(verdaccioRegisterWrap, ['-c', configPath, '-l', port], childOptions);
this.childFork.on('message', (msg) => {
if ('verdaccio_started' in msg) {
this.bridge.debug().status(HTTP_STATUS.OK).then((body) => {
this.bridge.auth(CREDENTIALS.user, CREDENTIALS.password)
.status(HTTP_STATUS.CREATED)
.body_ok(new RegExp(CREDENTIALS.user))
.then(() => {
resolve([this, body.pid]);
}, reject)
}, reject);
}
});
this.childFork.on('error', (err) => {
console.log('error process', err);
reject([err, this]);
});
this.childFork.on('disconnect', (err) => {
reject([err, this]);
});
this.childFork.on('exit', (err) => {
reject([err, this]);
});
}

1
test/test.conf.js Normal file
View File

@ -0,0 +1 @@
export const DOMAIN_SERVERS = 'localhost';

View File

@ -8,7 +8,10 @@ import publishMetadata from '../partials/publish-api';
import forbiddenPlace from '../partials/forbidden-place';
import Config from '../../../src/lib/config';
import endPointAPI from '../../../src/api/index';
import {HEADERS, API_ERROR} from '../../../src/lib/constants';
import {HEADERS, API_ERROR, HTTP_STATUS, HEADER_TYPE} from '../../../src/lib/constants';
import {mockServer} from './mock';
import {DOMAIN_SERVERS} from '../../functional/config.functional';
require('../../../src/lib/logger').setup([]);
const credentials = { name: 'Jota', password: 'secretPass' };
@ -16,10 +19,11 @@ const credentials = { name: 'Jota', password: 'secretPass' };
describe('endpoint unit test', () => {
let config;
let app;
jest.setTimeout(10000);
let mockRegistry;
beforeAll(function(done) {
const store = path.join(__dirname, '../partials/store/test-storage');
const mockServerPort = 55549;
rimraf(store, async () => {
const configForTest = _.clone(configDefault);
configForTest.auth = {
@ -27,21 +31,32 @@ describe('endpoint unit test', () => {
file: './test-storage/htpasswd-test'
}
};
configForTest.uplinks = {
npmjs: {
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`
}
};
configForTest.self_path = store;
config = new Config(configForTest);
app = await endPointAPI(config);
mockRegistry = await mockServer(mockServerPort).init();
done();
});
});
afterAll(function(done) {
mockRegistry[0].stop();
done();
});
describe('Registry API Endpoints', () => {
describe('should test ping api', () => {
test('should test endpoint /-/ping', (done) => {
request(app)
.get('/-/ping')
.expect('Content-Type', /json/)
.expect(200)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -55,8 +70,8 @@ describe('endpoint unit test', () => {
test('should test /-/whoami endpoint', (done) => {
request(app)
.get('/-/whoami')
.expect('Content-Type', /json/)
.expect(200)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -68,8 +83,8 @@ describe('endpoint unit test', () => {
test('should test /whoami endpoint', (done) => {
request(app)
.get('/-/whoami')
.expect('Content-Type', /json/)
.expect(200)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -86,8 +101,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/auth-package')
.set('authorization', 'FakeHader')
.expect('Content-Type', /json/)
.expect(403)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.FORBIDDEN)
.end(function(err, res) {
expect(res.body.error).toBeDefined();
expect(res.body.error).toMatch(/unregistered users are not allowed to access package auth-package/);
@ -99,8 +114,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/auth-package')
.set('authorization', 'Bearer')
.expect('Content-Type', /json/)
.expect(403)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.FORBIDDEN)
.end(function(err, res) {
expect(res.body.error).toBeDefined();
expect(res.body.error).toMatch(/unregistered users are not allowed to access package auth-package/);
@ -112,8 +127,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/auth-package')
.set('authorization', 'Bearer 12345')
.expect('Content-Type', /json/)
.expect(403)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.FORBIDDEN)
.end(function(err, res) {
expect(res.body.error).toBeDefined();
expect(res.body.error).toMatch(/unregistered users are not allowed to access package auth-package/);
@ -127,8 +142,8 @@ describe('endpoint unit test', () => {
request(app)
.put('/-/user/org.couchdb.user:jota')
.send(credentials)
.expect('Content-Type', /json/)
.expect(201)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.CREATED)
.end(function(err, res) {
if (err) {
return done(err);
@ -144,8 +159,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/vue')
.set('authorization', `Bearer ${token}`)
.expect('Content-Type', /json/)
.expect(200)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
expect(err).toBeNull();
expect(res.body).toBeDefined();
@ -163,8 +178,8 @@ describe('endpoint unit test', () => {
request(app)
.put('/-/user/org.couchdb.user:jota')
.send(credentialsShort)
.expect('Content-Type', /json/)
.expect(400)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.BAD_REQUEST)
.end(function(err, res) {
if (err) {
return done(err);
@ -184,8 +199,8 @@ describe('endpoint unit test', () => {
request(app)
.put('/-/user/org.couchdb.user:jota')
.send(credentialsShort)
.expect('Content-Type', /json/)
.expect(400)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.BAD_REQUEST)
.end(function(err, res) {
if (err) {
return done(err);
@ -205,8 +220,8 @@ describe('endpoint unit test', () => {
request(app)
.put('/-/user/org.couchdb.user:jotaNew')
.send(newCredentials)
.expect('Content-Type', /json/)
.expect(201)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.CREATED)
.end(function(err, res) {
if (err) {
return done(err);
@ -220,8 +235,8 @@ describe('endpoint unit test', () => {
request(app)
.put('/-/user/org.couchdb.user:jotaNew')
.send(credentials)
.expect('Content-Type', /json/)
.expect(409)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.CONFLICT)
.end(function(err, res) {
if (err) {
return done(err);
@ -240,8 +255,8 @@ describe('endpoint unit test', () => {
request(app)
.put('/-/user/org.couchdb.user:jota')
.send(credentialsShort)
.expect('Content-Type', /json/)
.expect(401)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.UNAUTHORIZED)
.end(function(err, res) {
if (err) {
return done(err);
@ -261,9 +276,9 @@ describe('endpoint unit test', () => {
request(app)
.get('/jquery')
.set('content-type', HEADERS.JSON_CHARSET)
.expect('Content-Type', /json/)
.expect(200)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -279,9 +294,9 @@ describe('endpoint unit test', () => {
request(app)
.get('/jquery/1.5.1')
.set('content-type', HEADERS.JSON_CHARSET)
.expect('Content-Type', /json/)
.expect(200)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -297,9 +312,9 @@ describe('endpoint unit test', () => {
request(app)
.get('/jquery/latest')
.set('content-type', HEADERS.JSON_CHARSET)
.expect('Content-Type', /json/)
.expect(200)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -315,9 +330,9 @@ describe('endpoint unit test', () => {
request(app)
.get('/jquery/never-will-exist-this-tag')
.set('content-type', HEADERS.JSON_CHARSET)
.expect('Content-Type', /json/)
.expect(404)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.NOT_FOUND)
.end(function(err, res) {
if (err) {
return done(err);
@ -330,9 +345,9 @@ describe('endpoint unit test', () => {
request(app)
.get('/@verdaccio/not-found')
.set('content-type', HEADERS.JSON_CHARSET)
.expect('Content-Type', /json/)
.expect(404)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.NOT_FOUND)
.end(function(err, res) {
if (err) {
return done(err);
@ -345,9 +360,9 @@ describe('endpoint unit test', () => {
request(app)
.get('/forbidden-place')
.set('content-type', HEADERS.JSON_CHARSET)
.expect('Content-Type', /json/)
.expect(403)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.FORBIDDEN)
.end(function(err, res) {
if (err) {
return done(err);
@ -360,8 +375,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/jquery/-/jquery-1.5.1.tgz')
.expect('Content-Type', /application\/octet-stream/)
.expect(200)
.expect(HEADER_TYPE.CONTENT_TYPE, /application\/octet-stream/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -376,8 +391,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/jquery/-/jquery-0.0.1.tgz')
.expect('Content-Type', /application\/octet-stream/)
.expect(404)
.expect(HEADER_TYPE.CONTENT_TYPE, /application\/octet-stream/)
.expect(HTTP_STATUS.NOT_FOUND)
.end(function(err, res) {
if (err) {
return done(err);
@ -403,8 +418,8 @@ describe('endpoint unit test', () => {
.send(JSON.stringify(jqueryVersion))
.set('accept', 'gzip')
.set('accept-encoding', HEADERS.JSON)
.set('content-type', HEADERS.JSON)
.expect(201)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.expect(HTTP_STATUS.CREATED)
.end(function(err, res) {
if (err) {
return done(err);
@ -421,8 +436,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/-/package/jquery/dist-tags')
.set('accept-encoding', HEADERS.JSON)
.set('content-type', HEADERS.JSON)
.expect(200)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -439,8 +454,8 @@ describe('endpoint unit test', () => {
request(app)
.post('/-/package/jquery/dist-tags')
.send(JSON.stringify(jqueryUpdatedVersion))
.set('content-type', HEADERS.JSON)
.expect(201)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.expect(HTTP_STATUS.CREATED)
.end(function(err, res) {
if (err) {
return done(err);
@ -457,8 +472,8 @@ describe('endpoint unit test', () => {
request(app)
.get('/-/package/jquery/dist-tags')
.set('accept-encoding', HEADERS.JSON)
.set('content-type', HEADERS.JSON)
.expect(200)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -475,9 +490,9 @@ describe('endpoint unit test', () => {
request(app)
.del('/-/package/jquery/dist-tags/verdaccio-tag')
.set('accept-encoding', HEADERS.JSON)
.set('content-type', HEADERS.JSON)
//.expect('Content-Type', /json/)
.expect(201)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
//.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.CREATED)
.end(function(err, res) {
if (err) {
return done(err);
@ -498,9 +513,9 @@ describe('endpoint unit test', () => {
request(app)
.get('/-/all/since?stale=update_after&startkey=' + cacheTime)
// .set('accept-encoding', HEADERS.JSON)
// .set('content-type', HEADERS.JSON)
//.expect('Content-Type', /json/)
.expect(200)
// .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
//.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
if (err) {
return done(err);
@ -517,9 +532,9 @@ describe('endpoint unit test', () => {
test('should publish a new package', (done) => {
request(app)
.put('/@scope%2fpk1-test')
.set('content-type', HEADERS.JSON)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.send(JSON.stringify(publishMetadata))
.expect(201)
.expect(HTTP_STATUS.CREATED)
.end(function(err, res) {
if (err) {
return done(err);
@ -536,8 +551,8 @@ describe('endpoint unit test', () => {
//FUTURE: for some reason it does not remove the scope folder
request(app)
.del('/@scope%2fpk1-test/-rev/4-6abcdb4efd41a576')
.set('content-type', HEADERS.JSON)
.expect(201)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.expect(HTTP_STATUS.CREATED)
.end(function(err, res) {
if (err) {
return done(err);
@ -554,15 +569,15 @@ describe('endpoint unit test', () => {
beforeAll(async function() {
await request(app)
.put('/@scope%2fpk1-test')
.set('content-type', HEADERS.JSON)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.send(JSON.stringify(publishMetadata))
.expect(201);
.expect(HTTP_STATUS.CREATED);
await request(app)
.put('/forbidden-place')
.set('content-type', HEADERS.JSON)
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
.send(JSON.stringify(forbiddenPlace))
.expect(201);
.expect(HTTP_STATUS.CREATED);
});
describe('Packages', () => {
@ -570,7 +585,7 @@ describe('endpoint unit test', () => {
test('should display all packages', (done) => {
request(app)
.get('/-/verdaccio/packages' )
.expect(200)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
expect(res.body).toHaveLength(1);
done();
@ -580,8 +595,8 @@ describe('endpoint unit test', () => {
test.skip('should display scoped readme', (done) => {
request(app)
.get('/-/verdaccio/package/readme/@scope/pk1-test')
.expect(200)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(HTTP_STATUS.OK)
.expect(HEADER_TYPE.CONTENT_TYPE, 'text/plain; charset=utf-8')
.end(function(err, res) {
expect(res.text).toMatch('<h1 id="test">test</h1>\n');
done();
@ -592,8 +607,8 @@ describe('endpoint unit test', () => {
test.skip('should display scoped readme 404', (done) => {
request(app)
.get('/-/verdaccio/package/readme/@scope/404')
.expect(200)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(HTTP_STATUS.OK)
.expect(HEADER_TYPE.CONTENT_TYPE, 'text/plain; charset=utf-8')
.end(function(err, res) {
expect(res.body.error).toMatch(API_ERROR.NO_PACKAGE);
done();
@ -603,8 +618,8 @@ describe('endpoint unit test', () => {
test('should display sidebar info', (done) => {
request(app)
.get('/-/verdaccio/sidebar/@scope/pk1-test')
.expect(200)
.expect('Content-Type', /json/)
.expect(HTTP_STATUS.OK)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.end(function(err, res) {
const sideBarInfo = res.body;
const latestVersion = publishMetadata.versions[publishMetadata['dist-tags'].latest];
@ -620,8 +635,8 @@ describe('endpoint unit test', () => {
test('should display sidebar info 404', (done) => {
request(app)
.get('/-/verdaccio/sidebar/@scope/404')
.expect(404)
.expect('Content-Type', /json/)
.expect(HTTP_STATUS.NOT_FOUND)
.expect(HEADER_TYPE.CONTENT_TYPE, /json/)
.end(function(err, res) {
done();
});
@ -633,7 +648,7 @@ describe('endpoint unit test', () => {
test('should search pk1-test', (done) => {
request(app)
.get('/-/verdaccio/search/scope')
.expect(200)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
expect(res.body).toHaveLength(1);
done();
@ -643,7 +658,7 @@ describe('endpoint unit test', () => {
test('should search with 404', (done) => {
request(app)
.get('/-/verdaccio/search/@')
.expect(200)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
// in a normal world, the output would be 1
// https://github.com/verdaccio/verdaccio/issues/345
@ -656,7 +671,7 @@ describe('endpoint unit test', () => {
test('should not find forbidden-place', (done) => {
request(app)
.get('/-/verdaccio/search/forbidden-place')
.expect(200)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
//this is expected since we are not logged
// and forbidden-place is allow_access: 'nobody'
@ -675,7 +690,7 @@ describe('endpoint unit test', () => {
username: credentials.name,
password: credentials.password
})
.expect(200)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
expect(res.body.error).toBeUndefined();
expect(res.body.token).toBeDefined();
@ -693,7 +708,7 @@ describe('endpoint unit test', () => {
password: 'fake'
}))
//FIXME: there should be 401
.expect(200)
.expect(HTTP_STATUS.OK)
.end(function(err, res) {
expect(res.body.error).toMatch(/bad username\/password, access denied/);
done();

18
test/unit/api/mock.js Normal file
View File

@ -0,0 +1,18 @@
// @flow
import path from 'path';
import {DOMAIN_SERVERS} from '../../functional/config.functional';
import VerdaccioProcess from '../../lib/server_process';
import {VerdaccioConfig} from '../../lib/verdaccio-server';
import Server from '../../lib/server';
import type {IServerBridge} from '../../types';
export function mockServer(port: number) {
const pathStore = path.join(__dirname, '../partials');
const verdaccioConfig = new VerdaccioConfig(
path.join(pathStore, '/mock-store'),
path.join(pathStore, '/config-unit-test.yaml'), `http://${DOMAIN_SERVERS}:${port}/`, port);
const server: IServerBridge = new Server(verdaccioConfig.domainPath);
return new VerdaccioProcess(verdaccioConfig, server, false, false, false);
}

View File

@ -1,7 +1,6 @@
// @flow
import _ from 'lodash';
import httpMocks from 'node-mocks-http';
// $FlowFixMe
import configExample from '../partials/config/index';
import AppConfig from '../../../src/lib/config';
@ -11,73 +10,69 @@ import {setup} from '../../../src/lib/logger';
import type {Config} from '@verdaccio/types';
import type {IStorageHandler} from '../../../types/index';
import {API_ERROR} from '../../../src/lib/constants';
import {mockServer} from './mock';
import {DOMAIN_SERVERS} from '../../functional/config.functional';
setup(configExample.logs);
const generateStorage = async function() {
const mockServerPort: number = 55548;
const generateStorage = async function(port = mockServerPort) {
const storageConfig = _.clone(configExample);
const storage = `./unit/partials/store/test-storage-store.spec`;
const storage = `./unit/partials/store/test-storage-store.spec`;
storageConfig.self_path = __dirname;
storageConfig.storage = storage;
const config: Config = new AppConfig(storageConfig);
storageConfig.uplinks = {
npmjs: {
url: `http://${DOMAIN_SERVERS}:${port}`
}
};
const config: Config = new AppConfig(storageConfig);
const store: IStorageHandler = new Storage(config);
await store.init(config);
return store;
return store;
}
describe('StorageTest', () => {
let mockRegistry;
jest.setTimeout(10000);
beforeAll(async () => {
mockRegistry = await mockServer(mockServerPort).init();
});
beforeAll(async (done)=> {
const storage: IStorageHandler = await generateStorage();
var request = httpMocks.createRequest({
method: 'GET',
url: '/react',
params: {}
});
afterAll(function(done) {
mockRegistry[0].stop();
done();
});
storage.getPackage({
name: 'react',
req: request,
callback: () => {
const stream = storage.getTarball('react', 'react-16.1.0.tgz');
stream.on('content-length', function(content) {
if (content) {
expect(content).toBeTruthy();
done();
}
});
},
});
});
test('should be defined', async () => {
const storage: IStorageHandler = await generateStorage();
test('should be defined', async () => {
const storage: IStorageHandler = await generateStorage();
expect(storage).toBeDefined();
});
});
test('should fetch from uplink react metadata from nmpjs', async (done) => {
const storage: IStorageHandler = await generateStorage();
test('should fetch from uplink jquery metadata from nmpjs', async (done) => {
const storage: IStorageHandler = await generateStorage();
// $FlowFixMe
storage._syncUplinksMetadata('react', null, {}, (err, metadata, errors) => {
expect(metadata).toBeInstanceOf(Object);
done();
});
});
// $FlowFixMe
storage._syncUplinksMetadata('jquery', null, {}, (err, metadata, errors) => {
expect(err).toBeNull();
expect(metadata).toBeDefined();
expect(metadata).toBeInstanceOf(Object);
done();
});
});
test('should fails on fetch from uplink metadata from nmpjs', async (done) => {
const storage: IStorageHandler = await generateStorage();
test('should fails on fetch from uplink metadata from nmpjs', async (done) => {
const storage: IStorageHandler = await generateStorage();
// $FlowFixMe
storage._syncUplinksMetadata('@verdaccio/404', null, {}, (err, metadata, errors) => {
expect(errors).toBeInstanceOf(Array);
expect(errors[0][0].statusCode).toBe(404);
expect(errors[0][0].message).toMatch(API_ERROR.NOT_PACKAGE_UPLINK);
done();
});
// $FlowFixMe
storage._syncUplinksMetadata('@verdaccio/404', null, {}, (err, metadata, errors) => {
expect(err).not.toBeNull();
expect(errors).toBeInstanceOf(Array);
expect(errors[0][0].statusCode).toBe(404);
expect(errors[0][0].message).toMatch(API_ERROR.NOT_PACKAGE_UPLINK);
done();
});
});
});

View File

@ -8,15 +8,17 @@ import {setup} from '../../../src/lib/logger';
import type {Config, UpLinkConf} from '@verdaccio/types';
import type {IProxy} from '../../../types/index';
import {API_ERROR, DEFAULT_REGISTRY} from "../../../src/lib/constants";
import {API_ERROR} from "../../../src/lib/constants";
import {mockServer} from './mock';
import {DOMAIN_SERVERS} from '../../functional/config.functional';
setup([]);
describe('UpStorge', () => {
jest.setTimeout(10000);
const mockServerPort: number = 55547;
let mockRegistry;
const uplinkDefault = {
url: DEFAULT_REGISTRY
url: `http://0.0.0.0:${mockServerPort}`
};
const generateProxy = (config: UpLinkConf = uplinkDefault) => {
const appConfig: Config = new AppConfig(configExample);
@ -24,6 +26,15 @@ describe('UpStorge', () => {
return new ProxyStorage(config, appConfig);
};
beforeAll(async () => {
mockRegistry = await mockServer(mockServerPort).init();
});
afterAll(function(done) {
mockRegistry[0].stop();
done();
});
test('should be defined', () => {
const proxy = generateProxy();
@ -69,7 +80,7 @@ describe('UpStorge', () => {
describe('UpStorge::fetchTarball', () => {
test('should fetch a tarball from uplink', (done) => {
const proxy = generateProxy();
const tarball:string = 'https://registry.npmjs.org/aaa/-/aaa-0.0.1.tgz';
const tarball:string = `http://${DOMAIN_SERVERS}:${mockServerPort}/jquery/-/jquery-1.5.1.tgz`;
const stream = proxy.fetchTarball(tarball);
stream.on('error', function(err) {
@ -86,7 +97,7 @@ describe('UpStorge', () => {
test('should throw a 404 on fetch a tarball from uplink', (done) => {
const proxy = generateProxy();
const tarball:string = 'https://google.es/aaa/-/aaa-0.0.1.tgz';
const tarball:string = `http://${DOMAIN_SERVERS}:${mockServerPort}/jquery/-/no-exist-1.5.1.tgz`;
const stream = proxy.fetchTarball(tarball);
stream.on('error', function(err) {

View File

@ -4,7 +4,7 @@ import _ from 'lodash';
import smartRequest, {PromiseAssert} from '../../lib/request';
import type {IRequestPromise} from '../../types';
describe('Request Functional', () => {
describe.skip('Request Functional', () => {
const restTest: string = "http://registry.npmjs.org/aaa";

View File

@ -0,0 +1,26 @@
storage: ./mock-store
web:
enable: true
title: verdaccio-server-unit-test
auth:
auth-memory:
users:
test:
name: test
password: test
logs:
- {type: stdout, format: pretty, level: warn}
packages:
'@*/*':
access: $all
publish: none
'**':
access: $all
publish: none
_debug: true