Skip to content

Commit 43c9169

Browse files
committed
(feat) auth permission model
1 parent dd6234c commit 43c9169

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

lib/auth_permission.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict'
2+
3+
const boolValidator = require('./validators/bool')
4+
const stringValidator = require('./validators/string')
5+
const Model = require('./model')
6+
const fields = {
7+
key: 0,
8+
read: 1,
9+
write: 2
10+
}
11+
const boolFields = ['read', 'write']
12+
13+
/**
14+
* Auth permission model
15+
*/
16+
class AuthPermission extends Model {
17+
/**
18+
* @param {object|Array} data - auth permission data
19+
* @param {string} data.key - operation key
20+
* @param {boolean} data.read - read permission
21+
* @param {boolean} data.write - write permission
22+
*/
23+
constructor (data = {}) {
24+
super({ data, fields, boolFields })
25+
}
26+
27+
/**
28+
* @param {object[]|object|Array[]|Array} data - data to convert to POJO
29+
* @returns {object} pojo
30+
*/
31+
static unserialize (data) {
32+
return super.unserialize({ data, fields, boolFields })
33+
}
34+
35+
/**
36+
* Validates a given auth permission setting instance
37+
*
38+
* @param {object[]|object|AuthPermission[]|AuthPermission|Array} data - instance to validate
39+
* @returns {string} error - null if instance is valid
40+
*/
41+
static validate (data) {
42+
return super.validate({
43+
data,
44+
fields,
45+
validators: {
46+
key: stringValidator,
47+
read: boolValidator,
48+
write: boolValidator
49+
}
50+
})
51+
}
52+
}
53+
54+
module.exports = AuthPermission

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = {
3636
SymbolDetails: require('./symbol_details'),
3737
TransactionFee: require('./transaction_fee'),
3838
AccountSummary: require('./account_summary'),
39+
AuthPermission: require('./auth_permission'),
3940

4041
isCollection: require('./util/is_collection')
4142
}

test/lib/models/auth_permission.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const assert = require('assert')
5+
const { AuthPermission } = require('../../../lib')
6+
const testModelValidation = require('../../helpers/test_model_validation')
7+
8+
const DATA = ['orders', 1, 0]
9+
10+
describe('Auth Permission model', () => {
11+
testModelValidation({
12+
model: AuthPermission,
13+
validData: {
14+
key: ['account', 'orders', 'funding', 'settings', 'wallets', 'withdraw'],
15+
read: [true, false],
16+
write: [true, false]
17+
}
18+
})
19+
20+
it('initializes correctly', () => {
21+
const perm = new AuthPermission(DATA)
22+
assert.strictEqual(perm.key, 'orders')
23+
assert.strictEqual(perm.read, true)
24+
assert.strictEqual(perm.write, false)
25+
})
26+
27+
it('serializes correctly', () => {
28+
const details = new AuthPermission(DATA)
29+
const arr = details.serialize()
30+
31+
assert.deepStrictEqual(arr, DATA)
32+
})
33+
34+
it('unserializes correctly', () => {
35+
const perm = AuthPermission.unserialize(DATA)
36+
assert.strictEqual(perm.key, 'orders')
37+
assert.strictEqual(perm.read, true)
38+
assert.strictEqual(perm.write, false)
39+
})
40+
})

0 commit comments

Comments
 (0)