Skip to content

Commit b7f0c58

Browse files
committed
fix(authorize): added guard functionality
BREAKING CHANGE: now authorize function requires clientId, clientSecret and grantType
1 parent 6f1c464 commit b7f0c58

11 files changed

+656
-189
lines changed

package-lock.json

+576-155
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"source": "src/index.js",
88
"scripts": {
99
"start": "./node_modules/.bin/nodemon --exec babel-node ./src/index.js",
10-
"test": "./node_modules/.bin/mocha --require dotenv/config --require @babel/register",
11-
"test:watch": "./node_modules/.bin/mocha --require dotenv/config --require @babel/register --watch --watch-extensions js \"test/**/*.js\"",
10+
"test": "./node_modules/.bin/mocha --require dotenv/config --require @babel/register --require chai/register-should",
11+
"test:watch": "./node_modules/.bin/mocha --require dotenv/config --require @babel/register --require chai/register-should --watch --watch-extensions js \"test/**/*.js\"",
1212
"build": "./node_modules/.bin/rollup -c",
1313
"serve": "node ./dist/index.js",
1414
"debug": "babel-node --inspect-brk ./src/index.js",
@@ -30,25 +30,29 @@
3030
"@babel/node": "^7.4.5",
3131
"@babel/preset-env": "^7.4.5",
3232
"@babel/register": "^7.4.4",
33-
"@pika/plugin-build-node": "^0.4.0",
34-
"@pika/plugin-standard-pkg": "^0.4.0",
3533
"builtin-modules": "^3.1.0",
3634
"chai": "^4.2.0",
35+
"cz-conventional-changelog": "^3.1.0",
3736
"mocha": "^6.1.4",
3837
"nock": "^10.0.6",
3938
"nodemon": "^1.19.1",
4039
"prettier": "1.18.2",
4140
"rollup": "^1.16.2",
4241
"rollup-plugin-commonjs": "^10.0.0",
4342
"rollup-plugin-json": "^4.0.0",
44-
"sinon": "^7.3.2",
45-
"semantic-release": "^15.13.18"
43+
"semantic-release": "^15.13.18",
44+
"sinon": "^7.3.2"
4645
},
4746
"repository": {
4847
"type": "git",
4948
"url": "https://github.com/hiddenboox/gateway-payu.git"
5049
},
5150
"publishConfig": {
5251
"access": "public"
52+
},
53+
"config": {
54+
"commitizen": {
55+
"path": "./node_modules/cz-conventional-changelog"
56+
}
5357
}
5458
}

src/authorize.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { post } from './helpers'
1+
import { post } from './helpers/request'
22
import { environment } from './env'
3+
import { required } from './helpers/validation'
34

