-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(amazon-cognito-identity-js): set crypto for Node (#7136)
* Set crypto for node * add crypto to webpack externals * add unit tests for crypto
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/amazon-cognito-identity-js/__tests__/cryptoSecureRandomInt-test.js
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,59 @@ | ||
const crypto = require('crypto'); | ||
|
||
describe('cryptoSecureRandomInt test', () => { | ||
let windowSpy; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
windowSpy = jest.spyOn(window, 'window', 'get'); | ||
}); | ||
|
||
afterEach(() => { | ||
windowSpy.mockRestore(); | ||
}); | ||
|
||
test('crypto is set for window (browser)', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
crypto: { | ||
getRandomValues: () => [12345], | ||
}, | ||
})); | ||
|
||
const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt') | ||
.default; | ||
expect(window.crypto).toBeTruthy(); | ||
expect(cryptoSecureRandomInt()).toBe(12345); | ||
}); | ||
|
||
test('crypto is set for window (IE 11)', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
crypto: undefined, | ||
msCrypto: { | ||
getRandomValues: () => [67890], | ||
}, | ||
})); | ||
|
||
const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt') | ||
.default; | ||
expect(window.msCrypto).toBeTruthy(); | ||
expect(cryptoSecureRandomInt()).toBe(67890); | ||
}); | ||
|
||
test('crypto is set for Node', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
crypto: null, | ||
})); | ||
|
||
const randomBytesMock = jest | ||
.spyOn(crypto, 'randomBytes') | ||
.mockImplementationOnce(() => ({ | ||
readInt32LE: jest.fn().mockReturnValueOnce(54321), | ||
})); | ||
|
||
const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt') | ||
.default; | ||
expect(cryptoSecureRandomInt()).toBe(54321); | ||
|
||
randomBytesMock.mockRestore(); | ||
}); | ||
}); |
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