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.
681 lines
17 KiB
681 lines
17 KiB
4 years ago
|
import { strict as assert } from 'assert';
|
||
|
import { ObservableStore } from '@metamask/obs-store';
|
||
|
import nanoid from 'nanoid';
|
||
|
import { useFakeTimers } from 'sinon';
|
||
5 years ago
|
|
||
|
import {
|
||
4 years ago
|
constants,
|
||
|
getters,
|
||
|
noop,
|
||
|
} from '../../../../test/mocks/permission-controller';
|
||
|
import { validateActivityEntry } from '../../../../test/helpers/permission-controller-helpers';
|
||
|
import PermissionsLogController from './permissionsLog';
|
||
|
import { LOG_LIMIT, LOG_METHOD_TYPES } from './enums';
|
||
5 years ago
|
|
||
4 years ago
|
const { PERMS, RPC_REQUESTS } = getters;
|
||
5 years ago
|
|
||
|
const {
|
||
4 years ago
|
ACCOUNTS,
|
||
5 years ago
|
EXPECTED_HISTORIES,
|
||
4 years ago
|
DOMAINS,
|
||
5 years ago
|
PERM_NAMES,
|
||
|
REQUEST_IDS,
|
||
|
RESTRICTED_METHODS,
|
||
4 years ago
|
} = constants;
|
||
5 years ago
|
|
||
4 years ago
|
let clock;
|
||
5 years ago
|
|
||
|
const initPermLog = () => {
|
||
|
return new PermissionsLogController({
|
||
|
store: new ObservableStore(),
|
||
|
restrictedMethods: RESTRICTED_METHODS,
|
||
4 years ago
|
});
|
||
|
};
|
||
5 years ago
|
|
||
|
const mockNext = (handler) => {
|
||
|
if (handler) {
|
||
4 years ago
|
handler(noop);
|
||
5 years ago
|
}
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
const initMiddleware = (permLog) => {
|
||
4 years ago
|
const middleware = permLog.createMiddleware();
|
||
5 years ago
|
return (req, res, next = mockNext) => {
|
||
4 years ago
|
middleware(req, res, next);
|
||
|
};
|
||
|
};
|
||
5 years ago
|
|
||
|
const initClock = () => {
|
||
4 years ago
|
// useFakeTimers, is in fact, not a react-hook
|
||
|
// eslint-disable-next-line
|
||
4 years ago
|
clock = useFakeTimers(1);
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
const tearDownClock = () => {
|
||
4 years ago
|
clock.restore();
|
||
|
};
|
||
5 years ago
|
|
||
|
const getSavedMockNext = (arr) => (handler) => {
|
||
4 years ago
|
arr.push(handler);
|
||
|
};
|
||
5 years ago
|
|
||
|
describe('permissions log', function () {
|
||
|
describe('activity log', function () {
|
||
4 years ago
|
let permLog, logMiddleware;
|
||
5 years ago
|
|
||
|
beforeEach(function () {
|
||
4 years ago
|
permLog = initPermLog();
|
||
|
logMiddleware = initMiddleware(permLog);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('records activity for restricted methods', function () {
|
||
4 years ago
|
let log, req, res;
|
||
5 years ago
|
|
||
|
// test_method, success
|
||
|
|
||
4 years ago
|
req = RPC_REQUESTS.test_method(DOMAINS.a.origin);
|
||
|
req.id = REQUEST_IDS.a;
|
||
|
res = { foo: 'bar' };
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, res);
|
||
5 years ago
|
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
const entry1 = log[0];
|
||
5 years ago
|
|
||
4 years ago
|
assert.equal(log.length, 1, 'log should have single entry');
|
||
5 years ago
|
validateActivityEntry(
|
||
4 years ago
|
entry1,
|
||
|
{ ...req },
|
||
|
{ ...res },
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// eth_accounts, failure
|
||
|
|
||
4 years ago
|
req = RPC_REQUESTS.eth_accounts(DOMAINS.b.origin);
|
||
|
req.id = REQUEST_IDS.b;
|
||
|
res = { error: new Error('Unauthorized.') };
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, res);
|
||
5 years ago
|
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
const entry2 = log[1];
|
||
5 years ago
|
|
||
4 years ago
|
assert.equal(log.length, 2, 'log should have 2 entries');
|
||
5 years ago
|
validateActivityEntry(
|
||
4 years ago
|
entry2,
|
||
|
{ ...req },
|
||
|
{ ...res },
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
false,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// eth_requestAccounts, success
|
||
|
|
||
4 years ago
|
req = RPC_REQUESTS.eth_requestAccounts(DOMAINS.c.origin);
|
||
|
req.id = REQUEST_IDS.c;
|
||
|
res = { result: ACCOUNTS.c.permitted };
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, res);
|
||
5 years ago
|
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
const entry3 = log[2];
|
||
5 years ago
|
|
||
4 years ago
|
assert.equal(log.length, 3, 'log should have 3 entries');
|
||
5 years ago
|
validateActivityEntry(
|
||
4 years ago
|
entry3,
|
||
|
{ ...req },
|
||
|
{ ...res },
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// test_method, no response
|
||
|
|
||
4 years ago
|
req = RPC_REQUESTS.test_method(DOMAINS.a.origin);
|
||
|
req.id = REQUEST_IDS.a;
|
||
|
res = null;
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, res);
|
||
5 years ago
|
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
const entry4 = log[3];
|
||
5 years ago
|
|
||
4 years ago
|
assert.equal(log.length, 4, 'log should have 4 entries');
|
||
5 years ago
|
validateActivityEntry(
|
||
4 years ago
|
entry4,
|
||
|
{ ...req },
|
||
|
null,
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
false,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// validate final state
|
||
|
|
||
4 years ago
|
assert.equal(entry1, log[0], 'first log entry should remain');
|
||
|
assert.equal(entry2, log[1], 'second log entry should remain');
|
||
|
assert.equal(entry3, log[2], 'third log entry should remain');
|
||
|
assert.equal(entry4, log[3], 'fourth log entry should remain');
|
||
|
});
|
||
5 years ago
|
|
||
|
it('handles responses added out of order', function () {
|
||
4 years ago
|
let log;
|
||
5 years ago
|
|
||
4 years ago
|
const handlerArray = [];
|
||
5 years ago
|
|
||
4 years ago
|
const id1 = nanoid();
|
||
|
const id2 = nanoid();
|
||
|
const id3 = nanoid();
|
||
5 years ago
|
|
||
4 years ago
|
const req = RPC_REQUESTS.test_method(DOMAINS.a.origin);
|
||
5 years ago
|
|
||
|
// get make requests
|
||
4 years ago
|
req.id = id1;
|
||
|
const res1 = { foo: id1 };
|
||
|
logMiddleware({ ...req }, { ...res1 }, getSavedMockNext(handlerArray));
|
||
5 years ago
|
|
||
4 years ago
|
req.id = id2;
|
||
|
const res2 = { foo: id2 };
|
||
|
logMiddleware({ ...req }, { ...res2 }, getSavedMockNext(handlerArray));
|
||
5 years ago
|
|
||
4 years ago
|
req.id = id3;
|
||
|
const res3 = { foo: id3 };
|
||
|
logMiddleware({ ...req }, { ...res3 }, getSavedMockNext(handlerArray));
|
||
5 years ago
|
|
||
|
// verify log state
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
assert.equal(log.length, 3, 'log should have 3 entries');
|
||
|
const entry1 = log[0];
|
||
|
const entry2 = log[1];
|
||
|
const entry3 = log[2];
|
||
5 years ago
|
assert.ok(
|
||
4 years ago
|
entry1.id === id1 &&
|
||
|
entry1.response === null &&
|
||
|
entry2.id === id2 &&
|
||
|
entry2.response === null &&
|
||
|
entry3.id === id3 &&
|
||
|
entry3.response === null,
|
||
4 years ago
|
'all entries should be in correct order and without responses',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// call response handlers
|
||
|
for (const i of [1, 2, 0]) {
|
||
4 years ago
|
handlerArray[i](noop);
|
||
5 years ago
|
}
|
||
|
|
||
|
// verify log state again
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
assert.equal(log.length, 3, 'log should have 3 entries');
|
||
5 years ago
|
|
||
|
// verify all entries
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
5 years ago
|
|
||
|
validateActivityEntry(
|
||
4 years ago
|
log[0],
|
||
|
{ ...req, id: id1 },
|
||
|
{ ...res1 },
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validateActivityEntry(
|
||
4 years ago
|
log[1],
|
||
|
{ ...req, id: id2 },
|
||
|
{ ...res2 },
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validateActivityEntry(
|
||
4 years ago
|
log[2],
|
||
|
{ ...req, id: id3 },
|
||
|
{ ...res3 },
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('handles a lack of response', function () {
|
||
4 years ago
|
let req = RPC_REQUESTS.test_method(DOMAINS.a.origin);
|
||
|
req.id = REQUEST_IDS.a;
|
||
|
let res = { foo: 'bar' };
|
||
5 years ago
|
|
||
|
// noop for next handler prevents recording of response
|
||
4 years ago
|
logMiddleware({ ...req }, res, noop);
|
||
5 years ago
|
|
||
4 years ago
|
let log = permLog.getActivityLog();
|
||
|
const entry1 = log[0];
|
||
5 years ago
|
|
||
4 years ago
|
assert.equal(log.length, 1, 'log should have single entry');
|
||
5 years ago
|
validateActivityEntry(
|
||
4 years ago
|
entry1,
|
||
|
{ ...req },
|
||
|
null,
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// next request should be handled as normal
|
||
4 years ago
|
req = RPC_REQUESTS.eth_accounts(DOMAINS.b.origin);
|
||
|
req.id = REQUEST_IDS.b;
|
||
|
res = { result: ACCOUNTS.b.permitted };
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, res);
|
||
5 years ago
|
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
const entry2 = log[1];
|
||
|
assert.equal(log.length, 2, 'log should have 2 entries');
|
||
5 years ago
|
validateActivityEntry(
|
||
4 years ago
|
entry2,
|
||
|
{ ...req },
|
||
|
{ ...res },
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// validate final state
|
||
4 years ago
|
assert.equal(entry1, log[0], 'first log entry remains');
|
||
|
assert.equal(entry2, log[1], 'second log entry remains');
|
||
|
});
|
||
5 years ago
|
|
||
|
it('ignores expected methods', function () {
|
||
4 years ago
|
let log = permLog.getActivityLog();
|
||
|
assert.equal(log.length, 0, 'log should be empty');
|
||
5 years ago
|
|
||
4 years ago
|
const res = { foo: 'bar' };
|
||
4 years ago
|
const req1 = RPC_REQUESTS.metamask_sendDomainMetadata(
|
||
4 years ago
|
DOMAINS.c.origin,
|
||
|
'foobar',
|
||
4 years ago
|
);
|
||
|
const req2 = RPC_REQUESTS.custom(DOMAINS.b.origin, 'eth_getBlockNumber');
|
||
|
const req3 = RPC_REQUESTS.custom(DOMAINS.b.origin, 'net_version');
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware(req1, res);
|
||
|
logMiddleware(req2, res);
|
||
|
logMiddleware(req3, res);
|
||
5 years ago
|
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
|
assert.equal(log.length, 0, 'log should still be empty');
|
||
|
});
|
||
5 years ago
|
|
||
|
it('enforces log limit', function () {
|
||
4 years ago
|
const req = RPC_REQUESTS.test_method(DOMAINS.a.origin);
|
||
|
const res = { foo: 'bar' };
|
||
5 years ago
|
|
||
|
// max out log
|
||
4 years ago
|
let lastId;
|
||
5 years ago
|
for (let i = 0; i < LOG_LIMIT; i++) {
|
||
4 years ago
|
lastId = nanoid();
|
||
|
logMiddleware({ ...req, id: lastId }, { ...res });
|
||
5 years ago
|
}
|
||
|
|
||
|
// check last entry valid
|
||
4 years ago
|
let log = permLog.getActivityLog();
|
||
5 years ago
|
assert.equal(
|
||
4 years ago
|
log.length,
|
||
|
LOG_LIMIT,
|
||
|
'log should have LOG_LIMIT num entries',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validateActivityEntry(
|
||
4 years ago
|
log[LOG_LIMIT - 1],
|
||
|
{ ...req, id: lastId },
|
||
|
res,
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// store the id of the current second entry
|
||
4 years ago
|
const nextFirstId = log[1].id;
|
||
5 years ago
|
|
||
|
// add one more entry to log, putting it over the limit
|
||
4 years ago
|
lastId = nanoid();
|
||
|
logMiddleware({ ...req, id: lastId }, { ...res });
|
||
5 years ago
|
|
||
|
// check log length
|
||
4 years ago
|
log = permLog.getActivityLog();
|
||
5 years ago
|
assert.equal(
|
||
4 years ago
|
log.length,
|
||
|
LOG_LIMIT,
|
||
|
'log should have LOG_LIMIT num entries',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// check first and last entries
|
||
|
validateActivityEntry(
|
||
4 years ago
|
log[0],
|
||
|
{ ...req, id: nextFirstId },
|
||
|
res,
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
validateActivityEntry(
|
||
4 years ago
|
log[LOG_LIMIT - 1],
|
||
|
{ ...req, id: lastId },
|
||
|
res,
|
||
|
LOG_METHOD_TYPES.restricted,
|
||
|
true,
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('permissions history', function () {
|
||
4 years ago
|
let permLog, logMiddleware;
|
||
5 years ago
|
|
||
|
beforeEach(function () {
|
||
4 years ago
|
permLog = initPermLog();
|
||
|
logMiddleware = initMiddleware(permLog);
|
||
|
initClock();
|
||
|
});
|
||
5 years ago
|
|
||
|
afterEach(function () {
|
||
4 years ago
|
tearDownClock();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('only updates history on responses', function () {
|
||
4 years ago
|
let permHistory;
|
||
5 years ago
|
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.test_method,
|
||
4 years ago
|
);
|
||
|
const res = { result: [PERMS.granted.test_method()] };
|
||
5 years ago
|
|
||
|
// noop => no response
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res }, noop);
|
||
5 years ago
|
|
||
4 years ago
|
permHistory = permLog.getHistory();
|
||
|
assert.deepEqual(permHistory, {}, 'history should not have been updated');
|
||
5 years ago
|
|
||
|
// response => records granted permissions
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
4 years ago
|
permHistory = permLog.getHistory();
|
||
5 years ago
|
assert.equal(
|
||
4 years ago
|
Object.keys(permHistory).length,
|
||
|
1,
|
||
4 years ago
|
'history should have single origin',
|
||
4 years ago
|
);
|
||
5 years ago
|
assert.ok(
|
||
4 years ago
|
Boolean(permHistory[DOMAINS.a.origin]),
|
||
4 years ago
|
'history should have expected origin',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('ignores malformed permissions requests', function () {
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.test_method,
|
||
4 years ago
|
);
|
||
|
delete req.params;
|
||
|
const res = { result: [PERMS.granted.test_method()] };
|
||
5 years ago
|
|
||
|
// no params => no response
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
4 years ago
|
assert.deepEqual(
|
||
|
permLog.getHistory(),
|
||
|
{},
|
||
|
'history should not have been updated',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('records and updates account history as expected', async function () {
|
||
4 years ago
|
let permHistory;
|
||
5 years ago
|
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
5 years ago
|
const res = {
|
||
4 years ago
|
result: [PERMS.granted.eth_accounts(ACCOUNTS.a.permitted)],
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
|
// validate history
|
||
|
|
||
4 years ago
|
permHistory = permLog.getHistory();
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
|
permHistory,
|
||
|
EXPECTED_HISTORIES.case1[0],
|
||
4 years ago
|
'should have correct history',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// mock permission requested again, with another approved account
|
||
|
|
||
4 years ago
|
clock.tick(1);
|
||
5 years ago
|
|
||
4 years ago
|
res.result = [PERMS.granted.eth_accounts([ACCOUNTS.a.permitted[0]])];
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
4 years ago
|
permHistory = permLog.getHistory();
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
|
permHistory,
|
||
|
EXPECTED_HISTORIES.case1[1],
|
||
4 years ago
|
'should have correct history',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('handles eth_accounts response without caveats', async function () {
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
5 years ago
|
const res = {
|
||
4 years ago
|
result: [PERMS.granted.eth_accounts(ACCOUNTS.a.permitted)],
|
||
4 years ago
|
};
|
||
|
delete res.result[0].caveats;
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
|
// validate history
|
||
|
|
||
|
assert.deepEqual(
|
||
4 years ago
|
permLog.getHistory(),
|
||
|
EXPECTED_HISTORIES.case2[0],
|
||
4 years ago
|
'should have expected history',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('handles extra caveats for eth_accounts', async function () {
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
5 years ago
|
const res = {
|
||
4 years ago
|
result: [PERMS.granted.eth_accounts(ACCOUNTS.a.permitted)],
|
||
4 years ago
|
};
|
||
|
res.result[0].caveats.push({ foo: 'bar' });
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
|
// validate history
|
||
|
|
||
|
assert.deepEqual(
|
||
|
permLog.getHistory(),
|
||
|
EXPECTED_HISTORIES.case1[0],
|
||
4 years ago
|
'should have correct history',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
// wallet_requestPermissions returns all permissions approved for the
|
||
|
// requesting origin, including old ones
|
||
|
it('handles unrequested permissions on the response', async function () {
|
||
|
const req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
5 years ago
|
const res = {
|
||
|
result: [
|
||
4 years ago
|
PERMS.granted.eth_accounts(ACCOUNTS.a.permitted),
|
||
5 years ago
|
PERMS.granted.test_method(),
|
||
|
],
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
|
// validate history
|
||
|
|
||
|
assert.deepEqual(
|
||
|
permLog.getHistory(),
|
||
|
EXPECTED_HISTORIES.case1[0],
|
||
4 years ago
|
'should have correct history',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('does not update history if no new permissions are approved', async function () {
|
||
|
let req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.test_method,
|
||
4 years ago
|
);
|
||
5 years ago
|
let res = {
|
||
4 years ago
|
result: [PERMS.granted.test_method()],
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
|
// validate history
|
||
|
|
||
|
assert.deepEqual(
|
||
|
permLog.getHistory(),
|
||
|
EXPECTED_HISTORIES.case4[0],
|
||
4 years ago
|
'should have correct history',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// new permission requested, but not approved
|
||
|
|
||
4 years ago
|
clock.tick(1);
|
||
5 years ago
|
|
||
|
req = RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
4 years ago
|
);
|
||
5 years ago
|
res = {
|
||
4 years ago
|
result: [PERMS.granted.test_method()],
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
logMiddleware({ ...req }, { ...res });
|
||
5 years ago
|
|
||
|
// validate history
|
||
|
|
||
|
assert.deepEqual(
|
||
|
permLog.getHistory(),
|
||
|
EXPECTED_HISTORIES.case4[0],
|
||
4 years ago
|
'should have same history as before',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('records and updates history for multiple origins, regardless of response order', async function () {
|
||
4 years ago
|
let permHistory;
|
||
5 years ago
|
|
||
|
// make first round of requests
|
||
|
|
||
4 years ago
|
const round1 = [];
|
||
|
const handlers1 = [];
|
||
5 years ago
|
|
||
|
// first origin
|
||
|
round1.push({
|
||
|
req: RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.test_method,
|
||
5 years ago
|
),
|
||
|
res: {
|
||
4 years ago
|
result: [PERMS.granted.test_method()],
|
||
5 years ago
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
// second origin
|
||
|
round1.push({
|
||
|
req: RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.b.origin,
|
||
|
PERM_NAMES.eth_accounts,
|
||
5 years ago
|
),
|
||
|
res: {
|
||
4 years ago
|
result: [PERMS.granted.eth_accounts(ACCOUNTS.b.permitted)],
|
||
5 years ago
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
// third origin
|
||
|
round1.push({
|
||
4 years ago
|
req: RPC_REQUESTS.requestPermissions(DOMAINS.c.origin, {
|
||
5 years ago
|
[PERM_NAMES.test_method]: {},
|
||
|
[PERM_NAMES.eth_accounts]: {},
|
||
|
}),
|
||
|
res: {
|
||
|
result: [
|
||
|
PERMS.granted.test_method(),
|
||
4 years ago
|
PERMS.granted.eth_accounts(ACCOUNTS.c.permitted),
|
||
5 years ago
|
],
|
||
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
// make requests and process responses out of order
|
||
|
round1.forEach((x) => {
|
||
4 years ago
|
logMiddleware({ ...x.req }, { ...x.res }, getSavedMockNext(handlers1));
|
||
|
});
|
||
5 years ago
|
|
||
|
for (const i of [1, 2, 0]) {
|
||
4 years ago
|
handlers1[i](noop);
|
||
5 years ago
|
}
|
||
|
|
||
|
// validate history
|
||
4 years ago
|
permHistory = permLog.getHistory();
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
4 years ago
|
permHistory,
|
||
|
EXPECTED_HISTORIES.case3[0],
|
||
4 years ago
|
'should have expected history',
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
// make next round of requests
|
||
|
|
||
4 years ago
|
clock.tick(1);
|
||
5 years ago
|
|
||
4 years ago
|
const round2 = [];
|
||
5 years ago
|
// we're just gonna process these in order
|
||
|
|
||
|
// first origin
|
||
|
round2.push({
|
||
|
req: RPC_REQUESTS.requestPermission(
|
||
4 years ago
|
DOMAINS.a.origin,
|
||
|
PERM_NAMES.test_method,
|
||
5 years ago
|
),
|
||
|
res: {
|
||
4 years ago
|
result: [PERMS.granted.test_method()],
|
||
5 years ago
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
// nothing for second origin
|
||
|
|
||
|
// third origin
|
||
|
round2.push({
|
||
4 years ago
|
req: RPC_REQUESTS.requestPermissions(DOMAINS.c.origin, {
|
||
5 years ago
|
[PERM_NAMES.eth_accounts]: {},
|
||
|
}),
|
||
|
res: {
|
||
4 years ago
|
result: [PERMS.granted.eth_accounts(ACCOUNTS.b.permitted)],
|
||
5 years ago
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
|
// make requests
|
||
|
round2.forEach((x) => {
|
||
4 years ago
|
logMiddleware({ ...x.req }, { ...x.res });
|
||
|
});
|
||
5 years ago
|
|
||
|
// validate history
|
||
4 years ago
|
permHistory = permLog.getHistory();
|
||
5 years ago
|
|
||
|
assert.deepEqual(
|
||
4 years ago
|
permHistory,
|
||
|
EXPECTED_HISTORIES.case3[1],
|
||
4 years ago
|
'should have expected history',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|