Skip to content

Commit

Permalink
test: add test coverage for utm auth
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Mar 11, 2020
1 parent aba5b03 commit 61e4932
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/lib/utm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as url from 'url';

const SNYK_UTM_MEDIUM = process.env.SNYK_UTM_MEDIUM || '';
const SNYK_UTM_SOURCE = process.env.SNYK_UTM_SOURCE || '';
const SNYK_UTM_CAMPAIGN = process.env.SNYK_UTM_CAMPAIGN || '';

export function getUtmsAsString(): string {
const SNYK_UTM_MEDIUM = process.env.SNYK_UTM_MEDIUM || '';
const SNYK_UTM_SOURCE = process.env.SNYK_UTM_SOURCE || '';
const SNYK_UTM_CAMPAIGN = process.env.SNYK_UTM_CAMPAIGN || '';

/* eslint-disable @typescript-eslint/camelcase */
const utmQueryParams = new url.URLSearchParams({
utm_medium: SNYK_UTM_MEDIUM,
Expand Down
51 changes: 50 additions & 1 deletion test/system/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ test('auth with no args', async (t) => {
opn: open,
});
// stub CI check (ensure returns false for system test)
sinon.stub(ciChecker, 'isCI').returns(false);
const ciStub = sinon.stub(ciChecker, 'isCI').returns(false);
// disable console.log
const enableLog = silenceLog();
try {
Expand All @@ -116,13 +116,62 @@ test('auth with no args', async (t) => {
'opens login with token param',
);
t.same(open.firstCall.args[1], { wait: false }, 'does not wait for open');
ciStub.restore();
} catch (e) {
t.threw(e);
}
// turn console.log back on
enableLog();
});

test('auth with UTMs in environment variables', async (t) => {
// stub open so browser window doesn't actually open
const open = sinon.stub();
const auth = proxyquire('../../src/cli/commands/auth', {
opn: open,
});
// stub CI check (ensure returns false for system test)
const ciStub = sinon.stub(ciChecker, 'isCI').returns(false);

// read data from console.log
let stdoutMessages = '';
const stubConsoleLog = (msg) => (stdoutMessages += msg);
const origConsoleLog = console.log;
console.log = stubConsoleLog;

process.env.SNYK_UTM_MEDIUM = 'ide';
process.env.SNYK_UTM_SOURCE = 'eclipse';
process.env.SNYK_UTM_CAMPAIGN = 'plugin';

try {
await auth();
t.match(
stdoutMessages,
'utm_medium=ide&utm_source=eclipse&utm_campaign=plugin',
'utm detected in environment variables',
);
t.ok(open.calledOnce, 'called open once');
t.match(
open.firstCall.args[0],
'&utm_medium=ide&utm_source=eclipse&utm_campaign=plugin',
'opens login with utm tokens provided',
);
t.same(open.firstCall.args[1], { wait: false }, 'does not wait for open');

// clean up environment variables
delete process.env.SNYK_UTM_MEDIUM;
delete process.env.SNYK_UTM_SOURCE;
delete process.env.SNYK_UTM_CAMPAIGN;
// clean up stubs
ciStub.restore();

// restore original console.log
console.log = origConsoleLog;
} catch (e) {
t.threw(e);
}
});

test('cli tests error paths', async (t) => {
try {
await cli.test('/', { json: true });
Expand Down

0 comments on commit 61e4932

Please sign in to comment.