2021-02-04 19:15:23 +01:00
|
|
|
|
import assert from 'assert';
|
|
|
|
|
import proxyquire from 'proxyquire';
|
|
|
|
|
import sinon from 'sinon';
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
REQUIRED_ERROR,
|
|
|
|
|
INVALID_RECIPIENT_ADDRESS_ERROR,
|
2019-01-22 20:05:59 +01:00
|
|
|
|
KNOWN_RECIPIENT_ADDRESS_ERROR,
|
2021-02-27 06:26:04 +01:00
|
|
|
|
CONFUSING_ENS_ERROR,
|
2021-03-03 01:28:12 +01:00
|
|
|
|
CONTRACT_ADDRESS_ERROR,
|
2021-03-16 22:00:08 +01:00
|
|
|
|
} from '../../send.constants';
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
|
|
|
|
const stubs = {
|
2020-11-03 00:41:28 +01:00
|
|
|
|
isValidAddress: sinon
|
|
|
|
|
.stub()
|
|
|
|
|
.callsFake((to) => Boolean(to.match(/^[0xabcdef123456798]+$/u))),
|
2021-02-04 19:15:23 +01:00
|
|
|
|
};
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
2021-03-16 22:00:08 +01:00
|
|
|
|
const toRowUtils = proxyquire('./add-recipient.js', {
|
2019-04-17 21:15:13 +02:00
|
|
|
|
'../../../../helpers/utils/util': {
|
2018-05-05 17:11:53 +02:00
|
|
|
|
isValidAddress: stubs.isValidAddress,
|
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
|
});
|
|
|
|
|
const { getToErrorObject, getToWarningObject } = toRowUtils;
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
|
describe('add-recipient utils', function () {
|
|
|
|
|
describe('getToErrorObject()', function () {
|
2020-05-29 20:31:57 +02:00
|
|
|
|
it('should return a required error if "to" is falsy', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
|
assert.deepStrictEqual(getToErrorObject(null), {
|
2018-05-05 17:11:53 +02:00
|
|
|
|
to: REQUIRED_ERROR,
|
2021-02-04 19:15:23 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
2020-05-29 20:31:57 +02:00
|
|
|
|
it('should return an invalid recipient error if "to" is truthy but invalid', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
|
assert.deepStrictEqual(getToErrorObject('mockInvalidTo'), {
|
2018-05-05 17:11:53 +02:00
|
|
|
|
to: INVALID_RECIPIENT_ADDRESS_ERROR,
|
2021-02-04 19:15:23 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
2020-05-29 20:31:57 +02:00
|
|
|
|
it('should return null if "to" is truthy and valid', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
|
assert.deepStrictEqual(getToErrorObject('0xabc123'), {
|
2018-05-05 17:11:53 +02:00
|
|
|
|
to: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
2021-03-03 01:28:12 +01:00
|
|
|
|
|
|
|
|
|
it('should return a contract address error if the recipient is the same as the tokens contract address', function () {
|
|
|
|
|
assert.deepStrictEqual(getToErrorObject('0xabc123', '0xabc123'), {
|
|
|
|
|
to: CONTRACT_ADDRESS_ERROR,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return null if the recipient address is not the token contract address', function () {
|
|
|
|
|
assert.deepStrictEqual(getToErrorObject('0xabc123', '0xabc456'), {
|
|
|
|
|
to: null,
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
|
describe('getToWarningObject()', function () {
|
2020-05-29 20:31:57 +02:00
|
|
|
|
it('should return a known address recipient error if "to" is a token address', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
|
assert.deepStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
|
getToWarningObject('0xabc123', [{ address: '0xabc123' }], {
|
|
|
|
|
address: '0xabc123',
|
|
|
|
|
}),
|
|
|
|
|
{
|
|
|
|
|
to: KNOWN_RECIPIENT_ADDRESS_ERROR,
|
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
|
);
|
|
|
|
|
});
|
2019-01-23 19:09:56 +01:00
|
|
|
|
|
2020-05-29 20:31:57 +02:00
|
|
|
|
it('should null if "to" is a token address but sendToken is falsy', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
|
assert.deepStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
|
getToWarningObject('0xabc123', [{ address: '0xabc123' }]),
|
|
|
|
|
{
|
|
|
|
|
to: null,
|
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
|
);
|
|
|
|
|
});
|
2019-01-23 19:09:56 +01:00
|
|
|
|
|
2020-05-29 20:31:57 +02:00
|
|
|
|
it('should return a known address recipient error if "to" is part of contract metadata', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
|
assert.deepStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
|
getToWarningObject(
|
|
|
|
|
'0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359',
|
|
|
|
|
[{ address: '0xabc123' }],
|
|
|
|
|
{ address: '0xabc123' },
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
to: KNOWN_RECIPIENT_ADDRESS_ERROR,
|
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
|
);
|
|
|
|
|
});
|
2020-05-29 20:31:57 +02:00
|
|
|
|
it('should null if "to" is part of contract metadata but sendToken is falsy', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
|
assert.deepStrictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
|
getToWarningObject(
|
|
|
|
|
'0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359',
|
|
|
|
|
[{ address: '0xabc123' }],
|
|
|
|
|
{ address: '0xabc123' },
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
to: KNOWN_RECIPIENT_ADDRESS_ERROR,
|
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
|
);
|
|
|
|
|
});
|
2021-02-27 06:26:04 +01:00
|
|
|
|
|
|
|
|
|
it('should warn if name is a valid domain and confusable', function () {
|
|
|
|
|
assert.deepEqual(getToWarningObject('vitalik.eth'), {
|
|
|
|
|
to: CONFUSING_ENS_ERROR,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not warn if name is a valid domain and not confusable', function () {
|
|
|
|
|
assert.deepEqual(getToWarningObject('vitalik.eth'), {
|
|
|
|
|
to: null,
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
|
});
|
|
|
|
|
});
|