2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-11-15 17:13:51 +01:00
|
|
|
import sinon from 'sinon';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction';
|
2021-03-16 22:00:08 +01:00
|
|
|
import PersonalMessageManager from './personal-message-manager';
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('Personal Message Manager', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let messageManager;
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
beforeEach(function () {
|
2021-11-15 17:13:51 +01:00
|
|
|
messageManager = new PersonalMessageManager({ metricsEvent: sinon.fake() });
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#getMsgList', function () {
|
|
|
|
it('when new should return empty array', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const result = messageManager.messages;
|
|
|
|
assert.ok(Array.isArray(result));
|
|
|
|
assert.equal(result.length, 0);
|
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#addMsg', function () {
|
|
|
|
it('adds a Msg returned in getMsgList', function () {
|
2020-11-07 08:38:12 +01:00
|
|
|
const Msg = {
|
|
|
|
id: 1,
|
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
messageManager.addMsg(Msg);
|
|
|
|
const result = messageManager.messages;
|
|
|
|
assert.ok(Array.isArray(result));
|
|
|
|
assert.equal(result.length, 1);
|
|
|
|
assert.equal(result[0].id, 1);
|
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#setMsgStatusApproved', function () {
|
|
|
|
it('sets the Msg status to approved', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const Msg = {
|
|
|
|
id: 1,
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
messageManager.addMsg(Msg);
|
|
|
|
messageManager.setMsgStatusApproved(1);
|
|
|
|
const result = messageManager.messages;
|
|
|
|
assert.ok(Array.isArray(result));
|
|
|
|
assert.equal(result.length, 1);
|
|
|
|
assert.equal(result[0].status, TRANSACTION_STATUSES.APPROVED);
|
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#rejectMsg', function () {
|
|
|
|
it('sets the Msg status to rejected', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const Msg = {
|
|
|
|
id: 1,
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
messageManager.addMsg(Msg);
|
|
|
|
messageManager.rejectMsg(1);
|
|
|
|
const result = messageManager.messages;
|
|
|
|
assert.ok(Array.isArray(result));
|
|
|
|
assert.equal(result.length, 1);
|
|
|
|
assert.equal(result[0].status, TRANSACTION_STATUSES.REJECTED);
|
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#_updateMsg', function () {
|
|
|
|
it('replaces the Msg with the same id', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '1',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '2',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager._updateMsg({
|
|
|
|
id: '1',
|
|
|
|
status: 'blah',
|
|
|
|
hash: 'foo',
|
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const result = messageManager.getMsg('1');
|
|
|
|
assert.equal(result.hash, 'foo');
|
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#getUnapprovedMsgs', function () {
|
|
|
|
it('returns unapproved Msgs in a hash', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '1',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '2',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const result = messageManager.getUnapprovedMsgs();
|
|
|
|
assert.equal(typeof result, 'object');
|
|
|
|
assert.equal(result['1'].status, TRANSACTION_STATUSES.UNAPPROVED);
|
|
|
|
assert.equal(result['2'], undefined);
|
|
|
|
});
|
|
|
|
});
|
2017-02-21 23:25:47 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#getMsg', function () {
|
|
|
|
it('returns a Msg with the requested id', function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '1',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
messageManager.addMsg({
|
|
|
|
id: '2',
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.APPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
metamaskNetworkId: 'unit test',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-07 08:38:12 +01:00
|
|
|
assert.equal(
|
|
|
|
messageManager.getMsg('1').status,
|
|
|
|
TRANSACTION_STATUSES.UNAPPROVED,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-07 08:38:12 +01:00
|
|
|
assert.equal(
|
|
|
|
messageManager.getMsg('2').status,
|
|
|
|
TRANSACTION_STATUSES.APPROVED,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2017-03-07 00:03:49 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#normalizeMsgData', function () {
|
|
|
|
it('converts text to a utf8 hex string', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const input = 'hello';
|
|
|
|
const output = messageManager.normalizeMsgData(input);
|
|
|
|
assert.equal(output, '0x68656c6c6f', 'predictably hex encoded');
|
|
|
|
});
|
2017-03-07 00:03:49 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
it('tolerates a hex prefix', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const input = '0x12';
|
|
|
|
const output = messageManager.normalizeMsgData(input);
|
|
|
|
assert.equal(output, '0x12', 'un modified');
|
|
|
|
});
|
2017-03-07 00:03:49 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
it('tolerates normal hex', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const input = '12';
|
|
|
|
const output = messageManager.normalizeMsgData(input);
|
|
|
|
assert.equal(output, '0x12', 'adds prefix');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|