Skip to content

Commit 4410565

Browse files
authored
Merge pull request #183 from eisbilir/demo-payment
demo-payment script
2 parents 3dec6c1 + de71a22 commit 4410565

File tree

7 files changed

+398
-5
lines changed

7 files changed

+398
-5
lines changed

app-constants.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ const Scopes = {
3636
READ_TAAS_TEAM: 'read:taas-teams'
3737
}
3838

39+
const ChallengeStatus = {
40+
DRAFT: 'Draft',
41+
ACTIVE: 'Active',
42+
COMPLETED: 'Completed'
43+
}
44+
3945
module.exports = {
4046
UserRoles,
4147
FullManagePermissionRoles,
42-
Scopes
48+
Scopes,
49+
ChallengeStatus
4350
}

config/default.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,11 @@ module.exports = {
124124
// SendGrid email template ID for requesting extension
125125
REQUEST_EXTENSION_SENDGRID_TEMPLATE_ID: process.env.REQUEST_EXTENSION_SENDGRID_TEMPLATE_ID,
126126
// the URL where TaaS App is hosted
127-
TAAS_APP_URL: process.env.TAAS_APP_URL || 'https://platform.topcoder-dev.com/taas/myteams'
127+
TAAS_APP_URL: process.env.TAAS_APP_URL || 'https://platform.topcoder-dev.com/taas/myteams',
128+
// environment variables for Payment Service
129+
ROLE_ID_SUBMITTER: process.env.ROLE_ID_SUBMITTER || '732339e7-8e30-49d7-9198-cccf9451e221',
130+
TYPE_ID_TASK: process.env.TYPE_ID_TASK || 'ecd58c69-238f-43a4-a4bb-d172719b9f31',
131+
DEFAULT_TIMELINE_TEMPLATE_ID: process.env.DEFAULT_TIMELINE_TEMPLATE_ID || '53a307ce-b4b3-4d6f-b9a1-3741a58f77e6',
132+
DEFAULT_TRACK_ID: process.env.DEFAULT_TRACK_ID || '9b6fc876-f4d9-4ccb-9dfd-419247628825'
133+
128134
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"services:logs": "docker-compose -f ./local/docker-compose.yml logs",
2626
"local:init": "npm run local:reset && npm run data:import -- --force",
2727
"local:reset": "npm run delete-index -- --force || true && npm run create-index -- --force && npm run init-db force",
28-
"cov": "nyc --reporter=html --reporter=text mocha test/unit/*.test.js --timeout 30000 --exit"
28+
"cov": "nyc --reporter=html --reporter=text mocha test/unit/*.test.js --timeout 30000 --exit",
29+
"demo-payment": "node scripts/demo-payment"
2930
},
3031
"keywords": [],
3132
"author": "",
@@ -84,4 +85,4 @@
8485
"test/unit/**"
8586
]
8687
}
87-
}
88+
}

scripts/demo-payment/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### DEMO PAYMENT SCRIPT
2+
3+
This demo script tests the functionality of PaymentService.
4+
5+
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.
6+
7+
Before start set the following environment variables:
8+
AUTH0_URL=
9+
AUTH0_AUDIENCE=
10+
AUTH0_AUDIENCE_UBAHN=
11+
AUTH0_CLIENT_ID=
12+
AUTH0_CLIENT_SECRET=
13+
14+
To run the script use the following commands:
15+
16+
```
17+
npm install
18+
npm run lint
19+
npm run demo-payment
20+
```
21+
22+
Read the logger to see results.

scripts/demo-payment/index.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
require('../../src/bootstrap')
2+
const logger = require('../../src/common/logger')
3+
const paymentService = require('../../src/services/PaymentService')
4+
5+
const options = [
6+
{
7+
name: 'Test joi validation for projectId-1',
8+
content: {
9+
userHandle: 'pshah_manager',
10+
amount: 3,
11+
billingAccountId: 80000069,
12+
name: 'test payment for pshah_manager',
13+
description: '## test payment'
14+
}
15+
},
16+
{
17+
name: 'Test joi validation for projectId-2',
18+
content: {
19+
projectId: 'project',
20+
userHandle: 'pshah_manager',
21+
amount: 3,
22+
billingAccountId: 80000069,
23+
name: 'test payment for pshah_manager',
24+
description: '## test payment'
25+
}
26+
},
27+
{
28+
name: 'Test joi validation for userHandle',
29+
content: {
30+
projectId: 17234,
31+
amount: 3,
32+
billingAccountId: 80000069,
33+
name: 'test payment for pshah_manager',
34+
description: '## test payment'
35+
}
36+
},
37+
{
38+
name: 'Test joi validation for amount-1',
39+
content: {
40+
projectId: 17234,
41+
userHandle: 'pshah_manager',
42+
billingAccountId: 80000069,
43+
name: 'test payment for pshah_manager',
44+
description: '## test payment'
45+
}
46+
},
47+
{
48+
name: 'Test joi validation for amount-2',
49+
content: {
50+
projectId: 17234,
51+
userHandle: 'pshah_manager',
52+
amount: -10,
53+
billingAccountId: 80000069,
54+
name: 'test payment for pshah_manager',
55+
description: '## test payment'
56+
}
57+
},
58+
{
59+
name: 'Successful payment creation',
60+
content: {
61+
projectId: 17234,
62+
userHandle: 'pshah_manager',
63+
amount: 3,
64+
billingAccountId: 80000069,
65+
name: 'test payment for pshah_manager',
66+
description: '## test payment'
67+
}
68+
},
69+
{
70+
name: 'Successful payment creation without name and description',
71+
content: {
72+
projectId: 17234,
73+
userHandle: 'pshah_customer',
74+
amount: 2,
75+
billingAccountId: 80000069
76+
}
77+
},
78+
{
79+
name: 'Failing payment creation with no active billing account',
80+
content: {
81+
projectId: 16839,
82+
userHandle: 'pshah_customer',
83+
amount: 2,
84+
billingAccountId: 80000069,
85+
name: 'test payment for pshah_customer',
86+
description: '## test payment'
87+
}
88+
},
89+
{
90+
name: 'Failing payment creation with non existing user',
91+
content: {
92+
projectId: 17234,
93+
userHandle: 'eisbilir',
94+
amount: 2,
95+
billingAccountId: 80000069
96+
}
97+
}
98+
]
99+
100+
const test = async () => {
101+
for (const option of options) {
102+
logger.info({ component: 'demo-payment', context: 'test', message: `Starting to create payment for: ${option.name}` })
103+
await paymentService.createPayment(option.content)
104+
.then(data => {
105+
logger.info({ component: 'demo-payment', context: 'test', message: `Payment successfuly created for: ${option.name}` })
106+
})
107+
// eslint-disable-next-line handle-callback-err
108+
.catch(err => {
109+
logger.error({ component: 'demo-payment', context: 'test', message: `Payment can't be created for: ${option.name}` })
110+
})
111+
}
112+
}
113+
// wait for bootstrap to complete it's job.
114+
setTimeout(test, 2000)

