|
| 1 | +import RemoteConfigEncoder from "@/features/projects/domain/RemoteConfigEncoder" |
| 2 | +import { IEncryptionService } from "@/features/encrypt/EncryptionService" |
| 3 | +import RemoteConfig from "@/features/projects/domain/RemoteConfig" |
| 4 | +import { ZodError } from "zod" |
| 5 | + |
| 6 | +describe('RemoteConfigEncoder', () => { |
| 7 | + const encryptionService: IEncryptionService = { |
| 8 | + encrypt: (data: string) => `encrypted-${data}`, |
| 9 | + decrypt: (data: string) => data.replace('encrypted-', '') |
| 10 | + } |
| 11 | + |
| 12 | + const encoder = new RemoteConfigEncoder(encryptionService) |
| 13 | + |
| 14 | + it('should encode a remote config by first encrypting and then encoding with base64', () => { |
| 15 | + const remoteConfig: RemoteConfig = { url: 'https://example.com/spec.yaml' } |
| 16 | + const encoded = encoder.encode(remoteConfig) |
| 17 | + const expectedEncoded = Buffer.from('encrypted-{"url":"https://example.com/spec.yaml"}').toString('base64') |
| 18 | + expect(encoded).toEqual(expectedEncoded) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should decode an encoded string', () => { |
| 22 | + const encodedString = Buffer.from('encrypted-{"url":"https://example.com/spec.yaml"}').toString('base64') |
| 23 | + const decoded = encoder.decode(encodedString) |
| 24 | + const expectedDecoded: RemoteConfig = { url: 'https://example.com/spec.yaml' } |
| 25 | + expect(decoded).toEqual(expectedDecoded) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should throw an error if the decrypted string is not valid JSON', () => { |
| 29 | + const invalidJson = Buffer.from('encrypted-invalid-json').toString('base64') |
| 30 | + expect(() => encoder.decode(invalidJson)).toThrow("Unexpected token i in JSON at position 0") |
| 31 | + }) |
| 32 | + |
| 33 | + it('should throw an error if the remote config is not valid', () => { |
| 34 | + const remoteConfig: RemoteConfig = { url: '' } |
| 35 | + const encoded = encoder.encode(remoteConfig) |
| 36 | + expect(() => encoder.decode(encoded)).toThrow(ZodError) |
| 37 | + }) |
| 38 | +}) |
0 commit comments