You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
951 lines
25 KiB
951 lines
25 KiB
4 years ago
|
import { strict as assert } from 'assert';
|
||
|
import sinon from 'sinon';
|
||
5 years ago
|
|
||
|
import {
|
||
|
constants,
|
||
|
getters,
|
||
|
getPermControllerOpts,
|
||
|
getPermissionsMiddleware,
|
||
4 years ago
|
} from '../../../../test/mocks/permission-controller';
|
||
|
import {
|
||
|
getUserApprovalPromise,
|
||
|
grantPermissions,
|
||
|
} from '../../../../test/helpers/permission-controller-helpers';
|
||
|
import { METADATA_STORE_KEY } from './enums';
|
||
|
|
||
|
import { PermissionsController } from '.';
|
||
5 years ago
|
|
||
4 years ago
|
const { CAVEATS, ERRORS, PERMS, RPC_REQUESTS } = getters;
|
||
5 years ago
|
|
||
4 years ago
|
const { ACCOUNTS, DOMAINS, PERM_NAMES } = constants;
|
||
5 years ago
|
|
||
4 years ago
|
const initPermController = () => {
|
||
|
return new PermissionsController({
|
||
|
...getPermControllerOpts(),
|
||
4 years ago
|
});
|
||
|
};
|
||
4 years ago
|
|
||
|
const createApprovalSpies = (permController) => {
|
||
4 years ago
|
sinon.spy(permController.approvals, '_add');
|
||
|
};
|
||
4 years ago
|
|
||
|
const getNextApprovalId = (permController) => {
|
||
4 years ago
|
return permController.approvals._approvals.keys().next().value;
|
||
|
};
|
||
4 years ago
|
|
||
5 years ago
|
const validatePermission = (perm, name, origin, caveats) => {
|
||
4 years ago
|
assert.equal(
|
||
|
name,
|
||
|
perm.parentCapability,
|
||
|
'should have expected permission name',
|
||
4 years ago
|
);
|
||
|
assert.equal(origin, perm.invoker, 'should have expected permission origin');
|
||
5 years ago
|
if (caveats) {
|
||
4 years ago
|
assert.deepEqual(
|
||
|
caveats,
|
||
|
perm.caveats,
|
||
|
'should have expected permission caveats',
|
||
4 years ago
|
);
|
||
5 years ago
|
} else {
|
||
4 years ago
|
assert.ok(!perm.caveats, 'should not have any caveats');
|
||
5 years ago
|
}
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
describe('permissions middleware', function () {
|
||
|
describe('wallet_requestPermissions', function () {
|
||
4 years ago
|
let permController;
|
||
5 years ago
|
|
||
|
beforeEach(function () {
|
||
4 years ago
|
permController = initPermController();
|
||
|
permController.notifyAccountsChanged = sinon.fake();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('grants permissions on user approval', async function () {
|
||
4 years ago
|
createApprovalSpies(permController);
|
||
4 years ago
|
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
const userApprovalPromise = getUserApprovalPromise(permController);
|
||
4 years ago
|
|
||
5 years ago
|
const pendingApproval = assert.doesNotReject(
|
||
|
aMiddleware(req, res),
|
||
4 years ago
|
'should not reject permissions request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
4 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
permController.approvals._add.calledOnce,
|
||
|
'should have added single approval request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const id = getNextApprovalId(permController);
|
||
4 years ago
|
const approvedReq = PERMS.approvedRequest(
|
||
|
id,
|
||
|
PERMS.requests.eth_accounts(),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await permController.approvePermissionsRequest(
|
||
|
approvedReq,
|
||
|
ACCOUNTS.a.permitted,
|
||
4 years ago
|
);
|
||
|
await pendingApproval;
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
res.result && !res.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.equal(
|
||
4 years ago
|
res.result.length,
|
||
|
1,
|
||
4 years ago
|
'origin should have single approved permission',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validatePermission(
|
||
|
res.result[0],
|
||
|
PERM_NAMES.eth_accounts,
|
||
5 years ago
|
DOMAINS.a.origin,
|
||
4 years ago
|
CAVEATS.eth_accounts(ACCOUNTS.a.permitted),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const aAccounts = await permController.getAccounts(DOMAINS.a.origin);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
aAccounts,
|
||
|
[ACCOUNTS.a.primary],
|
||
4 years ago
|
'origin should have correct accounts',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
permController.notifyAccountsChanged.calledOnceWith(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
aAccounts,
|
||
5 years ago
|
),
|
||
4 years ago
|
'expected notification call should have been made',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('handles serial approved requests that overwrite existing permissions', async function () {
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// create first request
|
||
|
|
||
|
const req1 = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
|
const res1 = {};
|
||
5 years ago
|
|
||
|
// send, approve, and validate first request
|
||
5 years ago
|
// note use of ACCOUNTS.a.permitted
|
||
5 years ago
|
|
||
4 years ago
|
let userApprovalPromise = getUserApprovalPromise(permController);
|
||
4 years ago
|
|
||
5 years ago
|
const pendingApproval1 = assert.doesNotReject(
|
||
|
aMiddleware(req1, res1),
|
||
4 years ago
|
'should not reject permissions request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
4 years ago
|
|
||
4 years ago
|
const id1 = getNextApprovalId(permController);
|
||
4 years ago
|
const approvedReq1 = PERMS.approvedRequest(
|
||
|
id1,
|
||
|
PERMS.requests.eth_accounts(),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await permController.approvePermissionsRequest(
|
||
|
approvedReq1,
|
||
|
ACCOUNTS.a.permitted,
|
||
4 years ago
|
);
|
||
|
await pendingApproval1;
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
res1.result && !res1.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.equal(
|
||
4 years ago
|
res1.result.length,
|
||
|
1,
|
||
4 years ago
|
'origin should have single approved permission',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validatePermission(
|
||
|
res1.result[0],
|
||
|
PERM_NAMES.eth_accounts,
|
||
5 years ago
|
DOMAINS.a.origin,
|
||
4 years ago
|
CAVEATS.eth_accounts(ACCOUNTS.a.permitted),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const accounts1 = await permController.getAccounts(DOMAINS.a.origin);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
accounts1,
|
||
|
[ACCOUNTS.a.primary],
|
||
4 years ago
|
'origin should have correct accounts',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
5 years ago
|
assert.ok(
|
||
|
permController.notifyAccountsChanged.calledOnceWith(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
accounts1,
|
||
5 years ago
|
),
|
||
4 years ago
|
'expected notification call should have been made',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
5 years ago
|
// create second request
|
||
|
|
||
|
const requestedPerms2 = {
|
||
|
...PERMS.requests.eth_accounts(),
|
||
|
...PERMS.requests.test_method(),
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
const req2 = RPC_REQUESTS.requestPermissions(DOMAINS.a.origin, {
|
||
|
...requestedPerms2,
|
||
4 years ago
|
});
|
||
|
const res2 = {};
|
||
5 years ago
|
|
||
|
// send, approve, and validate second request
|
||
5 years ago
|
// note use of ACCOUNTS.b.permitted
|
||
5 years ago
|
|
||
4 years ago
|
userApprovalPromise = getUserApprovalPromise(permController);
|
||
4 years ago
|
|
||
5 years ago
|
const pendingApproval2 = assert.doesNotReject(
|
||
|
aMiddleware(req2, res2),
|
||
4 years ago
|
'should not reject permissions request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
4 years ago
|
|
||
4 years ago
|
const id2 = getNextApprovalId(permController);
|
||
|
const approvedReq2 = PERMS.approvedRequest(id2, { ...requestedPerms2 });
|
||
5 years ago
|
|
||
4 years ago
|
await permController.approvePermissionsRequest(
|
||
|
approvedReq2,
|
||
|
ACCOUNTS.b.permitted,
|
||
4 years ago
|
);
|
||
|
await pendingApproval2;
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
res2.result && !res2.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.equal(
|
||
4 years ago
|
res2.result.length,
|
||
|
2,
|
||
4 years ago
|
'origin should have single approved permission',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validatePermission(
|
||
|
res2.result[0],
|
||
|
PERM_NAMES.eth_accounts,
|
||
5 years ago
|
DOMAINS.a.origin,
|
||
4 years ago
|
CAVEATS.eth_accounts(ACCOUNTS.b.permitted),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validatePermission(
|
||
|
res2.result[1],
|
||
|
PERM_NAMES.test_method,
|
||
5 years ago
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const accounts2 = await permController.getAccounts(DOMAINS.a.origin);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
accounts2,
|
||
|
[ACCOUNTS.b.primary],
|
||
4 years ago
|
'origin should have correct accounts',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.equal(
|
||
4 years ago
|
permController.notifyAccountsChanged.callCount,
|
||
|
2,
|
||
4 years ago
|
'should have called notification method 2 times in total',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
permController.notifyAccountsChanged.lastCall.calledWith(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
accounts2,
|
||
5 years ago
|
),
|
||
4 years ago
|
'expected notification call should have been made',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('rejects permissions on user rejection', async function () {
|
||
4 years ago
|
createApprovalSpies(permController);
|
||
4 years ago
|
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
const expectedError = ERRORS.rejectPermissionsRequest.rejection();
|
||
5 years ago
|
|
||
4 years ago
|
const userApprovalPromise = getUserApprovalPromise(permController);
|
||
4 years ago
|
|
||
5 years ago
|
const requestRejection = assert.rejects(
|
||
|
aMiddleware(req, res),
|
||
|
expectedError,
|
||
|
'request should be rejected with correct error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
4 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
permController.approvals._add.calledOnce,
|
||
|
'should have added single approval request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const id = getNextApprovalId(permController);
|
||
5 years ago
|
|
||
4 years ago
|
await permController.rejectPermissionsRequest(id);
|
||
|
await requestRejection;
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
4 years ago
|
!res.result && res.error && res.error.message === expectedError.message,
|
||
4 years ago
|
'response should have expected error and no result',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const aAccounts = await permController.getAccounts(DOMAINS.a.origin);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
aAccounts,
|
||
|
[],
|
||
|
'origin should have have correct accounts',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
permController.notifyAccountsChanged.notCalled,
|
||
4 years ago
|
'should not have called notification method',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('rejects requests with unknown permissions', async function () {
|
||
4 years ago
|
createApprovalSpies(permController);
|
||
4 years ago
|
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
|
const req = RPC_REQUESTS.requestPermissions(DOMAINS.a.origin, {
|
||
|
...PERMS.requests.does_not_exist(),
|
||
|
...PERMS.requests.test_method(),
|
||
4 years ago
|
});
|
||
|
const res = {};
|
||
5 years ago
|
|
||
|
const expectedError = ERRORS.rejectPermissionsRequest.methodNotFound(
|
||
4 years ago
|
PERM_NAMES.does_not_exist,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
await assert.rejects(
|
||
|
aMiddleware(req, res),
|
||
|
expectedError,
|
||
|
'request should be rejected with correct error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
permController.approvals._add.notCalled,
|
||
|
'no approval requests should have been added',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
4 years ago
|
!res.result && res.error && res.error.message === expectedError.message,
|
||
4 years ago
|
'response should have expected error and no result',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
permController.notifyAccountsChanged.notCalled,
|
||
4 years ago
|
'should not have called notification method',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('accepts only a single pending permissions request per origin', async function () {
|
||
4 years ago
|
createApprovalSpies(permController);
|
||
5 years ago
|
|
||
|
// two middlewares for two origins
|
||
|
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
4 years ago
|
const bMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.b.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// create and start processing first request for first origin
|
||
|
|
||
|
const reqA1 = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.test_method,
|
||
4 years ago
|
);
|
||
|
const resA1 = {};
|
||
5 years ago
|
|
||
4 years ago
|
let userApprovalPromise = getUserApprovalPromise(permController);
|
||
4 years ago
|
|
||
5 years ago
|
const requestApproval1 = assert.doesNotReject(
|
||
|
aMiddleware(reqA1, resA1),
|
||
4 years ago
|
'should not reject permissions request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
4 years ago
|
|
||
5 years ago
|
// create and start processing first request for second origin
|
||
|
|
||
|
const reqB1 = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.b.origin,
|
||
|
PERM_NAMES.test_method,
|
||
4 years ago
|
);
|
||
|
const resB1 = {};
|
||
5 years ago
|
|
||
4 years ago
|
userApprovalPromise = getUserApprovalPromise(permController);
|
||
4 years ago
|
|
||
5 years ago
|
const requestApproval2 = assert.doesNotReject(
|
||
|
bMiddleware(reqB1, resB1),
|
||
4 years ago
|
'should not reject permissions request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
4 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
permController.approvals._add.calledTwice,
|
||
|
'should have added two approval requests',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// create and start processing second request for first origin,
|
||
|
// which should throw
|
||
|
|
||
|
const reqA2 = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.test_method,
|
||
4 years ago
|
);
|
||
|
const resA2 = {};
|
||
5 years ago
|
|
||
4 years ago
|
userApprovalPromise = getUserApprovalPromise(permController);
|
||
4 years ago
|
|
||
4 years ago
|
const expectedError = ERRORS.pendingApprovals.requestAlreadyPending(
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
4 years ago
|
|
||
4 years ago
|
const requestApprovalFail = assert.rejects(
|
||
5 years ago
|
aMiddleware(reqA2, resA2),
|
||
|
expectedError,
|
||
|
'request should be rejected with correct error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
|
await requestApprovalFail;
|
||
4 years ago
|
|
||
5 years ago
|
assert.ok(
|
||
4 years ago
|
!resA2.result &&
|
||
|
resA2.error &&
|
||
|
resA2.error.message === expectedError.message,
|
||
4 years ago
|
'response should have expected error and no result',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.equal(
|
||
4 years ago
|
permController.approvals._add.callCount,
|
||
|
3,
|
||
|
'should have attempted to create three pending approvals',
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.equal(
|
||
|
permController.approvals._approvals.size,
|
||
4 years ago
|
2,
|
||
4 years ago
|
'should only have created two pending approvals',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// now, remaining pending requests should be approved without issue
|
||
|
|
||
4 years ago
|
for (const id of permController.approvals._approvals.keys()) {
|
||
5 years ago
|
await permController.approvePermissionsRequest(
|
||
4 years ago
|
PERMS.approvedRequest(id, PERMS.requests.test_method()),
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
4 years ago
|
await requestApproval1;
|
||
|
await requestApproval2;
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
resA1.result && !resA1.error,
|
||
4 years ago
|
'first response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
assert.equal(
|
||
4 years ago
|
resA1.result.length,
|
||
|
1,
|
||
4 years ago
|
'first origin should have single approved permission',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
resB1.result && !resB1.error,
|
||
4 years ago
|
'second response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
assert.equal(
|
||
4 years ago
|
resB1.result.length,
|
||
|
1,
|
||
4 years ago
|
'second origin should have single approved permission',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('restricted methods', function () {
|
||
4 years ago
|
let permController;
|
||
5 years ago
|
|
||
|
beforeEach(function () {
|
||
4 years ago
|
permController = initPermController();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('prevents restricted method access for unpermitted domain', async function () {
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.test_method(DOMAINS.a.origin);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
const expectedError = ERRORS.rpcCap.unauthorized();
|
||
5 years ago
|
|
||
|
await assert.rejects(
|
||
|
aMiddleware(req, res),
|
||
|
expectedError,
|
||
|
'request should be rejected with correct error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
4 years ago
|
!res.result && res.error && res.error.code === expectedError.code,
|
||
4 years ago
|
'response should have expected error and no result',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('allows restricted method access for permitted domain', async function () {
|
||
4 years ago
|
const bMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.b.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
grantPermissions(
|
||
|
permController,
|
||
|
DOMAINS.b.origin,
|
||
|
PERMS.finalizedRequests.test_method(),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.test_method(DOMAINS.b.origin, true);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(bMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
res.result && res.result === 1,
|
||
4 years ago
|
'response should have correct result',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('eth_accounts', function () {
|
||
4 years ago
|
let permController;
|
||
5 years ago
|
|
||
|
beforeEach(function () {
|
||
4 years ago
|
permController = initPermController();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('returns empty array for non-permitted domain', async function () {
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.eth_accounts(DOMAINS.a.origin);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(aMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
res.result && !res.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
|
assert.deepEqual(res.result, [], 'response should have correct result');
|
||
|
});
|
||
5 years ago
|
|
||
|
it('returns correct accounts for permitted domain', async function () {
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
grantPermissions(
|
||
4 years ago
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
PERMS.finalizedRequests.eth_accounts(ACCOUNTS.a.permitted),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.eth_accounts(DOMAINS.a.origin);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(aMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
|
res.result && !res.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
res.result,
|
||
|
[ACCOUNTS.a.primary],
|
||
4 years ago
|
'response should have correct result',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('eth_requestAccounts', function () {
|
||
4 years ago
|
let permController;
|
||
5 years ago
|
|
||
|
beforeEach(function () {
|
||
4 years ago
|
permController = initPermController();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('requests accounts for unpermitted origin, and approves on user approval', async function () {
|
||
4 years ago
|
createApprovalSpies(permController);
|
||
4 years ago
|
|
||
4 years ago
|
const userApprovalPromise = getUserApprovalPromise(permController);
|
||
5 years ago
|
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.eth_requestAccounts(DOMAINS.a.origin);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
|
const pendingApproval = assert.doesNotReject(
|
||
|
aMiddleware(req, res),
|
||
4 years ago
|
'should not reject permissions request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
permController.approvals._add.calledOnce,
|
||
|
'should have added single approval request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const id = getNextApprovalId(permController);
|
||
4 years ago
|
const approvedReq = PERMS.approvedRequest(
|
||
|
id,
|
||
|
PERMS.requests.eth_accounts(),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await permController.approvePermissionsRequest(
|
||
|
approvedReq,
|
||
|
ACCOUNTS.a.permitted,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
5 years ago
|
// wait for permission to be granted
|
||
4 years ago
|
await pendingApproval;
|
||
5 years ago
|
|
||
4 years ago
|
const perms = permController.permissions.getPermissionsForDomain(
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.equal(
|
||
4 years ago
|
perms.length,
|
||
|
1,
|
||
4 years ago
|
'domain should have correct number of permissions',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validatePermission(
|
||
|
perms[0],
|
||
|
PERM_NAMES.eth_accounts,
|
||
5 years ago
|
DOMAINS.a.origin,
|
||
4 years ago
|
CAVEATS.eth_accounts(ACCOUNTS.a.permitted),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// we should also see the accounts on the response
|
||
|
assert.ok(
|
||
|
res.result && !res.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
4 years ago
|
res.result,
|
||
|
[ACCOUNTS.a.primary],
|
||
4 years ago
|
'result should have correct accounts',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// we should also be able to get the accounts independently
|
||
4 years ago
|
const aAccounts = await permController.getAccounts(DOMAINS.a.origin);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
aAccounts,
|
||
|
[ACCOUNTS.a.primary],
|
||
|
'origin should have have correct accounts',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('requests accounts for unpermitted origin, and rejects on user rejection', async function () {
|
||
4 years ago
|
createApprovalSpies(permController);
|
||
4 years ago
|
|
||
4 years ago
|
const userApprovalPromise = getUserApprovalPromise(permController);
|
||
5 years ago
|
|
||
4 years ago
|
const aMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.a.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.eth_requestAccounts(DOMAINS.a.origin);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
const expectedError = ERRORS.rejectPermissionsRequest.rejection();
|
||
5 years ago
|
|
||
|
const requestRejection = assert.rejects(
|
||
|
aMiddleware(req, res),
|
||
|
expectedError,
|
||
|
'request should be rejected with correct error',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
await userApprovalPromise;
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(
|
||
|
permController.approvals._add.calledOnce,
|
||
|
'should have added single approval request',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const id = getNextApprovalId(permController);
|
||
5 years ago
|
|
||
4 years ago
|
await permController.rejectPermissionsRequest(id);
|
||
|
await requestRejection;
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
4 years ago
|
!res.result && res.error && res.error.message === expectedError.message,
|
||
4 years ago
|
'response should have expected error and no result',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const aAccounts = await permController.getAccounts(DOMAINS.a.origin);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
aAccounts,
|
||
|
[],
|
||
|
'origin should have have correct accounts',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('directly returns accounts for permitted domain', async function () {
|
||
4 years ago
|
const cMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
grantPermissions(
|
||
4 years ago
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
4 years ago
|
PERMS.finalizedRequests.eth_accounts(ACCOUNTS.c.permitted),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.eth_requestAccounts(DOMAINS.c.origin);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(cMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
5 years ago
|
res.result && !res.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
res.result,
|
||
|
[ACCOUNTS.c.primary],
|
||
4 years ago
|
'response should have correct result',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('rejects new requests when request already pending', async function () {
|
||
4 years ago
|
let unlock;
|
||
5 years ago
|
const unlockPromise = new Promise((resolve) => {
|
||
4 years ago
|
unlock = resolve;
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
permController.getUnlockPromise = () => unlockPromise;
|
||
5 years ago
|
|
||
4 years ago
|
const cMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
grantPermissions(
|
||
4 years ago
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
4 years ago
|
PERMS.finalizedRequests.eth_accounts(ACCOUNTS.c.permitted),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.eth_requestAccounts(DOMAINS.c.origin);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
|
// this will block until we resolve the unlock Promise
|
||
|
const requestApproval = assert.doesNotReject(
|
||
|
cMiddleware(req, res),
|
||
4 years ago
|
'should not reject',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// this will reject because of the already pending request
|
||
|
await assert.rejects(
|
||
|
cMiddleware({ ...req }, {}),
|
||
4 years ago
|
ERRORS.eth_requestAccounts.requestAlreadyPending(DOMAINS.c.origin),
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// now unlock and let through the first request
|
||
4 years ago
|
unlock();
|
||
5 years ago
|
|
||
4 years ago
|
await requestApproval;
|
||
5 years ago
|
|
||
|
assert.ok(
|
||
5 years ago
|
res.result && !res.error,
|
||
4 years ago
|
'response should have result and no error',
|
||
4 years ago
|
);
|
||
5 years ago
|
assert.deepEqual(
|
||
4 years ago
|
res.result,
|
||
|
[ACCOUNTS.c.primary],
|
||
4 years ago
|
'response should have correct result',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
describe('metamask_sendDomainMetadata', function () {
|
||
4 years ago
|
let permController, clock;
|
||
5 years ago
|
|
||
|
beforeEach(function () {
|
||
4 years ago
|
permController = initPermController();
|
||
|
clock = sinon.useFakeTimers(1);
|
||
|
});
|
||
5 years ago
|
|
||
|
afterEach(function () {
|
||
4 years ago
|
clock.restore();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('records domain metadata', async function () {
|
||
4 years ago
|
const name = 'BAZ';
|
||
5 years ago
|
|
||
4 years ago
|
const cMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.metamask_sendDomainMetadata(
|
||
|
DOMAINS.c.origin,
|
||
|
name,
|
||
4 years ago
|
);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(cMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(res.result, 'result should be true');
|
||
5 years ago
|
|
||
4 years ago
|
const metadataStore = permController.store.getState()[METADATA_STORE_KEY];
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
|
metadataStore,
|
||
5 years ago
|
{
|
||
|
[DOMAINS.c.origin]: {
|
||
|
name,
|
||
|
host: DOMAINS.c.host,
|
||
|
lastUpdated: 1,
|
||
|
},
|
||
|
},
|
||
4 years ago
|
'metadata should have been added to store',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('records domain metadata and preserves extensionId', async function () {
|
||
4 years ago
|
const extensionId = 'fooExtension';
|
||
5 years ago
|
|
||
4 years ago
|
const name = 'BAZ';
|
||
5 years ago
|
|
||
4 years ago
|
const cMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
|
extensionId,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.metamask_sendDomainMetadata(
|
||
|
DOMAINS.c.origin,
|
||
|
name,
|
||
4 years ago
|
);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(cMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(res.result, 'result should be true');
|
||
5 years ago
|
|
||
4 years ago
|
const metadataStore = permController.store.getState()[METADATA_STORE_KEY];
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
|
metadataStore,
|
||
5 years ago
|
{ [DOMAINS.c.origin]: { name, extensionId, lastUpdated: 1 } },
|
||
4 years ago
|
'metadata should have been added to store',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should not record domain metadata if no name', async function () {
|
||
4 years ago
|
const name = null;
|
||
5 years ago
|
|
||
4 years ago
|
const cMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.metamask_sendDomainMetadata(
|
||
|
DOMAINS.c.origin,
|
||
|
name,
|
||
4 years ago
|
);
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(cMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(res.result, 'result should be true');
|
||
5 years ago
|
|
||
4 years ago
|
const metadataStore = permController.store.getState()[METADATA_STORE_KEY];
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
4 years ago
|
metadataStore,
|
||
|
{},
|
||
4 years ago
|
'metadata should not have been added to store',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should not record domain metadata if no metadata', async function () {
|
||
4 years ago
|
const cMiddleware = getPermissionsMiddleware(
|
||
|
permController,
|
||
|
DOMAINS.c.origin,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.metamask_sendDomainMetadata(DOMAINS.c.origin);
|
||
|
delete req.domainMetadata;
|
||
|
const res = {};
|
||
5 years ago
|
|
||
4 years ago
|
await assert.doesNotReject(cMiddleware(req, res), 'should not reject');
|
||
5 years ago
|
|
||
4 years ago
|
assert.ok(res.result, 'result should be true');
|
||
5 years ago
|
|
||
4 years ago
|
const metadataStore = permController.store.getState()[METADATA_STORE_KEY];
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
4 years ago
|
metadataStore,
|
||
|
{},
|
||
4 years ago
|
'metadata should not have been added to store',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|