|
1 | | -import { describe, expect, it } from "vitest"; |
2 | | -import getEncryptKey from "./getEncryptKey"; |
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import crypto from "crypto"; |
| 3 | +import getEncryptPair from "./getEncryptPair"; |
3 | 4 |
|
4 | 5 | describe("getEncryptKey", () => { |
5 | 6 | it("should return undefined if key is not defined", () => { |
6 | | - const result = getEncryptKey(undefined); |
| 7 | + const result = getEncryptPair(undefined); |
7 | 8 |
|
8 | 9 | expect(result).toBe(undefined); |
9 | 10 | }); |
10 | 11 |
|
11 | | - it("should return String if key is defined", () => { |
12 | | - const result = getEncryptKey( |
| 12 | + it("should return object with key and generated iv if key is defined", () => { |
| 13 | + const spy = vi.spyOn(crypto, "randomBytes"); |
| 14 | + spy.mockImplementationOnce(() => |
| 15 | + Buffer.from("e664535f505d3b6ae939fe1169b22e2d", "hex") |
| 16 | + ); |
| 17 | + |
| 18 | + const result = getEncryptPair( |
13 | 19 | "1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1" |
14 | 20 | ); |
15 | 21 |
|
16 | | - expect(typeof result).toBe("string"); |
| 22 | + expect(result).toStrictEqual({ |
| 23 | + key: "1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1", |
| 24 | + iv: "e664535f505d3b6ae939fe1169b22e2d", |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should return object with key and iv if they are defined", () => { |
| 29 | + const result = getEncryptPair( |
| 30 | + "1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1", |
| 31 | + "e664535f505d3b6ae939fe1169b22e2d" |
| 32 | + ); |
| 33 | + |
| 34 | + expect(result).toStrictEqual({ |
| 35 | + key: "1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1", |
| 36 | + iv: "e664535f505d3b6ae939fe1169b22e2d", |
| 37 | + }); |
17 | 38 | }); |
18 | 39 | }); |
0 commit comments