2020-03-16 18:13:22 +01:00
|
|
|
import { strict as assert } from 'assert'
|
|
|
|
|
|
|
|
import { noop } from './mocks'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grants the given permissions to the given origin, using the given permissions
|
|
|
|
* controller.
|
|
|
|
*
|
|
|
|
* Just a wrapper for an rpc-cap middleware function.
|
|
|
|
*
|
|
|
|
* @param {PermissionsController} permController - The permissions controller.
|
|
|
|
* @param {string} origin - The origin to grant permissions to.
|
|
|
|
* @param {Object} permissions - The permissions to grant.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function grantPermissions(permController, origin, permissions) {
|
|
|
|
permController.permissions.grantNewPermissions(origin, permissions, {}, noop)
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-27 23:35:09 +02:00
|
|
|
* Returns a wrapper for the given permissions controller's requestUserApproval
|
|
|
|
* function, so we don't have to worry about its internals.
|
2020-03-16 18:13:22 +01:00
|
|
|
*
|
2020-07-27 23:35:09 +02:00
|
|
|
* @param {PermissionsController} permController - The permissions controller.
|
|
|
|
* @return {Function} A convenient wrapper for the requestUserApproval function.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getRequestUserApprovalHelper(permController) {
|
2020-07-27 23:35:09 +02:00
|
|
|
/**
|
|
|
|
* Returns a request object that can be passed to requestUserApproval.
|
|
|
|
*
|
|
|
|
* @param {string} id - The internal permissions request ID (not the RPC request ID).
|
|
|
|
* @param {string} [origin] - The origin of the request, if necessary.
|
|
|
|
* @returns {Object} The corresponding request object.
|
|
|
|
*/
|
|
|
|
return (id, origin = 'defaultOrigin') => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return permController.permissions.requestUserApproval({
|
|
|
|
metadata: { id, origin },
|
|
|
|
})
|
2020-07-27 23:35:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a Promise that resolves once a pending user approval has been set.
|
|
|
|
* Calls the underlying requestUserApproval function as normal, and restores it
|
|
|
|
* once the Promise is resolved.
|
|
|
|
*
|
|
|
|
* This function must be called on the permissions controller for each request.
|
2020-03-16 18:13:22 +01:00
|
|
|
*
|
|
|
|
* @param {PermissionsController} - A permissions controller.
|
|
|
|
* @returns {Promise<void>} A Promise that resolves once a pending approval
|
|
|
|
* has been set.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getUserApprovalPromise(permController) {
|
2020-07-27 23:35:09 +02:00
|
|
|
const originalFunction = permController.permissions.requestUserApproval
|
|
|
|
return new Promise((resolveHelperPromise) => {
|
|
|
|
permController.permissions.requestUserApproval = (req) => {
|
|
|
|
const userApprovalPromise = originalFunction(req)
|
|
|
|
permController.permissions.requestUserApproval = originalFunction
|
|
|
|
resolveHelperPromise()
|
|
|
|
return userApprovalPromise
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates an activity log entry with respect to a request, response, and
|
|
|
|
* relevant metadata.
|
|
|
|
*
|
|
|
|
* @param {Object} entry - The activity log entry to validate.
|
|
|
|
* @param {Object} req - The request that generated the entry.
|
|
|
|
* @param {Object} [res] - The response for the request, if any.
|
|
|
|
* @param {'restricted'|'internal'} methodType - The method log controller method type of the request.
|
|
|
|
* @param {boolean} success - Whether the request succeeded or not.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function validateActivityEntry(entry, req, res, methodType, success) {
|
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
_validateActivityEntry(entry, req, res, methodType, success)
|
|
|
|
}, 'should have expected activity entry')
|
2020-03-16 18:13:22 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function _validateActivityEntry(entry, req, res, methodType, success) {
|
2020-03-16 18:13:22 +01:00
|
|
|
assert.ok(entry, 'entry should exist')
|
|
|
|
|
|
|
|
assert.equal(entry.id, req.id)
|
|
|
|
assert.equal(entry.method, req.method)
|
|
|
|
assert.equal(entry.origin, req.origin)
|
|
|
|
assert.equal(entry.methodType, methodType)
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.deepEqual(entry.request, req, 'entry.request should equal the request')
|
2020-03-16 18:13:22 +01:00
|
|
|
|
|
|
|
if (res) {
|
|
|
|
assert.ok(
|
2020-11-03 00:41:28 +01:00
|
|
|
Number.isInteger(entry.requestTime) &&
|
|
|
|
Number.isInteger(entry.responseTime),
|
2020-07-14 17:20:41 +02:00
|
|
|
'request and response times should be numbers',
|
2020-03-16 18:13:22 +01:00
|
|
|
)
|
|
|
|
assert.ok(
|
2020-11-03 00:41:28 +01:00
|
|
|
entry.requestTime <= entry.responseTime,
|
2020-07-14 17:20:41 +02:00
|
|
|
'request time should be less than response time',
|
2020-03-16 18:13:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert.equal(entry.success, success)
|
|
|
|
assert.deepEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
entry.response,
|
|
|
|
res,
|
2020-07-14 17:20:41 +02:00
|
|
|
'entry.response should equal the response',
|
2020-03-16 18:13:22 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
assert.ok(
|
|
|
|
Number.isInteger(entry.requestTime) && entry.requestTime > 0,
|
2020-07-14 17:20:41 +02:00
|
|
|
'entry should have non-zero request time',
|
2020-03-16 18:13:22 +01:00
|
|
|
)
|
|
|
|
assert.ok(
|
2020-11-03 00:41:28 +01:00
|
|
|
entry.success === null &&
|
2020-03-16 18:13:22 +01:00
|
|
|
entry.responseTime === null &&
|
2020-11-03 00:41:28 +01:00
|
|
|
entry.response === null,
|
2020-07-14 17:20:41 +02:00
|
|
|
'entry response values should be null',
|
2020-03-16 18:13:22 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|