originstamp-client-fetch
Version:
TypeScript Fetch client for the OriginStamp service
104 lines • 4.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const apis_1 = require("../apis");
const crypto_1 = require("crypto");
// Example notification object with required "target" field to satisfy type requirements
const exampleNotification = {
target: "https://example.com/webhook-target",
};
describe('WebhookApi', () => {
let api;
beforeEach(() => {
api = new apis_1.WebhookApi();
// Mock 'fetch' globally and clear mocks before each test for isolation
global.fetch = jest.fn();
jest.clearAllMocks();
});
function generateRandomSha256() {
const randomData = (0, crypto_1.randomBytes)(32);
return (0, crypto_1.createHash)('sha256').update(randomData).digest('hex');
}
it('should successfully return a proof webhook example', async () => {
const mockProofWebhook = {
// typical required fields for ProofWebhook (adjust if needed)
sha256: generateRandomSha256(),
tree: {
"treeId": "eada2ff6-dc26-4c50-ba47-b5865d1ca6bd",
"currencyId": 0,
"currency": "BTC",
"submitStatus": 2,
"timestamp": Date.now(),
"rootSha256": generateRandomSha256(),
"transaction": generateRandomSha256()
},
};
// Mock fetch to return a successful response with the proof webhook data
global.fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => mockProofWebhook,
});
// Call the API method with example notification
const result = await api.getProofWebhookExample({ notification: exampleNotification });
// Assert that the returned result matches the mocked proof webhook response
expect(result).toEqual(mockProofWebhook);
// Assert fetch was called with the correct URL, method, headers, and body
expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('/v1/webhook/proof'), expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({
'Content-Type': 'application/json',
}),
body: JSON.stringify(exampleNotification),
}));
});
it('should throw error on invalid proof webhook response', async () => {
// Mock fetch to return an invalid (empty) response
global.fetch.mockResolvedValue({
json: async () => ({
example: "test"
}),
});
// Expect the API call to reject due to invalid response structure
await expect(api.getProofWebhookExample({ notification: exampleNotification })).rejects.toThrow();
});
it('should successfully return a tree webhook example', async () => {
const mockTimestampData = {
treeId: 'tree123',
currencyId: 0,
currency: 'BTC',
submitStatus: 1,
"timestamp": Date.now(),
"rootSha256": generateRandomSha256(),
"transaction": generateRandomSha256()
};
// Mock fetch to return a successful response with timestamp data
global.fetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => mockTimestampData
});
// Call the API method with example notification
const result = await api.getTreeWebhookExample({ notification: exampleNotification });
// Assert that the returned result matches the mocked timestamp data
expect(result).toEqual(mockTimestampData);
// Assert fetch was called with the correct URL, method, headers, and body
expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('/v1/webhook/tree'), expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({
'Content-Type': 'application/json'
}),
body: JSON.stringify(exampleNotification)
}));
});
it('should throw error on invalid tree webhook response', async () => {
// Mock fetch to return an invalid (empty) response
global.fetch.mockResolvedValue({
json: async () => ({
example: "test"
}),
});
// Expect the API call to reject due to invalid response structure
await expect(api.getTreeWebhookExample({ notification: exampleNotification })).rejects.toThrow();
});
});
//# sourceMappingURL=test_webhook.test.js.map