Skip to content

Commit

Permalink
wip: migrate auth tests to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahed Ahmed committed Nov 9, 2021
1 parent fbb741c commit d918397
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
4 changes: 4 additions & 0 deletions test/acceptance/fake-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type FakeServer = {
setNextResponse: (r: any) => void;
setNextStatusCode: (c: number) => void;
setFeatureFlag: (featureFlag: string, enabled: boolean) => void;
getSnykToken: () => string;
listen: (port: string | number, callback: () => void) => void;
restore: () => void;
close: (callback: () => void) => void;
Expand Down Expand Up @@ -66,6 +67,8 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
featureFlags.set(featureFlag, enabled);
};

const getSnykToken = (): string => snykToken;

const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use((req, res, next) => {
Expand Down Expand Up @@ -442,6 +445,7 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
setNextResponse,
setNextStatusCode,
setFeatureFlag,
getSnykToken,
listen,
restore,
close,
Expand Down
64 changes: 64 additions & 0 deletions test/jest/acceptance/snyk-auth/snyk-auth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { fakeServer } from '../../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';

jest.setTimeout(1000 * 60);

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

const removeAuth = (env: Record<string, string>) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { SNYK_TOKEN, ...result } = env;
return result;
};

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', // replace token from process.env
SNYK_DISABLE_ANALYTICS: '1',
};

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

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

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

it('accepts valid token', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setDepGraphResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`auth ${server.getSnykToken()}`, {
cwd: project.path(),
env: removeAuth(env),
});

expect(code).toEqual(0);
expect(stdout).toMatch('Your account has been authenticated.');
});

it('rejects invalid token', async () => {
const project = await createProjectFromWorkspace('fail-on/no-vulns');
server.setDepGraphResponse(await project.readJSON('vulns-result.json'));

const { code, stdout } = await runSnykCLI(`auth invalid-token`, {
cwd: project.path(),
env: removeAuth(env),
});

expect(code).toEqual(2);
expect(stdout).toMatch('Authentication failed.');
});
});
25 changes: 0 additions & 25 deletions test/system/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,6 @@ before('prime config', async (t) => {
t.pass('endpoint removed');
});

test('auth via key', async (t) => {
try {
const res = await cli.auth(apiKey);
t.notEqual(res.toLowerCase().indexOf('ready'), -1, 'snyk auth worked');
} catch (e) {
t.threw(e);
}
});

test('auth via invalid key', async (t) => {
const errors = require('../../src/lib/errors/legacy-errors');

try {
const res = await cli.auth('_____________');
t.fail('auth should not succeed: ' + res);
} catch (e) {
const message = stripAnsi(errors.message(e));
t.equal(
message.toLowerCase().indexOf('authentication failed'),
0,
'captured failed auth',
);
}
});

test('auth with no args', async (t) => {
// stub open so browser window doesn't actually open
const open = sinon.stub();
Expand Down

0 comments on commit d918397

Please sign in to comment.