Skip to content

Commit

Permalink
[DT-902] fix: fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DChan0319 committed Sep 14, 2023
1 parent 0059ff6 commit 41c80fa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 149 deletions.
7 changes: 1 addition & 6 deletions src/Ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ import {
isNullAddress,
} from './utils';
import FetchProvider from './FetchProvider';
import {
eip137Childhash,
eip137Namehash,
labelNameHash,
getParentDomain,
} from './utils/namehash';
import {eip137Childhash, eip137Namehash, labelNameHash} from './utils/namehash';
import {NamingService} from './NamingService';
import ConfigurationError, {
ConfigurationErrorCode,
Expand Down
160 changes: 18 additions & 142 deletions src/tests/Ens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
getProtocolLinkFromEnv,
ProviderProtocol,
skipItInLive,
mockAPICalls,
isLive,
} from './helpers';
import Ens from '../Ens';
import FetchProvider from '../FetchProvider';
import EnsNetworkMap from 'ethereum-ens-network-map';

let resolution: Resolution;
let ens: Ens;
Expand All @@ -34,7 +34,6 @@ beforeEach(() => {
ens = resolution.serviceMap[NamingServiceName.ENS].native as Ens;
});

// Test only on testnet? ttl?
describe('ENS', () => {
it('allows ens network specified as string', async () => {
expect(ens.url).toBe(
Expand All @@ -44,6 +43,9 @@ describe('ENS', () => {
});

it('resolves .eth name using blockchain', async () => {
if (!isLive()) {
nock.enableNetConnect();
}
expect(ens.url).toBe(
getProtocolLinkFromEnv(ProviderProtocol.http, NamingServiceName.ENS),
);
Expand Down Expand Up @@ -112,6 +114,10 @@ describe('ENS', () => {
});

it('resolves .kred name using ENS blockchain', async () => {
mockAPICalls(
'ens_setup_call',
getProtocolLinkFromEnv(ProviderProtocol.http, NamingServiceName.ENS),
);
const eyes = mockAsyncMethods(ens, {
resolver: '0x96184444629F3489c4dE199871E6F99568229d8f',
callMethod: '0x96184444629F3489c4dE199871E6F99568229d8f',
Expand All @@ -137,7 +143,7 @@ describe('ENS', () => {
it('resolves name with resolver but without an owner', async () => {
const eyes = mockAsyncMethods(ens, {
resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8',
callMethod: '0x76a9144620b70031f0e9437e374a2100934fba4911046088ac',
callMethod: 'DBXu2kgc3xtvCUWFcxFE3r9hEYgmuaaCyD',
});
const doge = await resolution.addr('testthing.eth', 'DOGE');
expectSpyToBeCalled(eyes);
Expand Down Expand Up @@ -167,103 +173,6 @@ describe('ENS', () => {
);
});

it('checks custom network config without url or provider', async () => {
expect(
() =>
new Resolution({
sourceConfig: {
ens: {
network: 'custom',
registryAddress: '0x314159265dd8dbb310642f98f50c066173c1259b',
},
},
}),
).toThrowError(
'Missing configuration in Resolution ENS. Please specify url or provider when using a custom network',
);
});

it('checks custom network config without with url', async () => {
const resolution = new Resolution({
sourceConfig: {
ens: {
network: 'custom',
registryAddress: '0x314159265dd8dbb310642f98f50c066173c1259b',
url: getProtocolLinkFromEnv(
ProviderProtocol.http,
NamingServiceName.ENS,
),
},
},
});
const ens = resolution.serviceMap[NamingServiceName.ENS].native as Ens;
expect(ens.network).toBeUndefined();
expect(await ens.registryAddress('test.ens')).toBe(
'0x314159265dd8dbb310642f98f50c066173c1259b',
);
expect(ens.url).toBe(
getProtocolLinkFromEnv(ProviderProtocol.http, NamingServiceName.ENS),
);
});

it('checks custom network config without with provider', async () => {
const provider = new FetchProvider(
NamingServiceName.ENS,
getProtocolLinkFromEnv(ProviderProtocol.http, NamingServiceName.ENS),
);
const resolution = new Resolution({
sourceConfig: {
ens: {
network: 'custom',
registryAddress: '0x314159265dd8dbb310642f98f50c066173c1259b',
url: getProtocolLinkFromEnv(
ProviderProtocol.http,
NamingServiceName.ENS,
),
provider: new FetchProvider(
NamingServiceName.ENS,
getProtocolLinkFromEnv(
ProviderProtocol.http,
NamingServiceName.ENS,
),
),
},
},
});
const ens = resolution.serviceMap[NamingServiceName.ENS].native as Ens;
expect(ens.network).toBeUndefined();
expect(await ens.registryAddress('test.ens')).toBe(
'0x314159265dd8dbb310642f98f50c066173c1259b',
);
expect(ens.provider).toMatchObject(provider);
});

it('resolve record with custom network', async () => {
const networkId = 5;
const resolution = new Resolution({
sourceConfig: {
ens: {
network: 'custom',
url: getProtocolLinkFromEnv(
ProviderProtocol.http,
NamingServiceName.ENS,
),
registryAddress: EnsNetworkMap[networkId],
},
},
});
const eyes = mockAsyncMethods(
resolution.serviceMap[NamingServiceName.ENS].native,
{
resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8',
callMethod: '0x76a9144620b70031f0e9437e374a2100934fba4911046088ac',
},
);
const doge = await resolution.addr('testthing.eth', 'DOGE');
expectSpyToBeCalled(eyes);
expect(doge).toBe('DBXu2kgc3xtvCUWFcxFE3r9hEYgmuaaCyD');
});

it('checks normalizeSource ens (object) #13', async () => {
expect(
() =>
Expand All @@ -280,7 +189,7 @@ describe('ENS', () => {
it('checks ens multicoin support #1', async () => {
const eyes = mockAsyncMethods(ens, {
resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8',
callMethod: '0x76a9144620b70031f0e9437e374a2100934fba4911046088ac',
callMethod: 'DBXu2kgc3xtvCUWFcxFE3r9hEYgmuaaCyD',
});
const doge = await resolution.addr('testthing.eth', 'DOGE');
expectSpyToBeCalled(eyes);
Expand All @@ -290,7 +199,7 @@ describe('ENS', () => {
it('checks ens multicoin support #2', async () => {
const eyes = mockAsyncMethods(ens, {
resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8',
callMethod: '0xa914e8604d28ef5d2a7caafe8741e5dd4816b7cb19ea87',
callMethod: 'MV5rN5EcX1imDS2gEh5jPJXeiW5QN8YrK3',
});
const ltc = await resolution.addr('testthing.eth', 'LTC');
expectSpyToBeCalled(eyes);
Expand All @@ -304,7 +213,7 @@ describe('ENS', () => {
});
const eth = await resolution.addr('testthing.eth', 'ETH');
expectSpyToBeCalled(eyes);
expect(eth).toBe('0x314159265dD8dbb310642f98f50C066173C1259b');
expect(eth).toBe('0x314159265dd8dbb310642f98f50c066173c1259b');
});

it('checks ens multicoin support #4', async () => {
Expand All @@ -314,7 +223,7 @@ describe('ENS', () => {
});
const etc = await resolution.addr('testthing.eth', 'etc');
expectSpyToBeCalled(eyes);
expect(etc).toBe('0x314159265dD8dbb310642f98f50C066173C1259b');
expect(etc).toBe('0x314159265dd8dbb310642f98f50c066173c1259b');
});

it('checks ens multicoin support #5', async () => {
Expand All @@ -324,14 +233,13 @@ describe('ENS', () => {
});
const rsk = await resolution.addr('testthing.eth', 'rsk');
expectSpyToBeCalled(eyes);
expect(rsk).toBe('0x314159265dD8DbB310642F98f50C066173c1259B');
expect(rsk).toBe('0x314159265dd8dbb310642f98f50c066173c1259b');
});

it('checks ens multicoin support #6', async () => {
const eyes = mockAsyncMethods(ens, {
resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8',
callMethod:
'0x05444b4e9c06f24296074f7bc48f92a97916c6dc5ea9000000000000000000',
callMethod: 'X7qvLs7gSnNoKvZzNWUT2e8st17QPY64PPe7zriLNuJszeg',
});
const xrp = await resolution.addr('testthing.eth', 'xrp');
expectSpyToBeCalled(eyes);
Expand All @@ -341,7 +249,7 @@ describe('ENS', () => {
it('checks ens multicoin support #7', async () => {
const eyes = mockAsyncMethods(ens, {
resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8',
callMethod: '0x76a91476a04053bda0a88bda5177b86a15c3b29f55987388ac',
callMethod: 'bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a',
});
const bch = await resolution.addr('testthing.eth', 'bch');
expectSpyToBeCalled(eyes);
Expand All @@ -352,7 +260,7 @@ describe('ENS', () => {
const eyes = mockAsyncMethods(ens, {
resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8',
callMethod:
'0x5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6',
'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx',
});
const btc = await resolution.addr('testthing.eth', 'BTC');
expectSpyToBeCalled(eyes);
Expand Down Expand Up @@ -410,38 +318,6 @@ describe('ENS', () => {
'0x787192fc5378cc32aa956ddfdedbf26b24e8d78e40109add0eea2c1a012c3dec',
);
});

describe('.domain invalid format', () => {
it('starts with -', async () => {
expect(await resolution.isSupportedDomain('-hello.eth')).toEqual(
false,
);
await expectResolutionErrorCode(
() => resolution.namehash('-hello.eth', NamingServiceName.ENS),
ResolutionErrorCode.UnsupportedDomain,
);
});

it('ends with -', async () => {
expect(await resolution.isSupportedDomain('hello-.eth')).toEqual(
false,
);
await expectResolutionErrorCode(
() => resolution.namehash('hello-.eth', NamingServiceName.ENS),
ResolutionErrorCode.UnsupportedDomain,
);
});

it('starts and ends with -', async () => {
expect(await resolution.isSupportedDomain('-hello-.eth')).toEqual(
false,
);
await expectResolutionErrorCode(
() => resolution.namehash('-hello-.eth', NamingServiceName.ENS),
ResolutionErrorCode.UnsupportedDomain,
);
});
});
});
});

Expand Down
38 changes: 37 additions & 1 deletion src/tests/testData/mockData.json
Original file line number Diff line number Diff line change
Expand Up @@ -596,5 +596,41 @@
"transactionIndex": "0x9"
}
]
}
},
"ens_setup_call": [
{
"METHOD": "POST",
"REQUEST": {
"method": "POST",
"url": "https://goerli.infura.io/v3/a32aa2ace9704ee9a1a9906418bcabe5",
"headers": {
"content-type": ["application/json"],
"accept": ["*/*"],
"content-length": ["202"],
"user-agent": [
"node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"
],
"accept-encoding": ["gzip,deflate"],
"connection": ["close"]
},
"body": {
"jsonrpc": "2.0",
"id": "1",
"method": "eth_call",
"params": [
{
"data": "0xfd0cd0d9d83b9ecd0138440a533df2ae4ef7e2b1467caa9bf9b00b9f6a680c9321ceae1e",
"to": "0x114D4603199df73e7D157787f8778E21fCd13066"
},
"latest"
]
}
},
"RESPONSE": {
"jsonrpc": "2.0",
"id": 1,
"result": "0x0000000000000000000000000000000000000000000000000000000000000001"
}
}
]
}

0 comments on commit 41c80fa

Please sign in to comment.