forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-decrypt.test.ts
More file actions
57 lines (50 loc) · 2.04 KB
/
Copy pathclient-decrypt.test.ts
File metadata and controls
57 lines (50 loc) · 2.04 KB
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
// src/lib/__tests__/client-decrypt.test.ts
import { describe, expect, it, vi } from "vitest"
vi.mock("@/lib/crypto", () => ({
decrypt: vi.fn((val: string) => `decrypted:${val}`),
}))
import { decrypt } from "@/lib/crypto"
import { isDecryptionError } from "@/lib/error-utils"
const { decryptClientCredentials } = await import("@/lib/download-clients/credentials")
describe("decryptClientCredentials", () => {
it("returns decrypted username and password", () => {
const client = { name: "Test", encryptedUsername: "enc-user", encryptedPassword: "enc-pass" }
const key = Buffer.from("a".repeat(64), "hex")
const result = decryptClientCredentials(client, key)
expect(result).toEqual({ username: "decrypted:enc-user", password: "decrypted:enc-pass" })
})
it("throws an error that isDecryptionError() recognises when decrypt throws a crypto error", () => {
// "bad decrypt" matches the /bad\s*decrypt/i pattern in isDecryptionError
;(decrypt as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("bad decrypt")
})
const client = { name: "MyClient", encryptedUsername: "x", encryptedPassword: "y" }
let thrown: unknown
expect(() => {
try {
decryptClientCredentials(client, Buffer.alloc(32))
} catch (err) {
thrown = err
throw err
}
}).toThrow()
expect(isDecryptionError(thrown)).toBe(true)
})
it("throws an error that isDecryptionError() does NOT recognise for non-crypto failures", () => {
// "bad key" does not match any pattern in isDecryptionError
;(decrypt as ReturnType<typeof vi.fn>).mockImplementation(() => {
throw new Error("bad key")
})
const client = { name: "MyClient", encryptedUsername: "x", encryptedPassword: "y" }
let thrown: unknown
expect(() => {
try {
decryptClientCredentials(client, Buffer.alloc(32))
} catch (err) {
thrown = err
throw err
}
}).toThrow(/Failed to read credentials for client "MyClient"/)
expect(isDecryptionError(thrown)).toBe(false)
})
})