diff --git a/src/__tests__/lib/mailtrap-client.test.ts b/src/__tests__/lib/mailtrap-client.test.ts index f92496a..7a976e6 100644 --- a/src/__tests__/lib/mailtrap-client.test.ts +++ b/src/__tests__/lib/mailtrap-client.test.ts @@ -15,7 +15,7 @@ import TemplatesBaseAPI from "../../lib/api/Templates"; const { ERRORS, CLIENT_SETTINGS } = CONFIG; const { TESTING_ENDPOINT, BULK_ENDPOINT, SENDING_ENDPOINT } = CLIENT_SETTINGS; -const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } = +const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } = ERRORS; describe("lib/mailtrap-client: ", () => { @@ -331,7 +331,49 @@ describe("lib/mailtrap-client: ", () => { } }); + it("throws MailtrapError(TEST_INBOX_ID_MISSING) when sending in sandbox mode without testInboxId", async () => { + const client = new MailtrapClient({ + token: "MY_API_TOKEN", + sandbox: true, + accountId: 123, + }); + + await expect( + client.send({ + from: { email: "a@b.com", name: "Sender" }, + to: [{ email: "c@d.com" }], + subject: "Test", + text: "Body", + }) + ).rejects.toEqual(new MailtrapError(TEST_INBOX_ID_MISSING)); + }); + describe("batch sending:", () => { + it("throws MailtrapError(TEST_INBOX_ID_MISSING) when batch sending in sandbox mode without testInboxId", async () => { + const client = new MailtrapClient({ + token: "MY_API_TOKEN", + sandbox: true, + accountId: 123, + }); + + const batchData = { + base: { + from: { email: "a@b.com", name: "Sender" }, + subject: "Test", + text: "Body", + }, + requests: [ + { + to: [{ email: "c@d.com" }], + }, + ], + }; + + await expect(client.batchSend(batchData)).rejects.toEqual( + new MailtrapError(TEST_INBOX_ID_MISSING) + ); + }); + it("rejects with Mailtrap error when bulk and sandbox modes are used together", async () => { const batchClient = new MailtrapClient({ token: "MY_API_TOKEN", @@ -664,7 +706,7 @@ describe("lib/mailtrap-client: ", () => { }); describe("get testing(): ", () => { - it("rejects with Mailtrap error, when `testInboxId` is missing.", () => { + it("rejects with Mailtrap error, when `accountId` is missing.", () => { const client = new MailtrapClient({ token: "MY_API_TOKEN", }); @@ -674,30 +716,25 @@ describe("lib/mailtrap-client: ", () => { try { client.testing; } catch (error) { - expect(error).toEqual(new MailtrapError(TEST_INBOX_ID_MISSING)); + expect(error).toEqual(new MailtrapError(ACCOUNT_ID_MISSING)); } }); - it("rejects with Mailtrap error, when `accountId` is missing.", () => { + it("returns testing API object when accountId is provided, even without testInboxId", () => { const client = new MailtrapClient({ token: "MY_API_TOKEN", - testInboxId: 5, + accountId: 123, + // testInboxId is intentionally omitted }); - expect.assertions(1); - try { - client.testing; - } catch (error) { - expect(error).toEqual(new MailtrapError(ACCOUNT_ID_MISSING)); - } + const testingClient = client.testing; + expect(testingClient).toBeInstanceOf(TestingAPI); }); it("returns testing API object, console warn is called twice.", () => { const client = new MailtrapClient({ token: "MY_API_TOKEN", - sandbox: true, - testInboxId: 10, accountId: 10, }); expect.assertions(1); diff --git a/src/lib/MailtrapClient.ts b/src/lib/MailtrapClient.ts index 280342d..8b17e5a 100644 --- a/src/lib/MailtrapClient.ts +++ b/src/lib/MailtrapClient.ts @@ -32,7 +32,7 @@ const { TESTING_ENDPOINT, BULK_ENDPOINT, } = CLIENT_SETTINGS; -const { TEST_INBOX_ID_MISSING, ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE } = +const { ACCOUNT_ID_MISSING, BULK_SANDBOX_INCOMPATIBLE, TEST_INBOX_ID_MISSING } = ERRORS; /** @@ -93,13 +93,18 @@ export default class MailtrapClient { } /** - * Getter for Testing API. Warns if some of the required keys are missing. + * Validates that test inbox ID is present, throws MailtrapError if missing. */ - get testing() { - if (!this.testInboxId) { + private validateTestInboxIdPresence(): void { + if (this.sandbox && !this.testInboxId) { throw new MailtrapError(TEST_INBOX_ID_MISSING); } + } + /** + * Getter for Testing API. Warns if some of the required keys are missing. + */ + get testing() { this.validateAccountIdPresence(); return new TestingAPI(this.axios, this.accountId); @@ -132,6 +137,9 @@ export default class MailtrapClient { return new ContactListsBaseAPI(this.axios, this.accountId); } + /** + * Getter for Templates API. + */ get templates() { this.validateAccountIdPresence(); @@ -164,8 +172,11 @@ export default class MailtrapClient { */ public async send(mail: Mail): Promise { const host = this.determineHost(); + + this.validateTestInboxIdPresence(); + const url = `${host}/api/send${ - this.testInboxId ? `/${this.testInboxId}` : "" + this.sandbox && this.testInboxId ? `/${this.testInboxId}` : "" }`; const preparedMail = encodeMailBuffers(mail); @@ -181,9 +192,13 @@ export default class MailtrapClient { ): Promise { const { requests, base } = request; const host = this.determineHost(); - const ifSandbox = + + this.validateTestInboxIdPresence(); + + const sandbox = this.sandbox && this.testInboxId ? `/${this.testInboxId}` : ""; - const url = `${host}/api/batch${ifSandbox}`; + + const url = `${host}/api/batch${sandbox}`; const preparedBase = base ? encodeMailBuffers(base) : undefined; const preparedRequests = requests.map((singleRequest) =>