From 050f857fb6c57c35711ace18608844bb1f91bfb8 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Sat, 30 Jun 2018 17:13:53 +0200 Subject: [PATCH] refactor: add new constants --- .eslintrc | 1 + src/lib/auth.js | 6 +++--- src/lib/constants.js | 3 +++ src/lib/storage-utils.js | 13 +++++++------ test/unit/api/api.spec.js | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.eslintrc b/.eslintrc index f0ed31c46..b50213f01 100644 --- a/.eslintrc +++ b/.eslintrc @@ -32,6 +32,7 @@ "rules": { "no-useless-escape": 2, "react/no-deprecated": 1, + "react/jsx-no-target-blank": 1, "handle-callback-err": 2, "no-fallthrough": 2, "no-new-require": 2, diff --git a/src/lib/auth.js b/src/lib/auth.js index c53af7ebc..1898e6804 100644 --- a/src/lib/auth.js +++ b/src/lib/auth.js @@ -8,7 +8,7 @@ import {aesDecrypt, aesEncrypt, signPayload, verifyPayload} from './crypto-utils import type {Config, Logger, Callback} from '@verdaccio/types'; import type {$Response, NextFunction} from 'express'; import type {$RequestExtend, JWTPayload} from '../../types'; -import {ROLES} from './constants'; +import {API_ERROR, ROLES} from './constants'; const LoggerApi = require('./logger'); @@ -63,11 +63,11 @@ class Auth { this.plugins.push({ authenticate: function(user, password, cb) { - cb(ErrorCode.getForbidden('bad username/password, access denied')); + cb(ErrorCode.getForbidden(API_ERROR.BAD_USERNAME_PASSWORD)); }, add_user: function(user, password, cb) { - return cb(ErrorCode.getConflict('bad username/password, access denied')); + return cb(ErrorCode.getConflict(API_ERROR.BAD_USERNAME_PASSWORD)); }, allow_access: allow_action('access'), diff --git a/src/lib/constants.js b/src/lib/constants.js index b39cf8a14..f7e892dca 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -65,17 +65,20 @@ export const API_MESSAGE = { }; export const API_ERROR = { + BAD_USERNAME_PASSWORD: 'bad username/password, access denied {APP}', NO_PACKAGE: 'no such package available', NOT_ALLOWED: 'not allowed to access package', INTERNAL_SERVER_ERROR: 'internal server error', UNKNOWN_ERROR: 'unknown error', NOT_PACKAGE_UPLINK: 'package does not exist on uplink', + UPLINK_OFFLINE: 'one of the uplinks is down, refuse to publish', CONTENT_MISMATCH: 'content length mismatch', NOT_FILE_UPLINK: 'file doesn\'t exist on uplink', MAX_USERS_REACHED: 'maximum amount of users reached', VERSION_NOT_EXIST: 'this version doesn\'t exist', FILE_NOT_FOUND: 'File not found', BAD_STATUS_CODE: 'bad status code', + PACKAGE_EXIST: 'this package is already present', WEB_DISABLED: 'Web interface is disabled in the config file', }; diff --git a/src/lib/storage-utils.js b/src/lib/storage-utils.js index f4790ebc6..b84ce60f1 100644 --- a/src/lib/storage-utils.js +++ b/src/lib/storage-utils.js @@ -7,6 +7,7 @@ import {generateRandomHexString} from '../lib/crypto-utils'; import type {Package, Version} from '@verdaccio/types'; import type {IStorage} from '../../types'; +import {API_ERROR, HTTP_STATUS} from './constants'; const pkgFileName = 'package.json'; const fileExist: string = 'EEXISTS'; @@ -123,11 +124,11 @@ export function cleanUpLinksRef(keepUpLinkData: boolean, result: Package): Packa export function checkPackageLocal(name: string, localStorage: IStorage): Promise { return new Promise((resolve, reject) => { localStorage.getPackageMetadata(name, (err, results) => { - if (!_.isNil(err) && err.status !== 404) { + if (!_.isNil(err) && err.status !== HTTP_STATUS.NOT_FOUND) { return reject(err); } if (results) { - return reject(ErrorCode.getConflict('this package is already present')); + return reject(ErrorCode.getConflict(API_ERROR.PACKAGE_EXIST)); } return resolve(); }); @@ -152,25 +153,25 @@ export function checkPackageRemote(name: string, isAllowPublishOffline: boolean, // $FlowFixMe syncMetadata(name, null, {}, (err, packageJsonLocal, upLinksErrors) => { // something weird - if (err && err.status !== 404) { + if (err && err.status !== HTTP_STATUS.NOT_FOUND) { return reject(err); } // checking package exist already if (_.isNil(packageJsonLocal) === false) { - return reject(ErrorCode.getConflict('this package is already present')); + return reject(ErrorCode.getConflict(API_ERROR.PACKAGE_EXIST)); } for (let errorItem = 0; errorItem < upLinksErrors.length; errorItem++) { // checking error // if uplink fails with a status other than 404, we report failure if (_.isNil(upLinksErrors[errorItem][0]) === false) { - if (upLinksErrors[errorItem][0].status !== 404) { + if (upLinksErrors[errorItem][0].status !== HTTP_STATUS.NOT_FOUND) { if (isAllowPublishOffline) { return resolve(); } - return reject(ErrorCode.getServiceUnavailable('one of the uplinks is down, refuse to publish')); + return reject(ErrorCode.getServiceUnavailable(API_ERROR.UPLINK_OFFLINE)); } } } diff --git a/test/unit/api/api.spec.js b/test/unit/api/api.spec.js index e8fd817e0..f21526cc4 100644 --- a/test/unit/api/api.spec.js +++ b/test/unit/api/api.spec.js @@ -29,7 +29,7 @@ describe('endpoint unit test', () => { const configForTest = _.clone(configDefault); configForTest.auth = { htpasswd: { - file: './test-storage/htpasswd-test' + file: './test-storage/.htpasswd' } }; configForTest.uplinks = {