Skip to content

Commit

Permalink
Test keyring vault encryption
Browse files Browse the repository at this point in the history
Introduce Jest as our test runner.
  • Loading branch information
mhluongo committed Sep 26, 2021
1 parent 947c5a8 commit 32ef8b1
Show file tree
Hide file tree
Showing 4 changed files with 1,544 additions and 27 deletions.
37 changes: 37 additions & 0 deletions background/tests/keyring-persistence.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { webcrypto } from "crypto"
import { encryptVault, decryptVault } from "../services/keyring/encryption"

beforeEach(() => {
// polyfill the WebCrypto API
global.crypto = webcrypto as unknown as Crypto
})

afterEach(() => {
delete global.crypto
})

test("can encrypt a vault", async () => {
const vault = { a: 1 }
const password = "this-is-a-poor-password"
await encryptVault(vault, password)
})

test("can decrypt an encrypted vault", async () => {
const vault = { a: 1 }
const password = "this-is-a-poor-password"
const encryptedVault = await encryptVault(vault, password)

const newVault = await decryptVault(encryptedVault, password)

expect(newVault).toEqual(vault)
})

test("can decrypt a complex encrypted vault", async () => {
const vault = { a: { b: [1, 2, 3] }, c: null, d: 123 }
const password = "this-is-a-poor-password"
const encryptedVault = await encryptVault(vault, password)

const newVault = await decryptVault(encryptedVault, password)

expect(newVault).toEqual(vault)
})
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"lint:docs:fix": "prettier -w '**/*.md'",
"lint:js": "eslint .",
"lint:js:fix": "eslint . --fix",
"test": "echo \"nothing to do\""
"test": "jest"
},
"workspaces": [
"ui",
Expand All @@ -52,6 +52,7 @@
"@types/archiver": "^5.1.0",
"@types/copy-webpack-plugin": "^8.0.0",
"@types/firefox-webext-browser": "^82.0.0",
"@types/jest": "^27.0.2",
"@types/terser-webpack-plugin": "^5.0.3",
"@types/webpack-livereload-plugin": "^2.3.2",
"@typescript-eslint/eslint-plugin": "^4.28.4",
Expand All @@ -73,6 +74,7 @@
"eslint-plugin-react-hooks": "^4.2.0",
"fork-ts-checker-webpack-plugin": "^6.3.2",
"install": "^0.13.0",
"jest": "^27.2.2",
"npm": "^7.5.6",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.2",
Expand Down
4 changes: 4 additions & 0 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This would be a fantastic spot for an integration test!
test("adds 1 + 2 to equal 3", () => {
expect(1 + 2).toBe(3)
})
Loading

0 comments on commit 32ef8b1

Please sign in to comment.