src/common/helper.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,82 @@ async function deleteProjectMember (currentUser, projectId, projectMemberId) {
11181118
}
11191119
}
11201120

1121+
/**
1122+
* Create a new challenge
1123+
*
1124+
* @param {Object} data challenge data
1125+
* @param {String} token m2m token
1126+
* @returns {Object} the challenge created
1127+
*/
1128+
async function createChallenge (data, token) {
1129+
if (!token) {
1130+
token = await getM2MToken()
1131+
}
1132+
const url = `${config.TC_API}/challenges`
1133+
localLogger.debug({ context: 'createChallenge', message: `EndPoint: POST ${url}` })
1134+
localLogger.debug({ context: 'createChallenge', message: `Request Body: ${JSON.stringify(data)}` })
1135+
const { body: challenge, status: httpStatus } = await request
1136+
.post(url)
1137+
.set('Authorization', `Bearer ${token}`)
1138+
.set('Content-Type', 'application/json')
1139+
.set('Accept', 'application/json')
1140+
.send(data)
1141+
localLogger.debug({ context: 'createChallenge', message: `Status Code: ${httpStatus}` })
1142+
localLogger.debug({ context: 'createChallenge', message: `Response Body: ${JSON.stringify(challenge)}` })
1143+
return challenge
1144+
}
1145+
1146+
/**
1147+
* Update a challenge
1148+
*
1149+
* @param {String} challengeId id of the challenge
1150+
* @param {Object} data challenge data
1151+
* @param {String} token m2m token
1152+
* @returns {Object} the challenge updated
1153+
*/
1154+
async function updateChallenge (challengeId, data, token) {
1155+
if (!token) {
1156+
token = await getM2MToken()
1157+
}
1158+
const url = `${config.TC_API}/challenges/${challengeId}`
1159+
localLogger.debug({ context: 'updateChallenge', message: `EndPoint: PATCH ${url}` })
1160+
localLogger.debug({ context: 'updateChallenge', message: `Request Body: ${JSON.stringify(data)}` })
1161+
const { body: challenge, status: httpStatus } = await request
1162+
.patch(url)
1163+
.set('Authorization', `Bearer ${token}`)
1164+
.set('Content-Type', 'application/json')
1165+
.set('Accept', 'application/json')
1166+
.send(data)
1167+
localLogger.debug({ context: 'updateChallenge', message: `Status Code: ${httpStatus}` })
1168+
localLogger.debug({ context: 'updateChallenge', message: `Response Body: ${JSON.stringify(challenge)}` })
1169+
return challenge
1170+
}
1171+
1172+
/**
1173+
* Create a challenge resource
1174+
*
1175+
* @param {Object} data resource
1176+
* @param {String} token m2m token
1177+
* @returns {Object} the resource created
1178+
*/
1179+
async function createChallengeResource (data, token) {
1180+
if (!token) {
1181+
token = await getM2MToken()
1182+
}
1183+
const url = `${config.TC_API}/resources`
1184+
localLogger.debug({ context: 'createChallengeResource', message: `EndPoint: POST ${url}` })
1185+
localLogger.debug({ context: 'createChallengeResource', message: `Request Body: ${JSON.stringify(data)}` })
1186+
const { body: resource, status: httpStatus } = await request
1187+
.post(url)
1188+
.set('Authorization', `Bearer ${token}`)
1189+
.set('Content-Type', 'application/json')
1190+
.set('Accept', 'application/json')
1191+
.send(data)
1192+
localLogger.debug({ context: 'createChallengeResource', message: `Status Code: ${httpStatus}` })
1193+
localLogger.debug({ context: 'createChallengeResource', message: `Response Body: ${JSON.stringify(resource)}` })
1194+
return resource
1195+
}
1196+
11211197
module.exports = {
11221198
getParamFromCliArgs,
11231199
promptUser,
@@ -1159,5 +1235,8 @@ module.exports = {
11591235
createProjectMember,
11601236
listProjectMembers,
11611237
listProjectMemberInvites,
1162-
deleteProjectMember
1238+
deleteProjectMember,
1239+
createChallenge,
1240+
updateChallenge,
1241+
createChallengeResource
11631242
}

0 commit comments

Comments
 (0)