1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/test/e2e/fixture-server.js
Peter Yinusa 0b4532ec1b
E2e test fixtures (#16061)
* remove state.json files

* move file

* Update Readme

* Create fixture builder

* load test fixture

* remove redundant method

* update snap tests

* update stats tests

* update extension tests

* update extension tests

* Update fixture data

* snap test dapp connection

* Update fixture data

* add onboarding fixture

* use onboarding fixture

* reuse import account vault

* remove unnecessary use of class

* use fixture builder in new tests

* switch to function

* update default fixture

* update default fixture

* update test

* update 1559 test fixttures

* update 1559 test fixtures

* update 1559 test fixtures

* dismiss 3box whats new

* remove redundant code

* move docs

* remove unused code

* token detection

* use default timeout

* remove redundant code

* Update fixture builder

hide `Protect your funds` dialog
remove browser environment
remove default network details
hide dismiss seed backup reminder
recursively merges fixture data

* add token to tokencontroller

* remove network details

* add missing identities to preference controller

* remove duplicate properties

* update bip-32 to use fixturebuilder

* alphabetise snap permissions

* update get snaps to use fixturebuilder

* Update test-snap-bip-32.spec.js

wait for window

* add popular network state

* update test

* lint
2022-10-28 09:42:12 +01:00

105 lines
2.8 KiB
JavaScript

const Koa = require('koa');
const { isObject, mapValues } = require('lodash');
const CURRENT_STATE_KEY = '__CURRENT__';
const DEFAULT_STATE_KEY = '__DEFAULT__';
const FIXTURE_SERVER_HOST = 'localhost';
const FIXTURE_SERVER_PORT = 12345;
const fixtureSubstitutionPrefix = '__FIXTURE_SUBSTITUTION__';
const fixtureSubstitutionCommands = {
currentDateInMilliseconds: 'currentDateInMilliseconds',
};
/**
* Perform substitutions on a single piece of state.
*
* @param {unknown} partialState - The piece of state to perform substitutions on.
* @returns {unknown} The partial state with substititions performed.
*/
function performSubstitution(partialState) {
if (Array.isArray(partialState)) {
return partialState.map(performSubstitution);
} else if (isObject(partialState)) {
return mapValues(partialState, performSubstitution);
} else if (
typeof partialState === 'string' &&
partialState.startsWith(fixtureSubstitutionPrefix)
) {
const substitutionCommand = partialState.substring(
fixtureSubstitutionPrefix.length,
);
if (
substitutionCommand ===
fixtureSubstitutionCommands.currentDateInMilliseconds
) {
return new Date().getTime();
}
throw new Error(`Unknown substitution command: ${substitutionCommand}`);
}
return partialState;
}
/**
* Substitute values in the state fixture.
*
* @param {object} rawState - The state fixture.
* @returns {object} The state fixture with substitutions performed.
*/
function performStateSubstitutions(rawState) {
return mapValues(rawState, performSubstitution);
}
class FixtureServer {
constructor() {
this._app = new Koa();
this._stateMap = new Map([[DEFAULT_STATE_KEY, Object.create(null)]]);
this._app.use(async (ctx) => {
// Firefox is _super_ strict about needing CORS headers
ctx.set('Access-Control-Allow-Origin', '*');
if (this._isStateRequest(ctx)) {
ctx.body = this._stateMap.get(CURRENT_STATE_KEY);
}
});
}
async start() {
const options = {
host: FIXTURE_SERVER_HOST,
port: FIXTURE_SERVER_PORT,
exclusive: true,
};
return new Promise((resolve, reject) => {
this._server = this._app.listen(options);
this._server.once('error', reject);
this._server.once('listening', resolve);
});
}
async stop() {
if (!this._server) {
return;
}
await new Promise((resolve, reject) => {
this._server.close();
this._server.once('error', reject);
this._server.once('close', resolve);
});
}
loadJsonState(rawState) {
const state = performStateSubstitutions(rawState);
this._stateMap.set(CURRENT_STATE_KEY, state);
}
_isStateRequest(ctx) {
return ctx.method === 'GET' && ctx.path === '/state.json';
}
}
module.exports = FixtureServer;