0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-22 08:40:03 +01:00
posthog/plugin-server/tests/cdp/encryption-utils.test.ts
Ben White eef1bc42c5
feat: Migrate Integration to encrypted fields (#25014)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
2024-09-20 13:24:07 +02:00

85 lines
3.1 KiB
TypeScript

import { EncryptedFields } from '../../src/cdp/encryption-utils'
import { Hub } from '../../src/types'
import { insertHogFunction as _insertHogFunction } from './fixtures'
describe('Encrypted fields', () => {
jest.setTimeout(1000)
let encryptedFields: EncryptedFields
const mockHub: Partial<Hub> = {
ENCRYPTION_SALT_KEYS: '00beef0000beef0000beef0000beef00',
}
beforeEach(() => {
encryptedFields = new EncryptedFields(mockHub as unknown as Hub)
})
describe('encryption and decryption', () => {
it('should encrypt and decrypt a string', () => {
const encrypted = encryptedFields.encrypt('test-case')
expect(encrypted).not.toEqual('test-case')
const decrypted = encryptedFields.decrypt(encrypted)
expect(decrypted).toEqual('test-case')
})
it('should decode django example', () => {
const encrypted =
'gAAAAABlkgC8AAAAAAAAAAAAAAAAAAAAAP89mTGU6xUyLcVUIB4ySnX2Y8ZgwLALpzYGfm76Fk64vPRY62flSIigMa_MqTlKyA=='
const decrypted = encryptedFields.decrypt(encrypted)
expect(decrypted).toEqual('test-case')
})
it('should throw on decryption error', () => {
expect(() => encryptedFields.decrypt('NOT VALID')).toThrow()
})
it('should not throw on decryption error if option passed', () => {
expect(() => encryptedFields.decrypt('NOT VALID', { ignoreDecryptionErrors: true })).not.toThrow()
expect(encryptedFields.decrypt('NOT VALID', { ignoreDecryptionErrors: true })).toEqual('NOT VALID')
})
describe('decrypting objects', () => {
it('should decrypt an object', () => {
const exampleObject = {
key: encryptedFields.encrypt('value'),
missing: null,
nested: {
key: encryptedFields.encrypt('nested-value'),
},
}
expect(encryptedFields.decryptObject(exampleObject)).toEqual({
key: 'value',
missing: null,
nested: {
key: 'nested-value',
},
})
})
it('should throw on decryption error', () => {
expect(() =>
encryptedFields.decryptObject({
key: 'NOT VALID',
})
).toThrow()
})
it('should not throw on decryption error if option passed', () => {
const exampleObject = {
key: 'not encrypted',
missing: null,
nested: {
key: 'also not encrypted',
},
}
expect(() =>
encryptedFields.decryptObject(exampleObject, { ignoreDecryptionErrors: true })
).not.toThrow()
expect(encryptedFields.decryptObject(exampleObject, { ignoreDecryptionErrors: true })).toEqual(
exampleObject
)
})
})
})
})