4-
export default async ({ clientId, clientSecret, grantType }) => {
5+
export default async ({ clientId, clientSecret, grantType } = {}) => {
6+
required('PAYU_CLIENT_ID', clientId)
7+
required('PAYU_CLIENT_SECRET', clientSecret)
8+
required('grantType', grantType)
9+
510
try {
611
const response = await post(
712
`${environment}/pl/standard/user/oauth/authorize`,

src/consts.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@ export const ContentType = {
22
JSON: 'application/json'
33
}
44

5+
export const HEADERS = {
6+
Authorization: 'Authorization',
7+
'Content-Type': 'Content-Type'
8+
}
9+
510
export const HttpVerb = {
611
POST: 'POST',
712
GET: 'GET',
813
}
914

15+
export const EnvironmentName = {
16+
SANDBOX: 'SANDBOX',
17+
PRODUCTION: 'PRODUCTION',
18+
}
19+
1020
export const Environment = {
11-
SANDBOX: 'https://secure.snd.payu.com',
12-
PRODUCTION: 'https://secure.payu.com',
13-
}
21+
[EnvironmentName.SANDBOX]: 'https://secure.snd.payu.com',
22+
[EnvironmentName.PRODUCTION]: 'https://secure.payu.com',
23+
}
24+

src/env.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { Environment } from "./consts"
1+
import { Environment, EnvironmentName } from "./consts"
22

3-
export const environment = Environment[(process.env.PAYU_ENVIRONMENT ? process.env.PAYU_ENVIRONMENT : 'sandbox').toUpperCase()]
3+
export const environment = Environment[process.env.PAYU_ENVIRONMENT ? process.env.PAYU_ENVIRONMENT : EnvironmentName.SANDBOX]

src/helpers.js src/helpers/request.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import querystring from 'querystring'
22
import https from 'https'
33
import { URL } from 'url'
4-
import { HttpVerb } from './consts'
4+
5+
import { HttpVerb } from '../consts'
56

67
export const request = ({ url, json, params, body, ...rest } = { method: 'GET', json: true }) => {
78
const uri = new URL(`${url}${params ? '?' + querystring.stringify(params) : ''}`)
@@ -17,9 +18,9 @@ export const request = ({ url, json, params, body, ...rest } = { method: 'GET',
1718
resolve(json ? JSON.parse(data) : data);
1819
});
1920

20-
}).on("error", (err) => {
21+
}).on('error', (err) => {
2122
console.log(err)
22-
reject("Error: " + err.message);
23+
reject('Error: ' + err.message);
2324
})
2425

2526
if (body) {
@@ -30,5 +31,5 @@ export const request = ({ url, json, params, body, ...rest } = { method: 'GET',
3031
})
3132
}
3233

33-
export const get = (url, params, options = {}) => request({ url, params, ...options, ...{ method: HttpVerb.GET } })
34-
export const post = (url, body, options = {}) => request({ url, body, ...options, ...{ method: HttpVerb.POST } })
34+
export const get = (url, params, options = {}) => request({ ...options, url, params, method: HttpVerb.GET })
35+
export const post = (url, body, options = {}) => request({ ...options, url, body, method: HttpVerb.POST })

src/helpers/validation.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import assert from 'assert'
2+
3+
export const required = (name, v) => assert.ok(v, `${name} should not be empty`)

src/order.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import assert from 'assert'
2-
3-
import { post } from './helpers'
4-
import { ContentType } from './consts'
1+
import { post } from './helpers/request'
2+
import { ContentType, HEADERS } from './consts'
53
import { environment } from './env'
4+
import { required } from './helpers/validation'
65

76
const { PAYU_CLIENT_NOTIFY_SITE_URL, PAYU_CLIENT_ID } = process.env
87

9-
export default async ({ accessToken, payment, cart, buyer, products }) => {
10-
assert.ok(accessToken, 'accessToken should not be empty')
11-
assert.ok(payment, 'payment should not be empty')
12-
assert.ok(cart, 'cart should not be empty')
13-
assert.ok(buyer, 'buyer should not be empty')
14-
assert.ok(products, 'products should not be empty')
8+
export default async ({ accessToken, payment, cart, buyer, products } = {}) => {
9+
required('accessToken', accessToken)
10+
required('payment', payment)
11+
required('cart', cart)
12+
required('buyer', buyer)
13+
required('products', products)
1514

1615
try {
1716
return await post(
@@ -27,8 +26,8 @@ export default async ({ accessToken, payment, cart, buyer, products }) => {
2726
{
2827
json: true,
2928
headers: {
30-
'Authorization': `Bearer ${accessToken}`,
31-
'Content-Type': ContentType.JSON,
29+
[HEADERS.Authorization]: `Bearer ${accessToken}`,
30+
[HEADERS.ContentType]: ContentType.JSON,
3231
},
3332
}
3433
)

test/authorize.test.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import 'chai/register-should'
21
import nock from 'nock'
2+
import assert from 'assert'
3+
import { expect } from 'chai'
34

45
import { mockAuthorize } from './server'
56

@@ -26,4 +27,28 @@ describe('authorize function', () => {
2627
response.should.have.property('expiresIn')
2728
response.should.have.property('grantType')
2829
})
30+
31+
it('should return validation error on PAYU_CLIENT_ID when field is not provided', async () => {
32+
try {
33+
await authorize()
34+
} catch (err) {
35+
should.throw(() => { throw err }, assert.AssertionError, 'PAYU_CLIENT_ID should not be empty')
36+
}
37+
})
38+
39+
it('should return validation error on PAYU_CLIENT_SECRET when field is not provided', async () => {
40+
try {
41+
await authorize({ clientId: 2 })
42+
} catch (err) {
43+
should.throw(() => { throw err }, assert.AssertionError, 'PAYU_CLIENT_SECRET should not be empty')
44+
}
45+
})
46+
47+
it('should return validation error on grantType when field is not provided', async () => {
48+
try {
49+
await authorize({ clientId: 2, clientSecret: 2 })
50+
} catch (err) {
51+
should.throw(() => { throw err }, assert.AssertionError, 'grantType should not be empty')
52+
}
53+
})
2954
})

test/order.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'chai/register-should'
21
import nock from 'nock'
32

43
import { mockOrder } from './server'

test/orderFlow.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import 'chai/register-should'
21
import nock from 'nock'
32

43
import { authorize, order } from '../src'
54
import { mockAuthorize, mockOrder } from './server'
65

7-
const { PAYU_CLIENT_ID, PAYU_CLIENT_SECRET, PAYU_CLIENT_NOTIFY_SITE_URL } = process.env
6+
const { PAYU_CLIENT_ID, PAYU_CLIENT_SECRET } = process.env
87

98
describe('order flow', () => {
109
afterEach(() => {

0 commit comments

Comments
 (0)