-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpk.test.js
77 lines (64 loc) · 3.13 KB
/
dpk.test.js
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
const crypto = require("crypto");
const { deterministicPartitionKey } = require("./dpk");
describe("deterministicPartitionKey", () => {
let encryptedValue = "my encrypted value";
let cryptoImplementation;
beforeEach(() => {
cryptoImplementation = {
update: jest.fn().mockReturnThis(),
digest: jest.fn().mockReturnValueOnce(encryptedValue),
};
jest
.spyOn(crypto, "createHash")
.mockImplementationOnce(() => cryptoImplementation);
jest.spyOn(cryptoImplementation, "update");
jest.spyOn(cryptoImplementation, "digest");
});
afterEach(() => {
jest.clearAllMocks();
});
it("Returns the literal '0' when given no input", () => {
const trivialKey = deterministicPartitionKey();
expect(trivialKey).toBe("0");
expect(crypto.createHash).not.toHaveBeenCalled();
});
it("Returns the literal encrypted when given input is a string.", () => {
const inputString = "custom-partition";
const trivialKey = deterministicPartitionKey(inputString);
expect(trivialKey).toBe(encryptedValue);
expect(crypto.createHash).toHaveBeenCalledTimes(1);
expect(crypto.createHash).toHaveBeenCalledWith("sha3-512");
expect(cryptoImplementation.update).toHaveBeenCalledWith(
JSON.stringify(inputString)
);
expect(cryptoImplementation.digest).toHaveBeenCalledWith("hex");
});
it("Returns the value from the property partitionKey when given input is an object with that property set as string.", () => {
const inputString = "custom-partition";
const trivialKey = deterministicPartitionKey({
partitionKey: inputString,
});
expect(trivialKey).toBe(inputString);
expect(crypto.createHash).not.toHaveBeenCalled();
});
it("Returns the value from the property partitionKey when given input is an object with that property set as object.", () => {
const inputObject = {
partitionKey: { internalPartition: "custom-partition" },
};
const trivialKey = deterministicPartitionKey(inputObject);
expect(trivialKey).toBe(JSON.stringify(inputObject.partitionKey));
expect(crypto.createHash).not.toHaveBeenCalled();
});
it("Returns the literal encrypted when given input is a string greater than 256.", () => {
const inputString =
"pmshjwbrthdaqzhvuqqtcxnorlhzdhwrfzzeiljjkociflyskgvxmkgmvxtvgrgziqotoxmvuvrfhbbesjiqxcsytsfxbwfbpagefuvmguwfoklgmuusvanypugdecqwtfhizdumcdcfljrhlzlevkdqzsyleagpporpyagjbyygmzwjjgwtmqdzxwwufcltvgapsncufnhtjmqbdipcuhkvejpibjthidvcjowjkfyqsjbrnbwlaulqlddlwocxlsbzyzdaghwtdqjdkrgrnzywwmzkbjaqiubaezneoxreewmnylcnuaxzkvcdxyzgtrdyjqbtyaipclbiavzitvxzysnxsipbxgykprmdhkrngjpxrqtcjcievmtyicffbxkupdqregjlqdutmmrifyrlzhqtlmbgqybfholyfivkhrkrbqaoycsirqaierhqopkqhdjhenheutrtmnpjbpnltohsvsrqvvertdomyquosaiqizxoyyofcfmzeygw";
const trivialKey = deterministicPartitionKey(inputString);
expect(trivialKey).toBe(encryptedValue);
expect(crypto.createHash).toHaveBeenCalledTimes(1);
expect(crypto.createHash).toHaveBeenCalledWith("sha3-512");
expect(cryptoImplementation.update).toHaveBeenCalledWith(
JSON.stringify(inputString)
);
expect(cryptoImplementation.digest).toHaveBeenCalledWith("hex");
});
});