-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit test for get timestamp generated token
- Loading branch information
Showing
2 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
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,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() | ||
}) |
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