From 1e6c7dd6ea13d4e3fd4ce1a2149c03ef72562781 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Mon, 24 Jul 2017 23:09:28 +0200 Subject: [PATCH] refactor: unit test for readme, preserve tags on publish --- src/api/web/index.js | 2 +- test/functional/index.js | 5 +- test/functional/lib/test.utils.js | 13 ++ test/functional/newnpmreg.js | 124 ------------------ test/functional/{ => package}/scoped.js | 17 +-- .../{fixtures => package}/scoped.json | 0 test/functional/readme/pkg-readme.json | 57 ++++++++ test/functional/readme/readme.spec.js | 46 +++++++ test/functional/store/config-2.yaml | 5 + test/functional/tags/preserve_tags.js | 113 ++++++++++++++++ .../preserve_tags.json} | 12 +- 11 files changed, 249 insertions(+), 145 deletions(-) create mode 100644 test/functional/lib/test.utils.js delete mode 100644 test/functional/newnpmreg.js rename test/functional/{ => package}/scoped.js (83%) rename test/functional/{fixtures => package}/scoped.json (100%) create mode 100644 test/functional/readme/pkg-readme.json create mode 100644 test/functional/readme/readme.spec.js create mode 100644 test/functional/tags/preserve_tags.js rename test/functional/{fixtures/newnpmreg.json => tags/preserve_tags.json} (84%) diff --git a/src/api/web/index.js b/src/api/web/index.js index 4bde9b753..e849a535b 100644 --- a/src/api/web/index.js +++ b/src/api/web/index.js @@ -19,7 +19,7 @@ module.exports = function(config, auth, storage) { // Static router.get('/-/static/:filename', function(req, res, next) { - let file = `${env.APP_ROOT}/static/${req.params.filename}`; + const file = `${env.APP_ROOT}/static/${req.params.filename}`; res.sendFile(file, function(err) { if (!err) { return; diff --git a/test/functional/index.js b/test/functional/index.js index 8d547c598..947b1b56a 100644 --- a/test/functional/index.js +++ b/test/functional/index.js @@ -63,11 +63,12 @@ describe('Create registry servers', function() { require('./gzip')(); require('./incomplete')(); require('./mirror')(); - require('./newnpmreg')(); + require('./tags/preserve_tags')(); + require('./readme/readme.spec')(); require('./nullstorage')(); require('./race')(); require('./racycrash')(); - require('./scoped')(); + require('./package/scoped')(); require('./security')(); require('./adduser')(); require('./logout')(); diff --git a/test/functional/lib/test.utils.js b/test/functional/lib/test.utils.js new file mode 100644 index 000000000..29b96fb60 --- /dev/null +++ b/test/functional/lib/test.utils.js @@ -0,0 +1,13 @@ + +const crypto = require('crypto'); +const fs = require('fs'); +const path = require('path'); + +exports.generateSha = function generateSha(key) { + return crypto.createHash('sha1', 'binary').update(key).digest('hex'); +}; + + +exports.readFile = function readFile(filePath) { + return fs.readFileSync(path.join(__dirname, `/${filePath}`)); +} diff --git a/test/functional/newnpmreg.js b/test/functional/newnpmreg.js deleted file mode 100644 index bc98f57dd..000000000 --- a/test/functional/newnpmreg.js +++ /dev/null @@ -1,124 +0,0 @@ -'use strict'; - -let assert = require('assert'); - -function readfile(x) { - return require('fs').readFileSync(__dirname + '/' + x); -} - -function sha(x) { - return require('crypto').createHash('sha1', 'binary').update(x).digest('hex'); -} - -module.exports = function() { - let server = process.server; - let server2 = process.server2; - let express = process.express; - - describe('newnpmreg', function() { - before(function() { - return server.request({ - uri: '/testpkg-newnpmreg', - headers: { - 'content-type': 'application/json', - }, - method: 'PUT', - json: JSON.parse(readfile('fixtures/newnpmreg.json')), - }).status(201); - }); - - it('add pkg', function() {}); - - it('server1 - tarball', function() { - return server.getTarball('testpkg-newnpmreg', 'testpkg-newnpmreg-0.0.0.tgz') - .status(200) - .then(function(body) { - // not real sha due to utf8 conversion - assert.strictEqual(sha(body), '8ee7331cbc641581b1a8cecd9d38d744a8feb863'); - }); - }); - - it('server2 - tarball', function() { - return server2.getTarball('testpkg-newnpmreg', 'testpkg-newnpmreg-0.0.0.tgz') - .status(200) - .then(function(body) { - // not real sha due to utf8 conversion - assert.strictEqual(sha(body), '8ee7331cbc641581b1a8cecd9d38d744a8feb863'); - }); - }); - - it('server1 - package', function() { - return server.getPackage('testpkg-newnpmreg') - .status(200) - .then(function(body) { - assert.equal(body.name, 'testpkg-newnpmreg'); - assert.equal(body.versions['0.0.0'].name, 'testpkg-newnpmreg'); - assert.equal(body.versions['0.0.0'].dist.tarball, 'http://localhost:55551/testpkg-newnpmreg/-/testpkg-newnpmreg-0.0.0.tgz'); - assert.deepEqual(body['dist-tags'], {foo: '0.0.0', latest: '0.0.0'}); - }); - }); - - it('server2 - package', function() { - return server2.getPackage('testpkg-newnpmreg') - .status(200) - .then(function(body) { - assert.equal(body.name, 'testpkg-newnpmreg'); - assert.equal(body.versions['0.0.0'].name, 'testpkg-newnpmreg'); - assert.equal(body.versions['0.0.0'].dist.tarball, 'http://localhost:55552/testpkg-newnpmreg/-/testpkg-newnpmreg-0.0.0.tgz'); - assert.deepEqual(body['dist-tags'], {foo: '0.0.0', latest: '0.0.0'}); - }); - }); - - // FIXME: Review this block of test - // it('server1 - readme', function() { - // return server.request({uri: '/-/readme/testpkg-newnpmreg'}) - // .status(200) - // .then(function(body) { - // assert.equal(body, '

blah blah blah

\n'); - // }); - // }); - // - // it('server2 - readme', function() { - // return server2.request({uri: '/-/readme/testpkg-newnpmreg'}) - // .status(200) - // .then(function(body) { - // assert.equal(body, '

blah blah blah

\n'); - // }); - // }); - - describe('search', function() { - function check(obj) { - obj['testpkg-newnpmreg'].time.modified = '2014-10-02T07:07:51.000Z'; - assert.deepEqual(obj['testpkg-newnpmreg'], - {'name': 'testpkg-newnpmreg', - 'description': '', - 'author': '', - 'license': 'ISC', - 'dist-tags': {latest: '0.0.0'}, - 'maintainers': [{name: 'alex', email: 'alex@kocharin.ru'}], - 'readmeFilename': '', - 'time': {modified: '2014-10-02T07:07:51.000Z'}, - 'versions': {}, - 'repository': {type: 'git', url: ''}}); - } - - before(function() { - express.get('/-/all', function(req, res) { - res.send({}); - }); - }); - - it('server1 - search', function() { - return server.request({uri: '/-/all'}) - .status(200) - .then(check); - }); - - it('server2 - search', function() { - return server2.request({uri: '/-/all'}) - .status(200) - .then(check); - }); - }); - }); -}; diff --git a/test/functional/scoped.js b/test/functional/package/scoped.js similarity index 83% rename from test/functional/scoped.js rename to test/functional/package/scoped.js index 5f9734a52..b2819de25 100644 --- a/test/functional/scoped.js +++ b/test/functional/package/scoped.js @@ -1,14 +1,7 @@ 'use strict'; -let assert = require('assert'); - -function readfile(x) { - return require('fs').readFileSync(__dirname + '/' + x); -} - -function sha(x) { - return require('crypto').createHash('sha1', 'binary').update(x).digest('hex'); -} +const assert = require('assert'); +const utils = require ('../lib/test.utils'); module.exports = function() { let server = process.server; @@ -22,7 +15,7 @@ module.exports = function() { 'content-type': 'application/json', }, method: 'PUT', - json: JSON.parse(readfile('fixtures/scoped.json')), + json: require('./scoped.json'), }).status(201); }); @@ -33,7 +26,7 @@ module.exports = function() { .status(200) .then(function(body) { // not real sha due to utf8 conversion - assert.strictEqual(sha(body), '6e67b14e2c0e450b942e2bc8086b49e90f594790'); + assert.strictEqual(utils.generateSha(body), '6e67b14e2c0e450b942e2bc8086b49e90f594790'); }); }); @@ -42,7 +35,7 @@ module.exports = function() { .status(200) .then(function(body) { // not real sha due to utf8 conversion - assert.strictEqual(sha(body), '6e67b14e2c0e450b942e2bc8086b49e90f594790'); + assert.strictEqual(utils.generateSha(body), '6e67b14e2c0e450b942e2bc8086b49e90f594790'); }); }); diff --git a/test/functional/fixtures/scoped.json b/test/functional/package/scoped.json similarity index 100% rename from test/functional/fixtures/scoped.json rename to test/functional/package/scoped.json diff --git a/test/functional/readme/pkg-readme.json b/test/functional/readme/pkg-readme.json new file mode 100644 index 000000000..e98a42c9e --- /dev/null +++ b/test/functional/readme/pkg-readme.json @@ -0,0 +1,57 @@ +{ + "_id": "readme-test", + "name": "readme-test", + "description": "", + "dist-tags": { + "foo": "0.0.0", + "latest": "0.0.0" + }, + "versions": { + "0.0.0": { + "name": "test-readme", + "version": "0.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "" + }, + "author": "", + "license": "ISC", + "_id": "test-readme@0.0.0", + "dist": { + "shasum": "8ee7331cbc641581b1a8cecd9d38d744a8feb863", + "tarball": "http:\/\/localhost:1234\/test-readme\/-\/test-readme-0.0.0.tgz" + }, + "_from": ".", + "_npmVersion": "1.3.1", + "_npmUser": { + "name": "alex", + "email": "alex@kocharin.ru" + }, + "maintainers": [ + { + "name": "juan", + "email": "juanpicado19@gmail.com" + } + ] + } + }, + "readme": "this is a readme", + "maintainers": [ + { + "name": "juan", + "email": "juanpicado19@gmail.com" + } + ], + "_attachments": { + "test-readme-0.0.0.tgz": { + "content_type": "application\/octet-stream", + "data": "H4sIAAAAAAAAA+2TsW7CMBCGM\/spTh6YKHUSIJLXqkPnrixWcIMLsS3btCDEu\/fs0Ba1SFVVVISUP8Odzqf\/zlY+K+qlaOSt7eLo2RudnVmMsel4DBjzasKOY1JZlJDlRVkU5aSspnnG8pIVOZ6fe5FTWvsgHK7yV5\/uLvARr0Q7qkUrKadB+mCXzY2Wr9q2TjZ0SF+k88poPGUj\/LAyl752yoauioVWqJgpPZcb\/Hmw0jV4ynfJEw9lvTAwo\/fOGcdBG4h18FbW6knJ+YzCYAByowLkdD+kTlrjVTBumzy2Nq7XqIDea7eKY7FJrMPCuG6Hlaql9rHr4fGO7i\/9pFcl+4X\/rWhX557xA\/9FVZ3gv+j5\/w9F+jl8g58c0OeQyCdH3HOglETsObxTTw7McwLJClt+wzz5JD45IPEcEHjMEfg0r8M9pQfaOSDs5NLP16tXr15XqzeJD6m5AAwAAA==", + "length": 352 + } + } +} diff --git a/test/functional/readme/readme.spec.js b/test/functional/readme/readme.spec.js new file mode 100644 index 000000000..f8e8f25aa --- /dev/null +++ b/test/functional/readme/readme.spec.js @@ -0,0 +1,46 @@ +/** + * Created by jpicado on 7/24/17. + */ +'use strict'; + +const assert = require('assert'); + +module.exports = function() { + let server = process.server; + let server2 = process.server2; + + describe('should test readme', () => { + + before(function() { + return server.request({ + uri: '/readme-test', + headers: { + 'content-type': 'application/json', + }, + method: 'PUT', + json: require('./pkg-readme.json'), + }).status(201); + }); + + it('add pkg', function() {}); + + describe('should check readme file', () => { + const matchReadme = (server) => { + return server.request({ + uri: '/-/verdaccio/package/readme/readme-test' + }).status(200).then(function(body) { + assert.equal(body, '

this is a readme

\n'); + }); + }; + + it('server1 - readme', function() { + return matchReadme(server); + }); + + it('server2 - readme', function() { + return matchReadme(server2); + }); + + }); + }); +}; diff --git a/test/functional/store/config-2.yaml b/test/functional/store/config-2.yaml index f3965bd0f..b09fb77a7 100644 --- a/test/functional/store/config-2.yaml +++ b/test/functional/store/config-2.yaml @@ -47,6 +47,11 @@ packages: allow_publish: test anonymous proxy_access: server1 + 'readme-*': + allow_access: test anonymous + allow_publish: test anonymous + proxy_access: server1 + 'test-nullstorage*': allow_access: all allow_publish: all diff --git a/test/functional/tags/preserve_tags.js b/test/functional/tags/preserve_tags.js new file mode 100644 index 000000000..e6bd7d97b --- /dev/null +++ b/test/functional/tags/preserve_tags.js @@ -0,0 +1,113 @@ +'use strict'; + +const assert = require('assert'); +const utils = require ('../lib/test.utils'); + +module.exports = function() { + let server = process.server; + let server2 = process.server2; + let express = process.express; + + describe('should test preserve tags when publishing something', () => { + + before(function() { + return server.request({ + uri: '/testpkg-preserve', + headers: { + 'content-type': 'application/json', + }, + method: 'PUT', + json: require('./preserve_tags.json'), + }).status(201); + }); + + it('add pkg', function() {}); + + describe('should check sha integrity', () => { + + const matchTarBallSha = (server) => { + return server.getTarball('testpkg-preserve', 'testpkg-preserve-0.0.0.tgz') + .status(200) + .then(function(body) { + // not real sha due to utf8 conversion + assert.strictEqual(utils.generateSha(body), '8ee7331cbc641581b1a8cecd9d38d744a8feb863'); + }); + }; + + it('server1 should match with sha key from published package', () => { + return matchTarBallSha(server); + }); + + it('server2 should match with sha key from published packagel', () => { + matchTarBallSha(server2); + }); + }); + + describe('should match dist-tags', () => { + const matchDisTags = (server, port) => { + return server.getPackage('testpkg-preserve') + .status(200) + .then(function(body) { + assert.equal(body.name, 'testpkg-preserve'); + assert.equal(body.versions['0.0.0'].name, 'testpkg-preserve'); + assert.equal(body.versions['0.0.0'].dist.tarball, `http://localhost:${port}/testpkg-preserve/-/testpkg-preserve-0.0.0.tgz`); + assert.deepEqual(body['dist-tags'], {foo: '0.0.0', latest: '0.0.0'}); + }); + }; + + it('server1 should be able to match latest dist-tags correctly', () => { + return matchDisTags(server, '55551'); + }); + + it('server2 should be able to match latest dist-tags correctly', function() { + return matchDisTags(server2, '55552'); + }); + }); + + describe('should test search', function() { + const check = (obj) => { + obj['testpkg-preserve'].time.modified = '2014-10-02T07:07:51.000Z'; + assert.deepEqual(obj['testpkg-preserve'], + { + 'name': 'testpkg-preserve', + 'description': '', + 'author': '', + 'license': 'ISC', + 'dist-tags': { + latest: '0.0.0' + }, + 'maintainers': [{ + name: 'alex', + email: 'alex@kocharin.ru' + }], + 'readmeFilename': '', + 'time': { + modified: '2014-10-02T07:07:51.000Z' + }, + 'versions': {}, + 'repository': { + type: 'git', url: ''} + }); + }; + + before(function() { + express.get('/-/all', (req, res) => { + res.send({}); + }); + }); + + it('server1 - search', () => { + return server.request({uri: '/-/all'}) + .status(200) + .then(check); + }); + + it('server2 - search', () => { + return server2.request({uri: '/-/all'}) + .status(200) + .then(check); + }); + + }); + }); +}; diff --git a/test/functional/fixtures/newnpmreg.json b/test/functional/tags/preserve_tags.json similarity index 84% rename from test/functional/fixtures/newnpmreg.json rename to test/functional/tags/preserve_tags.json index d0f8caf98..610ca9e0f 100644 --- a/test/functional/fixtures/newnpmreg.json +++ b/test/functional/tags/preserve_tags.json @@ -1,6 +1,6 @@ { - "_id": "testpkg-newnpmreg", - "name": "testpkg-newnpmreg", + "_id": "testpkg-preserve", + "name": "testpkg-preserve", "description": "", "dist-tags": { "foo": "0.0.0", @@ -8,7 +8,7 @@ }, "versions": { "0.0.0": { - "name": "testpkg-newnpmreg", + "name": "testpkg-preserve", "version": "0.0.0", "description": "", "main": "index.js", @@ -21,10 +21,10 @@ }, "author": "", "license": "ISC", - "_id": "testpkg-newnpmreg@0.0.0", + "_id": "testpkg-preserve@0.0.0", "dist": { "shasum": "8ee7331cbc641581b1a8cecd9d38d744a8feb863", - "tarball": "http:\/\/localhost:1234\/testpkg-newnpmreg\/-\/testpkg-newnpmreg-0.0.0.tgz" + "tarball": "http:\/\/localhost:1234\/testpkg-preserve\/-\/testpkg-preserve-0.0.0.tgz" }, "_from": ".", "_npmVersion": "1.3.1", @@ -48,7 +48,7 @@ } ], "_attachments": { - "testpkg-newnpmreg-0.0.0.tgz": { + "testpkg-preserve-0.0.0.tgz": { "content_type": "application\/octet-stream", "data": "H4sIAAAAAAAAA+2TsW7CMBCGM\/spTh6YKHUSIJLXqkPnrixWcIMLsS3btCDEu\/fs0Ba1SFVVVISUP8Odzqf\/zlY+K+qlaOSt7eLo2RudnVmMsel4DBjzasKOY1JZlJDlRVkU5aSspnnG8pIVOZ6fe5FTWvsgHK7yV5\/uLvARr0Q7qkUrKadB+mCXzY2Wr9q2TjZ0SF+k88poPGUj\/LAyl752yoauioVWqJgpPZcb\/Hmw0jV4ynfJEw9lvTAwo\/fOGcdBG4h18FbW6knJ+YzCYAByowLkdD+kTlrjVTBumzy2Nq7XqIDea7eKY7FJrMPCuG6Hlaql9rHr4fGO7i\/9pFcl+4X\/rWhX557xA\/9FVZ3gv+j5\/w9F+jl8g58c0OeQyCdH3HOglETsObxTTw7McwLJClt+wzz5JD45IPEcEHjMEfg0r8M9pQfaOSDs5NLP16tXr15XqzeJD6m5AAwAAA==", "length": 352