|
| 1 | +/* eslint-disable no-magic-numbers */ |
| 2 | +/* eslint-disable camelcase */ |
| 3 | +import { Cardano, ProviderError } from '@cardano-sdk/core'; |
| 4 | +import { KoraLabsHandleProvider } from '../../src'; |
| 5 | +import { |
| 6 | + getAliceHandleAPIResponse, |
| 7 | + getAliceHandleProviderResponse, |
| 8 | + getBobHandleAPIResponse, |
| 9 | + getBobHandleProviderResponse |
| 10 | +} from '../util'; |
| 11 | +import MockAdapter from 'axios-mock-adapter'; |
| 12 | +import axios from 'axios'; |
| 13 | + |
| 14 | +const config = { |
| 15 | + policyId: Cardano.PolicyId('50fdcdbfa3154db86a87e4b5697ae30d272e0bbcfa8122efd3e301cb'), |
| 16 | + serverUrl: 'http://some-hostname:3000' |
| 17 | +}; |
| 18 | + |
| 19 | +describe('KoraLabsHandleProvider', () => { |
| 20 | + let axiosMock: MockAdapter; |
| 21 | + let provider: KoraLabsHandleProvider; |
| 22 | + |
| 23 | + beforeAll(() => { |
| 24 | + axiosMock = new MockAdapter(axios); |
| 25 | + provider = new KoraLabsHandleProvider(config); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + axiosMock.reset(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterAll(() => { |
| 33 | + axiosMock.restore(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('resolveHandles', () => { |
| 37 | + test('HandlesProvider should resolve a single handle', async () => { |
| 38 | + axiosMock.onGet().replyOnce(200, getAliceHandleAPIResponse); |
| 39 | + const args = { |
| 40 | + handles: ['alice'] |
| 41 | + }; |
| 42 | + await expect(provider.resolveHandles(args)).resolves.toEqual([getAliceHandleProviderResponse]); |
| 43 | + }); |
| 44 | + |
| 45 | + test('HandleProvider should resolve multiple handles', async () => { |
| 46 | + axiosMock.onGet().replyOnce(200, getAliceHandleAPIResponse).onGet().replyOnce(200, getBobHandleAPIResponse); |
| 47 | + const args = { |
| 48 | + handles: ['alice', 'bob'] |
| 49 | + }; |
| 50 | + await expect(provider.resolveHandles(args)).resolves.toEqual([ |
| 51 | + getAliceHandleProviderResponse, |
| 52 | + getBobHandleProviderResponse |
| 53 | + ]); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('error checks', () => { |
| 58 | + test('HandleProvider should throw ProviderError with ConnectionFailure on request error', async () => { |
| 59 | + axiosMock.onGet('/handles/alice').networkError(); |
| 60 | + const args = { handles: ['alice'] }; |
| 61 | + await expect(provider.resolveHandles(args)).rejects.toThrowError(ProviderError); |
| 62 | + }); |
| 63 | + test('HandleProvider should return null for 404 response from API', async () => { |
| 64 | + axiosMock.onGet('/handles/alice').reply(404); |
| 65 | + const args = { handles: ['alice'] }; |
| 66 | + await expect(provider.resolveHandles(args)).resolves.toEqual([null]); |
| 67 | + }); |
| 68 | + test('HandleProvider should throw ProviderError with Unhealthy on other Axios error', async () => { |
| 69 | + axiosMock.onGet('/handles/bob').reply(500); |
| 70 | + const args = { handles: ['bob'] }; |
| 71 | + await expect(provider.resolveHandles(args)).rejects.toThrowError(ProviderError); |
| 72 | + }); |
| 73 | + test('HandleProvider should throw ProviderError', async () => { |
| 74 | + axiosMock.onGet('/handles/bob').networkError(); |
| 75 | + const args = { handles: ['bob'] }; |
| 76 | + await expect(provider.resolveHandles(args)).rejects.toThrowError(ProviderError); |
| 77 | + }); |
| 78 | + test('HandleProvider should throw ProviderError with Unknown, unable to resolve handle', async () => { |
| 79 | + axiosMock.onGet().replyOnce(304, getAliceHandleAPIResponse); |
| 80 | + const args = { handles: ['bob'] }; |
| 81 | + await expect(provider.resolveHandles(args)).rejects.toThrowError(ProviderError); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('health checks', () => { |
| 86 | + test('HandleProvider should get ok health check', async () => { |
| 87 | + axiosMock.onGet().replyOnce(200, {}); |
| 88 | + const result = await provider.healthCheck(); |
| 89 | + expect(result.ok).toEqual(true); |
| 90 | + }); |
| 91 | + |
| 92 | + test('HandleProvider should get not ok health check', async () => { |
| 93 | + const providerWithBadConfig = new KoraLabsHandleProvider({ |
| 94 | + policyId: Cardano.PolicyId('50fdcdbfa3154db86a87e4b5697ae30d272e0bbcfa8122efd3e301cb'), |
| 95 | + serverUrl: '' |
| 96 | + }); |
| 97 | + const result = await providerWithBadConfig.healthCheck(); |
| 98 | + expect(result.ok).toEqual(false); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('get policy ids', () => { |
| 103 | + test('HandleProvider should get handle policy ids', async () => { |
| 104 | + const policyIds = await provider.getPolicyIds(); |
| 105 | + await expect(policyIds.length).toEqual(1); |
| 106 | + await expect(policyIds).toEqual([config.policyId]); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments