1 | import * as postmark from "../../src/index";
|
2 |
|
3 | import { expect } from "chai";
|
4 | import "mocha";
|
5 |
|
6 | import * as nconf from "nconf";
|
7 | import * as sinon from "sinon";
|
8 | const testingKeys = nconf.env().file({ file: __dirname + "/../../testing_keys.json" });
|
9 |
|
10 | const packageJson = require("../../package.json");
|
11 | const clientVersion = packageJson.version;
|
12 |
|
13 | describe("AccountClient", () => {
|
14 | let client: postmark.AccountClient;
|
15 | const accountToken: string = testingKeys.get("ACCOUNT_TOKEN");
|
16 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
17 |
|
18 | beforeEach(() => {
|
19 | client = new postmark.AccountClient(accountToken);
|
20 | });
|
21 |
|
22 | describe("#new", () => {
|
23 | it("default clientOptions", () => {
|
24 | expect(client.getClientOptions()).to.eql({
|
25 | useHttps: true,
|
26 | requestHost: "api.postmarkapp.com",
|
27 | timeout: 60,
|
28 | });
|
29 | });
|
30 |
|
31 | it("clientVersion", () => {
|
32 | expect(client.clientVersion).to.equal(clientVersion);
|
33 | });
|
34 | });
|
35 |
|
36 | it("clientVersion=", () => {
|
37 | const customClientVersion: string = "test";
|
38 |
|
39 | client.clientVersion = customClientVersion;
|
40 | expect(client.clientVersion).to.equal(customClientVersion);
|
41 | });
|
42 |
|
43 | it("clientOptions=", () => {
|
44 | const requestHost = "test";
|
45 | const useHttps = false;
|
46 | const timeout = 10;
|
47 | client.setClientOptions({requestHost, useHttps, timeout});
|
48 |
|
49 | expect(client.getClientOptions()).to.eql({
|
50 | useHttps,
|
51 | requestHost,
|
52 | timeout,
|
53 | });
|
54 | });
|
55 |
|
56 | describe("errors", () => {
|
57 | it("empty token", () => {
|
58 | expect(() => new postmark.AccountClient(""))
|
59 | .to.throw("A valid API token must be provided.");
|
60 | });
|
61 |
|
62 | describe("request errors", () => {
|
63 | const errorType = "InternalServerError";
|
64 | const rejectError = {response: {status: 500, data: "response"}};
|
65 | let sandbox: sinon.SinonSandbox;
|
66 |
|
67 | beforeEach(() => {
|
68 | sandbox = sinon.createSandbox();
|
69 | });
|
70 |
|
71 | afterEach(() => {
|
72 | sandbox.restore();
|
73 | });
|
74 |
|
75 | it("promise error", () => {
|
76 | client = new postmark.AccountClient(serverToken);
|
77 | sandbox.stub(client.httpClient, "request").rejects(rejectError);
|
78 |
|
79 | return client.getSenderSignatures().then((result) => {
|
80 | throw Error(`Should not be here with result: ${result}`);
|
81 | }, (error) => {
|
82 | expect(error.name).to.equal(errorType);
|
83 | });
|
84 | });
|
85 |
|
86 | it("callback error", (done) => {
|
87 | client = new postmark.AccountClient("testToken");
|
88 | sandbox.stub(client.httpClient, "request").rejects(rejectError);
|
89 |
|
90 | client.getSenderSignatures(undefined, (error: any, data) => {
|
91 | expect(data).to.equal(null);
|
92 | expect(error.name).to.equal(errorType);
|
93 | done();
|
94 | });
|
95 | });
|
96 | });
|
97 | });
|
98 | });
|