Skip to content

Commit 4508d00

Browse files
committed
test(shiptheory) test config and authentication
1 parent 50354a2 commit 4508d00

File tree

6 files changed

+121
-3
lines changed

6 files changed

+121
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Run serverless functions on Netlify to work with the Foxy.io hypermedia API.",
55
"private": false,
66
"scripts": {
7-
"test": "nyc mocha \"src/**/*.test.js\"",
7+
"test": "nyc mocha \"test/**/*.test.js\"",
88
"start": "netlify dev",
99
"start-http": "http-server ./src/functions --port 9000 -c-1"
1010
},

src/functions/shiptheory/DataStore.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class DataStore extends DataStoreBase {
4040
*/
4141
async authenticate() {
4242
if (!this.token) {
43-
console.log('credentials', this.credentials);
4443
const response = await fetch(this.buildEndpoint('token'), {
4544
body: JSON.stringify({
4645
email: this.credentials.email,
@@ -50,7 +49,6 @@ class DataStore extends DataStoreBase {
5049
method: 'POST'
5150
});
5251
const parsed = await response.json();
53-
console.log('parsed', parsed);
5452
this.token = parsed.success && parsed.data.token;
5553
}
5654
return !!this.token;

src/functions/shiptheory/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This integration allows you to create shipments in ShipTheory for Foxy.io transa
99
- Grab the function URL
1010
- Configure your Foxy Store to use that URL
1111

12+
You will also need to configure your SipTheory rules.
1213

1314
# Reference
1415

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const { after, afterEach, before, beforeEach, describe, it } = require("mocha");
2+
const {expect} = require("chai");
3+
const rewire = require("rewire");
4+
const DataStore = rewire("../../../src/functions/shiptheory/DataStore.js");
5+
const mockShipTheoryAPI = require("./MockShipTheoryAPI");
6+
7+
8+
9+
10+
describe("ShipTheory Client", function() {
11+
let restore;
12+
before(
13+
function() {
14+
restore = DataStore.__set__('fetch', mockShipTheoryAPI.fetch);
15+
}
16+
);
17+
18+
after(
19+
function() {
20+
restore();
21+
}
22+
);
23+
24+
it ("Should be configured usig environment variables.", function() {
25+
const ds = new DataStore();
26+
process.env["FOXY_SHIPTHEORY_EMAIL"] = 'email';
27+
process.env["FOXY_SHIPTHEORY_PASSWORD"] = 'password';
28+
ds.setCredentials();
29+
expect(ds.credentials).to.exist;
30+
expect(ds.credentials).to.deep.equal({email:'email', password:'password'});
31+
});
32+
33+
it ("Should always add Accept and Content-Type to headers", function() {
34+
const ds = new DataStore();
35+
const defaultHeader = ds.getDefaultHeader();
36+
expect(defaultHeader.Accept).to.exist;
37+
expect(defaultHeader['Content-Type']).to.exist;
38+
expect(defaultHeader.Accept).to.equal('application/json');
39+
});
40+
41+
it ("Should authenticate in ShipTheory", async function() {
42+
const ds = new DataStore();
43+
const result = await ds.authenticate();
44+
expect(ds.token).to.exist;
45+
expect(ds.token).to.equal('token');;
46+
});
47+
48+
49+
50+
51+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
async function fetch(endpoint, options) {
4+
if (endpoint.match(/token/)){
5+
return {
6+
json: async function() {
7+
return {
8+
data: {token: 'token'},
9+
success: true,
10+
}
11+
}
12+
}
13+
} else if (enpoint.match(/shipments/)) {
14+
return {
15+
json: async function() {
16+
return {
17+
success: true,
18+
}
19+
}
20+
}
21+
}
22+
}
23+
24+
25+
exports.fetch = fetch
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { after, afterEach, before, beforeEach, describe, it } = require("mocha");
2+
const {expect} = require("chai");
3+
const handler = require("../../../src/functions/shiptheory/index.js");
4+
const sinon = require("sinon");
5+
6+
describe("Shiptheory", function() {
7+
8+
describe("Validation", function() {
9+
describe ("Should warn about invalid configuration", async function () {
10+
let stub;
11+
beforeEach(
12+
function () {
13+
stub = sinon.stub(console, "error");
14+
}
15+
);
16+
afterEach(
17+
function () {
18+
stub.restore();
19+
}
20+
);
21+
const requiredVariables = {
22+
"FOXY_SHIPTHEORY_EMAIL": "[email protected]",
23+
"FOXY_SHIPTHEORY_PASSWORD": "foobar",
24+
"FOXY_WEBHOOK_ENCRYPTION_KEY": "12341234"
25+
};
26+
for (let k of Object.keys(requiredVariables)) {
27+
const missingOne = {...requiredVariables};
28+
delete missingOne[k];
29+
process.env = missingOne;
30+
it ("Should detect invalid " + k, async function() {
31+
await handler.handler({});
32+
stub.calledWithMatch(k);
33+
});
34+
}
35+
});
36+
});
37+
38+
39+
describe("Authentication", function() {
40+
});
41+
42+
43+
});

0 commit comments

Comments
 (0)