UNPKG

6.27 kBJavaScriptView Raw
1import chai, { expect } from "chai";
2import sinon from "sinon";
3import sinonChai from "sinon-chai";
4import path from "path";
5import fs from "fs";
6
7import Nexmo from "../lib/Nexmo";
8import CallsResource from "../lib/CallsResource";
9
10chai.use(sinonChai);
11
12describe("Nexmo definition", () => {
13 describe(".generateJwt", () => {
14 it("should expose a generateJwt function", () => {
15 expect(Nexmo.generateJwt).to.be.a("function");
16 });
17
18 it("should throw an exception if privateKey file does not exist", () => {
19 var create = function() {
20 Nexmo.generateJwt("./no-key-here.key");
21 };
22 expect(create).to.throw(Error);
23 });
24
25 it("should create a JWT with a private key (file path) [static]", () => {
26 var token = Nexmo.generateJwt(path.join(__dirname, "private-test.key"));
27 expect(token).to.be.a("string");
28 });
29
30 it("should create a JWT with a private key (Buffer) [static]", () => {
31 var fileBuffer = fs.readFileSync(
32 path.join(__dirname, "private-test.key")
33 );
34 var token = Nexmo.generateJwt(fileBuffer);
35 expect(token).to.be.a("string");
36 });
37 });
38});
39
40describe("Nexmo Object instance", function() {
41 it("should expose a credentials object", function() {
42 var nexmo = new Nexmo({
43 apiKey: "test",
44 apiSecret: "test"
45 });
46 expect(nexmo.credentials).to.be.a("object");
47 });
48
49 it("should expose a message object", function() {
50 var nexmo = new Nexmo({
51 apiKey: "test",
52 apiSecret: "test"
53 });
54 expect(nexmo.message).to.be.a("object");
55 });
56
57 it("should expose a voice object", function() {
58 var nexmo = new Nexmo({
59 apiKey: "test",
60 apiSecret: "test"
61 });
62 expect(nexmo.voice).to.be.a("object");
63 });
64
65 it("should expose a number object", function() {
66 var nexmo = new Nexmo({
67 apiKey: "test",
68 apiSecret: "test"
69 });
70 expect(nexmo.number).to.be.a("object");
71 });
72
73 it("should expose a verify object", function() {
74 var nexmo = new Nexmo({
75 apiKey: "test",
76 apiSecret: "test"
77 });
78 expect(nexmo.verify).to.be.a("object");
79 });
80
81 it("should expose a numberInsight object", function() {
82 var nexmo = new Nexmo({
83 apiKey: "test",
84 apiSecret: "test"
85 });
86 expect(nexmo.numberInsight).to.be.a("object");
87 });
88
89 it("should expose a app object", function() {
90 var nexmo = new Nexmo({
91 apiKey: "test",
92 apiSecret: "test"
93 });
94 expect(nexmo.app).to.be.a("object");
95 });
96
97 it("should expose a applications object", function() {
98 var nexmo = new Nexmo({
99 apiKey: "test",
100 apiSecret: "test"
101 });
102 expect(nexmo.applications).to.be.a("object");
103 });
104
105 it("should alias apps to applications object", function() {
106 var nexmo = new Nexmo({
107 apiKey: "test",
108 apiSecret: "test"
109 });
110 expect(nexmo.applications).to.equal(nexmo.app);
111 });
112
113 it("should expose a account object", function() {
114 var nexmo = new Nexmo({
115 apiKey: "test",
116 apiSecret: "test"
117 });
118 expect(nexmo.account).to.be.a("object");
119 });
120
121 it("should expose a calls object", function() {
122 var nexmo = new Nexmo({
123 apiKey: "test",
124 apiSecret: "test"
125 });
126 expect(nexmo.calls).to.be.an.instanceOf(CallsResource);
127 });
128
129 it("should expose a files object", function() {
130 var nexmo = new Nexmo({
131 apiKey: "test",
132 apiSecret: "test"
133 });
134 expect(nexmo.files).to.be.a("object");
135 });
136
137 it("should allow options to be passed", function() {
138 var initializedSpy = sinon.spy();
139 var options = {
140 nexmoOverride: {
141 initialize: initializedSpy
142 },
143 appendToUserAgent: "EXT",
144 debug: true
145 };
146 new Nexmo(
147 {
148 apiKey: "test",
149 apiSecret: "test"
150 },
151 options
152 );
153 expect(initializedSpy.calledWith("test", "test", options)).to.be.true;
154 });
155
156 it("should have debug turned off by default", function() {
157 var nexmo = new Nexmo({
158 apiKey: "test",
159 apiSecret: "test"
160 });
161 expect(nexmo.options.debug).to.be.false;
162 });
163
164 it("should allow a custom logger to be set", function() {
165 var logger = {
166 info: function() {},
167 error: function() {},
168 warn: function() {}
169 };
170 var nexmo = new Nexmo(
171 {
172 apiKey: "test",
173 apiSecret: "test"
174 },
175 {
176 logger: logger
177 }
178 );
179 expect(nexmo.options.logger).to.equal(logger);
180 });
181
182 it("should allow a debug option to be set", function() {
183 var nexmo = new Nexmo(
184 {
185 apiKey: "test",
186 apiSecret: "test"
187 },
188 {
189 debug: true
190 }
191 );
192 expect(nexmo.options.debug).to.be.true;
193 });
194
195 it("should have a default user agent in the form LIBRARY-NAME/LIBRARY-VERSION/LANGUAGE-VERSION", function() {
196 var nexmo = new Nexmo({
197 apiKey: "test",
198 apiSecret: "test"
199 });
200 expect(nexmo.options.userAgent).to.match(
201 /^nexmo-node\/[\d.]* node\/[\d.]*$/
202 );
203 });
204
205 it("should append to the user agent when a appendToUserAgent option is passed", function() {
206 var options = {
207 appendToUserAgent: "nexmo-cli/1.0.0"
208 };
209 var nexmo = new Nexmo(
210 {
211 apiKey: "test",
212 apiSecret: "test"
213 },
214 options
215 );
216 expect(nexmo.options.userAgent).to.match(
217 /nexmo-node\/[\d.]* node\/[\d.]* nexmo-cli\/1\.0\.0/
218 );
219 });
220
221 it("should create a JWT", () => {
222 var nexmo = new Nexmo({
223 apiKey: "test",
224 apiSecret: "test",
225 privateKey: path.join(__dirname, "private-test.key"),
226 application_id: "app-id"
227 });
228 var token = nexmo.generateJwt();
229 expect(token).to.be.a("string");
230 });
231
232 it("should create same JWT as static function", () => {
233 var iat = parseInt(Date.now() / 1000, 10);
234 var jti = "some_jti";
235 var appId = "app_id";
236 var privateKey = path.join(__dirname, "private-test.key");
237
238 var expectedJwt = Nexmo.generateJwt(privateKey, {
239 application_id: appId,
240 iat: iat,
241 jti: jti
242 });
243
244 var nexmo = new Nexmo({
245 apiKey: "test",
246 apiSecret: "test",
247 privateKey: privateKey,
248 applicationId: appId
249 });
250 var token = nexmo.generateJwt({ iat: iat, jti: jti });
251 expect(token).to.equal(expectedJwt);
252 });
253});