-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateItems.test.js
More file actions
72 lines (60 loc) · 2.5 KB
/
CreateItems.test.js
File metadata and controls
72 lines (60 loc) · 2.5 KB
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
const sinon = require("sinon");
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const expect = chai.expect;
const {DOMParser} = require("xmldom");
const fetch = require("./fetch");
const createItems = require("./CreateItems");
const encode64 = require("./encode64");
const fakersm = require("./fake").response;
const RSM_CREATEITEMS_PATH = "/AppController/commands_RSM/api/api_createItem.php";
const RSM_HOST = "https://localhost";
describe("RSM CreateItems", () => {
let sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
it("should create two items", async () => {
const stub = sandbox.stub(fetch, "fetch")
.resolves(new DOMParser()
.parseFromString(
fakersm([{result: "OK", itemID: [9,10]}])));
const response = await createItems("fakeapi", RSM_HOST)
.item({"54": "something", "83": "otherthing"})
.item({"54": "potatoes", "83": "breakfast"})
.send();
expect(stub.calledOnceWithExactly(RSM_HOST, RSM_CREATEITEMS_PATH, {
RStoken: "fakeapi",
RSdata: `54:${encode64("something")};83:${encode64("otherthing")},54:${encode64("potatoes")};83:${encode64("breakfast")}`
})).to.be.true;
expect(response).to.deep.equal(["9","10"]);
});
it("should return permission denied", async () => {
const stub = sandbox.stub(fetch, "fetch")
.resolves(new DOMParser()
.parseFromString(
fakersm([{result: "NOK", description: "YOU DONT HAVE PERMISSIONS TO CREATE THIS ITEM"}])));
await expect(
createItems("otherfakeapi", RSM_HOST)
.item({"53": "something", "66": "otherthing"})
.item({"53": "potatoes", "68": "breakfast"})
.send()
).to.be.rejectedWith("NOK");
expect(stub.calledOnceWithExactly( RSM_HOST, RSM_CREATEITEMS_PATH,{
RStoken: "otherfakeapi",
RSdata: `53:${encode64("something")};66:${encode64("otherthing")},53:${encode64("potatoes")};68:${encode64("breakfast")}`
})).to.be.true;
});
it("should return Connection Failure", async () => {
const stub = sandbox.stub(fetch, "fetch")
.rejects(new Error("Request Failed"));
await expect(
createItems("otherfakeapi", RSM_HOST)
.item({"53": "something", "66": "otherthing"})
.send()
).to.be.rejectedWith("Request Failed");
expect(stub.calledOnceWithExactly( RSM_HOST, RSM_CREATEITEMS_PATH,{
RStoken: "otherfakeapi",
RSdata: `53:${encode64("something")};66:${encode64("otherthing")}`
})).to.be.true;
});
});