UNPKG

7.15 kBJavaScriptView Raw
1import Message from "../lib/Message";
2import Credentials from "../lib/Credentials";
3import HttpClient from "../lib/HttpClient";
4import NullLogger from "../lib/ConsoleLogger.js";
5
6import NexmoStub from "./NexmoStub";
7import ResourceTestHelper from "./ResourceTestHelper";
8
9import sinon from "sinon";
10import chai, { expect } from "chai";
11
12var smsAPIs = {
13 sendBinaryMessage: "sendBinaryMessage",
14 sendWapPushMessage: "sendWapPushMessage",
15 sendTextMessage: "sendSms",
16 shortcodeAlert: "shortcodeAlert",
17 shortcode2FA: "shortcode2FA",
18 shortcodeMarketing: "shortcodeMarketing"
19};
20
21describe("Message Object", function() {
22 it("should implement all v1 APIs", function() {
23 NexmoStub.checkAllFunctionsAreDefined(smsAPIs, Message);
24 });
25
26 it("should proxy the function call to the underlying `nexmo` object", function() {
27 NexmoStub.checkAllFunctionsAreCalled(smsAPIs, Message);
28 });
29});
30
31describe("Message", function() {
32 beforeEach(function() {
33 var creds = Credentials.parse({
34 apiKey: "myKey",
35 apiSecret: "mySecret"
36 });
37
38 this.httpClientStub = new HttpClient(
39 {
40 logger: new NullLogger()
41 },
42 creds
43 );
44
45 sinon.stub(this.httpClientStub, "request");
46
47 var options = {
48 rest: this.httpClientStub
49 };
50
51 this.message = new Message(creds, options);
52 });
53
54 describe("#search", function() {
55 it("should call the correct endpoint (single)", function(done) {
56 this.httpClientStub.request.yields(null, {});
57
58 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch({
59 path: "/search/message?id=0D00000068264896"
60 });
61
62 this.message.search(
63 "0D00000068264896",
64 function(err, data) {
65 expect(this.httpClientStub.request).to.have.been.calledWith(
66 sinon.match(expectedRequestArgs)
67 );
68
69 done();
70 }.bind(this)
71 );
72 });
73
74 it("should call the correct endpoint (multiple)", function(done) {
75 this.httpClientStub.request.yields(null, {});
76
77 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch({
78 path: "/search/messages?ids=1&ids=2"
79 });
80
81 this.message.search(
82 [1, 2],
83 function(err, data) {
84 expect(this.httpClientStub.request).to.have.been.calledWith(
85 sinon.match(expectedRequestArgs)
86 );
87
88 done();
89 }.bind(this)
90 );
91 });
92
93 it("returns data on a successful request (single)", function(done) {
94 const mockData = {
95 "message-id": "0D00000068264896",
96 "account-id": "abc123",
97 network: "23430",
98 from: "TestTest",
99 to: "442079460000",
100 body: "Hello",
101 price: "0.03330000",
102 "date-received": "2017-11-24 15:09:30",
103 "final-status": "DELIVRD",
104 "date-closed": "2017-11-24 15:09:45",
105 latency: 14806,
106 type: "MT"
107 };
108
109 this.httpClientStub.request.yields(null, mockData);
110 this.message.search("0D00000068264896", function(err, data) {
111 expect(err).to.eql(null);
112 expect(data).to.eql(mockData);
113 done();
114 });
115 });
116
117 it("returns data on a successful request (multiple)", function(done) {
118 const mockData = {
119 count: 1,
120 items: [
121 {
122 "message-id": "0D00000068264896",
123 "account-id": "abc123",
124 network: "23430",
125 from: "TestTest",
126 to: "442079460000",
127 body: "Hello",
128 price: "0.03330000",
129 "date-received": "2017-11-24 15:09:30",
130 "final-status": "DELIVRD",
131 "date-closed": "2017-11-24 15:09:45",
132 latency: 14806,
133 type: "MT"
134 }
135 ]
136 };
137
138 this.httpClientStub.request.yields(null, mockData);
139 this.message.search(["0D00000068264896"], function(err, data) {
140 expect(err).to.eql(null);
141 expect(data).to.eql(mockData);
142 done();
143 });
144 });
145
146 it("returns an error when the connection fails", function(done) {
147 const mockError = {
148 body: {
149 "error-code": "401",
150 "error-code-label": "authentication failed"
151 },
152 headers: {
153 "content-type": "application/json;charset=UTF-8",
154 date: "Thu, 30 Nov 2017 14:41:50 GMT",
155 server: "nginx",
156 "strict-transport-security": "max-age=31536000; includeSubdomains",
157 "x-frame-options": "deny",
158 "x-nexmo-trace-id": "91f401d459aa5050af280aee53288135",
159 "x-xss-protection": "1; mode=block;",
160 "content-length": "63",
161 connection: "close"
162 },
163 statusCode: 401
164 };
165
166 this.httpClientStub.request.yields(mockError, null);
167 this.message.search("0D00000068264896", function(err, data) {
168 expect(err).to.eql(mockError);
169 expect(data).to.eql(null);
170 done();
171 });
172 });
173 });
174
175 describe("#searchRejections", function() {
176 it("should call the correct endpoint (multiple)", function(done) {
177 this.httpClientStub.request.yields(null, {});
178
179 var expectedRequestArgs = ResourceTestHelper.requestArgsMatch({
180 path: "/search/rejections?to=INVALID&date=2020-01-01"
181 });
182
183 this.message.searchRejections(
184 "INVALID",
185 "2020-01-01",
186 function(err, data) {
187 expect(this.httpClientStub.request).to.have.been.calledWith(
188 sinon.match(expectedRequestArgs)
189 );
190
191 done();
192 }.bind(this)
193 );
194 });
195
196 it("returns data on a successful request", function(done) {
197 const mockData = {
198 count: 1,
199 items: [
200 {
201 "account-id": "API_KEY",
202 from: "447700900000",
203 to: "INVALID",
204 "date-received": "2020-01-01 12:00:00",
205 "error-code": "3",
206 "error-code-label": "to address is not numeric"
207 }
208 ]
209 };
210
211 this.httpClientStub.request.yields(null, mockData);
212 this.message.search("0D00000068264896", function(err, data) {
213 expect(err).to.eql(null);
214 expect(data).to.eql(mockData);
215 done();
216 });
217 });
218
219 it("returns an error when invalid parameters are provided", function(done) {
220 const mockError = {
221 body: { "error-code": "400", "error-code-label": "wrong parameters" },
222 headers: {
223 "content-disposition": 'attachment; filename="api.txt"',
224 "content-type": "application/json;charset=UTF-8",
225 date: "Thu, 14 Dec 2017 11:40:08 GMT",
226 server: "nginx",
227 "strict-transport-security": "max-age=31536000; includeSubdomains",
228 "x-frame-options": "deny",
229 "x-nexmo-trace-id": "38ad97a406aa8cc104cecf21feaf7da3",
230 "x-xss-protection": "1; mode=block;",
231 "content-length": "58",
232 connection: "close"
233 },
234 statusCode: 400
235 };
236
237 this.httpClientStub.request.yields(mockError, null);
238 this.message.searchRejections("123456", null, function(err, data) {
239 expect(err).to.eql(mockError);
240 expect(data).to.eql(null);
241 done();
242 });
243 });
244 });
245});