1 | import * as postmark from "../../src/index";
|
2 |
|
3 | import { expect } from "chai";
|
4 | import "mocha";
|
5 |
|
6 | import * as nconf from "nconf";
|
7 | const testingKeys = nconf.env().file({ file: __dirname + "/../../testing_keys.json" });
|
8 |
|
9 | const packageJson = require("../../package.json");
|
10 | const clientVersion = packageJson.version;
|
11 |
|
12 | describe("ServerClient", () => {
|
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 | describe("#new", () => {
|
21 | it("default clientOptions", () => {
|
22 | expect(client.clientOptions).to.eql({
|
23 | useHttps: true,
|
24 | requestHost: "api.postmarkapp.com",
|
25 | timeout: 30,
|
26 | });
|
27 | });
|
28 |
|
29 | it("clientVersion", () => {
|
30 | expect(client.clientVersion).to.equal(clientVersion);
|
31 | });
|
32 | });
|
33 |
|
34 | it("clientVersion=", () => {
|
35 | const customClientVersion = "test";
|
36 | client.clientVersion = customClientVersion;
|
37 | expect(client.clientVersion).to.equal(customClientVersion);
|
38 | });
|
39 |
|
40 | describe("clientOptions", () => {
|
41 | it("clientOptions=", () => {
|
42 | const requestHost = "test";
|
43 | const useHttps = false;
|
44 | const timeout = 10;
|
45 |
|
46 | client.clientOptions.requestHost = requestHost;
|
47 | client.clientOptions.useHttps = useHttps;
|
48 | client.clientOptions.timeout = timeout;
|
49 |
|
50 | expect(client.clientOptions).to.eql({
|
51 | useHttps,
|
52 | requestHost,
|
53 | timeout,
|
54 | });
|
55 | });
|
56 |
|
57 | it("new clientOptions as object", () => {
|
58 | const requestHost = "test";
|
59 | const useHttps = false;
|
60 | const timeout = 50;
|
61 | const clientOptions = new postmark.Models.ClientOptions.Configuration(useHttps, requestHost, timeout);
|
62 | client = new postmark.ServerClient(serverToken, clientOptions);
|
63 |
|
64 | expect(client.clientOptions).to.eql({
|
65 | useHttps,
|
66 | requestHost,
|
67 | timeout,
|
68 | });
|
69 | });
|
70 |
|
71 | it("new clientOptions as parameter", () => {
|
72 | const requestHost = "test";
|
73 | const useHttps = false;
|
74 | const timeout = 50;
|
75 |
|
76 | client = new postmark.ServerClient(serverToken, {
|
77 | useHttps,
|
78 | requestHost,
|
79 | timeout,
|
80 | });
|
81 |
|
82 | expect(client.clientOptions).to.eql({
|
83 | useHttps,
|
84 | requestHost,
|
85 | timeout,
|
86 | });
|
87 | });
|
88 |
|
89 | });
|
90 |
|
91 | describe("errors", () => {
|
92 | const invalidTokenError = "InvalidAPIKeyError";
|
93 |
|
94 | it("empty token", () => {
|
95 | expect(() => new postmark.ServerClient(""))
|
96 | .to.throw("A valid API token must be provided when creating a ClientOptions");
|
97 | });
|
98 |
|
99 | it("promise error", () => {
|
100 | return client.getBounces().then((result) => {
|
101 | return result;
|
102 | }, (error) => {
|
103 | expect(error.name).to.equal(invalidTokenError);
|
104 | });
|
105 | });
|
106 |
|
107 | it("callback error", (done) => {
|
108 | client = new postmark.ServerClient("testToken");
|
109 | client.getBounces(undefined, (error: any, data) => {
|
110 | expect(data).to.equal(null);
|
111 | expect(error.name).to.equal(invalidTokenError);
|
112 | done();
|
113 | });
|
114 | });
|
115 | });
|
116 | });
|