|
| 1 | +const assert = require('assert'); |
| 2 | +const fs = require('fs'); |
| 3 | +const net = require('net'); |
| 4 | +const connParameters = require('../../authentication/connectionParameters'); |
| 5 | +const axios = require('axios'); |
| 6 | +const AuthTest = require('../../authentication/authTestsBaseClass'); |
| 7 | +const WireMockRestClient = require('wiremock-rest-client').WireMockRestClient; |
| 8 | +const { exec } = require('child_process'); |
| 9 | +const Os = require("os"); |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +async function runWireMockAsync(port) { |
| 14 | + let timeoutHandle; |
| 15 | + const waitingWireMockPromise = new Promise(async (resolve, reject) => { |
| 16 | + try { |
| 17 | + exec(`npx wiremock --enable-browser-proxying --proxy-pass-through false --port ${port} `, (error, stdout, stderr) => { |
| 18 | + if (error) { |
| 19 | + console.error(`exec error: ${error}`); |
| 20 | + return; |
| 21 | + } |
| 22 | + console.log(`stdout: ${stdout}`); |
| 23 | + console.error(`stderr: ${stderr}`); |
| 24 | + }); |
| 25 | + const wireMock = new WireMockRestClient(`http://localhost:${port}`); |
| 26 | + const readyWireMock = await waitForWiremockStarted(wireMock); |
| 27 | + resolve(readyWireMock); |
| 28 | + } catch (err) { |
| 29 | + reject(err); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + |
| 34 | + const timeout = new Promise((resolve, reject) => |
| 35 | + timeoutHandle = setTimeout( |
| 36 | + () => reject('Wiremock unavailable after 26000 ms.'), |
| 37 | + 26000)); |
| 38 | + return Promise.race([waitingWireMockPromise, timeout]) |
| 39 | + .then(result => { |
| 40 | + clearTimeout(timeoutHandle); |
| 41 | + return result; |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +async function waitForWiremockStarted(wireMock) { |
| 46 | + return fetch(wireMock.baseUri) |
| 47 | + .then(async (resp) => { |
| 48 | + if (resp.ok) { |
| 49 | + return Promise.resolve(wireMock); |
| 50 | + } else { |
| 51 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 52 | + console.log(`Retry connection to WireMock after wrong response status: ${resp.status}`); |
| 53 | + return await waitForWiremockStarted(wireMock); |
| 54 | + } |
| 55 | + }) |
| 56 | + .catch(async (err) => { |
| 57 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 58 | + console.log(`Retry connection to WireMock after error: ${err}`); |
| 59 | + return await waitForWiremockStarted(wireMock); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +if (Os.platform() === 'linux') { |
| 64 | + describe('Wiremock test', function () { |
| 65 | + let port, wireMock; |
| 66 | + before(async () => { |
| 67 | + port = await getPortFree(); |
| 68 | + wireMock = await runWireMockAsync(port); |
| 69 | + }); |
| 70 | + after(async () => { |
| 71 | + await wireMock.global.shutdown(); |
| 72 | + }); |
| 73 | + it('Run Wiremock instance, wait, verify connection and shutdown', async function () { |
| 74 | + assert.doesNotReject(async () => await wireMock.mappings.getAllMappings()); |
| 75 | + }); |
| 76 | + it('Add mappings', async function () { |
| 77 | + const requests = JSON.parse(fs.readFileSync('wiremock/mappings/test.json', 'utf8')); |
| 78 | + for (const mapping of requests.mappings) { |
| 79 | + await wireMock.mappings.createMapping(mapping); |
| 80 | + } |
| 81 | + const mappings = await wireMock.mappings.getAllMappings(); |
| 82 | + assert.strictEqual(mappings.mappings.length, 2); |
| 83 | + const response = await axios.get(`http://localhost:${port}/test/authorize.html`); |
| 84 | + assert.strictEqual(response.status, 200); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe.only('Oauth PAT authentication', function () { |
| 89 | + let port; |
| 90 | + let authTest; |
| 91 | + let wireMock; |
| 92 | + before(async () => { |
| 93 | + port = await getPortFree(); |
| 94 | + wireMock = await runWireMockAsync(port); |
| 95 | + }); |
| 96 | + beforeEach(async () => { |
| 97 | + authTest = new AuthTest(); |
| 98 | + }); |
| 99 | + afterEach(async () => { |
| 100 | + wireMock.scenarios.resetAllScenarios(); |
| 101 | + }); |
| 102 | + after(async () => { |
| 103 | + await wireMock.global.shutdown(); |
| 104 | + }); |
| 105 | + |
| 106 | + |
| 107 | + it('Successful flow scenario PAT as token', async function () { |
| 108 | + await addWireMockMappingsFromFile('wiremock/mappings/pat/successful_flow.json'); |
| 109 | + const connectionOption = {...connParameters.oauthPATOnWiremock, token: 'MOCK_TOKEN', port: port}; |
| 110 | + authTest.createConnection(connectionOption); |
| 111 | + await authTest.connectAsync(); |
| 112 | + authTest.verifyNoErrorWasThrown(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('Successful flow scenario PAT as password', async function () { |
| 116 | + await addWireMockMappingsFromFile('wiremock/mappings/pat/successful_flow.json'); |
| 117 | + const connectionOption = {...connParameters.oauthPATOnWiremock, password: 'MOCK_TOKEN', port: port}; |
| 118 | + authTest.createConnection(connectionOption); |
| 119 | + await authTest.connectAsync(); |
| 120 | + authTest.verifyNoErrorWasThrown(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('Invalid token', async function () { |
| 124 | + await addWireMockMappingsFromFile('wiremock/mappings/pat/invalid_pat_token.json'); |
| 125 | + const connectionOption = {...connParameters.oauthPATOnWiremock, token: 'INVALID_TOKEN', port: port}; |
| 126 | + authTest.createConnection(connectionOption); |
| 127 | + await authTest.connectAsync(); |
| 128 | + authTest.verifyErrorWasThrown('Programmatic access token is invalid.'); |
| 129 | + }); |
| 130 | + |
| 131 | + async function addWireMockMappingsFromFile(filePath) { |
| 132 | + const requests = JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 133 | + for (const mapping of requests.mappings) { |
| 134 | + await wireMock.mappings.createMapping(mapping); |
| 135 | + } |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + // describe('Oauth PAT authentication', function () { |
| 140 | + // let port; |
| 141 | + // let authTest; |
| 142 | + // let wireMock; |
| 143 | + // before(async () => { |
| 144 | + // port = await getPortFree(); |
| 145 | + // wireMock = await runWireMockAsync(port); |
| 146 | + // }); |
| 147 | + // beforeEach(async () => { |
| 148 | + // authTest = new AuthTest(); |
| 149 | + // }); |
| 150 | + // afterEach(async () => { |
| 151 | + // wireMock.scenarios.resetAllScenarios(); |
| 152 | + // }); |
| 153 | + // after(async () => { |
| 154 | + // await wireMock.global.shutdown(); |
| 155 | + // }); |
| 156 | + // |
| 157 | + // |
| 158 | + // it('Successful flow scenario PAT as token', async function () { |
| 159 | + // await addWireMockMappingsFromFile('wiremock/mappings/pat/successful_flow.json'); |
| 160 | + // const connectionOption = {...connParameters.oauthPATOnWiremock, token: 'MOCK_TOKEN', port: port}; |
| 161 | + // authTest.createConnection(connectionOption); |
| 162 | + // await authTest.connectAsync(); |
| 163 | + // authTest.verifyNoErrorWasThrown(); |
| 164 | + // }); |
| 165 | + // |
| 166 | + // it('Successful flow scenario PAT as password', async function () { |
| 167 | + // await addWireMockMappingsFromFile('wiremock/mappings/pat/successful_flow.json'); |
| 168 | + // const connectionOption = {...connParameters.oauthPATOnWiremock, password: 'MOCK_TOKEN', port: port}; |
| 169 | + // authTest.createConnection(connectionOption); |
| 170 | + // await authTest.connectAsync(); |
| 171 | + // authTest.verifyNoErrorWasThrown(); |
| 172 | + // }); |
| 173 | + // |
| 174 | + // it('Invalid token', async function () { |
| 175 | + // await addWireMockMappingsFromFile('wiremock/mappings/pat/invalid_pat_token.json'); |
| 176 | + // const connectionOption = {...connParameters.oauthPATOnWiremock, token: 'INVALID_TOKEN', port: port}; |
| 177 | + // authTest.createConnection(connectionOption); |
| 178 | + // await authTest.connectAsync(); |
| 179 | + // authTest.verifyErrorWasThrown('Programmatic access token is invalid.'); |
| 180 | + // }); |
| 181 | + // |
| 182 | + // async function addWireMockMappingsFromFile(filePath) { |
| 183 | + // const requests = JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 184 | + // for (const mapping of requests.mappings) { |
| 185 | + // await wireMock.mappings.createMapping(mapping); |
| 186 | + // } |
| 187 | + // } |
| 188 | + // }); |
| 189 | + |
| 190 | + async function getPortFree() { |
| 191 | + return new Promise(res => { |
| 192 | + const srv = net.createServer(); |
| 193 | + srv.listen(0, () => { |
| 194 | + const port = srv.address().port; |
| 195 | + srv.close((err) => res(port)); |
| 196 | + }); |
| 197 | + }); |
| 198 | + } |
| 199 | +} |
0 commit comments