Skip to content

Commit 3ea4762

Browse files
committed
Merge branch 'basic-auth' of github.com:shapehq/framna-docs into basic-auth
# Conflicts: # src/app/(authed)/(home)/encrypt/page.tsx
2 parents b481ff2 + e44a49b commit 3ea4762

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import RsaEncryptionService from '../../src/features/encrypt/EncryptionService'
2+
import { generateKeyPairSync } from 'crypto'
3+
4+
describe('RsaEncryptionService', () => {
5+
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
6+
modulusLength: 2048,
7+
publicKeyEncoding: {
8+
type: 'spki',
9+
format: 'pem'
10+
},
11+
privateKeyEncoding: {
12+
type: 'pkcs8',
13+
format: 'pem'
14+
}
15+
})
16+
17+
const encryptionService = new RsaEncryptionService({ publicKey, privateKey })
18+
19+
it('should encrypt and decrypt data correctly', () => {
20+
const data = 'Hello, World!'
21+
const encryptedData = encryptionService.encrypt(data)
22+
const decryptedData = encryptionService.decrypt(encryptedData)
23+
24+
expect(decryptedData).toBe(data)
25+
})
26+
27+
it('should throw an error when decrypting with incorrect data', () => {
28+
const incorrectData = 'invalidEncryptedData'
29+
30+
expect(() => {
31+
encryptionService.decrypt(incorrectData)
32+
}).toThrow()
33+
})
34+
35+
it('should throw an error when encrypting with an invalid public key', () => {
36+
const invalidPublicKey = 'invalidPublicKey'
37+
const invalidEncryptionService = new RsaEncryptionService({ publicKey: invalidPublicKey, privateKey })
38+
39+
expect(() => {
40+
invalidEncryptionService.encrypt('test')
41+
}).toThrow()
42+
})
43+
44+
it('should throw an error when decrypting with an invalid private key', () => {
45+
const data = 'Hello, World!'
46+
const encryptedData = encryptionService.encrypt(data)
47+
const invalidPrivateKey = 'invalidPrivateKey'
48+
const invalidEncryptionService = new RsaEncryptionService({ publicKey, privateKey: invalidPrivateKey })
49+
50+
expect(() => {
51+
invalidEncryptionService.decrypt(encryptedData)
52+
}).toThrow()
53+
})
54+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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/)
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

Comments
 (0)