Skip to content

Commit 5a4a7cb

Browse files
test automated testing with the library
1 parent 375deea commit 5a4a7cb

File tree

6 files changed

+26
-194
lines changed

6 files changed

+26
-194
lines changed

ReadMe.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ The following parameters can be set in config files or in env variables:
7171
- RESOURCE_DELETE_TOPIC: the resource delete Kafka topic, default value is 'challenge.action.resource.delete',
7272
- RESOURCE_ROLE_CREATE_TOPIC: the resource role create topic, default value is 'challenge.action.resource.role.create',
7373
- RESOURCE_ROLE_UPDATE_TOPIC: the resource role update topic, default value is 'challenge.action.resource.role.update'
74+
- AUTOMATED_TESTING_NAME_PREFIX: the role name prefix for every `ResourceRole` record
7475

7576
Configuration for testing is at `config/test.js`, only add such new configurations different from `config/default.js`
7677
- WAIT_TIME: wait time used in test, default is 6000 or 6 seconds
@@ -84,8 +85,8 @@ Configuration for testing is at `config/test.js`, only add such new configuratio
8485
- COPILOT_CREDENTIALS_PASSWORD: The user's password with copilot role
8586
- USER_CREDENTIALS_USERNAME: The user's username with user role
8687
- USER_CREDENTIALS_PASSWORD: The user's password with user role
87-
- AUTOMATED_TESTING_NAME_PREFIX: the role name prefix for every `ResourceRole` record
88-
- MOCK_BUS_API_BY_NOCK: indicates whether Nock is used for mocking Bus API.
88+
- AUTOMATED_TESTING_REPORTERS_FORMAT: indicates reporters format. It is an array of the formats. e.g. `['html']` produces html format. `['cli', 'json', 'junit', 'html']` is the full format.
89+
*For the details of the supported format, please refer to https://www.npmjs.com/package/newman#reporters*.
8990

9091
## Available commands
9192
- Install dependencies `npm install`

config/test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ module.exports = {
1717
COPILOT_CREDENTIALS_PASSWORD: process.env.COPILOT_CREDENTIALS_PASSWORD || '',
1818
USER_CREDENTIALS_USERNAME: process.env.USER_CREDENTIALS_USERNAME || '',
1919
USER_CREDENTIALS_PASSWORD: process.env.USER_CREDENTIALS_PASSWORD || '',
20-
AUTOMATED_TESTING_NAME_PREFIX: process.env.AUTOMATED_TESTING_NAME_PREFIX || 'POSTMANE2E-',
21-
MOCK_BUS_API_BY_NOCK: process.env.MOCK_BUS_API_BY_NOCK ? process.env.MOCK_BUS_API_BY_NOCK === 'true' : true
20+
AUTOMATED_TESTING_REPORTERS_FORMAT: process.env.AUTOMATED_TESTING_REPORTERS_FORMAT || ['cli', 'html']
2221
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
"should": "^13.2.3",
2727
"mocha": "^5.2.0",
2828
"mocha-prepare": "^0.1.0",
29-
"newman": "^5.2.2",
3029
"nyc": "^14.0.0",
3130
"standard": "^12.0.1",
32-
"nock": "^10.0.6"
31+
"nock": "^10.0.6",
32+
"tc-api-testing-lib": "topcoder-platform/api-automated-testing.git"
3333
},
3434
"dependencies": {
3535
"aws-sdk": "^2.466.0",

test/postman/ClearPostmanData.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ info: Done!
3737
1. Setup the `AUTOMATED_TESTING_NAME_PREFIX` from the test environment. This prefix should be a name that will never be used
3838
set as part of the role name. e.g. 'POSTMANE2E-'. In this case, the created `ResourceRole` will have a name like 'POSTMANE2E-submitter'.
3939

40-
2. Choose either one solution for mocking the Bus API. We can not ignore this, becuase in production environment, it is
41-
not allowed to send the Kafka messge to the Bus API.
42-
a. Set `MOCK_BUS_API_BY_NOCK` to `true` from the test environment. In this way, Nock will return the response if any events
43-
is posted to the Bus API.
44-
b. You can use use Postman's mock server. You can refer to https://drive.google.com/file/d/1GXMzyqpzwix-LDBwieiRFfpJlJxrTIgI/view?usp=sharing
40+
2. You can use use Postman's mock server for the bus api. You can refer to https://drive.google.com/file/d/1GXMzyqpzwix-LDBwieiRFfpJlJxrTIgI/view?usp=sharing
4541
for details. You need to update the environment variable `BUSAPI_URL` to your Postman mock server.
4642

4743
3. Steps of clearing the test data from Postman tests.

test/postman/envHelper.js

-108
This file was deleted.

test/postman/newman.js

+19-75
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const newman = require('newman')
2-
const _ = require('lodash')
3-
const envHelper = require('./envHelper')
4-
const nock = require('nock')
51
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')
65

76
const requests = [
87
{
@@ -171,82 +170,27 @@ const requests = [
171170
}
172171
]
173172

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-
189173
/**
190-
* Sleep for the given time
191-
* @param ms the miliseconds
192-
* @returns {Promise<unknown>}
174+
* Clear the test data.
175+
* @return {Promise<void>}
193176
*/
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.')
196181
}
197182

198183
/**
199-
* Clean the Nock.
184+
* Run the postman tests.
200185
*/
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()
248190
}).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+
}
252196
})

0 commit comments

Comments
 (0)