Skip to content

Commit

Permalink
fix: bug with --experimental-dep-graph and wrong auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
dkontorovskyy committed Nov 27, 2019
1 parent 2d19f4d commit 924f10a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/cli/commands/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { MonitorOptions, MonitorMeta } from '../../../lib/types';
import { MethodArgs, ArgsOptions } from '../../args';
import { maybePrintDeps } from '../../../lib/print-deps';
import * as analytics from '../../../lib/analytics';
import { MonitorError, UnsupportedFeatureFlagError } from '../../../lib/errors';
import {
AuthFailedError,
MonitorError,
UnsupportedFeatureFlagError,
} from '../../../lib/errors';
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
import { isFeatureFlagSupportedForOrg } from '../../../lib/feature-flags';
import { formatMonitorOutput } from './formatters/format-monitor-response';
Expand Down Expand Up @@ -88,6 +92,10 @@ async function monitor(...args0: MethodArgs): Promise<any> {
_.camelCase('experimental-dep-graph'),
);

if (isFFSupported.code === 401) {
throw AuthFailedError(isFFSupported.error, isFFSupported.code);
}

if (!isFFSupported.ok) {
throw new UnsupportedFeatureFlagError(
'experimental-dep-graph',
Expand Down
15 changes: 8 additions & 7 deletions src/lib/errors/authentication-failed-error.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { CustomError } from './custom-error';
import * as config from '../config';

export function AuthFailedError(errorMessage, errorCode) {
const message = errorMessage
? errorMessage
: 'Authentication failed. Please check the API token on ' + config.ROOT;
const error = new CustomError(message);
error.code = errorCode || 401;
export function AuthFailedError(
errorMessage: string = 'Authentication failed. Please check the API token on ' +
config.ROOT,
errorCode = 401,
) {
const error = new CustomError(errorMessage);
error.code = errorCode;
error.strCode = 'authfail';
error.userMessage = message;
error.userMessage = errorMessage;
return error;
}
1 change: 1 addition & 0 deletions src/lib/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export { UnsupportedFeatureFlagError } from './unsupported-feature-flag-error';
export { UnsupportedPackageManagerError } from './unsupported-package-manager-error';
export { FailedToRunTestError } from './failed-to-run-test-error';
export { TooManyVulnPaths } from './too-many-vuln-paths';
export { AuthFailedError } from './authentication-failed-error';
4 changes: 3 additions & 1 deletion src/lib/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import snyk = require('.'); // TODO(kyegupov): fix import
import * as config from './config';

interface OrgFeatureFlagResponse {
ok: boolean;
ok?: boolean;
userMessage?: string;
code?: number;
error?: string;
}

export async function isFeatureFlagSupportedForOrg(
Expand Down
26 changes: 24 additions & 2 deletions test/acceptance/cli-monitor.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,32 @@ test('monitor for package with no name in lockfile', async (t) => {
t.pass('succeed');
});

test('`monitor npm-package with experimental-dep-graph enabled, but bad auth token`', async (t) => {
chdirWorkspaces();

const validTokenStub = sinon
.stub(needle, 'request')
.yields(null, null, { code: 401, error: 'Invalid auth token provided' });

try {
await cli.monitor('npm-package', { 'experimental-dep-graph': true });
t.fail('shoud have thrown an error');
} catch (e) {
t.equal(e.name, 'CustomError', 'correct error was thrown');
t.equal(
e.userMessage,
'Invalid auth token provided',
'correct default error message',
);

validTokenStub.restore();
}
});

test('`monitor npm-package with experimental-dep-graph not enabled`', async (t) => {
chdirWorkspaces();

const featureFlagRequestStub = sinon
const needleRequestStub = sinon
.stub(needle, 'request')
.yields(null, null, { ok: false });

Expand All @@ -152,7 +174,7 @@ test('`monitor npm-package with experimental-dep-graph not enabled`', async (t)
'correct default error message',
);

featureFlagRequestStub.restore();
needleRequestStub.restore();
}
});

Expand Down

0 comments on commit 924f10a

Please sign in to comment.