This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
message-test.js
275 lines (233 loc) · 7.08 KB
/
message-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var nock = require("nock");
var CatapultClient = require("../index");
var baseUrl = "https://api.catapult.inetwork.com";
describe("Message API", function () {
var client;
var userId = "fakeUserId";
var apiToken = "fakeApiToken";
var apiSecret = "fakeapiSecret";
var newTestMessage = {
from : "+12345678901",
to : "+12345678902",
text : "Hello world."
};
var otherTestMessage = {
from : "+12345678902",
text : "Hello world."
};
var testMessage = {
"id" : "fakeMessageId",
"messageId" : "fakeMessageId",
"from" : "+12345678901",
"to" : "+12345678902",
"text" : "Good morning, this is a test message",
"time" : "2012-10-05T20:37:38.048Z",
"direction" : "out",
"state" : "sent",
"media" : []
};
var someOtherTestMessage = {
"id" : "fakeMessageId2",
"messageId" : "fakeMessageId2",
"from" : "+12345678902",
"to" : "+12345678901",
"text" : "I received your test message",
"time" : "2012-10-05T20:38:11.023Z",
"direction" : "in",
"state" : "received",
"media" : []
};
var messagesList = [ testMessage, someOtherTestMessage ];
var fromDateTime = "2012-10-04";
var toDateTime = "2012-10-06";
describe("global methods using single page response", function () {
before(function () {
client = new CatapultClient({
userId : userId,
apiToken : apiToken,
apiSecret : apiSecret
});
nock.disableNetConnect();
nock("https://api.catapult.inetwork.com")
.persist()
.post("/v1/users/" + userId + "/messages", newTestMessage)
.reply(201,
{},
{
"Location" : "/v1/users/" + userId + "/messages/fakeMessageId"
})
.post("/v1/users/" + userId + "/messages", [ newTestMessage, otherTestMessage ])
.reply(202,
[
{
result : "accepted",
location : "https://api.catapult.inetwork.com/v1/users/" + userId +
"/messages/fakeMessageId"
},
{
result : "error",
error : {
category : "bad-request",
code : "blank-property",
message : "The 'message' resource property 'to' must contain at least" +
" one non-whitespace character",
details : []
}
}
]
)
.get("/v1/users/" + userId + "/messages/" + testMessage.id)
.reply(200, testMessage)
.get("/v1/users/" + userId + "/messages?fromDateTime=" + fromDateTime + "&" + "toDateTime=" + toDateTime)
.reply(200, messagesList)
.patch("/v1/users/" + userId + "/messages/" + testMessage.id, { text : "" })
.reply(200);
});
after(function () {
nock.cleanAll();
nock.enableNetConnect();
});
it("should send a message, promise style", function () {
return client.Message.send(newTestMessage)
.then(function (message) {
message.should.eql(newTestMessage);
});
});
it("should send a message, callback style", function (done) {
client.Message.send(newTestMessage, function (err, message) {
if (err) {
throw err;
}
message.should.eql(newTestMessage);
done();
});
});
it("should send multiple messages", function () {
return client.Message.sendMultiple([ newTestMessage, otherTestMessage ])
.then(function (messages) {
messages[0].message.should.eql(newTestMessage);
messages[1].error.should.eql({
category : "bad-request",
code : "blank-property",
message : "The 'message' resource property 'to' must contain at " +
"least one non-whitespace character",
details : []
});
});
});
it("should get a message, promise style", function () {
return client.Message.get(testMessage.id)
.then(function (message) {
message.should.eql(testMessage);
});
});
it("should get a list of messages, promise style", function () {
return client.Message.list({
fromDateTime : fromDateTime,
toDateTime : toDateTime
})
.then(function (messageResponse) {
var messages = messageResponse.messages;
messages.should.eql(messagesList);
});
});
it("should contain the nextLink in response", function () {
return client.Message.list({
fromDateTime : fromDateTime,
toDateTime : toDateTime
})
.then(function (messageResponse) {
var nextLink = messageResponse.nextLink;
nextLink.should.eql({});
});
});
it("should get a list of messages, callback style", function (done) {
client.Message.list({
fromDateTime : fromDateTime,
toDateTime : toDateTime
}, function (err, messageResponse) {
if (err) {
throw err;
}
var messages = messageResponse.messages;
messages.should.eql(messagesList);
done();
});
});
it("should patch a message, promise style", function () {
return client.Message.patch(testMessage.id, { text : "" });
});
it("should patch a message, callback style", function (done) {
return client.Message.patch(testMessage.id, { text : "" }, done);
});
});
describe("list function with a multiple page response", function () {
before(function () {
nock.disableNetConnect();
nock("https://api.catapult.inetwork.com")
.persist()
.get("/v1/users/" + userId + "/messages")
.reply(200, messagesList, {
"link" : "<https://api.catapult.inetwork.com" +
"/v1/users/" + userId + "/messages?" + "sortKeyLT=1>; rel=\"next\""
})
.get("/v1/users/" + userId + "/messages?sortKeyLT=1")
.reply(200, []);
});
after(function () {
nock.cleanAll();
nock.enableNetConnect();
});
it("should get the next page of messages (if it exists)", function () {
//Simulating a response which has 1 page of messages
return client.Message.list()
.then(function (messageResponse) {
var messages = messageResponse.messages;
messages[0].should.eql(messagesList[0]);
messages[1].should.eql(messagesList[1]);
return messageResponse.getNextPage();
})
.then(function (otherMessageResponse) {
messages = otherMessageResponse.messages;
(messages[0] === undefined).should.be.true;
return otherMessageResponse.getNextPage();
})
.catch(function (err) {
err.should.eql("Next page does not exist.");
});
});
it("should return the nextLink header", function () {
//Simulating a response which has 1 page of messages
return client.Message.list()
.then(function (messageResponse) {
var nextLink = messageResponse.nextLink;
nextLink.should.eql({ sortKeyLT : "1" });
});
});
});
describe("list function with no messages available", function () {
before(function () {
nock.disableNetConnect();
nock("https://api.catapult.inetwork.com")
.persist()
.get("/v1/users/" + userId + "/messages")
.reply(200, []);
});
after(function () {
nock.cleanAll();
nock.enableNetConnect();
});
it("should not get the next page of messages", function () {
//Simulating a response which has 0 pages of messages
return client.Message.list()
.then(function (messageResponse) {
var messages = messageResponse.messages;
(messages[0] === undefined).should.be.true;
return messageResponse.getNextPage();
})
.catch(function (err) {
err.should.eql("Next page does not exist.");
});
});
});
});