-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for api-requests modules (#44)
- Loading branch information
Showing
11 changed files
with
426 additions
and
43 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/data-pusher/src/api-requests/data-provider.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { api as nodeApiModule } from '@api3/airnode-node'; | ||
import { makeTemplateRequests } from './data-provider'; | ||
import * as stateModule from '../state'; | ||
import * as loggerModule from '../logger'; | ||
import { | ||
config, | ||
createMockedLogger, | ||
nodaryTemplateRequestErrorResponse, | ||
nodaryTemplateRequestResponseData, | ||
nodaryTemplateResponses, | ||
} from '../../test/fixtures'; | ||
|
||
describe(makeTemplateRequests.name, () => { | ||
it('makes a single template request for multiple beacons', async () => { | ||
const state = stateModule.getInitialState(config); | ||
jest.spyOn(stateModule, 'getState').mockReturnValue(state); | ||
const logger = createMockedLogger(); | ||
jest.spyOn(loggerModule, 'getLogger').mockReturnValue(logger); | ||
jest.spyOn(nodeApiModule, 'performApiCall').mockResolvedValue([[], nodaryTemplateRequestResponseData]); | ||
|
||
const response = await makeTemplateRequests(config.triggers.signedApiUpdates[0]!); | ||
|
||
expect(response).toEqual(nodaryTemplateResponses); | ||
}); | ||
|
||
it('handles request failure', async () => { | ||
const state = stateModule.getInitialState(config); | ||
jest.spyOn(stateModule, 'getState').mockReturnValue(state); | ||
const logger = createMockedLogger(); | ||
jest.spyOn(loggerModule, 'getLogger').mockReturnValue(logger); | ||
jest.spyOn(nodeApiModule, 'performApiCall').mockRejectedValue(nodaryTemplateRequestErrorResponse); | ||
|
||
await expect(makeTemplateRequests(config.triggers.signedApiUpdates[0]!)).rejects.toEqual({ | ||
errorMessage: 'Invalid API key', | ||
success: false, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/data-pusher/src/api-requests/signed-api.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import axios from 'axios'; | ||
import { ZodError } from 'zod'; | ||
import { postSignedApiData, signTemplateResponses } from './signed-api'; | ||
import { | ||
config, | ||
createMockedLogger, | ||
signedApiResponse, | ||
nodarySignedTemplateResponses, | ||
nodaryTemplateResponses, | ||
} from '../../test/fixtures'; | ||
import * as loggerModule from '../logger'; | ||
import * as stateModule from '../state'; | ||
|
||
describe(signTemplateResponses.name, () => { | ||
it('signs template responses', async () => { | ||
const state = stateModule.getInitialState(config); | ||
jest.spyOn(stateModule, 'getState').mockReturnValue(state); | ||
const logger = createMockedLogger(); | ||
jest.spyOn(loggerModule, 'getLogger').mockReturnValue(logger); | ||
jest.useFakeTimers().setSystemTime(new Date('2023-01-20')); | ||
|
||
const signedTemplateResponses = await signTemplateResponses(nodaryTemplateResponses); | ||
|
||
expect(signedTemplateResponses).toEqual(nodarySignedTemplateResponses); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
}); | ||
|
||
describe(postSignedApiData.name, () => { | ||
it('posts data to central api', async () => { | ||
const state = stateModule.getInitialState(config); | ||
// Assumes the template responses are for unique template IDs (which is true in the test fixtures). | ||
state.templateValues = Object.fromEntries( | ||
nodarySignedTemplateResponses.map(([templateId, signedData]) => { | ||
const dataQueue = new stateModule.DelayedSignedDataQueue(); | ||
dataQueue.put(signedData); | ||
return [templateId, dataQueue]; | ||
}) | ||
); | ||
jest.spyOn(stateModule, 'getState').mockReturnValue(state); | ||
const logger = createMockedLogger(); | ||
jest.spyOn(loggerModule, 'getLogger').mockReturnValue(logger); | ||
jest.spyOn(axios, 'post').mockResolvedValue(signedApiResponse); | ||
|
||
const response = await postSignedApiData(config.triggers.signedApiUpdates[0]!); | ||
|
||
expect(response).toEqual({ count: 3, success: true }); | ||
}); | ||
|
||
it('handles invalid response from signed API', async () => { | ||
const state = stateModule.getInitialState(config); | ||
// Assumes the template responses are for unique template IDs (which is true in the test fixtures). | ||
state.templateValues = Object.fromEntries( | ||
nodarySignedTemplateResponses.map(([templateId, signedData]) => { | ||
const dataQueue = new stateModule.DelayedSignedDataQueue(); | ||
dataQueue.put(signedData); | ||
return [templateId, dataQueue]; | ||
}) | ||
); | ||
jest.spyOn(stateModule, 'getState').mockReturnValue(state); | ||
const logger = createMockedLogger(); | ||
jest.spyOn(loggerModule, 'getLogger').mockReturnValue(logger); | ||
jest.spyOn(axios, 'post').mockResolvedValue({ youHaveNotThoughAboutThisDidYou: 'yes-I-did' }); | ||
|
||
const response = await postSignedApiData(config.triggers.signedApiUpdates[0]!); | ||
|
||
expect(response).toEqual({ success: false }); | ||
expect(logger.warn).toHaveBeenCalledWith('Failed to parse response from the signed API.', { | ||
errors: new ZodError([ | ||
{ | ||
code: 'invalid_type', | ||
expected: 'object', | ||
received: 'undefined', | ||
path: [], | ||
message: 'Required', | ||
}, | ||
]), | ||
signedApiName: 'localhost', | ||
updateDelay: 5, | ||
}); | ||
}); | ||
|
||
it('handles request failure', async () => { | ||
const state = stateModule.getInitialState(config); | ||
// Assumes the template responses are for unique template IDs (which is true in the test fixtures). | ||
state.templateValues = Object.fromEntries( | ||
nodarySignedTemplateResponses.map(([templateId, signedData]) => { | ||
const dataQueue = new stateModule.DelayedSignedDataQueue(); | ||
dataQueue.put(signedData); | ||
return [templateId, dataQueue]; | ||
}) | ||
); | ||
jest.spyOn(stateModule, 'getState').mockReturnValue(state); | ||
const logger = createMockedLogger(); | ||
jest.spyOn(loggerModule, 'getLogger').mockReturnValue(logger); | ||
jest.spyOn(axios, 'post').mockRejectedValue('simulated-network-error'); | ||
|
||
const response = await postSignedApiData(config.triggers.signedApiUpdates[0]!); | ||
|
||
expect(response).toEqual({ success: false }); | ||
expect(logger.warn).toHaveBeenCalledWith('Failed to make update signed API request.', { | ||
axiosResponse: undefined, | ||
signedApiName: 'localhost', | ||
updateDelay: 5, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.