Skip to content

demo-payment script #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion app-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ const Scopes = {
READ_TAAS_TEAM: 'read:taas-teams'
}

const ChallengeStatus = {
DRAFT: 'Draft',
ACTIVE: 'Active',
COMPLETED: 'Completed'
}

module.exports = {
UserRoles,
FullManagePermissionRoles,
Scopes
Scopes,
ChallengeStatus
}
8 changes: 7 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,11 @@ module.exports = {
// SendGrid email template ID for requesting extension
REQUEST_EXTENSION_SENDGRID_TEMPLATE_ID: process.env.REQUEST_EXTENSION_SENDGRID_TEMPLATE_ID,
// the URL where TaaS App is hosted
TAAS_APP_URL: process.env.TAAS_APP_URL || 'https://platform.topcoder-dev.com/taas/myteams'
TAAS_APP_URL: process.env.TAAS_APP_URL || 'https://platform.topcoder-dev.com/taas/myteams',
// environment variables for Payment Service
ROLE_ID_SUBMITTER: process.env.ROLE_ID_SUBMITTER || '732339e7-8e30-49d7-9198-cccf9451e221',
TYPE_ID_TASK: process.env.TYPE_ID_TASK || 'ecd58c69-238f-43a4-a4bb-d172719b9f31',
DEFAULT_TIMELINE_TEMPLATE_ID: process.env.DEFAULT_TIMELINE_TEMPLATE_ID || '53a307ce-b4b3-4d6f-b9a1-3741a58f77e6',
DEFAULT_TRACK_ID: process.env.DEFAULT_TRACK_ID || '9b6fc876-f4d9-4ccb-9dfd-419247628825'

}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"services:logs": "docker-compose -f ./local/docker-compose.yml logs",
"local:init": "npm run local:reset && npm run data:import -- --force",
"local:reset": "npm run delete-index -- --force || true && npm run create-index -- --force && npm run init-db force",
"cov": "nyc --reporter=html --reporter=text mocha test/unit/*.test.js --timeout 30000 --exit"
"cov": "nyc --reporter=html --reporter=text mocha test/unit/*.test.js --timeout 30000 --exit",
"demo-payment": "node scripts/demo-payment"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -84,4 +85,4 @@
"test/unit/**"
]
}
}
}
22 changes: 22 additions & 0 deletions scripts/demo-payment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### DEMO PAYMENT SCRIPT

This demo script tests the functionality of PaymentService.

Parameters for creating payments are hardcoded in the script. There are severel groups of parameters, each of them tests a certain functionality of the demo service. You can always insert new group of parameters to run in the script.

Before start set the following environment variables:
AUTH0_URL=
AUTH0_AUDIENCE=
AUTH0_AUDIENCE_UBAHN=
AUTH0_CLIENT_ID=
AUTH0_CLIENT_SECRET=

To run the script use the following commands:

```
npm install
npm run lint
npm run demo-payment
```

Read the logger to see results.
114 changes: 114 additions & 0 deletions scripts/demo-payment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
require('../../src/bootstrap')
const logger = require('../../src/common/logger')
const paymentService = require('../../src/services/PaymentService')

const options = [
{
name: 'Test joi validation for projectId-1',
content: {
userHandle: 'pshah_manager',
amount: 3,
billingAccountId: 80000069,
name: 'test payment for pshah_manager',
description: '## test payment'
}
},
{
name: 'Test joi validation for projectId-2',
content: {
projectId: 'project',
userHandle: 'pshah_manager',
amount: 3,
billingAccountId: 80000069,
name: 'test payment for pshah_manager',
description: '## test payment'
}
},
{
name: 'Test joi validation for userHandle',
content: {
projectId: 17234,
amount: 3,
billingAccountId: 80000069,
name: 'test payment for pshah_manager',
description: '## test payment'
}
},
{
name: 'Test joi validation for amount-1',
content: {
projectId: 17234,
userHandle: 'pshah_manager',
billingAccountId: 80000069,
name: 'test payment for pshah_manager',
description: '## test payment'
}
},
{
name: 'Test joi validation for amount-2',
content: {
projectId: 17234,
userHandle: 'pshah_manager',
amount: -10,
billingAccountId: 80000069,
name: 'test payment for pshah_manager',
description: '## test payment'
}
},
{
name: 'Successful payment creation',
content: {
projectId: 17234,
userHandle: 'pshah_manager',
amount: 3,
billingAccountId: 80000069,
name: 'test payment for pshah_manager',
description: '## test payment'
}
},
{
name: 'Successful payment creation without name and description',
content: {
projectId: 17234,
userHandle: 'pshah_customer',
amount: 2,
billingAccountId: 80000069
}
},
{
name: 'Failing payment creation with no active billing account',
content: {
projectId: 16839,
userHandle: 'pshah_customer',
amount: 2,
billingAccountId: 80000069,
name: 'test payment for pshah_customer',
description: '## test payment'
}
},
{
name: 'Failing payment creation with non existing user',
content: {
projectId: 17234,
userHandle: 'eisbilir',
amount: 2,
billingAccountId: 80000069
}
}
]

const test = async () => {
for (const option of options) {
logger.info({ component: 'demo-payment', context: 'test', message: `Starting to create payment for: ${option.name}` })
await paymentService.createPayment(option.content)
.then(data => {
logger.info({ component: 'demo-payment', context: 'test', message: `Payment successfuly created for: ${option.name}` })
})
// eslint-disable-next-line handle-callback-err
.catch(err => {
logger.error({ component: 'demo-payment', context: 'test', message: `Payment can't be created for: ${option.name}` })
})
}
}
// wait for bootstrap to complete it's job.
setTimeout(test, 2000)
81 changes: 80 additions & 1 deletion src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,82 @@ async function deleteProjectMember (currentUser, projectId, projectMemberId) {
}
}

/**
* Create a new challenge
*
* @param {Object} data challenge data
* @param {String} token m2m token
* @returns {Object} the challenge created
*/
async function createChallenge (data, token) {
if (!token) {
token = await getM2MToken()
}
const url = `${config.TC_API}/challenges`
localLogger.debug({ context: 'createChallenge', message: `EndPoint: POST ${url}` })
localLogger.debug({ context: 'createChallenge', message: `Request Body: ${JSON.stringify(data)}` })
const { body: challenge, status: httpStatus } = await request
.post(url)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(data)
localLogger.debug({ context: 'createChallenge', message: `Status Code: ${httpStatus}` })
localLogger.debug({ context: 'createChallenge', message: `Response Body: ${JSON.stringify(challenge)}` })
return challenge
}

/**
* Update a challenge
*
* @param {String} challengeId id of the challenge
* @param {Object} data challenge data
* @param {String} token m2m token
* @returns {Object} the challenge updated
*/
async function updateChallenge (challengeId, data, token) {
if (!token) {
token = await getM2MToken()
}
const url = `${config.TC_API}/challenges/${challengeId}`
localLogger.debug({ context: 'updateChallenge', message: `EndPoint: PATCH ${url}` })
localLogger.debug({ context: 'updateChallenge', message: `Request Body: ${JSON.stringify(data)}` })
const { body: challenge, status: httpStatus } = await request
.patch(url)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(data)
localLogger.debug({ context: 'updateChallenge', message: `Status Code: ${httpStatus}` })
localLogger.debug({ context: 'updateChallenge', message: `Response Body: ${JSON.stringify(challenge)}` })
return challenge
}

/**
* Create a challenge resource
*
* @param {Object} data resource
* @param {String} token m2m token
* @returns {Object} the resource created
*/
async function createChallengeResource (data, token) {
if (!token) {
token = await getM2MToken()
}
const url = `${config.TC_API}/resources`
localLogger.debug({ context: 'createChallengeResource', message: `EndPoint: POST ${url}` })
localLogger.debug({ context: 'createChallengeResource', message: `Request Body: ${JSON.stringify(data)}` })
const { body: resource, status: httpStatus } = await request
.post(url)
.set('Authorization', `Bearer ${token}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json')
.send(data)
localLogger.debug({ context: 'createChallengeResource', message: `Status Code: ${httpStatus}` })
localLogger.debug({ context: 'createChallengeResource', message: `Response Body: ${JSON.stringify(resource)}` })
return resource
}

module.exports = {
getParamFromCliArgs,
promptUser,
Expand Down Expand Up @@ -1159,5 +1235,8 @@ module.exports = {
createProjectMember,
listProjectMembers,
listProjectMemberInvites,
deleteProjectMember
deleteProjectMember,
createChallenge,
updateChallenge,
createChallengeResource
}
Loading