UNPKG

3.23 kBPlain TextView Raw
1import * as postmark from "../../src/index";
2
3import { expect } from "chai";
4import "mocha";
5
6import * as sinon from 'sinon';
7import * as nconf from "nconf";
8import BaseClient from "../../src/client/BaseClient";
9const testingKeys = nconf.env().file({ file: __dirname + "/../../testing_keys.json" });
10
11const packageJson = require("../../package.json");
12const clientVersion = packageJson.version;
13
14describe("AccountClient", () => {
15 let client: postmark.AccountClient;
16 const accountToken: string = testingKeys.get("ACCOUNT_TOKEN");
17
18 beforeEach(() => {
19 client = new postmark.AccountClient(accountToken);
20 });
21
22 describe("#new", () => {
23 it("default clientOptions", () => {
24 expect(client.clientOptions).to.eql({
25 useHttps: true,
26 requestHost: "api.postmarkapp.com",
27 timeout: 30,
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
48 client.clientOptions.requestHost = requestHost;
49 client.clientOptions.useHttps = useHttps;
50 client.clientOptions.timeout = timeout;
51
52 expect(client.clientOptions).to.eql({
53 useHttps,
54 requestHost,
55 timeout,
56 });
57 });
58
59 describe("errors", () => {
60 it("empty token", () => {
61 expect(() => new postmark.AccountClient(""))
62 .to.throw("A valid API token must be provided.");
63 });
64
65 describe("request errors", () => {
66 const invalidTokenError = "InvalidAPIKeyError";
67 let sandbox: sinon.SinonSandbox;
68
69 beforeEach(() => {
70 sandbox = sinon.createSandbox();
71 });
72
73 afterEach(() => {
74 sandbox.restore();
75 });
76
77 it("promise error", () => {
78 client = new postmark.AccountClient("testToken");
79 sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'});
80
81 return client.getSenderSignatures().then((result) => {
82 throw Error(`Should not be here with result: ${result}`);
83 }, (error) => {
84 expect(error.name).to.equal(invalidTokenError);
85 });
86 });
87
88 it("callback error", (done) => {
89 client = new postmark.AccountClient("testToken");
90 sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'});
91
92 client.getSenderSignatures(undefined, (error: any, data) => {
93 expect(data).to.equal(null);
94 expect(error.name).to.equal(invalidTokenError);
95 done();
96 });
97 });
98 });
99 });
100});