From 21b519424bc4d1b41a280882bc669b56a6933f51 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 2 Oct 2024 22:22:08 +0100 Subject: [PATCH] test: unit test for get timestamp generated token --- .../getSignedTimestamp/__tests__/index.ts | 43 +++++++++++++++++++ src/utils/getSignedTimestamp/index.ts | 4 +- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/utils/getSignedTimestamp/__tests__/index.ts diff --git a/src/utils/getSignedTimestamp/__tests__/index.ts b/src/utils/getSignedTimestamp/__tests__/index.ts new file mode 100644 index 000000000..b2f3965d8 --- /dev/null +++ b/src/utils/getSignedTimestamp/__tests__/index.ts @@ -0,0 +1,43 @@ +import { Buffer } from 'buffer' +import sphinx from 'sphinx-bridge' +import { bytesFromUrlBase64, getSignedTimestamp } from '..' + +// Returns a valid signed timestamp when sphinx.signMessage is successful +it('should return a valid signed timestamp when sphinx.signMessage is successful', async () => { + const mockSignature = 'mockSignature' + const mockResponse = { signature: mockSignature } + jest.spyOn(sphinx, 'signMessage').mockResolvedValue(mockResponse) + + const result = await getSignedTimestamp() + + expect(result).toBeDefined() + expect(result).not.toBe('') +}) + +// Handles errors from sphinx.signMessage gracefully +it('should return an empty string when sphinx.signMessage throws an error', async () => { + jest.spyOn(sphinx, 'signMessage').mockRejectedValue(new Error('signMessage error')) + + const result = await getSignedTimestamp() + + expect(result).toBe('') +}) + +// Correctly converts current time to hexadecimal and then to bytes +it('should convert current time to bytes correctly', async () => { + const currentTimeInSeconds = Math.floor(Date.now() / 1000) + const timeInBytes = Buffer.from(currentTimeInSeconds.toString(16), 'hex') + + expect(timeInBytes).toBeDefined() +}) + +// Properly concatenates time bytes and signed bytes +it('should concatenate time and signed bytes properly', async () => { + const currentTimeInSeconds = Math.floor(Date.now() / 1000) + const timeInBytes = Buffer.from(currentTimeInSeconds.toString(16), 'hex') + const res = { signature: 'mockedSignature' } + const signedByte = bytesFromUrlBase64(res.signature) + const concatenatedBytes = Buffer.concat([timeInBytes, signedByte], signedByte.length + timeInBytes.length) + + expect(concatenatedBytes).toBeDefined() +}) diff --git a/src/utils/getSignedTimestamp/index.ts b/src/utils/getSignedTimestamp/index.ts index 24f96ef5e..3a6fa9f3e 100644 --- a/src/utils/getSignedTimestamp/index.ts +++ b/src/utils/getSignedTimestamp/index.ts @@ -29,7 +29,7 @@ export async function getSignedTimestamp(): Promise { } } -function bytesFromUrlBase64(base64Url: string): Buffer { +export function bytesFromUrlBase64(base64Url: string): Buffer { // Revert the URL-safe changes back to regular Base64 const base64 = base64Url.replace(/_/g, '/').replace(/-/g, '+') @@ -40,7 +40,7 @@ function bytesFromUrlBase64(base64Url: string): Buffer { return Buffer.from(paddedBase64, 'base64') } -function urlBase64(buf: Uint8Array): string { +export function urlBase64(buf: Uint8Array): string { // Convert Uint8Array to a string let binary = '' const len = buf.byteLength