|
| 1 | +import { |
| 2 | + TestAdapter, |
| 3 | + setEnvVariables, |
| 4 | +} from '@chainlink/external-adapter-framework/util/testing-utils' |
| 5 | +import * as nock from 'nock' |
| 6 | + |
| 7 | +import { |
| 8 | + mockHappyPathResponseSuccessAsset, |
| 9 | + mockResponseApiFailureAsset, |
| 10 | + mockResponseFailureAsset, |
| 11 | +} from './utils/fixtures' |
| 12 | +import { TEST_FAILURE_ASSET_ID, TEST_SUCCESS_ASSET_ID, TEST_URL } from './utils/testConfig' |
| 13 | +import { clearTestCache } from './utils/utilFunctions' |
| 14 | + |
| 15 | +describe('LiveArt NAV', () => { |
| 16 | + let testAdapter: TestAdapter |
| 17 | + let spy: jest.SpyInstance |
| 18 | + let oldEnv: NodeJS.ProcessEnv |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + oldEnv = JSON.parse(JSON.stringify(process.env)) |
| 22 | + // Mock time for request's timestamp |
| 23 | + const mockDate = new Date('2001-01-01T11:11:11.111Z') |
| 24 | + spy = jest.spyOn(Date, 'now').mockReturnValue(mockDate.getTime()) |
| 25 | + // Set environment variables |
| 26 | + process.env.API_BASE_URL = TEST_URL |
| 27 | + process.env.BACKGROUND_EXECUTE_MS = '0' |
| 28 | + |
| 29 | + // Create adapter instance only once |
| 30 | + const adapter = (await import('../../src')).adapter |
| 31 | + adapter.rateLimiting = undefined |
| 32 | + testAdapter = await TestAdapter.startWithMockedCache(adapter, { |
| 33 | + testAdapter: {} as TestAdapter<never>, |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + afterAll(async () => { |
| 38 | + clearTestCache(testAdapter) |
| 39 | + await testAdapter.api.close() |
| 40 | + spy.mockRestore() |
| 41 | + setEnvVariables(oldEnv) |
| 42 | + nock.restore() |
| 43 | + nock.cleanAll() |
| 44 | + }) |
| 45 | + |
| 46 | + describe('endpoints', () => { |
| 47 | + describe('nav', () => { |
| 48 | + it('should return success for valid assetId', async () => { |
| 49 | + const dataInput = { |
| 50 | + assetId: TEST_SUCCESS_ASSET_ID, |
| 51 | + endpoint: 'nav', |
| 52 | + } |
| 53 | + |
| 54 | + mockHappyPathResponseSuccessAsset(dataInput.assetId) |
| 55 | + const response = await testAdapter.request(dataInput) |
| 56 | + |
| 57 | + expect(response.statusCode).toBe(200) |
| 58 | + expect(response.json()).toMatchSnapshot() |
| 59 | + }) |
| 60 | + |
| 61 | + it('should return error for invalid assetId', async () => { |
| 62 | + const data = { |
| 63 | + assetId: 'invalid-asset-id', |
| 64 | + endpoint: 'nav', |
| 65 | + } |
| 66 | + |
| 67 | + // Mock for other assetId |
| 68 | + mockHappyPathResponseSuccessAsset(data.assetId) |
| 69 | + |
| 70 | + const response = await testAdapter.request(data) |
| 71 | + expect(response.statusCode).toBe(502) |
| 72 | + expect(response.json()).toMatchSnapshot() |
| 73 | + }) |
| 74 | + |
| 75 | + it('should handle upstream bad response for unsuccessful request', async () => { |
| 76 | + const data = { |
| 77 | + assetId: TEST_FAILURE_ASSET_ID, |
| 78 | + endpoint: 'nav', |
| 79 | + } |
| 80 | + |
| 81 | + mockResponseFailureAsset(data.assetId) |
| 82 | + |
| 83 | + const response = await testAdapter.request(data) |
| 84 | + expect(response.statusCode).toBe(502) |
| 85 | + expect(response.json()).toMatchSnapshot() |
| 86 | + }) |
| 87 | + |
| 88 | + it('framework should handle API 422 bad response', async () => { |
| 89 | + const data = { |
| 90 | + assetId: 'abcd', |
| 91 | + endpoint: 'nav', |
| 92 | + } |
| 93 | + |
| 94 | + // prep cache |
| 95 | + await testAdapter.request(data) |
| 96 | + |
| 97 | + mockResponseApiFailureAsset() |
| 98 | + |
| 99 | + const response = await testAdapter.request(data) |
| 100 | + expect(response.statusCode).toBe(502) |
| 101 | + expect(response.json()).toMatchSnapshot() |
| 102 | + }) |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments