-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use rewire to test exhort services
Signed-off-by: Ilona Shishov <[email protected]>
- Loading branch information
1 parent
3f9a400
commit e5ae4c6
Showing
12 changed files
with
312 additions
and
99 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as chai from 'chai'; | ||
import * as sinon from 'sinon'; | ||
import * as sinonChai from 'sinon-chai'; | ||
import { rewireModule, cleanupRewireFiles } from './utils'; | ||
|
||
const expect = chai.expect; | ||
chai.use(sinonChai); | ||
|
||
suite('ExhortServices module', async () => { | ||
let sandbox: sinon.SinonSandbox; | ||
|
||
const compiledFilePath = 'out/src/exhortServices'; | ||
const stackAnalysisReportHtmlMock = '<html>RHDA Report Mock</html>'; | ||
|
||
let exhortMock = { | ||
default: { | ||
stackAnalysis: async () => stackAnalysisReportHtmlMock, | ||
validateToken: async (statusCode) => statusCode, | ||
} | ||
}; | ||
|
||
let vscodeMock = { | ||
window: { | ||
showInformationMessage: sinon.spy(), | ||
showWarningMessage: sinon.spy(), | ||
showErrorMessage: sinon.spy(), | ||
} | ||
}; | ||
|
||
let exhortServicesRewire; | ||
|
||
setup(async () => { | ||
sandbox = sinon.createSandbox(); | ||
exhortServicesRewire = await rewireModule(compiledFilePath); | ||
exhortServicesRewire.__Rewire__('exhort_javascript_api_1', exhortMock); | ||
exhortServicesRewire.__Rewire__('vscode', vscodeMock); | ||
}); | ||
|
||
teardown(() => { | ||
sandbox.restore(); | ||
cleanupRewireFiles(compiledFilePath); | ||
}); | ||
|
||
test('should generate RHDA report HTML from Exhort Stack Analysis service', async () => { | ||
await exhortServicesRewire.stackAnalysisService('mock/path/to/manifest', {}) | ||
.then((result) => { | ||
expect(result).to.equal(stackAnalysisReportHtmlMock); | ||
}) | ||
}); | ||
|
||
test('should perform token validation with Exhort Validate Token service', async () => { | ||
await exhortServicesRewire.tokenValidationService(200, 'provider'); | ||
await exhortServicesRewire.tokenValidationService(400, 'provider'); | ||
await exhortServicesRewire.tokenValidationService(401, 'provider'); | ||
await exhortServicesRewire.tokenValidationService(403, 'provider'); | ||
await exhortServicesRewire.tokenValidationService(429, 'provider'); | ||
await exhortServicesRewire.tokenValidationService(500, 'provider'); | ||
|
||
expect(vscodeMock.window.showInformationMessage).to.have.been.calledWith('provider Token Validated Successfully'); | ||
expect(vscodeMock.window.showWarningMessage).to.have.been.calledWith('Missing token. Please provide a valid provider Token in the extension workspace settings. Status: 400'); | ||
expect(vscodeMock.window.showWarningMessage).to.have.been.calledWith('Invalid token. Please provide a valid provider Token in the extension workspace settings. Status: 401'); | ||
expect(vscodeMock.window.showWarningMessage).to.have.been.calledWith('Forbidden. The token does not have permissions. Please provide a valid provider Token in the extension workspace settings. Status: 403'); | ||
expect(vscodeMock.window.showWarningMessage).to.have.been.calledWith('Too many requests. Rate limit exceeded. Please try again in a little while. Status: 429'); | ||
expect(vscodeMock.window.showWarningMessage).to.have.been.calledWith('Failed to validate token. Status: 500'); | ||
}); | ||
|
||
test('should fail to generate RHDA report HTML from Exhort Stack Analysis service and reject with error', async () => { | ||
|
||
let exhortMock = { | ||
default: { | ||
stackAnalysis: async () => { | ||
throw new Error('Analysis Error'); | ||
}, | ||
} | ||
}; | ||
|
||
exhortServicesRewire.__Rewire__('exhort_javascript_api_1', exhortMock); | ||
|
||
await exhortServicesRewire.stackAnalysisService('mock/path/to/manifest', {}) | ||
.then(() => { | ||
throw new Error('should have thrown Analysis Error') | ||
}) | ||
.catch((error) => { | ||
expect(error.message).to.equal('Analysis Error'); | ||
}) | ||
}); | ||
|
||
test('should fail to perform token validation with Exhort Validate Token service and display error message', async () => { | ||
|
||
let exhortMock = { | ||
default: { | ||
validateToken: async () => { | ||
throw new Error('Validation Error'); | ||
}, | ||
} | ||
}; | ||
|
||
exhortServicesRewire.__Rewire__('exhort_javascript_api_1', exhortMock); | ||
|
||
await exhortServicesRewire.tokenValidationService(500, 'provider'); | ||
|
||
expect(vscodeMock.window.showErrorMessage).to.have.been.calledWith('Failed to validate token, Error: Validation Error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as chai from 'chai'; | ||
import * as sinon from 'sinon'; | ||
import * as sinonChai from 'sinon-chai'; | ||
import { rewireModule, cleanupRewireFiles } from './utils'; | ||
|
||
const expect = chai.expect; | ||
chai.use(sinonChai); | ||
|
||
suite('RedhatTelemetry module', async () => { | ||
let sandbox: sinon.SinonSandbox; | ||
|
||
const compiledFilePath = 'out/src/redhatTelemetry'; | ||
const redHatUUIDMock = 'Mock UUID'; | ||
|
||
let getRedHatUUIDMock = { | ||
getRedHatUUID: async () => redHatUUIDMock | ||
} | ||
|
||
let sendEventMock = { | ||
sendStartupEvent: sinon.spy(), | ||
send: sinon.spy() | ||
} | ||
|
||
let getIdProviderMock = { | ||
getIdProvider: async () => getRedHatUUIDMock, | ||
getTelemetryService: async () => sendEventMock | ||
} | ||
|
||
let getRedHatServiceMock = { | ||
getRedHatService: async () => getIdProviderMock | ||
}; | ||
|
||
let redhatTelemetryRewire; | ||
|
||
setup(async () => { | ||
sandbox = sinon.createSandbox(); | ||
redhatTelemetryRewire = await rewireModule(compiledFilePath); | ||
redhatTelemetryRewire.__Rewire__('vscode_redhat_telemetry_1', getRedHatServiceMock); | ||
}); | ||
|
||
teardown(() => { | ||
sandbox.restore(); | ||
cleanupRewireFiles(compiledFilePath); | ||
}); | ||
|
||
test('should get UUID from vscode redhat telemetry service', async () => { | ||
let telemetryId = await redhatTelemetryRewire.getTelemetryId({}) | ||
expect(telemetryId).to.equal(redHatUUIDMock); | ||
}); | ||
|
||
test('should send statup telemetry event', async () => { | ||
await redhatTelemetryRewire.startUp({}) | ||
expect(sendEventMock.sendStartupEvent).to.have.been.calledOnce; | ||
}); | ||
|
||
test('should record telemetry event', async () => { | ||
await redhatTelemetryRewire.record({}, 'telemetry_event_mock', { mockProp: 'mockValue' }) | ||
expect(sendEventMock.send).to.have.been.calledWith({ | ||
type: 'track', | ||
name: 'telemetry_event_mock', | ||
properties: { mockProp: 'mockValue' } | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as babelCore from "@babel/core"; | ||
import * as fs from "fs"; | ||
|
||
/** | ||
* Asynchronously imports a module. | ||
* | ||
* @param filepath Path to the module to be imported. | ||
* @returns A promise that resolves to the module's exports. | ||
*/ | ||
async function dynamicImportProvider(filepath) { | ||
return await import(filepath) | ||
} | ||
|
||
/** | ||
* Rewires a module for testing purposes. | ||
* @param filepath path to the compiled JavaScript file of the tested module. | ||
* @return A module instance that exposes private methods/functions/properties to be mocked/stubbed. | ||
*/ | ||
export function rewireModule(filepath) { | ||
let moduleBuffeer = fs.readFileSync(filepath + ".js") | ||
let moduleSource = babelCore.transform(moduleBuffeer, { plugins: ["babel-plugin-rewire"] }).code; | ||
fs.writeFileSync(filepath + "_rewire.js", moduleSource) | ||
return dynamicImportProvider("../../" + filepath + "_rewire.js") | ||
} | ||
|
||
/** | ||
* Removes rewired modules from file system. | ||
* @param filepath path to the compiled JavaScript file of the tested module. | ||
*/ | ||
export function cleanupRewireFiles(filepath) { | ||
const fileToRemove = filepath + "_rewire.js" | ||
try { | ||
fs.unlinkSync(fileToRemove); | ||
} catch (err) { | ||
console.error(`Error deleting rewire module ${fileToRemove}: ${err.message}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters