-
Notifications
You must be signed in to change notification settings - Fork 0
[PB-6431]: feat/implement RecipientKeysService for managing public keys and add lookup functionality in MailService #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2f4efb7
feat(recipient-keys): implement RecipientKeysService for managing pub…
jzunigax2 a7685f1
feat(mail): enhance ComposeMessageDialog with email encryption, recip…
jzunigax2 c203985
feat(mail): implement email preview decryption and enhance ComposeMes…
jzunigax2 c1b7544
feat(mail): integrate decrypted previews in email formatting and enha…
jzunigax2 7b4f50d
chore: add unit tests for useDecryptedMail and useDecryptedPreviews h…
jzunigax2 1df2887
feat(recipient-keys): implement RecipientKeysService for managing pub…
jzunigax2 0cc0eb2
chore(deps): bump SDK version
xabg2 31ee18f
feat(mail): refactor email decryption logic in useDecryptedMail and u…
jzunigax2 da9b73c
chore: update sdk and crypto
jzunigax2 a79dcd0
chore: refactor email encryption handling by introducing MailEncrypti…
jzunigax2 ef5cb17
feat(mail): refactor ComposeMessageDialog and introduce useComposeSen…
jzunigax2 b6ddff7
test: add unit tests for useComposeSend hook to validate email sendin…
jzunigax2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
src/components/compose-message/hooks/useComposeSend.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
| import type { Editor } from '@tiptap/react'; | ||
| import type { EncryptionBlock } from '@internxt/sdk/dist/mail/types'; | ||
| import useComposeSend from './useComposeSend'; | ||
| import { MailEncryptionService } from '@/services/mail-encryption'; | ||
| import notificationsService from '@/services/notifications'; | ||
| import type { Recipient } from '../types'; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| activeDomains: undefined as { domain: string }[] | undefined, | ||
| senderKeys: undefined as { address: string; publicKey: string } | undefined, | ||
| triggerLookup: vi.fn(), | ||
| sendEmail: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('@/store/api/mail', () => ({ | ||
| useGetActiveDomainsQuery: () => ({ data: mocks.activeDomains }), | ||
| useGetMailAccountKeysQuery: () => ({ data: mocks.senderKeys }), | ||
| useLazyLookupRecipientKeysQuery: () => [mocks.triggerLookup], | ||
| useSendEmailMutation: () => [mocks.sendEmail, { isLoading: false }], | ||
| })); | ||
|
|
||
| vi.mock('@/i18n', () => ({ useTranslationContext: () => ({ translate: (key: string) => key }) })); | ||
|
|
||
| vi.mock('@/services/notifications', () => ({ | ||
| default: { show: vi.fn() }, | ||
| ToastType: { Success: 'success', Error: 'error', Warning: 'warning', Info: 'info', Loading: 'loading' }, | ||
| })); | ||
|
|
||
| const editor = { getHTML: () => '<p>body</p>', getText: () => 'body' } as unknown as Editor; | ||
| const recipient = (email: string): Recipient => ({ id: email, email }); | ||
| const show = vi.mocked(notificationsService.show); | ||
|
|
||
| const renderSend = (overrides: Partial<Parameters<typeof useComposeSend>[0]> = {}) => { | ||
| const onSent = vi.fn(); | ||
| const { result } = renderHook(() => | ||
| useComposeSend({ | ||
| toRecipients: [], | ||
| ccRecipients: [], | ||
| bccRecipients: [], | ||
| subject: 'Hi', | ||
| editor, | ||
| onSent, | ||
| ...overrides, | ||
| }), | ||
| ); | ||
| return { result, onSent }; | ||
| }; | ||
|
|
||
| describe('useComposeSend', () => { | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks(); | ||
| mocks.triggerLookup.mockReset(); | ||
| mocks.sendEmail.mockReset(); | ||
| show.mockReset(); | ||
|
|
||
| mocks.activeDomains = [{ domain: 'inxt.me' }]; | ||
| mocks.senderKeys = { address: 'me@inxt.me', publicKey: 'sender-pk' }; | ||
| mocks.triggerLookup.mockReturnValue({ unwrap: () => Promise.resolve([]) }); | ||
| mocks.sendEmail.mockReturnValue({ unwrap: () => Promise.resolve({ id: 'mail-1' }) }); | ||
| }); | ||
|
|
||
| test('When there are no recipients, then it warns and does not send', async () => { | ||
| const { result, onSent } = renderSend(); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(show).toHaveBeenCalledWith(expect.objectContaining({ text: 'errors.mail.noRecipients' })); | ||
| expect(mocks.sendEmail).not.toHaveBeenCalled(); | ||
| expect(onSent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('When the active domains have not resolved, then the send is blocked to avoid a cleartext downgrade', async () => { | ||
| mocks.activeDomains = undefined; | ||
|
|
||
| const { result, onSent } = renderSend({ toRecipients: [recipient('bob@inxt.me')] }); | ||
|
|
||
| expect(result.current.encryptionState).toBe('unknown'); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(show).toHaveBeenCalledWith(expect.objectContaining({ text: 'errors.mail.encryptionUnavailable' })); | ||
| expect(mocks.sendEmail).not.toHaveBeenCalled(); | ||
| expect(onSent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('When sending encrypted but the sender keys are missing, then it reports a key lookup failure', async () => { | ||
| mocks.senderKeys = undefined; | ||
|
|
||
| const { result, onSent } = renderSend({ toRecipients: [recipient('bob@inxt.me')] }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(show).toHaveBeenCalledWith(expect.objectContaining({ text: 'errors.mail.keyLookupFailed' })); | ||
| expect(mocks.sendEmail).not.toHaveBeenCalled(); | ||
| expect(onSent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('When the recipient key lookup throws, then it reports a key lookup failure rather than a send failure', async () => { | ||
| mocks.triggerLookup.mockReturnValue({ unwrap: () => Promise.reject(new Error('network')) }); | ||
|
|
||
| const { result, onSent } = renderSend({ toRecipients: [recipient('bob@inxt.me')] }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(show).toHaveBeenCalledWith(expect.objectContaining({ text: 'errors.mail.keyLookupFailed' })); | ||
| expect(mocks.sendEmail).not.toHaveBeenCalled(); | ||
| expect(onSent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('When some recipients have no key, then it reports a key lookup failure', async () => { | ||
| mocks.triggerLookup.mockReturnValue({ | ||
| unwrap: () => Promise.resolve([{ address: 'bob@inxt.me', publicKey: null }]), | ||
| }); | ||
|
|
||
| const { result, onSent } = renderSend({ toRecipients: [recipient('bob@inxt.me')] }); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(show).toHaveBeenCalledWith(expect.objectContaining({ text: 'errors.mail.keyLookupFailed' })); | ||
| expect(mocks.sendEmail).not.toHaveBeenCalled(); | ||
| expect(onSent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('When the send mutation fails, then it reports a send failure and does not close the dialog', async () => { | ||
| mocks.sendEmail.mockReturnValue({ unwrap: () => Promise.reject(new Error('boom')) }); | ||
|
|
||
| const { result, onSent } = renderSend({ toRecipients: [recipient('bob@gmail.com')] }); | ||
|
|
||
| expect(result.current.encryptionState).toBe('cleartext'); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(show).toHaveBeenCalledWith(expect.objectContaining({ text: 'errors.mail.sendFailed' })); | ||
| expect(onSent).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('When all recipients are external, then it sends cleartext and closes the dialog', async () => { | ||
| const { result, onSent } = renderSend({ toRecipients: [recipient('bob@gmail.com')] }); | ||
|
|
||
| expect(result.current.encryptionState).toBe('cleartext'); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(mocks.sendEmail).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| to: [{ email: 'bob@gmail.com' }], | ||
| subject: 'Hi', | ||
| htmlBody: '<p>body</p>', | ||
| textBody: 'body', | ||
| }), | ||
| ); | ||
| expect(onSent).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('When all recipients are Internxt, then it encrypts the body and sends with the sender included', async () => { | ||
| mocks.triggerLookup.mockReturnValue({ | ||
| unwrap: () => Promise.resolve([{ address: 'bob@inxt.me', publicKey: 'bob-pk' }]), | ||
| }); | ||
| const buildSpy = vi | ||
| .spyOn(MailEncryptionService.instance, 'buildEncryptionBlock') | ||
| .mockResolvedValue({ | ||
| version: 'v1', | ||
| encryptedText: 'ct', | ||
| encryptedPreview: 'cp', | ||
| wrappedKeys: [], | ||
| } as EncryptionBlock); | ||
|
|
||
| const { result, onSent } = renderSend({ toRecipients: [recipient('bob@inxt.me')] }); | ||
|
|
||
| expect(result.current.encryptionState).toBe('encrypted'); | ||
|
|
||
| await act(async () => { | ||
| await result.current.send(); | ||
| }); | ||
|
|
||
| expect(buildSpy).toHaveBeenCalledWith( | ||
| { body: '<p>body</p>', previewText: 'body' }, | ||
| expect.arrayContaining([ | ||
| { address: 'bob@inxt.me', publicKey: 'bob-pk' }, | ||
| { address: 'me@inxt.me', publicKey: 'sender-pk' }, | ||
| ]), | ||
| ); | ||
| expect(mocks.sendEmail).toHaveBeenCalledWith( | ||
| expect.objectContaining({ subject: 'Hi', encryption: expect.objectContaining({ version: 'v1' }) }), | ||
| ); | ||
| expect(onSent).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Use project path aliases for internal imports in tests.
Switch the relative imports to
@/*aliases to match repository import policy.Proposed patch
As per coding guidelines, "Use the path alias
@/*→src/*when importing internal modules."🤖 Prompt for AI Agents