diff options
-rw-r--r-- | packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts b/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts new file mode 100644 index 0000000..c372559 --- /dev/null +++ b/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts @@ -0,0 +1,75 @@ +import { assertEquals } from 'https://deno.land/std@0.198.0/assert/mod.ts'; +import { returnsNext, stub } from 'https://deno.land/std@0.198.0/testing/mock.ts'; + +import { _getWebCryptoInternals, getWebCrypto } from './getWebCrypto.ts'; + +Deno.test('Should return globalThis.crypto when present', async () => { + // Back up globalThis.crypto + const originalCrypto = globalThis.crypto; + + // Overwrite globalThis.crypto + const newCrypto = {}; + Object.defineProperty(globalThis, 'crypto', { value: newCrypto, writable: true }); + + const returnedCrypto = await getWebCrypto(); + + assertEquals(returnedCrypto, newCrypto); + + // Restore globalThis.crypto + Object.defineProperty(globalThis, 'crypto', { value: originalCrypto, writable: true }); +}); + +Deno.test('Should return node:crypto.webcrypto when globalThis.crypto is missing', async () => { + // Mock out just enough of the 'node:crypto' module + const fakeNodeCrypto = { webcrypto: {} }; + const mockDecodeClientData = stub( + _getWebCryptoInternals, + 'stubThisImportNodeCrypto', + // @ts-ignore: Pretending to return something from Node + returnsNext([fakeNodeCrypto]), + ); + + // Back up globalThis.crypto + const originalCrypto = globalThis.crypto; + + // Overwrite globalThis.crypto + const newCrypto = undefined; + Object.defineProperty(globalThis, 'crypto', { value: newCrypto, writable: true }); + + const returnedCrypto = await getWebCrypto(); + + assertEquals(returnedCrypto, fakeNodeCrypto.webcrypto); + + // Restore globalThis.crypto + Object.defineProperty(globalThis, 'crypto', { value: originalCrypto, writable: true }); + mockDecodeClientData.restore(); +}); + +Deno.test( + 'Should return globalThis.crypto when present, while node:crypto is present but missing webcrypto', + async () => { + // Mock out just enough of the 'node:crypto' module, but like we're in Node v14 + const fakeNodeCrypto = { webcrypto: undefined }; + const mockDecodeClientData = stub( + _getWebCryptoInternals, + 'stubThisImportNodeCrypto', + // @ts-ignore: Pretending to return something from Node + returnsNext([fakeNodeCrypto]), + ); + + // Back up globalThis.crypto + const originalCrypto = globalThis.crypto; + + // Overwrite globalThis.crypto + const fakeGlobalCrypto = {}; + Object.defineProperty(globalThis, 'crypto', { value: fakeGlobalCrypto, writable: true }); + + const returnedCrypto = await getWebCrypto(); + + assertEquals(returnedCrypto, fakeGlobalCrypto); + + // Restore globalThis.crypto + Object.defineProperty(globalThis, 'crypto', { value: originalCrypto, writable: true }); + mockDecodeClientData.restore(); + }, +); |