diff --git a/package.json b/package.json index b685233..c36c011 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@internxt/sdk", "author": "Internxt ", - "version": "1.16.1", + "version": "1.16.2", "description": "An sdk for interacting with Internxt's services", "repository": { "type": "git", @@ -50,4 +50,4 @@ "prettier --write" ] } -} +} \ No newline at end of file diff --git a/src/mail/index.ts b/src/mail/index.ts index 876dfd4..91f41d0 100644 --- a/src/mail/index.ts +++ b/src/mail/index.ts @@ -184,13 +184,15 @@ export class MailApi { } /** - * Gets the mail account keys for the given address + * Gets the mail account keys for the given address. When omitted, the + * backend returns the keys for the caller's default address. * - * @param address - The mail address whose keys should be retrieved + * @param address - Optional. The mail address whose keys should be retrieved * @returns The public, encrypted private and recovery keys plus the salt */ - getMailAccountKeys(address: string): Promise { - return this.client.getWithParams('/users/me/mail-account/keys', { address }, this.headers()); + getMailAccountKeys(address?: string): Promise { + const params = address ? { address } : {}; + return this.client.getWithParams('/users/me/mail-account/keys', params, this.headers()); } /** diff --git a/test/mail/index.test.ts b/test/mail/index.test.ts new file mode 100644 index 0000000..78b39ba --- /dev/null +++ b/test/mail/index.test.ts @@ -0,0 +1,87 @@ +import { HttpClient } from '../../src/shared/http/client'; +import { MailApi } from '../../src/mail/index'; +import { ApiSecurity, AppDetails } from '../../src/shared'; +import { headersWithToken } from '../../src/shared/headers'; +import { describe, it, expect, beforeEach, vi } from 'vitest'; + +describe('Mail service tests', () => { + beforeEach(() => { + vi.restoreAllMocks(); + }); + + describe('test mail account setup', () => { + it('When a mail account setup is requested, then it should POST to /users/me/mail-account and return the created address', async () => { + const { client, headers } = clientAndHeadersWithToken(); + const postStub = vi.spyOn(HttpClient.prototype, 'post').mockResolvedValue({ address: 'user@domain.com' }); + const payload = { + address: 'user', + domain: 'domain.com', + displayName: 'User', + password: 'mail-password', + keys: { + publicKey: 'public-key', + encryptionPrivateKey: 'encryption-private-key', + recoveryPrivateKey: 'recovery-private-key', + }, + }; + + const result = await client.setupMailAccount(payload); + + expect(postStub).toHaveBeenCalledWith('/users/me/mail-account', payload, headers); + expect(result).toEqual({ address: 'user@domain.com' }); + }); + + it('When the mail account keys are requested with an address, then it should GET /users/me/mail-account/keys with that address', async () => { + const { client, headers } = clientAndHeadersWithToken(); + const expectedKeys = { + address: 'user@domain.com', + publicKey: 'public-key', + encryptionPrivateKey: 'encryption-private-key', + recoveryPrivateKey: 'recovery-private-key', + }; + const getStub = vi.spyOn(HttpClient.prototype, 'getWithParams').mockResolvedValue(expectedKeys); + + const result = await client.getMailAccountKeys('user@domain.com'); + + expect(getStub).toHaveBeenCalledWith('/users/me/mail-account/keys', { address: 'user@domain.com' }, headers); + expect(result).toEqual(expectedKeys); + }); + + it('When the mail account keys are requested without an address, then it should GET /users/me/mail-account/keys without query params', async () => { + const { client, headers } = clientAndHeadersWithToken(); + const expectedKeys = { + address: 'user@domain.com', + publicKey: 'public-key', + encryptionPrivateKey: 'encryption-private-key', + recoveryPrivateKey: 'recovery-private-key', + }; + const getStub = vi.spyOn(HttpClient.prototype, 'getWithParams').mockResolvedValue(expectedKeys); + + const result = await client.getMailAccountKeys(); + + expect(getStub).toHaveBeenCalledWith('/users/me/mail-account/keys', {}, headers); + expect(result).toEqual(expectedKeys); + }); + }); +}); + +function clientAndHeadersWithToken( + apiUrl = '', + clientName = 'c-name', + clientVersion = '0.1', + token = 'my-token', +): { + client: MailApi; + headers: object; +} { + const appDetails: AppDetails = { + clientName, + clientVersion, + }; + const apiSecurity: ApiSecurity = { + token, + }; + const client = MailApi.client(apiUrl, appDetails, apiSecurity); + const headers = headersWithToken({ clientName, clientVersion, token }); + return { client, headers }; +}