generated from 47ng/typescript-library-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathencryption.test.ts
162 lines (140 loc) · 4.45 KB
/
encryption.test.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { formatKey } from '@47ng/cloak/dist/key'
import { DMMFModels } from 'dmmf'
import { MiddlewareParams } from 'types'
import { configureKeys, decryptOnRead, encryptOnWrite } from './encryption'
import { errors } from './errors'
const ENCRYPTION_TEST_KEY =
'k1.aesgcm256.DbQoar8ZLuUsOHZNyrnjlskInHDYlzF3q6y1KGM7DUM='
const DECRYPTION_TEST_KEYS = [
'k1.aesgcm256.4BNYdJnjOQJP2adq9cGM9kb4dZxDujUs6aPS0VeRtAM=',
'k1.aesgcm256.El9unG7WBAVRQdATOyMggE3XrLV2ZjTGKdajfmIeBPs='
]
describe('encryption', () => {
describe('configureKeys', () => {
test('No encryption key specified', () => {
const run = () => configureKeys({})
expect(run).toThrowError(errors.noEncryptionKey)
})
test('Providing encryptionKey directly', () => {
const { encryptionKey } = configureKeys({
encryptionKey: ENCRYPTION_TEST_KEY
})
expect(formatKey(encryptionKey.raw as Uint8Array)).toEqual(
ENCRYPTION_TEST_KEY
)
})
test('Providing encryptionKey via the environment', () => {
process.env.PRISMA_FIELD_ENCRYPTION_KEY = ENCRYPTION_TEST_KEY
const { encryptionKey } = configureKeys({})
expect(formatKey(encryptionKey.raw as Uint8Array)).toEqual(
ENCRYPTION_TEST_KEY
)
process.env.PRISMA_FIELD_ENCRYPTION_KEY = undefined
})
test('Encryption key is in the keychain', () => {
const { encryptionKey, keychain } = configureKeys({
encryptionKey: ENCRYPTION_TEST_KEY
})
expect(keychain[encryptionKey.fingerprint].key).toEqual(encryptionKey)
})
test('Loading decryption keys directly', () => {
const { keychain } = configureKeys({
encryptionKey: ENCRYPTION_TEST_KEY,
decryptionKeys: DECRYPTION_TEST_KEYS
})
expect(Object.values(keychain).length).toEqual(3)
})
test('Loading decryption keys via the environment', () => {
process.env.PRISMA_FIELD_DECRYPTION_KEYS = DECRYPTION_TEST_KEYS.join(',')
const { keychain } = configureKeys({
encryptionKey: ENCRYPTION_TEST_KEY
})
expect(Object.values(keychain).length).toEqual(3)
process.env.PRISMA_FIELD_DECRYPTION_KEYS = undefined
})
})
describe('encryptOnWrite', () => {
test('Should call custom encrypt function', async () => {
const encryptFunction = jest.fn(
(decripted: string) => `fake-encription-${decripted}`
)
const name = 'value'
const params: MiddlewareParams = {
model: 'User',
action: 'create',
args: { data: { name } },
runInTransaction: true,
dataPath: ['any']
}
const dmmfModels: DMMFModels = {
User: {
connections: {
'fake-connection': {
modelName: 'User',
isList: false
}
},
fields: {
name: {
encrypt: true,
strictDecryption: false
}
}
}
}
const keys = configureKeys({
encryptionKey: ENCRYPTION_TEST_KEY,
decryptionKeys: DECRYPTION_TEST_KEYS
})
encryptOnWrite(params, keys, dmmfModels, 'User.create', encryptFunction)
expect(encryptFunction).toBeCalledTimes(1)
expect(encryptFunction).toBeCalledWith(name)
})
})
describe('decryptOnRead', () => {
test('Should call custom decryp function', async () => {
const decryptFunction = jest.fn(
(encrypted: string) => `fake-decription-${encrypted}`
)
const params: MiddlewareParams = {
model: 'User',
action: 'findFirst',
args: { where: { name: 'value' } },
runInTransaction: true,
dataPath: ['any']
}
const dmmfModels: DMMFModels = {
User: {
connections: {
'fake-connection': {
modelName: 'User',
isList: false
}
},
fields: {
name: {
encrypt: true,
strictDecryption: false
}
}
}
}
const keys = configureKeys({
encryptionKey: ENCRYPTION_TEST_KEY,
decryptionKeys: DECRYPTION_TEST_KEYS
})
const encryptedName = 'a1b2c3d4e5d6'
const result = { name: encryptedName }
decryptOnRead(
params,
result,
keys,
dmmfModels,
'User.findFirst',
decryptFunction
)
expect(decryptFunction).toBeCalledTimes(1)
expect(decryptFunction).toBeCalledWith(encryptedName)
})
})
})