1 import { Result } from '@proton/docs-shared'
2 import type { EncryptionService } from '../Services/Encryption/EncryptionService'
3 import type { EncryptionContext } from '../Services/Encryption/EncryptionContext'
4 import type { DecryptMessageDTO } from './DecryptMessage'
5 import { DecryptMessage } from './DecryptMessage'
6 import type { DocumentUpdate } from '@proton/docs-proto'
7 import type { SessionKey } from '@proton/crypto/lib'
9 describe('DecryptMessage', () => {
10 let decryptMessage: DecryptMessage
11 let encryptionSerivce: EncryptionService<EncryptionContext.RealtimeMessage>
13 const key = '' as unknown as SessionKey
17 decryptData: jest.fn().mockReturnValue(Result.ok({ content: new Uint8Array(), signature: new Uint8Array() })),
18 getVerificationKey: jest.fn().mockReturnValue(Result.ok(new Uint8Array())),
19 verifyData: jest.fn().mockReturnValue(Result.ok(2)),
20 } as unknown as jest.Mocked<EncryptionService<EncryptionContext.RealtimeMessage>>
21 decryptMessage = new DecryptMessage(encryptionSerivce)
24 it('should decrypt data', async () => {
25 const dto: DecryptMessageDTO = {
27 content: new Uint8Array(),
28 authorAddress: 'string',
31 } as unknown as jest.Mocked<DocumentUpdate>,
32 documentContentKey: key,
35 await decryptMessage.execute(dto)
37 expect(encryptionSerivce.decryptData).toHaveBeenCalled()
40 it('should fetch verification key', async () => {
41 const dto: DecryptMessageDTO = {
43 content: new Uint8Array(),
44 authorAddress: 'string',
47 } as unknown as jest.Mocked<DocumentUpdate>,
48 documentContentKey: key,
51 await decryptMessage.execute(dto)
53 expect(encryptionSerivce.getVerificationKey).toHaveBeenCalled()
56 it('should fail if cannot fetch verification key', async () => {
57 ;(encryptionSerivce.getVerificationKey as jest.Mock).mockReturnValue(Result.fail('error'))
59 const dto: DecryptMessageDTO = {
61 content: new Uint8Array(),
62 authorAddress: 'string',
65 } as unknown as jest.Mocked<DocumentUpdate>,
66 documentContentKey: key,
70 const result = await decryptMessage.execute(dto)
72 expect(result.isFailed()).toBeTruthy()
75 it('should verify data', async () => {
76 const dto: DecryptMessageDTO = {
78 content: new Uint8Array(),
79 authorAddress: 'string',
82 } as unknown as jest.Mocked<DocumentUpdate>,
83 documentContentKey: key,
86 await decryptMessage.execute(dto)
88 expect(encryptionSerivce.verifyData).toHaveBeenCalled()
91 it('should fail if verification fails', async () => {
92 ;(encryptionSerivce.verifyData as jest.Mock).mockReturnValue(Result.fail('error'))
94 const dto: DecryptMessageDTO = {
96 content: new Uint8Array(),
97 authorAddress: 'string',
100 } as unknown as jest.Mocked<DocumentUpdate>,
101 documentContentKey: key,
105 const result = await decryptMessage.execute(dto)
107 expect(result.isFailed()).toBeTruthy()
110 it('should not verify if verify is false', async () => {
111 const dto: DecryptMessageDTO = {
113 content: new Uint8Array(),
114 authorAddress: 'string',
117 } as unknown as jest.Mocked<DocumentUpdate>,
118 documentContentKey: key,
122 await decryptMessage.execute(dto)
124 expect(encryptionSerivce.verifyData).not.toHaveBeenCalled()