summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.test.ts
blob: 24b37aa8958844a26ac497ebf587f154e3bf2c3e (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { assertEquals, assertRejects } 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, MissingWebCrypto } from './getWebCrypto.ts';

Deno.test('should return globalThis.crypto when present', async () => {
  // Clear whatever version of crypto might have been set
  _getWebCryptoInternals.setCachedCrypto(undefined);

  // Pretend globalThis.crypto exists
  const newGlobalThisCrypto = {};
  const mockGlobalThisCrypto = stub(
    _getWebCryptoInternals,
    'stubThisGlobalThisCrypto',
    // @ts-ignore: globalThis.crypto
    returnsNext([newGlobalThisCrypto]),
  );

  const returnedCrypto = await getWebCrypto();

  assertEquals(returnedCrypto, newGlobalThisCrypto);

  mockGlobalThisCrypto.restore();
});

Deno.test('should return node:crypto.webcrypto when globalThis.crypto is missing', async () => {
  // Clear whatever version of crypto might have been set
  _getWebCryptoInternals.setCachedCrypto(undefined);

  // Pretend globalThis.crypto doesn't exist
  const mockGlobalThisCrypto = stub(
    _getWebCryptoInternals,
    'stubThisGlobalThisCrypto',
    // @ts-ignore: globalThis.crypto
    returnsNext([undefined]),
  );

  // Mock out just enough of the 'node:crypto' module
  const fakeNodeCrypto = { webcrypto: {} };
  const mockImportNodeCrypto = stub(
    _getWebCryptoInternals,
    'stubThisImportNodeCrypto',
    // @ts-ignore: node:crypto
    returnsNext([Promise.resolve(fakeNodeCrypto)]),
  );

  const returnedCrypto = await getWebCrypto();

  assertEquals(returnedCrypto, fakeNodeCrypto.webcrypto);

  mockGlobalThisCrypto.restore();
  mockImportNodeCrypto.restore();
});

Deno.test(
  'should return globalThis.crypto when present, while node:crypto.webcrypto is present',
  async () => {
    // Clear whatever version of crypto might have been set
    _getWebCryptoInternals.setCachedCrypto(undefined);

    // Pretend globalThis.crypto exists
    const fakeGlobalThisCrypto = {};
    const mockGlobalThisCrypto = stub(
      _getWebCryptoInternals,
      'stubThisGlobalThisCrypto',
      // @ts-ignore: globalThis.crypto
      returnsNext([fakeGlobalThisCrypto]),
    );

    // Mock out just enough of the 'node:crypto' module, but like we're in Node v14
    const fakeNodeCrypto = { webcrypto: {} };
    const mockImportNodeCrypto = stub(
      _getWebCryptoInternals,
      'stubThisImportNodeCrypto',
      // @ts-ignore: node:crypto
      returnsNext([Promise.resolve(fakeNodeCrypto)]),
    );

    const returnedCrypto = await getWebCrypto();

    assertEquals(returnedCrypto, fakeGlobalThisCrypto);

    mockGlobalThisCrypto.restore();
    mockImportNodeCrypto.restore();
  },
);

Deno.test(
  'should return globalThis.crypto when present, while node:crypto is present but missing webcrypto',
  async () => {
    // Clear whatever version of crypto might have been set
    _getWebCryptoInternals.setCachedCrypto(undefined);

    // Pretend globalThis.crypto exists
    const fakeGlobalThisCrypto = {};
    const mockGlobalThisCrypto = stub(
      _getWebCryptoInternals,
      'stubThisGlobalThisCrypto',
      // @ts-ignore: globalThis.crypto
      returnsNext([fakeGlobalThisCrypto]),
    );

    // Mock out just enough of the 'node:crypto' module, but like we're in Node v14
    const fakeNodeCrypto = { webcrypto: undefined };
    const mockImportNodeCrypto = stub(
      _getWebCryptoInternals,
      'stubThisImportNodeCrypto',
      // @ts-ignore: node:crypto
      returnsNext([Promise.resolve(fakeNodeCrypto)]),
    );

    const returnedCrypto = await getWebCrypto();

    assertEquals(returnedCrypto, fakeGlobalThisCrypto);

    mockGlobalThisCrypto.restore();
    mockImportNodeCrypto.restore();
  },
);

Deno.test('should raise MissingWebCrypto error when nothing is available', async () => {
  // Clear whatever version of crypto might have been set
  _getWebCryptoInternals.setCachedCrypto(undefined);

  // Pretend globalThis.crypto doesn't exist
  const mockGlobalThisCrypto = stub(
    _getWebCryptoInternals,
    'stubThisGlobalThisCrypto',
    // @ts-ignore: globalThis.crypto
    returnsNext([undefined]),
  );

  // Pretend node:crypto doesn't exist
  const mockImportNodeCrypto = stub(
    _getWebCryptoInternals,
    'stubThisImportNodeCrypto',
    // @ts-ignore: node:crypto
    returnsNext([Promise.resolve({ webcrypto: undefined })]),
  );

  await assertRejects(
    () => getWebCrypto(),
    MissingWebCrypto,
    'Crypto API could not be located',
  );

  mockGlobalThisCrypto.restore();
  mockImportNodeCrypto.restore();
});