diff --git a/.gitignore b/.gitignore index 6dbf2a18b..be2cc587a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ test-storage* node_modules package-lock.json build/ +npm_test-fails-add-tarball* # Istanbul diff --git a/test/unit/local-storage.spec.js b/test/unit/local-storage.spec.js index 8aab67426..a02c14288 100644 --- a/test/unit/local-storage.spec.js +++ b/test/unit/local-storage.spec.js @@ -1,5 +1,6 @@ // @flow import rimRaf from 'rimraf'; +import path from 'path'; import LocalStorage from '../../src/lib/local-storage'; import AppConfig from '../../src/lib/config'; import configExample from './partials/config'; @@ -15,9 +16,15 @@ setup([]); describe('LocalStorage', () => { let storage: IStorage; const pkgName: string = 'npm_test'; + const pkgNameScoped = `@scope/${pkgName}-scope`; + const tarballName: string = `${pkgName}-add-tarball-1.0.4.tgz`; + const tarballName2: string = `${pkgName}-add-tarball-1.0.5.tgz`; beforeAll(function () { - storage = new LocalStorage(new AppConfig(configExample), Logger.logger); + const config: Config = new AppConfig(configExample); + config.self_path = path.join('../partials/store'); + + storage = new LocalStorage(config, Logger.logger); }); test('should be defined', () => { @@ -39,6 +46,21 @@ describe('LocalStorage', () => { }); }); + test('should add a @scope package', (done) => { + const metadata = JSON.parse(readMetadata()); + const pkgStoragePath: string = storage._getLocalStorage(pkgNameScoped); + + rimRaf(pkgStoragePath.path, (err) => { + expect(err).toBeNull(); + storage.addPackage(pkgNameScoped, metadata, (err, data) => { + expect(data.version).toMatch(/1.0.0/); + expect(data.dist.tarball).toMatch(/npm_test-1.0.0.tgz/); + expect(data.name).toMatch(pkgName); + done(); + }); + }); + }); + test('should fails on add a package', (done) => { const metadata = JSON.parse(readMetadata()); @@ -82,7 +104,18 @@ describe('LocalStorage', () => { done(); }); }); + }); + describe('LocalStorage::updateVersions', () => { + test('should update versions from origin', (done) => { + const metadata = JSON.parse(readMetadata('metadata-update-versions-tags')); + + storage.updateVersions(pkgName, metadata, (err, data) => { + expect(err).toBeNull(); + expect(data.versions['1.0.3']).toBeDefined(); + done(); + }); + }); }); describe('LocalStorage::changePackage', () => { @@ -98,9 +131,10 @@ describe('LocalStorage', () => { }); describe('LocalStorage::addTarball', () => { + test('should add a new tarball', (done) => { const tarballData = JSON.parse(readMetadata('addTarball')); - const stream = storage.addTarball(pkgName, `${pkgName}-add-tarball-1.0.4.tgz`); + const stream = storage.addTarball(pkgName, tarballName); stream.on('error', function(err) { expect(err).toBeNull(); done(); @@ -113,9 +147,42 @@ describe('LocalStorage', () => { stream.done(); }); + test('should add a new second tarball', (done) => { + const tarballData = JSON.parse(readMetadata('addTarball')); + const stream = storage.addTarball(pkgName, tarballName2); + stream.on('error', function(err) { + expect(err).toBeNull(); + done(); + }); + stream.on('success', function() { + done(); + }); + + stream.end(new Buffer(tarballData.data, 'base64')); + stream.done(); + }); + + test('should fails on add a duplicated new tarball ', (done) => { + const tarballData = JSON.parse(readMetadata('addTarball')); + const stream = storage.addTarball(pkgName, tarballName); + stream.on('error', function(err) { + expect(err).not.toBeNull(); + expect(err.statusCode).toEqual(409); + expect(err.message).toMatch(/this package is already present/); + done(); + }); + + stream.on('success', function() { + done(); + }); + + stream.end(new Buffer(tarballData.data, 'base64')); + stream.done(); + }); + test('should fails on add a new tarball on missing package', (done) => { const tarballData = JSON.parse(readMetadata('addTarball')); - const stream = storage.addTarball('unexsiting-package', `${pkgName}-add-tarball-1.0.4.tgz`); + const stream = storage.addTarball('unexsiting-package', tarballName); stream.on('error', function(err) { expect(err).not.toBeNull(); expect(err.statusCode).toEqual(404); @@ -157,23 +224,102 @@ describe('LocalStorage', () => { }); }); - // describe('LocalStorage::removePackage', () => { - // test('should remove completely package', (done) => { - // storage.removePackage(pkgName, (err, data) => { - // expect(err).toBeNull(); - // expect(data).toBeUndefined(); - // done(); - // }); - // }); - // - // test('should fails with package not found', (done) => { - // const pkgName: string = 'npm_test_fake'; - // storage.removePackage(pkgName, (err, data) => { - // expect(err).not.toBeNull(); - // expect(err.message).toMatch(/no such package available/); - // done(); - // }); - // }); - // }); + describe('LocalStorage::removeTarball', () => { + + test('should remove a tarball', (done) => { + storage.removeTarball(pkgName, tarballName2, 'rev', (err, pkg) => { + expect(err).toBeNull(); + expect(pkg).toBeUndefined(); + done(); + }); + }); + + test('should remove a tarball that does not exist', (done) => { + storage.removeTarball(pkgName, tarballName2, 'rev', (err) => { + expect(err).not.toBeNull(); + expect(err.statusCode).toEqual(404); + expect(err.message).toMatch(/no such file available/); + done(); + }); + }); + }); + + describe('LocalStorage::getTarball', () => { + test('should get a existing tarball', (done) => { + const stream = storage.getTarball(pkgName, tarballName); + stream.on('content-length', function(contentLength) { + expect(contentLength).toBe(279); + done(); + }); + stream.on('open', function() { + done(); + }); + }); + + test('should fails on get a tarball that does not exist', (done) => { + const stream = storage.getTarball('fake', tarballName); + stream.on('error', function(err) { + expect(err).not.toBeNull(); + expect(err.statusCode).toEqual(404); + expect(err.message).toMatch(/no such file available/); + done(); + }); + }); + }); + + describe('LocalStorage::search', () => { + test('should find a tarball', (done) => { + const stream = storage.search('99999'); + + stream.on('data', function each(pkg) { + expect(pkg.name).toEqual(pkgName); + }); + + stream.on('error', function(err) { + expect(err).not.toBeNull(); + done(); + }); + + stream.on('end', function() { + done(); + }); + }); + + }); + + describe('LocalStorage::removePackage', () => { + test('should remove completely package', (done) => { + storage.removePackage(pkgName, (err, data) => { + expect(err).toBeNull(); + expect(data).toBeUndefined(); + done(); + }); + }); + + test('should remove completely @scoped package', (done) => { + storage.removePackage(pkgNameScoped, (err, data) => { + expect(err).toBeNull(); + expect(data).toBeUndefined(); + done(); + }); + }); + + test('should fails with package not found', (done) => { + const pkgName: string = 'npm_test_fake'; + storage.removePackage(pkgName, (err, data) => { + expect(err).not.toBeNull(); + expect(err.message).toMatch(/no such package available/); + done(); + }); + }); + + test('should fails with @scoped package not found', (done) => { + storage.removePackage(pkgNameScoped, (err, data) => { + expect(err).not.toBeNull(); + expect(err.message).toMatch(/no such package available/); + done(); + }); + }); + }); }); diff --git a/test/unit/partials/metadata-update-versions-tags b/test/unit/partials/metadata-update-versions-tags new file mode 100644 index 000000000..e8791b893 --- /dev/null +++ b/test/unit/partials/metadata-update-versions-tags @@ -0,0 +1,91 @@ +{ + "name": "npm_test", + "versions": { + "1.0.1": { + "name": "npm_test", + "version": "1.0.1", + "description": "", + "main": "index.js", + "dependencies": { + "test": "^1.4.1" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "_id": "npm_test@1.0.1", + "_npmVersion": "5.5.1", + "_nodeVersion": "9.3.0", + "_npmUser": {}, + "dist": { + "integrity": "sha512-zVEqt1JUCOPsash9q4wMkJEDPD+QCx95TRhQII+JnoS31uBUKoZxhzvvUJCcLVy2CQG4QdwXARU7dYWPnrwhGg==", + "shasum": "b7088c30970489637f8b4e6795e8cf2b699d7569", + "tarball": "http://localhost:4873/npm_test/-/npm_test-1.0.1.tgz" + } + }, + "1.0.2": { + "name": "npm_test", + "version": "1.0.1", + "description": "", + "main": "index.js", + "dependencies": { + "test": "^1.4.1" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "_id": "npm_test@1.0.1", + "_npmVersion": "5.5.1", + "_nodeVersion": "9.3.0", + "_npmUser": {}, + "dist": { + "integrity": "sha512-zVEqt1JUCOPsash9q4wMkJEDPD+QCx95TRhQII+JnoS31uBUKoZxhzvvUJCcLVy2CQG4QdwXARU7dYWPnrwhGg==", + "shasum": "b7088c30970489637f8b4e6795e8cf2b699d7569", + "tarball": "http://localhost:4873/npm_test/-/npm_test-1.0.1.tgz" + } + }, + "1.0.3": { + "name": "npm_test", + "version": "1.0.3", + "description": "", + "main": "index.js", + "dependencies": { + "test": "^1.4.1" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "_id": "npm_test@1.0.1", + "_npmVersion": "5.5.1", + "_nodeVersion": "9.3.0", + "_npmUser": {}, + "dist": { + "integrity": "sha512-zVEqt1JUCOPsash9q4wMkJEDPD+QCx95TRhQII+JnoS31uBUKoZxhzvvUJCcLVy2CQG4QdwXARU7dYWPnrwhGg==", + "shasum": "b7088c30970489637f8b4e6795e8cf2b699d7569", + "tarball": "http://localhost:4873/npm_test/-/npm_test-1.0.1.tgz" + } + } + }, + "dist-tags": { + "latest": "1.0.1", + "beta": "1.0.2", + "next": "1.0.3" + }, + "time": {}, + "_distfiles": {}, + "_attachments": {}, + "_uplinks": {}, + "_rev": "2-b8a00ec71cdc7323", + "readme": "ERROR: No README data found!" +}