1 import { CommentType } from '@proton/docs-shared'
2 import { DecryptComment } from './DecryptComment'
3 import { Result } from '@proton/docs-shared'
4 import type { CommentResponseDto } from '../Api/Types/CommentResponseDto'
5 import type { DocumentKeys } from '@proton/drive-store'
6 import type { EncryptionContext } from '../Services/Encryption/EncryptionContext'
7 import type { EncryptionService } from '../Services/Encryption/EncryptionService'
8 import type { PublicDocumentKeys } from '../Types/DocumentEntitlements'
10 describe('DecryptComment', () => {
11 let decryptComment: DecryptComment
12 let encryptionSerivce: EncryptionService<EncryptionContext.PersistentComment>
14 const comments: CommentResponseDto[] = []
16 const dto: CommentResponseDto = {
22 AuthorEmail: 'string',
23 ParentCommentID: 'string',
25 Type: CommentType.Comment,
29 documentContentKey: '',
31 userAddressPrivateKey: '',
32 } as unknown as DocumentKeys
35 documentContentKey: '',
36 } as unknown as PublicDocumentKeys
38 const markId = 'markId'
42 decryptData: jest.fn().mockReturnValue(Result.ok({ content: new Uint8Array(), signature: new Uint8Array() })),
43 getVerificationKey: jest.fn().mockReturnValue(Result.ok(new Uint8Array())),
44 verifyData: jest.fn().mockReturnValue(Result.ok(2)),
45 } as unknown as jest.Mocked<EncryptionService<EncryptionContext.PersistentComment>>
46 decryptComment = new DecryptComment(encryptionSerivce)
49 it('should decrypt data', async () => {
50 await decryptComment.execute(dto, markId, privateKeys)
52 expect(encryptionSerivce.decryptData).toHaveBeenCalled()
55 it('should fetch verification key', async () => {
56 await decryptComment.execute(dto, markId, privateKeys)
58 expect(encryptionSerivce.getVerificationKey).toHaveBeenCalled()
61 it('should fail if cannot fetch verification key', async () => {
62 ;(encryptionSerivce.getVerificationKey as jest.Mock).mockReturnValue(Result.fail('error'))
64 const result = await decryptComment.execute(dto, markId, privateKeys)
66 expect(result.isFailed()).toBeTruthy()
69 it('should verify data', async () => {
70 await decryptComment.execute(dto, markId, privateKeys)
72 expect(encryptionSerivce.verifyData).toHaveBeenCalled()
75 it('should fail if verification fails', async () => {
76 ;(encryptionSerivce.verifyData as jest.Mock).mockReturnValue(Result.fail('error'))
78 const result = await decryptComment.execute(dto, markId, privateKeys)
80 expect(result.isFailed()).toBeTruthy()
83 it('should not verify if public viewer/editor', async () => {
84 encryptionSerivce.verifyData = jest.fn()
86 const result = await decryptComment.execute(dto, markId, publicKeys)
88 expect(encryptionSerivce.verifyData).not.toHaveBeenCalled()
90 expect(result.isFailed()).toBeFalsy()