Skip to content

Commit

Permalink
Merge pull request #2 from hyperwallet/feature/webhook-implementation
Browse files Browse the repository at this point in the history
Webhook endpoints implementation
  • Loading branch information
fkrauthan-hyperwallet committed Jan 9, 2017
2 parents d560cf3 + d705961 commit a22aa3b
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Hyperwallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,35 @@ export default class Hyperwallet {
this.client.doGet(`users/${encodeURIComponent(userToken)}/prepaid-cards/${encodeURIComponent(prepaidCardToken)}/receipts`, options, Hyperwallet.handle204Response(callback));
}

//--------------------------------------
// Webhooks: Notifications
//-------------------------------------

/**
* List webhook notifications
*
* @param {Object} options - The query parameters to send
* @param {api-callback} callback - The callback for this call
*/
listWebhookNotifications(options, callback) {
this.client.doGet("webhook-notifications", options, Hyperwallet.handle204Response(callback));
}

/**
* Get a single webhook notification
*
* @param {string} webhookToken - Webhook token
* @param {api-callback} callback - The callback for this call
*
* @throws Will throw an error if webhookToken is not provided
*/
getWebhookNotification(webhookToken, callback) {
if (!webhookToken) {
throw new Error("webhookToken is required");
}
this.client.doGet(`webhook-notifications/${encodeURIComponent(webhookToken)}`, {}, callback);
}

//--------------------------------------
// Internal utils
//--------------------------------------
Expand Down
89 changes: 89 additions & 0 deletions test/Hyperwallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,95 @@ describe("Hyperwallet", () => {
});
});

//--------------------------------------
// Webhooks
//--------------------------------------

describe("listWebhookNotifications()", () => {
let client;
let apiClientSpy;

beforeEach(() => {
apiClientSpy = sinon.spy();
client = new Hyperwallet({
username: "test-username",
password: "test-password",
});
client.client = {
doGet: apiClientSpy,
};
});

/** @test {Hyperwallet#listWebhookNotifications} */
it("should do get call with options", () => {
const callback = () => null;
client.listWebhookNotifications({ test: "value" }, callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("webhook-notifications", { test: "value" });
});

/** @test {Hyperwallet#listWebhookNotifications} */
it("should do get call without options", () => {
const callback = () => null;
client.listWebhookNotifications({}, callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("webhook-notifications", {});
});

/** @test {Hyperwallet#listWebhookNotifications} */
it("should handle 204 return", (cb) => {
const callback = (err, data) => {
data.should.be.deep.equal({
count: 0,
data: [],
});

cb();
};
client.listWebhookNotifications({}, callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("webhook-notifications", {});

apiClientSpy.getCall(0).args[2](undefined, {}, {
status: 204,
});
});
});

describe("getWebhookNotification", () => {
let client;
let apiClientSpy;

beforeEach(() => {
apiClientSpy = sinon.spy();
client = new Hyperwallet({
username: "test-username",
password: "test-password",
});
client.client = {
doGet: apiClientSpy,
};
});

/** @test {Hyperwallet#getWebhookNotification} */
it("should throw error if webhookToken is missing", () => {
const callback = () => null;
expect(() => client.getWebhookNotification(undefined, {}, callback)).to.throw("webhookToken is required");
});

/** @test {Hyperwallet#getWebhookNotification} */
it("should do get call if webhookToken is provided", () => {
const callback = () => null;
client.getWebhookNotification("webhook-token", callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("webhook-notifications/webhook-token", {}, callback);
});
});

//--------------------------------------
// Internal utils
//--------------------------------------
Expand Down

0 comments on commit a22aa3b

Please sign in to comment.