Skip to content

Commit

Permalink
test: migrate no auth test to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahed Ahmed committed Nov 9, 2021
1 parent 85b681b commit fbb741c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
50 changes: 50 additions & 0 deletions test/jest/acceptance/snyk-test/no-auth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { fakeServer } from '../../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';

jest.setTimeout(1000 * 60);

describe('snyk test without authentication', () => {
let server: ReturnType<typeof fakeServer>;
let env: Record<string, string>;

beforeAll((done) => {
const apiPath = '/api/v1';
const apiPort = process.env.PORT || process.env.SNYK_PORT || '12345';
env = {
...process.env,
SNYK_API: 'http://localhost:' + apiPort + apiPath,
SNYK_TOKEN: '123456789',
SNYK_DISABLE_ANALYTICS: '1',
};

server = fakeServer(apiPath, env.SNYK_TOKEN);
server.listen(apiPort, () => done());
});

afterEach(() => {
server.restore();
});

afterAll((done) => {
server.close(() => done());
});

it('errors when auth token is not provided', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setDepGraphResponse(await project.readJSON('vulns-result.json'));

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { SNYK_TOKEN, ...envWithoutToken } = env;

const { code, stdout } = await runSnykCLI(`test`, {
cwd: project.path(),
env: envWithoutToken,
});

expect(code).toEqual(2);
expect(stdout).toMatch(
'`snyk` requires an authenticated account. Please run `snyk auth` and try again.',
);
});
});
16 changes: 0 additions & 16 deletions test/system/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,6 @@ before('prime config', async (t) => {
t.pass('endpoint removed');
});

test('test without authentication', async (t) => {
await cli.config('unset', 'api');
try {
await cli.test('semver@2');
t.fail('test should not pass if not authenticated');
} catch (error) {
t.deepEquals(error.strCode, 'NO_API_TOKEN', 'string code is as expected');
t.match(
error.message,
'`snyk` requires an authenticated account. Please run `snyk auth` and try again.',
'error message is shown as expected',
);
}
await cli.config('set', 'api=' + apiKey);
});

test('auth via key', async (t) => {
try {
const res = await cli.auth(apiKey);
Expand Down

0 comments on commit fbb741c

Please sign in to comment.