From 61e4932b79a0dafefbe7351f8319aea260e0ee59 Mon Sep 17 00:00:00 2001 From: Liran Tal Date: Wed, 11 Mar 2020 11:19:16 +0200 Subject: [PATCH] test: add test coverage for utm auth --- src/lib/utm.ts | 8 +++---- test/system/cli.test.ts | 51 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/lib/utm.ts b/src/lib/utm.ts index c77b33c4d1..d587c537c9 100644 --- a/src/lib/utm.ts +++ b/src/lib/utm.ts @@ -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, diff --git a/test/system/cli.test.ts b/test/system/cli.test.ts index 63b8c8ad16..a9810776c6 100644 --- a/test/system/cli.test.ts +++ b/test/system/cli.test.ts @@ -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 { @@ -116,6 +116,7 @@ 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); } @@ -123,6 +124,54 @@ test('auth with no args', async (t) => { 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 });