Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug with --experimental-dep-graph and wrong auth token #882

Merged
merged 1 commit into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice refactor

) {
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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