|
1 |
| -const newman = require('newman') |
2 |
| -const _ = require('lodash') |
3 |
| -const envHelper = require('./envHelper') |
4 |
| -const nock = require('nock') |
5 | 1 | const config = require('config')
|
| 2 | +const apiTestLib = require('tc-api-testing-lib') |
| 3 | +const helper = require('../../src/common/helper') |
| 4 | +const logger = require('../../src/common/logger') |
6 | 5 |
|
7 | 6 | const requests = [
|
8 | 7 | {
|
@@ -171,82 +170,27 @@ const requests = [
|
171 | 170 | }
|
172 | 171 | ]
|
173 | 172 |
|
174 |
| -const options = { |
175 |
| - collection: require('./resource-api.postman_collection.json'), |
176 |
| - exportEnvironment: 'test/postman/resource-api.postman_environment.json', |
177 |
| - reporters: 'cli' |
178 |
| -} |
179 |
| -const runner = (options) => new Promise((resolve, reject) => { |
180 |
| - newman.run(options, function (err, results) { |
181 |
| - if (err) { |
182 |
| - reject(err) |
183 |
| - return |
184 |
| - } |
185 |
| - resolve(results) |
186 |
| - }) |
187 |
| -}) |
188 |
| - |
189 | 173 | /**
|
190 |
| - * Sleep for the given time |
191 |
| - * @param ms the miliseconds |
192 |
| - * @returns {Promise<unknown>} |
| 174 | + * Clear the test data. |
| 175 | + * @return {Promise<void>} |
193 | 176 | */
|
194 |
| -function sleep (ms) { |
195 |
| - return new Promise(resolve => setTimeout(resolve, ms)) |
| 177 | +async function clearTestData () { |
| 178 | + logger.info('Clear the Postman test data.') |
| 179 | + await helper.postRequest(`${config.API_BASE_URL}/${config.API_VERSION}/resources/internal/jobs/clean`) |
| 180 | + logger.info('Finished clear the Postman test data.') |
196 | 181 | }
|
197 | 182 |
|
198 | 183 | /**
|
199 |
| - * Clean the Nock. |
| 184 | + * Run the postman tests. |
200 | 185 | */
|
201 |
| -function cleanNock () { |
202 |
| - if (config.MOCK_BUS_API_BY_NOCK) { |
203 |
| - nock.cleanAll() |
204 |
| - } |
205 |
| -} |
206 |
| - |
207 |
| -;(async () => { |
208 |
| - const m2mToken = await envHelper.getM2MToken() |
209 |
| - const adminToken = await envHelper.getAdminToken() |
210 |
| - const copilotToken = await envHelper.getCopilotToken() |
211 |
| - const userToken = await envHelper.getUserToken() |
212 |
| - const originalEnvVars = [ |
213 |
| - { key: 'M2M_TOKEN', value: `${m2mToken}` }, |
214 |
| - { key: 'admin_token', value: `${adminToken}` }, |
215 |
| - { key: 'copilot_token', value: `${copilotToken}` }, |
216 |
| - { key: 'user_token', value: `${userToken}` } |
217 |
| - ] |
218 |
| - if (config.MOCK_BUS_API_BY_NOCK) { |
219 |
| - nock(config.BUSAPI_URL) |
220 |
| - .persist() |
221 |
| - .post('/bus/events') |
222 |
| - .reply(204) |
223 |
| - } |
224 |
| - for (const request of requests) { |
225 |
| - options.envVar = [ |
226 |
| - ...originalEnvVars, |
227 |
| - ..._.map(_.keys(request.iterationData || {}), key => ({ key, value: request.iterationData[key] })) |
228 |
| - ] |
229 |
| - delete require.cache[require.resolve('./resource-api.postman_environment.json')] |
230 |
| - options.environment = require('./resource-api.postman_environment.json') |
231 |
| - options.folder = request.folder |
232 |
| - options.iterationData = request.iterationData |
233 |
| - try { |
234 |
| - const results = await runner(options) |
235 |
| - if (_.get(results, 'run.failures.length', 0) > 0) { |
236 |
| - cleanNock() |
237 |
| - process.exit(-1) |
238 |
| - } |
239 |
| - } catch (err) { |
240 |
| - console.log(err) |
241 |
| - } |
242 |
| - await sleep(config.WAIT_TIME) |
243 |
| - } |
244 |
| -})().then(async () => { |
245 |
| - cleanNock() |
246 |
| - console.log('newman test completed!') |
247 |
| - process.exit(0) |
| 186 | +apiTestLib.runTests(requests, require.resolve('./resource-api.postman_collection.json'), |
| 187 | + require.resolve('./resource-api.postman_environment.json')).then(async () => { |
| 188 | + logger.info('newman test completed!') |
| 189 | + await clearTestData() |
248 | 190 | }).catch(async (err) => {
|
249 |
| - cleanNock() |
250 |
| - console.log(err) |
251 |
| - process.exit(1) |
| 191 | + logger.logFullError(err) |
| 192 | + // Only calling the clean up function when it is not validation error. |
| 193 | + if (err.name !== 'ValidationError') { |
| 194 | + await clearTestData() |
| 195 | + } |
252 | 196 | })
|
0 commit comments