Skip to content

Commit

Permalink
Add ability to search SMS
Browse files Browse the repository at this point in the history
Resolves #139
  • Loading branch information
mheap committed Nov 30, 2017
1 parent 194f711 commit 5f1d621
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 4 deletions.
32 changes: 32 additions & 0 deletions src/Message.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

import querystring from "querystring";
import nexmo from "./index";

class Message {
Expand Down Expand Up @@ -64,6 +65,37 @@ class Message {
shortcodeMarketing() {
this._nexmo.shortcodeMarketing.apply(this._nexmo, arguments);
}

search(id, callback) {
this.options.httpClient.request(
this.getRequestConfig("GET", "/search/message", { id: id }),
callback
);
}

getRequestConfig(verb, path, params) {
params["api_key"] = this.options.auth.api_key;
params["api_secret"] = this.options.auth.api_secret;

var bodyParams = "";
params = querystring.stringify(params);

if (verb === "GET") {
path += "?" + params;
} else {
bodyParams = params;
}

return {
host: "rest.nexmo.com",
path: path,
method: verb,
body: bodyParams,
headers: {
"Content-Type": "application/json"
}
};
}
}

export default Message;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ exports.initialize = function(pkey, psecret, options) {
api_key: pkey,
api_secret: psecret
};

options.auth = up;
_options = options;
};

Expand Down
103 changes: 103 additions & 0 deletions test/Message-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import Message from "../lib/Message";
import Credentials from "../lib/Credentials";
import HttpClient from "../lib/HttpClient";
import NullLogger from "../lib/ConsoleLogger.js";

import NexmoStub from "./NexmoStub";
import ResourceTestHelper from "./ResourceTestHelper";

import sinon from "sinon";
import chai, { expect } from "chai";

var smsAPIs = {
sendBinaryMessage: "sendBinaryMessage",
Expand All @@ -20,3 +27,99 @@ describe("Message Object", function() {
NexmoStub.checkAllFunctionsAreCalled(smsAPIs, Message);
});
});

describe("Message", function() {
beforeEach(function() {
var creds = Credentials.parse({
apiKey: "myKey",
apiSecret: "mySecret"
});

this.httpClientStub = sinon.createStubInstance(HttpClient);

var options = {
httpClient: this.httpClientStub
};

this.message = new Message(creds, options);
});

describe("#search", function() {
it("should call the correct endpoint", function(done) {
this.httpClientStub.request.yields(null, { a: "b" });

var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
host: "rest.nexmo.com",
path: "/search/message?id=0D00000068264896",
method: "GET",
body: "",
headers: {
"Content-Type": "application/json"
}
});

this.message.search(
"0D00000068264896",
function(err, data) {
expect(this.httpClientStub.request).to.have.been.calledWith(
sinon.match(expectedRequestArgs)
);

done();
}.bind(this)
);
});

it("returns data on a successful request", function(done) {
const mockData = {
"message-id": "0D00000068264896",
"account-id": "abc123",
network: "23430",
from: "TestTest",
to: "442079460000",
body: "Hello",
price: "0.03330000",
"date-received": "2017-11-24 15:09:30",
"final-status": "DELIVRD",
"date-closed": "2017-11-24 15:09:45",
latency: 14806,
type: "MT"
};

this.httpClientStub.request.yields(null, mockData);
this.message.search("0D00000068264896", function(err, data) {
expect(err).to.eql(null);
expect(data).to.eql(mockData);
done();
});
});

it("returns an error when the connection fails", function(done) {
const mockError = {
body: {
"error-code": "401",
"error-code-label": "authentication failed"
},
headers: {
"content-type": "application/json;charset=UTF-8",
date: "Thu, 30 Nov 2017 14:41:50 GMT",
server: "nginx",
"strict-transport-security": "max-age=31536000; includeSubdomains",
"x-frame-options": "deny",
"x-nexmo-trace-id": "91f401d459aa5050af280aee53288135",
"x-xss-protection": "1; mode=block;",
"content-length": "63",
connection: "close"
},
statusCode: 401
};

this.httpClientStub.request.yields(mockError, null);
this.message.search("0D00000068264896", function(err, data) {
expect(err).to.eql(mockError);
expect(data).to.eql(null);
done();
});
});
});
});
26 changes: 22 additions & 4 deletions test/ResourceTestHelper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import querystring from "querystring";

class ResourceTestHelper {
static getRequestArgs(params, overrides = {}) {
var callsArgs = {
Expand Down Expand Up @@ -30,15 +32,31 @@ class ResourceTestHelper {
requestOverrides
);

// We strip api_key and api_secret out of `path` so that our tests
// only look for specific parameters
var qs = actual.path.split("?");
var qsParts = querystring.parse(qs[1]);
delete qsParts["api_key"];
delete qsParts["api_secret"];
if (Object.keys(qsParts).length){
actual.path = qs[0] + "?" + querystring.stringify(qsParts);
}

var match =
expected.host === actual.host &&
expected.path === actual.path &&
expected.method === actual.method &&
expected.body === actual.body &&
expected.headers["Content-Type"] === actual.headers["Content-Type"] &&
actual.headers["Authorization"].indexOf(
expected.headers["Authorization"]
) === 0;
expected.headers["Content-Type"] === actual.headers["Content-Type"];

// Some requests don't use the auth header, so only check if it's set
if (actual.headers["Authorization"]) {
match =
match &&
actual.headers["Authorization"].indexOf(
expected.headers["Authorization"]
) === 0;
}
return match;
};
}
Expand Down

0 comments on commit 5f1d621

Please sign in to comment.