From ca53f945cf1a409fd871482957b0e699cfb06a40 Mon Sep 17 00:00:00 2001 From: Ilona Shishov Date: Thu, 29 Feb 2024 14:44:06 +0200 Subject: [PATCH] chore: add feedback notifications Signed-off-by: Ilona Shishov --- src/config.ts | 3 ++- src/exhortServices.ts | 2 ++ src/extension.ts | 2 +- src/tokenValidation.ts | 7 +++++-- test/config.test.ts | 23 +++++++++++++++++++++++ test/exhortServices.test.ts | 5 +++++ test/redhatTelemetry.test.ts | 4 ++-- test/tokenValidation.test.ts | 8 ++++++-- 8 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/config.ts b/src/config.ts index f554b6b87..3968d8c69 100644 --- a/src/config.ts +++ b/src/config.ts @@ -120,10 +120,11 @@ class Config { * @returns A Promise that resolves when the token is set. */ async setSnykToken(token: string | undefined): Promise { - if (!token) { return; } + if (token === undefined) { return; } try { await this.secrets.store(SNYK_TOKEN_KEY, token); + vscode.window.showInformationMessage(`Snyk token has been ${token === '' ? 'removed' : 'saved'} successfully`); } catch (error) { vscode.window.showErrorMessage(`Failed to save Snyk token to VSCode Secret Storage, Error: ${error.message}`); } diff --git a/src/exhortServices.ts b/src/exhortServices.ts index d2d688d3a..331ee78a7 100644 --- a/src/exhortServices.ts +++ b/src/exhortServices.ts @@ -1,5 +1,6 @@ 'use strict'; +import * as vscode from 'vscode'; import exhort from '@RHEcosystemAppEng/exhort-javascript-api'; /** @@ -35,6 +36,7 @@ async function tokenValidationService(options, source): Promise { if ( tokenValidationStatus === 200 ) { + vscode.window.showInformationMessage(`${source} token validated successfully`); return; } else if ( tokenValidationStatus === 400 diff --git a/src/extension.ts b/src/extension.ts index 3f7031164..6ec5a08ce 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -83,7 +83,7 @@ export function activate(context: vscode.ExtensionContext) { validateInput: validateSnykToken }); - if (!token) { return; } + if (token === undefined) { return; } await globalConfig.setSnykToken(token); } ); diff --git a/src/tokenValidation.ts b/src/tokenValidation.ts index db0931636..908005c42 100644 --- a/src/tokenValidation.ts +++ b/src/tokenValidation.ts @@ -1,5 +1,7 @@ 'use strict'; +import * as vscode from 'vscode'; + import { globalConfig } from './config'; import { SNYK_URL } from './constants'; import { tokenValidationService } from './exhortServices'; @@ -25,9 +27,10 @@ async function validateSnykToken(token: string): Promise { } else { - return `Please note that if you fail to provide a valid Snyk Token in the extension workspace settings, + vscode.window.showInformationMessage(`Please note that if you fail to provide a valid Snyk Token in the extension workspace settings, Snyk vulnerabilities will not be displayed. - To resolve this issue, please obtain a valid token from the following link: [here](${SNYK_URL}).`; + To resolve this issue, please obtain a valid token from the following link: [here](${SNYK_URL}).`); + return; } } diff --git a/test/config.test.ts b/test/config.test.ts index 8ea5ba7c4..76a3867fe 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -87,11 +87,34 @@ suite('Config module', () => { }); const showErrorMessageSpy = sandbox.spy(vscode.window, 'showErrorMessage'); + const showInformationMessageSpy = sandbox.spy(vscode.window, 'showInformationMessage'); const storeSecretSpy = sandbox.spy(globalConfig.secrets, 'store'); await globalConfig.setSnykToken(mockToken); expect(storeSecretSpy).to.have.been.calledWith(SNYK_TOKEN_KEY, mockToken); + expect(showInformationMessageSpy).to.have.been.calledWith('Snyk token has been saved successfully'); + expect(showErrorMessageSpy.called).to.be.false; + }); + + test('should remove Snyk token in VSCode SecretStorage', async () => { + + globalConfig.linkToSecretStorage({ + secrets: { + store: () => sandbox.stub(), + get: () => '', + delete: () => sandbox.stub() + } + }); + + const showErrorMessageSpy = sandbox.spy(vscode.window, 'showErrorMessage'); + const showInformationMessageSpy = sandbox.spy(vscode.window, 'showInformationMessage'); + const storeSecretSpy = sandbox.spy(globalConfig.secrets, 'store'); + + await globalConfig.setSnykToken(''); + + expect(storeSecretSpy).to.have.been.calledWith(SNYK_TOKEN_KEY, ''); + expect(showInformationMessageSpy).to.have.been.calledWith('Snyk token has been removed successfully'); expect(showErrorMessageSpy.called).to.be.false; }); diff --git a/test/exhortServices.test.ts b/test/exhortServices.test.ts index a86a6a03d..45d4a6379 100644 --- a/test/exhortServices.test.ts +++ b/test/exhortServices.test.ts @@ -2,6 +2,7 @@ import * as chai from 'chai'; import * as sinon from 'sinon'; import * as sinonChai from 'sinon-chai'; import { rewireModule, cleanupRewireFiles } from './utils'; +import * as vscode from 'vscode'; const expect = chai.expect; chai.use(sinonChai); @@ -40,7 +41,11 @@ suite('ExhortServices module', async () => { }); test('should perform token validation with Exhort Validate Token service', async () => { + sandbox.spy(vscode.window, 'showInformationMessage'); + expect(await exhortServicesRewire.tokenValidationService(200, 'provider')).to.equal(undefined); + expect(vscode.window.showInformationMessage).to.have.been.calledWith('provider token validated successfully'); + expect(await exhortServicesRewire.tokenValidationService(400, 'provider')).to.equal('Missing token. Please provide a valid provider Token in the extension workspace settings. Status: 400'); expect(await exhortServicesRewire.tokenValidationService(401, 'provider')).to.equal('Invalid token. Please provide a valid provider Token in the extension workspace settings. Status: 401'); expect(await exhortServicesRewire.tokenValidationService(403, 'provider')).to.equal('Forbidden. The token does not have permissions. Please provide a valid provider Token in the extension workspace settings. Status: 403'); diff --git a/test/redhatTelemetry.test.ts b/test/redhatTelemetry.test.ts index 9c7bc6d05..c032fee99 100644 --- a/test/redhatTelemetry.test.ts +++ b/test/redhatTelemetry.test.ts @@ -17,8 +17,8 @@ suite('RedhatTelemetry module', async () => { } let sendEventMock = { - sendStartupEvent: sinon.spy(), - send: sinon.spy() + sendStartupEvent: sandbox.spy(), + send: sandbox.spy() } let getIdProviderMock = { diff --git a/test/tokenValidation.test.ts b/test/tokenValidation.test.ts index f981a1ece..c1e965bc7 100644 --- a/test/tokenValidation.test.ts +++ b/test/tokenValidation.test.ts @@ -40,8 +40,12 @@ suite('TokenValidation module', () => { test('should validate empty Snyk token', async () => { const expectedMsg = `Please note that if you fail to provide a valid Snyk Token in the extension workspace settings, Snyk vulnerabilities will not be displayed. To resolve this issue, please obtain a valid token from the following link: [here](${SNYK_URL}).`; - const message = await validateSnykToken(''); + const showInformationMessageStub = sandbox.stub(vscode.window, 'showInformationMessage'); - expect(message.replace(/\s+/g, ' ').replace(/\n/g, ' ')).to.equal(expectedMsg); + await validateSnykToken(''); + + const showInformationMessageCall = showInformationMessageStub.getCall(0); + const showInformationMessageMsg = showInformationMessageCall.args[0]; + expect(showInformationMessageMsg.replace(/\s+/g, ' ').replace(/\n/g, ' ')).to.equal(expectedMsg); }); }); \ No newline at end of file