From 2c8c8f02950ba5810ffc25b41bd9996fa68a441c Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Thu, 21 Jun 2018 08:10:33 +0200 Subject: [PATCH] refactor: update jetst match using more consntants --- src/lib/constants.js | 2 + test/functional/adduser/adduser.js | 8 ++- test/functional/adduser/logout.js | 4 +- test/functional/basic/basic.js | 101 +++++++++++++++-------------- test/functional/basic/ping.js | 5 +- test/functional/basic/whoIam.js | 4 +- test/functional/config.func.js | 4 ++ 7 files changed, 73 insertions(+), 55 deletions(-) create mode 100644 test/functional/config.func.js diff --git a/src/lib/constants.js b/src/lib/constants.js index ef1d4f836..0b46d3fa7 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -17,10 +17,12 @@ export const DEFAULT_REGISTRY = 'https://registry.npmjs.org/'; export const HTTP_STATUS = { INTERNAL_ERROR: 500, + SERVICE_UNAVAILABLE: 503, OK: 200, CREATED: 201, BAD_REQUEST: 400, UNAUTHORIZED: 401, FORBIDDEN: 403, NOT_FOUND: 404, + CONFLICT: 409, }; diff --git a/test/functional/adduser/adduser.js b/test/functional/adduser/adduser.js index 0a139ca73..e28679b25 100644 --- a/test/functional/adduser/adduser.js +++ b/test/functional/adduser/adduser.js @@ -1,3 +1,5 @@ +import {HTTP_STATUS} from "../../../src/lib/constants"; + export default function(server) { describe('npm adduser', () => { const user = String(Math.random()); @@ -5,7 +7,7 @@ export default function(server) { beforeAll(function() { return server.auth(user, pass) - .status(201) + .status(HTTP_STATUS.CREATED) .body_ok(/user .* created/); }); @@ -13,13 +15,13 @@ export default function(server) { test('should log in', () => { return server.auth(user, pass) - .status(201) + .status(HTTP_STATUS.CREATED) .body_ok(/you are authenticated as/); }); test('should not register more users', () => { return server.auth(String(Math.random()), String(Math.random())) - .status(409) + .status(HTTP_STATUS.CONFLICT) .body_error(/maximum amount of users reached/); }); }); diff --git a/test/functional/adduser/logout.js b/test/functional/adduser/logout.js index 0e03efd91..bfdeac96d 100644 --- a/test/functional/adduser/logout.js +++ b/test/functional/adduser/logout.js @@ -1,9 +1,11 @@ +import {HTTP_STATUS} from "../../../src/lib/constants"; + export default function(server) { describe('logout', () => { test('should log out', () => { return server.logout('some-token') - .status(200) + .status(HTTP_STATUS.OK) .body_ok(/Logged out/); }); }); diff --git a/test/functional/basic/basic.js b/test/functional/basic/basic.js index b75f30d40..b784677b5 100644 --- a/test/functional/basic/basic.js +++ b/test/functional/basic/basic.js @@ -1,34 +1,39 @@ -import assert from 'assert'; -import crypto from 'crypto'; +// @flow + +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) { - return require('fs').readFileSync(__dirname + '/' + folderPath); + return fs.readFileSync(path.join(__dirname, '/', folderPath)); } function getPackage(name) { return require('../fixtures/package')(name); } -function createHash() { - return crypto.createHash('sha1'); -} - -export default function(server, server2) { +export default function(server: any, server2: any) { describe('basic test endpoints', () => { + const PKG_NAME:string = 'testpkg'; + beforeAll(function() { - return server.auth('test', 'test') - .status(201) + return server.auth(CREDENTIALS.user, CREDENTIALS.password) + .status(HTTP_STATUS.CREATED) .body_ok(/'test'/); }); - require('./whoIam')(server); - require('./ping')(server); + whoIam(server); + ping(server); describe('handling packages', () => { beforeAll(function () { - return server.addPackage('testpkg'); + return server.addPackage(PKG_NAME); }); beforeAll(function () { @@ -39,29 +44,31 @@ export default function(server, server2) { }); 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', () => { - return server.putTarballIncomplete('testpkg', 'blahblah1', readfile('../fixtures/binary'), 3000); + return server.putTarballIncomplete(PKG_NAME, 'blahblah1', readfile('../fixtures/binary'), 3000); }); describe('publishing package', () => { beforeAll(function () { - return server.putTarball('testpkg', 'blahblah', readfile('../fixtures/binary')) - .status(201) + return server.putTarball(PKG_NAME, 'blahblah', readfile('../fixtures/binary')) + .status(HTTP_STATUS.CREATED) .body_ok(/.*/); }); beforeAll(function () { return server.putTarball('testpkg-single-tarball', 'single', readfile('../fixtures/binary')) - .status(201) + .status(HTTP_STATUS.CREATED) .body_ok(/.*/); }); afterAll(function () { - return server.removeTarball('testpkg').status(201); + return server.removeTarball(PKG_NAME).status(HTTP_STATUS.CREATED); }); test('remove a tarball', () => { @@ -73,46 +80,46 @@ export default function(server, server2) { }); 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', () => { - return server.removeSingleTarball('', 'fakeFile').status(404); + return server.removeSingleTarball('', 'fakeFile').status(HTTP_STATUS.NOT_FOUND); }); // testexp-incomplete 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 test('downloading newly created tarball', () => { - return server.getTarball('testpkg', 'blahblah') + return server.getTarball(PKG_NAME, 'blahblah') .status(200) .then(function (body) { - assert.deepEqual(body, readfile('../fixtures/binary')); + expect(body).toEqual(readfile('../fixtures/binary')); }); }); test('uploading new package version (bad sha)', () => { - let pkg = getPackage('testpkg'); - pkg.dist.shasum = createHash().update('fake').digest('hex'); + let pkg = getPackage(PKG_NAME); + pkg.dist.shasum = createTarballHash().update('fake').digest('hex'); - return server.putVersion('testpkg', '0.0.1', pkg) - .status(400) + return server.putVersion(PKG_NAME, '0.0.1', pkg) + .status(HTTP_STATUS.BAD_REQUEST) .body_error(/shasum error/); }); describe('publishing version', () => { beforeAll(function () { - const pkg = getPackage('testpkg'); + const pkg = getPackage(PKG_NAME); - pkg.dist.shasum = createHash().update(readfile('../fixtures/binary')).digest('hex'); - return server.putVersion('testpkg', '0.0.1', pkg) - .status(201) + pkg.dist.shasum = createTarballHash().update(readfile('../fixtures/binary')).digest('hex'); + return server.putVersion(PKG_NAME, '0.0.1', pkg) + .status(HTTP_STATUS.CREATED) .body_ok(/published/); }); @@ -123,31 +130,31 @@ export default function(server, server2) { describe('should download a package', () => { beforeAll(function() { return server.auth('test', 'test') - .status(201) + .status(HTTP_STATUS.CREATED) .body_ok(/'test'/); }); test('should download a newly created package from server1', () => { - return server.getPackage('testpkg') + return server.getPackage(PKG_NAME) .status(200) .then(function (body) { - assert.equal(body.name, 'testpkg'); - assert.equal(body.versions['0.0.1'].name, 'testpkg'); - assert.equal(body.versions['0.0.1'].dist.tarball, 'http://localhost:55551/testpkg/-/blahblah'); - assert.deepEqual(body['dist-tags'], { + expect(body.name).toEqual(PKG_NAME); + expect(body.versions['0.0.1'].name).toEqual(PKG_NAME); + expect(body.versions['0.0.1'].dist.tarball).toEqual('http://localhost:55551/testpkg/-/blahblah'); + expect(body['dist-tags']).toEqual({ latest: '0.0.1' }); }); }); test('should downloading a package from server2', () => { - return server2.getPackage('testpkg') + return server2.getPackage(PKG_NAME) .status(200) .then(function (body) { - assert.equal(body.name, 'testpkg'); - assert.equal(body.versions['0.0.1'].name, 'testpkg'); - assert.equal(body.versions['0.0.1'].dist.tarball, 'http://localhost:55552/testpkg/-/blahblah'); - assert.deepEqual(body['dist-tags'], { + expect(body.name).toEqual(PKG_NAME); + expect(body.versions['0.0.1'].name).toEqual(PKG_NAME); + expect(body.versions['0.0.1'].dist.tarball).toEqual('http://localhost:55552/testpkg/-/blahblah'); + expect(body['dist-tags']).toEqual({ latest: '0.0.1' }); }); @@ -163,27 +170,27 @@ export default function(server, server2) { 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( 'should fails on publish a version for non existing package', () => { return server.putVersion('testpxg', '0.0.1', getPackage('testpxg')) - .status(404) + .status(HTTP_STATUS.NOT_FOUND) .body_error(/no such package/); } ); test('should be a package not found', () => { return server.putTarball('nonExistingPackage', 'blahblah', readfile('../fixtures/binary')) - .status(404) + .status(HTTP_STATUS.NOT_FOUND) .body_error(/no such/); }); test('should fails on publish package in a bad uplink', () => { return server.putPackage('baduplink', getPackage('baduplink')) - .status(503) + .status(HTTP_STATUS.SERVICE_UNAVAILABLE) .body_error(/one of the uplinks is down, refuse to publish/); }); diff --git a/test/functional/basic/ping.js b/test/functional/basic/ping.js index 11517573f..43cfc1b0b 100644 --- a/test/functional/basic/ping.js +++ b/test/functional/basic/ping.js @@ -1,12 +1,11 @@ -const assert = require('assert'); -const _ = require('lodash'); +import _ from 'lodash'; module.exports = function(server) { test('ping', () => { return server.ping().then(function (data) { // it's always an empty object - assert.ok(_.isObject(data)); + expect(_.isObject(data)).toBeDefined(); }); }); diff --git a/test/functional/basic/whoIam.js b/test/functional/basic/whoIam.js index 386c00b3f..6910b66e3 100644 --- a/test/functional/basic/whoIam.js +++ b/test/functional/basic/whoIam.js @@ -1,8 +1,10 @@ +import {CREDENTIALS} from "../config.func"; + module.exports = function(server) { test('who am I?', () => { return server.whoami().then(function (username) { - expect(username).toMatch('test'); + expect(username).toMatch(CREDENTIALS.user); }); }); diff --git a/test/functional/config.func.js b/test/functional/config.func.js new file mode 100644 index 000000000..fffc3ad6c --- /dev/null +++ b/test/functional/config.func.js @@ -0,0 +1,4 @@ +export const CREDENTIALS = { + user: 'test', + password: 'test' +};