-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Storage.prototype.remove and Storage.prototype.removeAll to hand…
…le namespace correctly (#32) * Fix Storage.prototype.remove to handle namespaces. And add coverage for basic CRUD operations. * Add remove to SecureStorage and test for CRUD ops * reverting accidental change in package.json * Use mts instead for typing --------- Co-authored-by: L❤️ ☮️ ✋ <[email protected]>
- Loading branch information
Showing
7 changed files
with
323 additions
and
18 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
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,67 @@ | ||
import { jest } from "@jest/globals" | ||
|
||
/** | ||
* Mimic the webcrypto API without implementing the actual encryption | ||
* algorithms. Only the mock implementations used by the SecureStorage | ||
*/ | ||
export const cryptoMock = { | ||
subtle: { | ||
importKey: jest.fn(), | ||
deriveKey: jest.fn(), | ||
decrypt: jest.fn(), | ||
encrypt: jest.fn(), | ||
digest: jest.fn() | ||
}, | ||
getRandomValues: jest.fn() | ||
} | ||
|
||
cryptoMock.subtle.importKey.mockImplementation( | ||
(format, keyData, algorithm, extractable, keyUsages) => { | ||
return Promise.resolve({ | ||
format, | ||
keyData, | ||
algorithm, | ||
extractable, | ||
keyUsages | ||
}) | ||
} | ||
) | ||
|
||
cryptoMock.subtle.deriveKey.mockImplementation( | ||
(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages) => { | ||
return Promise.resolve({ | ||
algorithm, | ||
baseKey, | ||
derivedKeyAlgorithm, | ||
extractable, | ||
keyUsages | ||
}) | ||
} | ||
) | ||
|
||
cryptoMock.subtle.decrypt.mockImplementation((_, __, data: ArrayBufferLike) => { | ||
return Promise.resolve(new Uint8Array(data)) | ||
}) | ||
|
||
cryptoMock.subtle.encrypt.mockImplementation((_, __, data: ArrayBufferLike) => { | ||
return Promise.resolve(new Uint8Array(data)) | ||
}) | ||
|
||
cryptoMock.subtle.digest.mockImplementation((_, __) => { | ||
return Promise.resolve(new Uint8Array([0x01, 0x02, 0x03, 0x04])) | ||
}) | ||
|
||
cryptoMock.getRandomValues.mockImplementation((array: Array<any>) => { | ||
for (let i = 0; i < array.length; i++) { | ||
array[i] = Math.floor(Math.random() * 256) | ||
} | ||
return array | ||
}) | ||
|
||
// The globalThis does not define crypto by default | ||
Object.defineProperty(globalThis, "crypto", { | ||
value: cryptoMock, | ||
writable: true, | ||
enumerable: true, | ||
configurable: true | ||
}) |
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
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
Oops, something went wrong.