From be8fed50d956be541ecc79fb0b7a916c25da8ca0 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Sat, 27 Jul 2019 23:47:04 +0200 Subject: [PATCH] refactor: add generatePackageMetadata utility - Tests suites must be independent each other, this is a refactor that allow star endpoint to be fully independent. - Add putPackage utility to put new packages --- .secrets-baseline | 6 +- src/api/endpoint/api/publish.ts | 1 + test/unit/__helper/api.ts | 25 +++- test/unit/__helper/utils.ts | 54 ++++++++ test/unit/modules/api/api.spec.ts | 181 +++++++++++++------------- test/unit/modules/api/publish.spec.ts | 4 + 6 files changed, 177 insertions(+), 94 deletions(-) create mode 100644 test/unit/__helper/utils.ts diff --git a/.secrets-baseline b/.secrets-baseline index 795c65d14..ef855b871 100644 --- a/.secrets-baseline +++ b/.secrets-baseline @@ -3,7 +3,7 @@ "files": null, "lines": null }, - "generated_at": "2019-07-27T05:17:42Z", + "generated_at": "2019-07-27T21:44:36Z", "plugins_used": [ { "name": "AWSKeyDetector" @@ -270,12 +270,12 @@ "test/unit/modules/api/api.spec.ts": [ { "hashed_secret": "97752a468368b0d6b192140d6a140c38fd0cbd8b", - "line_number": 292, + "line_number": 293, "type": "Secret Keyword" }, { "hashed_secret": "364bdf2ed77a8544d3b711a03b69eeadcc63c9d7", - "line_number": 799, + "line_number": 802, "type": "Secret Keyword" } ], diff --git a/src/api/endpoint/api/publish.ts b/src/api/endpoint/api/publish.ts index 6dc3c9058..4322d0b42 100644 --- a/src/api/endpoint/api/publish.ts +++ b/src/api/endpoint/api/publish.ts @@ -150,6 +150,7 @@ export function publishPackage(storage: IStorageHandler, config: Config) { }); } } catch (error) { + logger.error({error}, 'error on publish, bad package data @{error}'); return next(ErrorCode.getBadData(API_ERROR.BAD_PACKAGE_DATA)); } }; diff --git a/test/unit/__helper/api.ts b/test/unit/__helper/api.ts index 16046256c..fe7e8e2b9 100644 --- a/test/unit/__helper/api.ts +++ b/test/unit/__helper/api.ts @@ -1,7 +1,6 @@ -// @flow - import {HEADER_TYPE, HEADERS, HTTP_STATUS, TOKEN_BEARER} from '../../../src/lib/constants'; -import {buildToken} from "../../../src/lib/utils"; +import {buildToken} from '../../../src/lib/utils'; +import { Package } from '@verdaccio/types'; // API Helpers @@ -9,7 +8,25 @@ import {buildToken} from "../../../src/lib/utils"; // Please, comply with the following: // - Promisify everything // - Encourage using constants or create new ones if it's needed -// - // $FlowFixMe or any is fine if there is no other way +// - // @ts-ignore or any is fine if there is no other way + +export function putPackage( + request: any, + pkgName: string, + publishMetadata: Package +): Promise { + return new Promise((resolve) => { + request.put(pkgName) + .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) + .send(JSON.stringify(publishMetadata)) + .set('accept', 'gzip') + .set('accept-encoding', HEADERS.JSON) + .expect(HTTP_STATUS.CREATED) + .end(function(err, res) { + resolve([err, res]); + }); + }); +} export function getPackage( request: any, diff --git a/test/unit/__helper/utils.ts b/test/unit/__helper/utils.ts new file mode 100644 index 000000000..06eecfe6b --- /dev/null +++ b/test/unit/__helper/utils.ts @@ -0,0 +1,54 @@ +import { Package } from "@verdaccio/types"; + +export function generatePackageMetadata(pkgName: string): Package { + // @ts-ignore + return { + "_id": pkgName, + "name": pkgName, + "dist-tags": { + "latest": "1.0.0" + }, + "versions": { + "1.0.0": { + "name": pkgName, + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + + ], + "author": { + "name": "User NPM", + "email": "user@domain.com" + }, + "license": "ISC", + "dependencies": { + "verdaccio": "^2.7.2" + }, + "readme": "# test", + "readmeFilename": "README.md", + "_id": `${pkgName}@1.0.0`, + "_npmVersion": "5.5.1", + "_npmUser": { + 'name': 'foo', + }, + "dist": { + "integrity": "sha512-6gHiERpiDgtb3hjqpQH5\/i7zRmvYi9pmCjQf2ZMy3QEa9wVk9RgdZaPWUt7ZOnWUPFjcr9cmE6dUBf+XoPoH4g==", + "shasum": "2c03764f651a9f016ca0b7620421457b619151b9", // pragma: allowlist secret + "tarball": `http:\/\/localhost:5555\/${pkgName}\/-\/${pkgName}-1.0.0.tgz` + } + } + }, + "readme": "# test", + "_attachments": { + [`${pkgName}-1.0.0.tgz`]: { + "content_type": "application\/octet-stream", + "data": "H4sIAAAAAAAAE+2W32vbMBDH85y\/QnjQp9qxLEeBMsbGlocNBmN7bFdQ5WuqxJaEpGQdo\/\/79KPeQsnIw5KUDX\/9IOvurLuz\/DHSjK\/YAiY6jcXSKjk6sMqypHWNdtmD6hlBI0wqQmo8nVbVqMR4OsNoVB66kF1aW8eML+Vv10m9oF\/jP6IfY4QyyTrILlD2eqkcm+gVzpdrJrPz4NuAsULJ4MZFWdBkbcByI7R79CRjx0ScCdnAvf+SkjUFWu8IubzBgXUhDPidQlfZ3BhlLpBUKDiQ1cDFrYDmKkNnZwjuhUM4808+xNVW8P2bMk1Y7vJrtLC1u1MmLPjBF40+Cc4ahV6GDmI\/DWygVRpMwVX3KtXUCg7Sxp7ff3nbt6TBFy65gK1iffsN41yoEHtdFbOiisWMH8bPvXUH0SP3k+KG3UBr+DFy7OGfEJr4x5iWVeS\/pLQe+D+FIv\/agIWI6GX66kFuIhT+1gDjrp\/4d7WAvAwEJPh0u14IufWkM0zaW2W6nLfM2lybgJ4LTJ0\/jWiAK8OcMjt8MW3OlfQppcuhhQ6k+2OgkK2Q8DssFPi\/IHpU9fz3\/+xj5NjDf8QFE39VmE4JDfzPCBn4P4X6\/f88f\/Pu47zomiPk2Lv\/dOv8h+P\/34\/D\/p9CL+Kp67mrGDRo0KBBp9ZPsETQegASAAA=", + "length": 512 + } + } + } +} diff --git a/test/unit/modules/api/api.spec.ts b/test/unit/modules/api/api.spec.ts index a7c35508d..98910d96c 100644 --- a/test/unit/modules/api/api.spec.ts +++ b/test/unit/modules/api/api.spec.ts @@ -12,14 +12,15 @@ import {HEADERS, API_ERROR, HTTP_STATUS, HEADER_TYPE, API_MESSAGE, TOKEN_BEARER} import {mockServer} from '../../__helper/mock'; import {DOMAIN_SERVERS} from '../../../functional/config.functional'; import {buildToken} from '../../../../src/lib/utils'; -import {getNewToken} from '../../__helper/api'; +import {getNewToken, putPackage} from '../../__helper/api'; +import { generatePackageMetadata } from '../../__helper/utils'; require('../../../../src/lib/logger').setup([ { type: 'stdout', format: 'pretty', level: 'info' } ]); const credentials = { name: 'jota', password: 'secretPass' }; -const putPackage = (app, name, publishMetadata) => { +const putVersion = (app, name, publishMetadata) => { return request(app) .put(name) .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) @@ -28,7 +29,7 @@ const putPackage = (app, name, publishMetadata) => { .set('accept', 'gzip') .set('accept-encoding', HEADERS.JSON) .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON); -} +}; describe('endpoint unit test', () => { let app; @@ -507,7 +508,7 @@ describe('endpoint unit test', () => { }; test('should set a new tag on jquery', (done) => { - putPackage(app, '/jquery/verdaccio-tag', jqueryVersion) + putVersion(app, '/jquery/verdaccio-tag', jqueryVersion) .expect(HTTP_STATUS.CREATED) .end(function(err, res) { if (err) { @@ -602,7 +603,7 @@ describe('endpoint unit test', () => { describe('should test search api', () => { test('should perform a search', (done) => { - const now = Date.now() + const now = Date.now(); const cacheTime = now - 6000000; request(app) .get('/-/all/since?stale=update_after&startkey=' + cacheTime) @@ -624,7 +625,9 @@ describe('endpoint unit test', () => { }); describe('should test publish/unpublish api', () => { - test('should publish a new package with no credentials', (done) => { + test('should publish a new package with no credentials', async (done) => { + // const token = await putPackage('@scope%2fpk1-test'); + request(app) .put('/@scope%2fpk1-test') .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) @@ -643,87 +646,6 @@ describe('endpoint unit test', () => { }); }); - describe('should test star and stars api', () => { - test('should star a package', (done) => { - request(app) - .put('/@scope%2fpk1-test') - .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) - .send(JSON.stringify({ - ...starMetadata, - users: { - [credentials.name]: true - } - })) - .expect(HTTP_STATUS.OK) - .end(function(err, res) { - if (err) { - expect(err).toBeNull(); - return done(err); - } - expect(res.body.success).toBeDefined(); - expect(res.body.success).toBeTruthy(); - done(); - }); - }); - - test('should unstar a package', (done) => { - request(app) - .put('/@scope%2fpk1-test') - .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) - .send(JSON.stringify(starMetadata)) - .expect(HTTP_STATUS.OK) - .end(function(err, res) { - if (err) { - expect(err).toBeNull(); - return done(err); - } - expect(res.body.success).toBeDefined(); - expect(res.body.success).toBeTruthy(); - done(); - }); - }); - - test('should retrieve stars list with credentials', async (done) => { - const credentials = { name: 'star_user', password: 'secretPass' }; - const token = await getNewToken(request(app), credentials); - request(app) - .put('/@scope%2fpk1-test') - .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token)) - .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) - .send(JSON.stringify({ - ...starMetadata, - users: { - [credentials.name]: true - } - })) - .expect(HTTP_STATUS.OK).end(function(err) { - if (err) { - expect(err).toBeNull(); - return done(err); - } - request(app) - .get('/-/_view/starredByUser') - .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token)) - .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) - .send(JSON.stringify({ - key: [credentials.name] - })) - .expect(HTTP_STATUS.OK) - .end(function(err, res) { - if (err) { - expect(err).toBeNull(); - return done(err); - } - expect(res.body.rows).toBeDefined(); - expect(res.body.rows).toHaveLength(1); - done(); - }); - }); - }); - }); - - - test('should unpublish a new package with credentials', async (done) => { const credentials = { name: 'jota_unpublish', password: 'secretPass' }; @@ -829,5 +751,90 @@ describe('endpoint unit test', () => { }); }); }); + + describe('should test star and stars api', () => { + const pkgName = '@scope/starPackage'; + beforeAll(async (done) =>{ + await putPackage(request(app), `/${pkgName}`, generatePackageMetadata(pkgName)); + done(); + }); + + test('should star a package', (done) => { + request(app) + .put(`/${pkgName}`) + .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) + .send(JSON.stringify({ + ...starMetadata, + users: { + [credentials.name]: true + } + })) + .expect(HTTP_STATUS.OK) + .end(function(err, res) { + if (err) { + expect(err).toBeNull(); + return done(err); + } + expect(res.body.success).toBeDefined(); + expect(res.body.success).toBeTruthy(); + done(); + }); + }); + + test('should unstar a package', (done) => { + request(app) + .put(`/${pkgName}`) + .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) + .send(JSON.stringify(starMetadata)) + .expect(HTTP_STATUS.OK) + .end(function(err, res) { + if (err) { + expect(err).toBeNull(); + return done(err); + } + expect(res.body.success).toBeDefined(); + expect(res.body.success).toBeTruthy(); + done(); + }); + }); + + test('should retrieve stars list with credentials', async (done) => { + const credentials = { name: 'star_user', password: 'secretPass' }; + const token = await getNewToken(request(app), credentials); + request(app) + .put(`/${pkgName}`) + .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token)) + .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) + .send(JSON.stringify({ + ...starMetadata, + users: { + [credentials.name]: true + } + })) + .expect(HTTP_STATUS.OK).end(function(err) { + if (err) { + expect(err).toBeNull(); + return done(err); + } + request(app) + .get('/-/_view/starredByUser') + .set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token)) + .set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON) + .send(JSON.stringify({ + key: [credentials.name] + })) + .expect(HTTP_STATUS.OK) + .end(function(err, res) { + if (err) { + expect(err).toBeNull(); + return done(err); + } + expect(res.body.rows).toBeDefined(); + expect(res.body.rows).toHaveLength(1); + done(); + }); + }); + }); + }); }); }); diff --git a/test/unit/modules/api/publish.spec.ts b/test/unit/modules/api/publish.spec.ts index 4d7e98fbd..cb87397a8 100644 --- a/test/unit/modules/api/publish.spec.ts +++ b/test/unit/modules/api/publish.spec.ts @@ -3,6 +3,10 @@ import { HTTP_STATUS, API_ERROR } from '../../../../src/lib/constants'; const REVISION_MOCK = '15-e53a77096b0ee33e'; +require('../../../../src/lib/logger').setup([ + { type: 'stdout', format: 'pretty', level: 'info' } +]); + describe('Publish endpoints - add a tag', () => { let req; let res;