Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <hello@internxt.com>",
"version": "1.16.1",
"version": "1.16.2",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down Expand Up @@ -50,4 +50,4 @@
"prettier --write"
]
}
}
}
10 changes: 6 additions & 4 deletions src/mail/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MailAccountKeysResponse> {
return this.client.getWithParams('/users/me/mail-account/keys', { address }, this.headers());
getMailAccountKeys(address?: string): Promise<MailAccountKeysResponse> {
const params = address ? { address } : {};
return this.client.getWithParams('/users/me/mail-account/keys', params, this.headers());
}

/**
Expand Down
87 changes: 87 additions & 0 deletions test/mail/index.test.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
Loading