-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
556e783
commit 5044a00
Showing
20 changed files
with
406 additions
and
8 deletions.
There are no files selected for viewing
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,29 @@ | ||
const Util = require('../util'); | ||
/** | ||
* Creates an oauth PAT authenticator. | ||
* | ||
* @param {String} token | ||
* @param {String} password | ||
* | ||
* @returns {Object} | ||
* @constructor | ||
*/ | ||
function AuthOauthPAT(token, password) { | ||
/** | ||
* Update JSON body with token. | ||
* | ||
* @param {JSON} body | ||
* | ||
* @returns {null} | ||
*/ | ||
this.updateBody = function (body) { | ||
if (Util.exists(token)) { | ||
body['data']['TOKEN'] = token; | ||
} else if (Util.exists(password)) { | ||
body['data']['TOKEN'] = password; | ||
} | ||
}; | ||
|
||
this.authenticate = async function () {}; | ||
} | ||
module.exports = AuthOauthPAT; |
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
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,51 @@ | ||
const net = require('net'); | ||
const connParameters = require('../../authentication/connectionParameters'); | ||
const AuthTest = require('../../authentication/authTestsBaseClass'); | ||
const { runWireMockAsync, addWireMockMappingsFromFile, } = require('../../wiremockRunner'); | ||
const os = require('os'); | ||
const { getFreePort } = require('../../../lib/util'); | ||
|
||
if (os.platform !== 'win32') { | ||
describe('Oauth PAT authentication', function () { | ||
let port; | ||
let authTest; | ||
let wireMock; | ||
before(async () => { | ||
port = await getFreePort(); | ||
wireMock = await runWireMockAsync(port); | ||
}); | ||
beforeEach(async () => { | ||
authTest = new AuthTest(); | ||
}); | ||
afterEach(async () => { | ||
wireMock.scenarios.resetAllScenarios(); | ||
}); | ||
after(async () => { | ||
await wireMock.global.shutdown(); | ||
}); | ||
|
||
it('Successful flow scenario PAT as token', async function () { | ||
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/pat/successful_flow.json'); | ||
const connectionOption = { ...connParameters.oauthPATOnWiremock, token: 'MOCK_TOKEN', port: port }; | ||
authTest.createConnection(connectionOption); | ||
await authTest.connectAsync(); | ||
authTest.verifyNoErrorWasThrown(); | ||
}); | ||
|
||
it('Successful flow scenario PAT as password', async function () { | ||
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/pat/successful_flow.json'); | ||
const connectionOption = { ...connParameters.oauthPATOnWiremock, password: 'MOCK_TOKEN', port: port }; | ||
authTest.createConnection(connectionOption); | ||
await authTest.connectAsync(); | ||
authTest.verifyNoErrorWasThrown(); | ||
}); | ||
|
||
it('Invalid token', async function () { | ||
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/pat/invalid_pat_token.json'); | ||
const connectionOption = { ...connParameters.oauthPATOnWiremock, token: 'INVALID_TOKEN', port: port }; | ||
authTest.createConnection(connectionOption); | ||
await authTest.connectAsync(); | ||
authTest.verifyErrorWasThrown('Programmatic access token is invalid.'); | ||
}); | ||
}); | ||
} |
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,43 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const net = require('net'); | ||
const axios = require('axios'); | ||
const { runWireMockAsync } = require('../../wiremockRunner'); | ||
const os = require('os'); | ||
|
||
async function getFreePort() { | ||
return new Promise(res => { | ||
const srv = net.createServer(); | ||
srv.listen(0, () => { | ||
const port = srv.address().port; | ||
srv.close(() => res(port)); | ||
}); | ||
}); | ||
} | ||
|
||
if (os.platform !== 'win32') { | ||
describe('Wiremock test', function () { | ||
let port, wireMock; | ||
before(async () => { | ||
port = await getFreePort(); | ||
wireMock = await runWireMockAsync(port); | ||
}); | ||
after(async () => { | ||
await wireMock.global.shutdown(); | ||
}); | ||
it('Run Wiremock instance, wait, verify connection and shutdown', async function () { | ||
assert.doesNotReject(async () => await wireMock.mappings.getAllMappings()); | ||
}); | ||
it('Add mappings', async function () { | ||
const requests = JSON.parse(fs.readFileSync('wiremock/mappings/testMapping.json', 'utf8')); | ||
for (const mapping of requests.mappings) { | ||
await wireMock.mappings.createMapping(mapping); | ||
} | ||
const mappings = await wireMock.mappings.getAllMappings(); | ||
assert.strictEqual(mappings.mappings.length, 2); | ||
const response = await axios.get(`http://localhost:${port}/test/authorize.html`); | ||
assert.strictEqual(response.status, 200); | ||
}); | ||
}); | ||
|
||
} |
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,58 @@ | ||
const WireMockRestClient = require('wiremock-rest-client').WireMockRestClient; | ||
const { exec } = require('child_process'); | ||
const Logger = require('../lib/logger'); | ||
const fs = require('fs'); | ||
|
||
|
||
async function runWireMockAsync(port) { | ||
let timeoutHandle; | ||
const waitingWireMockPromise = new Promise( (resolve, reject) => { | ||
try { | ||
exec(`npx wiremock --enable-browser-proxying --proxy-pass-through false --port ${port} `); | ||
const wireMock = new WireMockRestClient(`http://localhost:${port}`, { logLevel: 'trace' }); | ||
const readyWireMock = waitForWiremockStarted(wireMock); | ||
resolve(readyWireMock); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
|
||
const timeout = new Promise((resolve, reject) => | ||
timeoutHandle = setTimeout( | ||
() => reject('Wiremock unavailable after 30s.'), | ||
30000)); | ||
return Promise.race([waitingWireMockPromise, timeout]) | ||
.then(result => { | ||
clearTimeout(timeoutHandle); | ||
return result; | ||
}); | ||
} | ||
|
||
async function waitForWiremockStarted(wireMock) { | ||
return fetch(wireMock.baseUri) | ||
.then(async (resp) => { | ||
if (resp.ok) { | ||
return Promise.resolve(wireMock); | ||
} else { | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
Logger.getInstance().info(`Retry connection to WireMock after wrong response status: ${resp.status}`); | ||
return await waitForWiremockStarted(wireMock); | ||
} | ||
}) | ||
.catch(async (err) => { | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
Logger.getInstance().info(`Retry connection to WireMock after error: ${err}`); | ||
return await waitForWiremockStarted(wireMock); | ||
}); | ||
} | ||
|
||
async function addWireMockMappingsFromFile(wireMock, filePath) { | ||
const requests = JSON.parse(fs.readFileSync(filePath, 'utf8')); | ||
for (const mapping of requests.mappings) { | ||
await wireMock.mappings.createMapping(mapping); | ||
} | ||
} | ||
|
||
exports.runWireMockAsync = runWireMockAsync; | ||
exports.addWireMockMappingsFromFile = addWireMockMappingsFromFile; | ||
|
Oops, something went wrong.