From 7c3fd6f8dc04f56221ed876be623189855040a62 Mon Sep 17 00:00:00 2001 From: DChan0319 Date: Wed, 13 Sep 2023 16:43:33 -0700 Subject: [PATCH] [DT-902] fix: fixing unit tests --- src/Ens.ts | 7 +- src/tests/Ens.test.ts | 161 ++++--------------------------- src/tests/testData/mockData.json | 38 +++++++- 3 files changed, 57 insertions(+), 149 deletions(-) diff --git a/src/Ens.ts b/src/Ens.ts index 48fc53fb..e8415b4c 100644 --- a/src/Ens.ts +++ b/src/Ens.ts @@ -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, diff --git a/src/tests/Ens.test.ts b/src/tests/Ens.test.ts index 70fcb5b0..d1303be0 100644 --- a/src/tests/Ens.test.ts +++ b/src/tests/Ens.test.ts @@ -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; @@ -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( @@ -44,6 +43,10 @@ describe('ENS', () => { }); it('resolves .eth name using blockchain', async () => { + console.log(isLive()); + if (!isLive()) { + nock.enableNetConnect(); + } expect(ens.url).toBe( getProtocolLinkFromEnv(ProviderProtocol.http, NamingServiceName.ENS), ); @@ -112,6 +115,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', @@ -137,7 +144,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); @@ -167,103 +174,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( () => @@ -280,7 +190,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); @@ -290,7 +200,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); @@ -304,7 +214,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 () => { @@ -314,7 +224,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 () => { @@ -324,14 +234,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); @@ -341,7 +250,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); @@ -352,7 +261,7 @@ describe('ENS', () => { const eyes = mockAsyncMethods(ens, { resolver: '0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8', callMethod: - '0x5128751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd6', + 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx', }); const btc = await resolution.addr('testthing.eth', 'BTC'); expectSpyToBeCalled(eyes); @@ -410,38 +319,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, - ); - }); - }); }); }); diff --git a/src/tests/testData/mockData.json b/src/tests/testData/mockData.json index 15f80d37..baa3a175 100644 --- a/src/tests/testData/mockData.json +++ b/src/tests/testData/mockData.json @@ -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" + } + } + ] }