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 BaseClient from "../../src/client/BaseClient";
|
8 | import * as sinon from 'sinon';
|
9 |
|
10 | const testingKeys = nconf.env().file({file: __dirname + "/../../testing_keys.json"});
|
11 |
|
12 | describe("ServerClient - Errors", () => {
|
13 | let client: postmark.ServerClient;
|
14 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
15 |
|
16 | beforeEach(() => {
|
17 | client = new postmark.ServerClient(serverToken);
|
18 | });
|
19 |
|
20 | const invalidTokenError = "InvalidAPIKeyError";
|
21 |
|
22 | let sandbox: sinon.SinonSandbox;
|
23 |
|
24 | beforeEach(() => {
|
25 | sandbox = sinon.createSandbox();
|
26 | });
|
27 |
|
28 | afterEach(() => {
|
29 | sandbox.restore();
|
30 | });
|
31 |
|
32 | describe("handling errors", () => {
|
33 | it("throw basic error - promise", () => {
|
34 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(new Error("Basic error"));
|
35 |
|
36 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
37 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
38 |
|
39 | return client.getServer().then((result) => {
|
40 | return result;
|
41 | }, (error) => {
|
42 | expect(error).to.be.instanceOf(postmark.Errors.PostmarkError);
|
43 | expect(error.message).to.equal("Basic error");
|
44 | });
|
45 | });
|
46 |
|
47 | it("throw api key error - promise", () => {
|
48 | let error: any = new Error("Basic error");
|
49 | error.statusCode = 401;
|
50 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error);
|
51 |
|
52 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
53 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
54 |
|
55 | return client.getServer().then((result) => {
|
56 | return result;
|
57 | }, (error) => {
|
58 | expect(error).to.be.instanceOf(postmark.Errors.InvalidAPIKeyError);
|
59 | });
|
60 | });
|
61 |
|
62 | describe("http status code errors", () => {
|
63 | it("404", () => {
|
64 | let error: any = new Error("Basic error");
|
65 | error.statusCode = 404;
|
66 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error);
|
67 |
|
68 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
69 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
70 |
|
71 | return client.getServer().then((result) => {
|
72 | return result;
|
73 | }, (error) => {
|
74 | expect(error).to.be.instanceOf(postmark.Errors.PostmarkError);
|
75 | });
|
76 | });
|
77 |
|
78 | it("422", () => {
|
79 | let error: any = new Error("Basic error");
|
80 | error.statusCode = 422;
|
81 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error)
|
82 |
|
83 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
84 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
85 |
|
86 | return client.getServer().then((result) => {
|
87 | return result;
|
88 | }, (error) => {
|
89 | expect(error).to.be.instanceOf(postmark.Errors.ApiInputError);
|
90 | });
|
91 | });
|
92 |
|
93 | it("500", () => {
|
94 | let error: any = new Error("Basic error");
|
95 | error.statusCode = 500;
|
96 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error);
|
97 |
|
98 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
99 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
100 |
|
101 | return client.getServer().then((result) => {
|
102 | return result;
|
103 | }, (error) => {
|
104 | expect(error).to.be.instanceOf(postmark.Errors.InternalServerError);
|
105 | });
|
106 | });
|
107 |
|
108 | it("503", () => {
|
109 | let error: any = new Error("Basic error");
|
110 | error.statusCode = 503;
|
111 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error);
|
112 |
|
113 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
114 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
115 |
|
116 | return client.getServer().then((result) => {
|
117 | return result;
|
118 | }, (error) => {
|
119 | expect(error).to.be.instanceOf(postmark.Errors.ServiceUnavailablerError);
|
120 | });
|
121 | });
|
122 |
|
123 | it("505", () => {
|
124 | let error: any = new Error("Basic error");
|
125 | error.statusCode = 505;
|
126 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(error);
|
127 |
|
128 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
129 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
130 |
|
131 | return client.getServer().then((result) => {
|
132 | return result;
|
133 | }, (error) => {
|
134 | expect(error).to.be.instanceOf(postmark.Errors.UnknownError);
|
135 | });
|
136 | });
|
137 | });
|
138 |
|
139 | it("throw basic error - callback", (done) => {
|
140 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").throws(new Error("Basic error"));
|
141 |
|
142 | const serverToken: string = testingKeys.get("SERVER_TOKEN");
|
143 | let client: postmark.ServerClient = new postmark.ServerClient(serverToken);
|
144 |
|
145 | client.getServer((error: any, data) => {
|
146 | expect(data).to.equal(null);
|
147 | expect(error.name).to.equal('PostmarkError');
|
148 | done();
|
149 | });
|
150 | });
|
151 | });
|
152 |
|
153 | it("empty token", () => {
|
154 | expect(() => new postmark.ServerClient(""))
|
155 | .to.throw("A valid API token must be provided.");
|
156 | });
|
157 |
|
158 | it("promise error", () => {
|
159 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'});
|
160 |
|
161 | return client.getBounces().then((result) => {
|
162 | return result;
|
163 | }, (error) => {
|
164 | expect(error.name).to.equal(invalidTokenError);
|
165 | });
|
166 | });
|
167 |
|
168 | it("callback error", (done) => {
|
169 | client = new postmark.ServerClient("testToken");
|
170 | sandbox.stub(BaseClient.prototype, <any> "httpRequest").yields(undefined, {statusCode: 401, body: 'response'});
|
171 |
|
172 | client.getBounces(undefined, (error: any, data) => {
|
173 | expect(data).to.equal(null);
|
174 | expect(error.name).to.equal(invalidTokenError);
|
175 | done();
|
176 | });
|
177 | });
|
178 | });
|