Skip to content

Commit 83c242c

Browse files
committed
test: add integration tests for custom encryption handling in EncryptedHandler
1 parent 8752f06 commit 83c242c

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

tests/integration/tests/enhancements/with-encrypted/with-encrypted.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FieldInfo } from '@zenstackhq/runtime';
12
import { loadSchema } from '@zenstackhq/testtools';
23
import path from 'path';
34

@@ -12,7 +13,7 @@ describe('Encrypted test', () => {
1213
process.chdir(origDir);
1314
});
1415

15-
it('encrypted tests', async () => {
16+
it('Simple encryption test', async () => {
1617
const { enhance } = await loadSchema(`
1718
model User {
1819
id String @id @default(cuid())
@@ -21,6 +22,7 @@ describe('Encrypted test', () => {
2122
@@allow('all', true)
2223
}`);
2324

25+
const sudoDb = enhance(undefined, { kinds: [] });
2426
const db = enhance(undefined, { encryption: { encryptionKey: 'c558Gq0YQK2QcqtkMF9BGXHCQn4dMF8w' } });
2527

2628
const create = await db.user.create({
@@ -36,7 +38,65 @@ describe('Encrypted test', () => {
3638
},
3739
});
3840

41+
const sudoRead = await sudoDb.user.findUnique({
42+
where: {
43+
id: '1',
44+
},
45+
});
46+
47+
expect(create.encrypted_value).toBe('abc123');
48+
expect(read.encrypted_value).toBe('abc123');
49+
expect(sudoRead.encrypted_value).not.toBe('abc123');
50+
});
51+
52+
it('Custom encryption test', async () => {
53+
const { enhance } = await loadSchema(`
54+
model User {
55+
id String @id @default(cuid())
56+
encrypted_value String @encrypted()
57+
58+
@@allow('all', true)
59+
}`);
60+
61+
const sudoDb = enhance(undefined, { kinds: [] });
62+
const db = enhance(undefined, {
63+
encryption: {
64+
encrypt: async (model: string, field: FieldInfo, data: string) => {
65+
// Add _enc to the end of the input
66+
return data + '_enc';
67+
},
68+
decrypt: async (model: string, field: FieldInfo, cipher: string) => {
69+
// Remove _enc from the end of the input explicitly
70+
if (cipher.endsWith('_enc')) {
71+
return cipher.slice(0, -4); // Remove last 4 characters (_enc)
72+
}
73+
74+
return cipher;
75+
},
76+
},
77+
});
78+
79+
const create = await db.user.create({
80+
data: {
81+
id: '1',
82+
encrypted_value: 'abc123',
83+
},
84+
});
85+
86+
const read = await db.user.findUnique({
87+
where: {
88+
id: '1',
89+
},
90+
});
91+
92+
const sudoRead = await sudoDb.user.findUnique({
93+
where: {
94+
id: '1',
95+
},
96+
});
97+
3998
expect(create.encrypted_value).toBe('abc123');
4099
expect(read.encrypted_value).toBe('abc123');
100+
expect(sudoRead.encrypted_value).toBe('abc123_enc');
41101
});
42102
});

0 commit comments

Comments
 (0)