Skip to content

Commit

Permalink
test: unit test for get timestamp generated token
Browse files Browse the repository at this point in the history
  • Loading branch information
tobi-bams committed Oct 2, 2024
1 parent 04f6838 commit 21b5194
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions src/utils/getSignedTimestamp/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 9 in src/utils/getSignedTimestamp/__tests__/index.ts

View workflow job for this annotation

GitHub Actions / eslint-run

Expected blank line before this statement

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()
})
4 changes: 2 additions & 2 deletions src/utils/getSignedTimestamp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function getSignedTimestamp(): Promise<string> {
}
}

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, '+')

Expand All @@ -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
Expand Down

0 comments on commit 21b5194

Please sign in to comment.