Skip to content

Commit 2c8bb65

Browse files
committed
test(foxywebhook): basic tests
1 parent 4508d00 commit 2c8bb65

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

test/foxy/FoxyWebhook.test.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const crypto = require("crypto");
2+
const FoxyWebhook = require('../../src/foxy/FoxyWebhook.js');
3+
const sinon = require("sinon");
4+
5+
const { expect } = require("chai");
6+
const { after, before, describe, it } = require("mocha");
7+
8+
function silenceLog() {
9+
log = sinon.stub(console, 'log');
10+
logError = sinon.stub(console, 'error');
11+
}
12+
13+
function restoreLog() {
14+
log.restore();
15+
logError.restore();
16+
}
17+
18+
describe("Handles Foxy Webhook Requests", function() {
19+
let log;
20+
let logError;
21+
22+
before(silenceLog);
23+
after(restoreLog);
24+
25+
describe("Should retrieve items from a Webhook Request", function() {
26+
it("Should retrieve items when they are present.", function() {
27+
expect(FoxyWebhook.getItems({_embedded: {'fx:items': [{}, {}]}}).length).to.equal(2);
28+
});
29+
30+
it("Should return an empty list if fx:items is not available", function() {
31+
expect(FoxyWebhook.getItems({_embedded: {'items': [{}, {}]}}).length).to.equal(0);
32+
expect(FoxyWebhook.getItems({items: {'items': [{}, {}]}}).length).to.equal(0);
33+
});
34+
35+
});
36+
});
37+
38+
describe("Builds Foxy Webhook Responses", function() {
39+
let log;
40+
let logError;
41+
42+
before(silenceLog);
43+
after(restoreLog);
44+
45+
it ("Should not accept error responses without details", function () {
46+
expect(() => FoxyWebhook.response("", 500)).to.throw(Error, /An error response needs to specify details/);
47+
expect(() => FoxyWebhook.response(null, 500)).to.throw(Error, /An error response needs to specify details/);
48+
expect(() => FoxyWebhook.response(undefined, 500)).to.throw(Error, /An error response needs to specify details/);
49+
});
50+
});
51+
52+
describe("Verifies Foxy Webhook Signatures", function () {
53+
let log;
54+
let logError;
55+
56+
before(silenceLog);
57+
after(restoreLog);
58+
it("Accepts the correct signature",function () {
59+
const foxySignature = crypto.createHmac('sha256', 'foo').update('bar').digest('hex');
60+
expect(FoxyWebhook.validSignature('bar', foxySignature, 'foo')).to.be.true;
61+
});
62+
63+
it("Rejects incorrect signatures",function () {
64+
const cases = [0, 1, 2];
65+
for (let c of cases) {
66+
const foxySignature = crypto.createHmac('sha256', 'foo').update('bar').digest('hex');
67+
const values = ['bar', foxySignature, 'foo'];
68+
values[c] = 'wrong';
69+
expect(FoxyWebhook.validSignature(...values)).to.be.false;
70+
values[c] = undefined;
71+
expect(FoxyWebhook.validSignature(...values)).to.be.false;
72+
}
73+
});
74+
});
75+
76+
describe("Builds useful error messages", function() {
77+
let log;
78+
let logError;
79+
80+
before(silenceLog);
81+
after(restoreLog);
82+
83+
describe("Responds useful messages", function () {
84+
it("Informs the invalid items when the price is wrong.", async function () {
85+
const message = FoxyWebhook.messagePriceMismatch([
86+
[{name: 'foo'}, {name: 'foo'}],
87+
[{name: 'bar'}, {name: 'bar'}],
88+
])
89+
expect(message).to.match(/foo/);
90+
expect(message).to.match(/bar/);
91+
});
92+
93+
it("Informs the items with insufficient inventory and the current available inventory.", async function () {
94+
const message = FoxyWebhook.messageInsufficientInventory([
95+
[{name: 'foo', quantity:3}, {inventory: 2, name: 'foo'}],
96+
[{name: 'bar', quantity:3}, {inventory: 1, name: 'bar'}],
97+
])
98+
expect(message).to.match(/2 available/);
99+
expect(message).to.match(/1 available/);
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)