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';
|
2021-04-15 20:01:46 +02:00
|
|
|
import { getToErrorObject, getToWarningObject } from './add-recipient';
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
jest.mock('../../../../../app/helpers/utils/util', () => ({
|
|
|
|
isDefaultMetaMaskChain: jest.fn().mockReturnValue(true),
|
|
|
|
isEthNetwork: jest.fn().mockReturnValue(true),
|
|
|
|
checkExistingAddresses: jest.fn().mockReturnValue(true),
|
|
|
|
isValidAddress: jest.fn((to) => Boolean(to.match(/^[0xabcdef123456798]+$/u))),
|
|
|
|
isValidDomainName: jest.requireActual('../../../../../app/helpers/utils/util')
|
|
|
|
.isValidDomainName,
|
|
|
|
isOriginContractAddress: jest.requireActual(
|
|
|
|
'../../../../../app/helpers/utils/util',
|
|
|
|
).isOriginContractAddress,
|
|
|
|
}));
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('add-recipient utils', () => {
|
|
|
|
describe('getToErrorObject()', () => {
|
|
|
|
it('should return a required error if "to" is falsy', () => {
|
|
|
|
expect(getToErrorObject(null)).toStrictEqual({
|
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
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return an invalid recipient error if "to" is truthy but invalid', () => {
|
|
|
|
expect(getToErrorObject('mockInvalidTo')).toStrictEqual({
|
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
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return null if "to" is truthy and valid', () => {
|
|
|
|
expect(getToErrorObject('0xabc123')).toStrictEqual({
|
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
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return a contract address error if the recipient is the same as the tokens contract address', () => {
|
|
|
|
expect(getToErrorObject('0xabc123', '0xabc123')).toStrictEqual({
|
2021-03-03 01:28:12 +01:00
|
|
|
to: CONTRACT_ADDRESS_ERROR,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return null if the recipient address is not the token contract address', () => {
|
|
|
|
expect(getToErrorObject('0xabc123', '0xabc456')).toStrictEqual({
|
2021-03-03 01:28:12 +01:00
|
|
|
to: null,
|
|
|
|
});
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('getToWarningObject()', () => {
|
|
|
|
it('should return a known address recipient error if "to" is a token address', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
getToWarningObject('0xabc123', [{ address: '0xabc123' }], {
|
|
|
|
address: '0xabc123',
|
|
|
|
}),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({
|
|
|
|
to: KNOWN_RECIPIENT_ADDRESS_ERROR,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-01-23 19:09:56 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should null if "to" is a token address but sendToken is falsy', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
getToWarningObject('0xabc123', [{ address: '0xabc123' }]),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({
|
|
|
|
to: null,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-01-23 19:09:56 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should return a known address recipient error if "to" is part of contract metadata', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
getToWarningObject(
|
|
|
|
'0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359',
|
|
|
|
[{ address: '0xabc123' }],
|
|
|
|
{ address: '0xabc123' },
|
|
|
|
),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({
|
|
|
|
to: KNOWN_RECIPIENT_ADDRESS_ERROR,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should null if "to" is part of contract metadata but sendToken is falsy', () => {
|
|
|
|
expect(
|
2020-11-03 00:41:28 +01:00
|
|
|
getToWarningObject(
|
|
|
|
'0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359',
|
|
|
|
[{ address: '0xabc123' }],
|
|
|
|
{ address: '0xabc123' },
|
|
|
|
),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual({
|
|
|
|
to: KNOWN_RECIPIENT_ADDRESS_ERROR,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-02-27 06:26:04 +01:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should warn if name is a valid domain and confusable', () => {
|
|
|
|
expect(getToWarningObject('demo.eth')).toStrictEqual({
|
2021-02-27 06:26:04 +01:00
|
|
|
to: CONFUSING_ENS_ERROR,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('should not warn if name is a valid domain and not confusable', () => {
|
|
|
|
expect(getToWarningObject('vitalik.eth')).toStrictEqual({
|
2021-02-27 06:26:04 +01:00
|
|
|
to: null,
|
|
|
|
});
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|