0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-27 22:59:51 -05:00

refactor: update jetst match

using more consntants
This commit is contained in:
Juan Picado @jotadeveloper 2018-06-21 08:10:33 +02:00
parent 55927d5408
commit 2c8c8f0295
No known key found for this signature in database
GPG key ID: 18AC54485952D158
7 changed files with 73 additions and 55 deletions

View file

@ -17,10 +17,12 @@ export const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';
export const HTTP_STATUS = { export const HTTP_STATUS = {
INTERNAL_ERROR: 500, INTERNAL_ERROR: 500,
SERVICE_UNAVAILABLE: 503,
OK: 200, OK: 200,
CREATED: 201, CREATED: 201,
BAD_REQUEST: 400, BAD_REQUEST: 400,
UNAUTHORIZED: 401, UNAUTHORIZED: 401,
FORBIDDEN: 403, FORBIDDEN: 403,
NOT_FOUND: 404, NOT_FOUND: 404,
CONFLICT: 409,
}; };

View file

@ -1,3 +1,5 @@
import {HTTP_STATUS} from "../../../src/lib/constants";
export default function(server) { export default function(server) {
describe('npm adduser', () => { describe('npm adduser', () => {
const user = String(Math.random()); const user = String(Math.random());
@ -5,7 +7,7 @@ export default function(server) {
beforeAll(function() { beforeAll(function() {
return server.auth(user, pass) return server.auth(user, pass)
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/user .* created/); .body_ok(/user .* created/);
}); });
@ -13,13 +15,13 @@ export default function(server) {
test('should log in', () => { test('should log in', () => {
return server.auth(user, pass) return server.auth(user, pass)
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/you are authenticated as/); .body_ok(/you are authenticated as/);
}); });
test('should not register more users', () => { test('should not register more users', () => {
return server.auth(String(Math.random()), String(Math.random())) return server.auth(String(Math.random()), String(Math.random()))
.status(409) .status(HTTP_STATUS.CONFLICT)
.body_error(/maximum amount of users reached/); .body_error(/maximum amount of users reached/);
}); });
}); });

View file

@ -1,9 +1,11 @@
import {HTTP_STATUS} from "../../../src/lib/constants";
export default function(server) { export default function(server) {
describe('logout', () => { describe('logout', () => {
test('should log out', () => { test('should log out', () => {
return server.logout('some-token') return server.logout('some-token')
.status(200) .status(HTTP_STATUS.OK)
.body_ok(/Logged out/); .body_ok(/Logged out/);
}); });
}); });

View file

@ -1,34 +1,39 @@
import assert from 'assert'; // @flow
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import {createTarballHash} from "../../../src/lib/crypto-utils";
import {HTTP_STATUS} from "../../../src/lib/constants";
import {CREDENTIALS} from "../config.func";
import whoIam from './whoIam';
import ping from './ping';
function readfile(folderPath) { function readfile(folderPath) {
return require('fs').readFileSync(__dirname + '/' + folderPath); return fs.readFileSync(path.join(__dirname, '/', folderPath));
} }
function getPackage(name) { function getPackage(name) {
return require('../fixtures/package')(name); return require('../fixtures/package')(name);
} }
function createHash() { export default function(server: any, server2: any) {
return crypto.createHash('sha1');
}
export default function(server, server2) {
describe('basic test endpoints', () => { describe('basic test endpoints', () => {
const PKG_NAME:string = 'testpkg';
beforeAll(function() { beforeAll(function() {
return server.auth('test', 'test') return server.auth(CREDENTIALS.user, CREDENTIALS.password)
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/'test'/); .body_ok(/'test'/);
}); });
require('./whoIam')(server); whoIam(server);
require('./ping')(server); ping(server);
describe('handling packages', () => { describe('handling packages', () => {
beforeAll(function () { beforeAll(function () {
return server.addPackage('testpkg'); return server.addPackage(PKG_NAME);
}); });
beforeAll(function () { beforeAll(function () {
@ -39,29 +44,31 @@ export default function(server, server2) {
}); });
test('downloading non-existent tarball', () => { test('downloading non-existent tarball', () => {
return server.getTarball('testpkg', 'blahblah').status(404).body_error(/no such file/); return server.getTarball(PKG_NAME, 'blahblah')
.status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such file/);
}); });
test('uploading incomplete tarball', () => { test('uploading incomplete tarball', () => {
return server.putTarballIncomplete('testpkg', 'blahblah1', readfile('../fixtures/binary'), 3000); return server.putTarballIncomplete(PKG_NAME, 'blahblah1', readfile('../fixtures/binary'), 3000);
}); });
describe('publishing package', () => { describe('publishing package', () => {
beforeAll(function () { beforeAll(function () {
return server.putTarball('testpkg', 'blahblah', readfile('../fixtures/binary')) return server.putTarball(PKG_NAME, 'blahblah', readfile('../fixtures/binary'))
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/.*/); .body_ok(/.*/);
}); });
beforeAll(function () { beforeAll(function () {
return server.putTarball('testpkg-single-tarball', 'single', readfile('../fixtures/binary')) return server.putTarball('testpkg-single-tarball', 'single', readfile('../fixtures/binary'))
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/.*/); .body_ok(/.*/);
}); });
afterAll(function () { afterAll(function () {
return server.removeTarball('testpkg').status(201); return server.removeTarball(PKG_NAME).status(HTTP_STATUS.CREATED);
}); });
test('remove a tarball', () => { test('remove a tarball', () => {
@ -73,46 +80,46 @@ export default function(server, server2) {
}); });
test('remove non existing tarball', () => { test('remove non existing tarball', () => {
return server.removeTarball('testpkg404').status(404); return server.removeTarball('testpkg404').status(HTTP_STATUS.NOT_FOUND);
}); });
test('remove non existing single tarball', () => { test('remove non existing single tarball', () => {
return server.removeSingleTarball('', 'fakeFile').status(404); return server.removeSingleTarball('', 'fakeFile').status(HTTP_STATUS.NOT_FOUND);
}); });
// testexp-incomplete // testexp-incomplete
test('remove existing single tarball', () => { test('remove existing single tarball', () => {
return server.removeSingleTarball('testpkg-single-tarball', 'single').status(201); return server.removeSingleTarball('testpkg-single-tarball', 'single').status(HTTP_STATUS.CREATED);
}); });
// testexp-incomplete // testexp-incomplete
test('downloading newly created tarball', () => { test('downloading newly created tarball', () => {
return server.getTarball('testpkg', 'blahblah') return server.getTarball(PKG_NAME, 'blahblah')
.status(200) .status(200)
.then(function (body) { .then(function (body) {
assert.deepEqual(body, readfile('../fixtures/binary')); expect(body).toEqual(readfile('../fixtures/binary'));
}); });
}); });
test('uploading new package version (bad sha)', () => { test('uploading new package version (bad sha)', () => {
let pkg = getPackage('testpkg'); let pkg = getPackage(PKG_NAME);
pkg.dist.shasum = createHash().update('fake').digest('hex'); pkg.dist.shasum = createTarballHash().update('fake').digest('hex');
return server.putVersion('testpkg', '0.0.1', pkg) return server.putVersion(PKG_NAME, '0.0.1', pkg)
.status(400) .status(HTTP_STATUS.BAD_REQUEST)
.body_error(/shasum error/); .body_error(/shasum error/);
}); });
describe('publishing version', () => { describe('publishing version', () => {
beforeAll(function () { beforeAll(function () {
const pkg = getPackage('testpkg'); const pkg = getPackage(PKG_NAME);
pkg.dist.shasum = createHash().update(readfile('../fixtures/binary')).digest('hex'); pkg.dist.shasum = createTarballHash().update(readfile('../fixtures/binary')).digest('hex');
return server.putVersion('testpkg', '0.0.1', pkg) return server.putVersion(PKG_NAME, '0.0.1', pkg)
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/published/); .body_ok(/published/);
}); });
@ -123,31 +130,31 @@ export default function(server, server2) {
describe('should download a package', () => { describe('should download a package', () => {
beforeAll(function() { beforeAll(function() {
return server.auth('test', 'test') return server.auth('test', 'test')
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/'test'/); .body_ok(/'test'/);
}); });
test('should download a newly created package from server1', () => { test('should download a newly created package from server1', () => {
return server.getPackage('testpkg') return server.getPackage(PKG_NAME)
.status(200) .status(200)
.then(function (body) { .then(function (body) {
assert.equal(body.name, 'testpkg'); expect(body.name).toEqual(PKG_NAME);
assert.equal(body.versions['0.0.1'].name, 'testpkg'); expect(body.versions['0.0.1'].name).toEqual(PKG_NAME);
assert.equal(body.versions['0.0.1'].dist.tarball, 'http://localhost:55551/testpkg/-/blahblah'); expect(body.versions['0.0.1'].dist.tarball).toEqual('http://localhost:55551/testpkg/-/blahblah');
assert.deepEqual(body['dist-tags'], { expect(body['dist-tags']).toEqual({
latest: '0.0.1' latest: '0.0.1'
}); });
}); });
}); });
test('should downloading a package from server2', () => { test('should downloading a package from server2', () => {
return server2.getPackage('testpkg') return server2.getPackage(PKG_NAME)
.status(200) .status(200)
.then(function (body) { .then(function (body) {
assert.equal(body.name, 'testpkg'); expect(body.name).toEqual(PKG_NAME);
assert.equal(body.versions['0.0.1'].name, 'testpkg'); expect(body.versions['0.0.1'].name).toEqual(PKG_NAME);
assert.equal(body.versions['0.0.1'].dist.tarball, 'http://localhost:55552/testpkg/-/blahblah'); expect(body.versions['0.0.1'].dist.tarball).toEqual('http://localhost:55552/testpkg/-/blahblah');
assert.deepEqual(body['dist-tags'], { expect(body['dist-tags']).toEqual({
latest: '0.0.1' latest: '0.0.1'
}); });
}); });
@ -163,27 +170,27 @@ export default function(server, server2) {
test('should fails trying to fetch non-existent package', () => { test('should fails trying to fetch non-existent package', () => {
return server.getPackage('testpkg').status(404).body_error(/no such package/); return server.getPackage(PKG_NAME).status(HTTP_STATUS.NOT_FOUND).body_error(/no such package/);
}); });
test( test(
'should fails on publish a version for non existing package', 'should fails on publish a version for non existing package',
() => { () => {
return server.putVersion('testpxg', '0.0.1', getPackage('testpxg')) return server.putVersion('testpxg', '0.0.1', getPackage('testpxg'))
.status(404) .status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such package/); .body_error(/no such package/);
} }
); );
test('should be a package not found', () => { test('should be a package not found', () => {
return server.putTarball('nonExistingPackage', 'blahblah', readfile('../fixtures/binary')) return server.putTarball('nonExistingPackage', 'blahblah', readfile('../fixtures/binary'))
.status(404) .status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such/); .body_error(/no such/);
}); });
test('should fails on publish package in a bad uplink', () => { test('should fails on publish package in a bad uplink', () => {
return server.putPackage('baduplink', getPackage('baduplink')) return server.putPackage('baduplink', getPackage('baduplink'))
.status(503) .status(HTTP_STATUS.SERVICE_UNAVAILABLE)
.body_error(/one of the uplinks is down, refuse to publish/); .body_error(/one of the uplinks is down, refuse to publish/);
}); });

View file

@ -1,12 +1,11 @@
const assert = require('assert'); import _ from 'lodash';
const _ = require('lodash');
module.exports = function(server) { module.exports = function(server) {
test('ping', () => { test('ping', () => {
return server.ping().then(function (data) { return server.ping().then(function (data) {
// it's always an empty object // it's always an empty object
assert.ok(_.isObject(data)); expect(_.isObject(data)).toBeDefined();
}); });
}); });

View file

@ -1,8 +1,10 @@
import {CREDENTIALS} from "../config.func";
module.exports = function(server) { module.exports = function(server) {
test('who am I?', () => { test('who am I?', () => {
return server.whoami().then(function (username) { return server.whoami().then(function (username) {
expect(username).toMatch('test'); expect(username).toMatch(CREDENTIALS.user);
}); });
}); });

View file

@ -0,0 +1,4 @@
export const CREDENTIALS = {
user: 'test',
password: 'test'
};