1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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();
},
);
|