Skip to content

Commit

Permalink
chore: add feedback notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov committed Feb 29, 2024
1 parent 45116cc commit ca53f94
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ class Config {
* @returns A Promise that resolves when the token is set.
*/
async setSnykToken(token: string | undefined): Promise<void> {
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}`);
}
Expand Down
2 changes: 2 additions & 0 deletions src/exhortServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import * as vscode from 'vscode';
import exhort from '@RHEcosystemAppEng/exhort-javascript-api';

/**
Expand Down Expand Up @@ -35,6 +36,7 @@ async function tokenValidationService(options, source): Promise<string> {
if (
tokenValidationStatus === 200
) {
vscode.window.showInformationMessage(`${source} token validated successfully`);
return;
} else if (
tokenValidationStatus === 400
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function activate(context: vscode.ExtensionContext) {
validateInput: validateSnykToken
});

if (!token) { return; }
if (token === undefined) { return; }
await globalConfig.setSnykToken(token);
}
);
Expand Down
7 changes: 5 additions & 2 deletions src/tokenValidation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

import * as vscode from 'vscode';

import { globalConfig } from './config';
import { SNYK_URL } from './constants';
import { tokenValidationService } from './exhortServices';
Expand All @@ -25,9 +27,10 @@ async function validateSnykToken(token: string): Promise<string> {

} 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;

}
}
Expand Down
23 changes: 23 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
5 changes: 5 additions & 0 deletions test/exhortServices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions test/redhatTelemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ suite('RedhatTelemetry module', async () => {
}

let sendEventMock = {
sendStartupEvent: sinon.spy(),
send: sinon.spy()
sendStartupEvent: sandbox.spy(),
send: sandbox.spy()
}

let getIdProviderMock = {
Expand Down
8 changes: 6 additions & 2 deletions test/tokenValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit ca53f94

Please sign in to comment.