2018-08-28 16:35:28 +03:00
|
|
|
import assert from "assert";
|
2019-03-19 20:29:27 +03:00
|
|
|
import { TriggerOperation, TriggerType } from "../../dist-esm";
|
2019-04-04 01:07:52 +03:00
|
|
|
import { TriggerDefinition, Container } from "../../dist-esm/client";
|
|
|
|
import { getTestContainer, removeAllDatabases } from "../common/TestHelpers";
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-06-30 02:51:57 +03:00
|
|
|
const notFoundErrorCode = 404;
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-06-30 02:51:57 +03:00
|
|
|
// Mock for trigger function bodies
|
|
|
|
declare var getContext: any;
|
2018-06-05 00:03:49 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
describe("NodeJS CRUD Tests", function() {
|
|
|
|
this.timeout(process.env.MOCHA_TIMEOUT || 10000);
|
2019-04-04 01:07:52 +03:00
|
|
|
let container: Container;
|
2018-06-30 02:51:57 +03:00
|
|
|
|
2018-07-13 02:30:18 +03:00
|
|
|
beforeEach(async function() {
|
2018-07-24 04:06:35 +03:00
|
|
|
await removeAllDatabases();
|
2019-04-04 01:07:52 +03:00
|
|
|
container = await getTestContainer("trigger container");
|
2018-07-13 02:30:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Validate Trigger CRUD", function() {
|
|
|
|
it("nativeApi Should do trigger CRUD operations successfully name based", async function() {
|
|
|
|
// read triggers
|
2019-04-04 01:07:52 +03:00
|
|
|
const { resources: triggers } = await container.scripts.triggers.readAll().fetchAll();
|
2018-07-13 02:30:18 +03:00
|
|
|
assert.equal(Array.isArray(triggers), true);
|
|
|
|
|
|
|
|
// create a trigger
|
|
|
|
const beforeCreateTriggersCount = triggers.length;
|
|
|
|
// tslint:disable:no-var-keyword
|
|
|
|
// tslint:disable:prefer-const
|
|
|
|
const triggerDefinition: TriggerDefinition = {
|
|
|
|
id: "sample trigger",
|
|
|
|
body: "serverScript() { var x = 10; }",
|
2018-12-14 16:12:55 +03:00
|
|
|
triggerType: TriggerType.Pre,
|
|
|
|
triggerOperation: TriggerOperation.All
|
2018-07-13 02:30:18 +03:00
|
|
|
};
|
|
|
|
// tslint:enable:no-var-keyword
|
|
|
|
// tslint:enable:prefer-const
|
|
|
|
|
2019-04-04 01:07:52 +03:00
|
|
|
const { resource: trigger } = await container.scripts.triggers.create(triggerDefinition);
|
2018-07-13 02:30:18 +03:00
|
|
|
|
|
|
|
assert.equal(trigger.id, triggerDefinition.id);
|
|
|
|
assert.equal(trigger.body, "serverScript() { var x = 10; }");
|
|
|
|
|
|
|
|
// read triggers after creation
|
2019-04-04 01:07:52 +03:00
|
|
|
const { resources: triggersAfterCreation } = await container.scripts.triggers.readAll().fetchAll();
|
2018-07-13 02:30:18 +03:00
|
|
|
assert.equal(
|
|
|
|
triggersAfterCreation.length,
|
|
|
|
beforeCreateTriggersCount + 1,
|
|
|
|
"create should increase the number of triggers"
|
|
|
|
);
|
|
|
|
|
|
|
|
// query triggers
|
|
|
|
const querySpec = {
|
|
|
|
query: "SELECT * FROM root r WHERE r.id=@id",
|
|
|
|
parameters: [
|
|
|
|
{
|
|
|
|
name: "@id",
|
|
|
|
value: triggerDefinition.id
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2019-04-04 01:07:52 +03:00
|
|
|
const { resources: results } = await container.scripts.triggers.query(querySpec).fetchAll();
|
2018-07-13 02:30:18 +03:00
|
|
|
assert(results.length > 0, "number of results for the query should be > 0");
|
|
|
|
|
|
|
|
// replace trigger
|
|
|
|
// prettier-ignore
|
2019-03-07 23:32:56 +03:00
|
|
|
trigger.body = function () { const x = 20; };
|
2019-04-04 01:07:52 +03:00
|
|
|
const { resource: replacedTrigger } = await container.scripts.trigger(trigger.id).replace(trigger);
|
2018-07-13 02:30:18 +03:00
|
|
|
|
|
|
|
assert.equal(replacedTrigger.id, trigger.id);
|
|
|
|
assert.equal(replacedTrigger.body, "function () { const x = 20; }");
|
|
|
|
|
|
|
|
// read trigger
|
2019-04-04 01:07:52 +03:00
|
|
|
const { resource: triggerAfterReplace } = await container.scripts.trigger(replacedTrigger.id).read();
|
2018-07-13 02:30:18 +03:00
|
|
|
assert.equal(replacedTrigger.id, triggerAfterReplace.id);
|
|
|
|
|
|
|
|
// delete trigger
|
2019-04-04 01:07:52 +03:00
|
|
|
await await container.scripts.trigger(replacedTrigger.id).delete();
|
2018-07-13 02:30:18 +03:00
|
|
|
|
|
|
|
// read triggers after deletion
|
|
|
|
try {
|
2019-04-04 01:07:52 +03:00
|
|
|
await container.scripts.trigger(replacedTrigger.id).read();
|
2018-07-13 02:30:18 +03:00
|
|
|
assert.fail("Must fail to read after deletion");
|
|
|
|
} catch (err) {
|
|
|
|
assert.equal(err.code, notFoundErrorCode, "response should return error code 404");
|
|
|
|
}
|
2018-06-30 02:51:57 +03:00
|
|
|
});
|
2018-07-13 02:30:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("validate trigger functionality", function() {
|
|
|
|
const triggers: TriggerDefinition[] = [
|
|
|
|
{
|
|
|
|
id: "t1",
|
|
|
|
// tslint:disable:no-var-keyword
|
|
|
|
// tslint:disable:prefer-const
|
|
|
|
// tslint:disable:curly
|
|
|
|
// tslint:disable:no-string-throw
|
|
|
|
// tslint:disable:object-literal-shorthand
|
|
|
|
body: function() {
|
|
|
|
var item = getContext()
|
|
|
|
.getRequest()
|
|
|
|
.getBody();
|
|
|
|
item.id = item.id.toUpperCase() + "t1";
|
|
|
|
getContext()
|
|
|
|
.getRequest()
|
|
|
|
.setBody(item);
|
|
|
|
},
|
2018-12-14 16:12:55 +03:00
|
|
|
triggerType: TriggerType.Pre,
|
|
|
|
triggerOperation: TriggerOperation.All
|
2018-07-13 02:30:18 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "t2",
|
|
|
|
body: "function() { }", // trigger already stringified
|
2018-12-14 16:12:55 +03:00
|
|
|
triggerType: TriggerType.Pre,
|
|
|
|
triggerOperation: TriggerOperation.All
|
2018-07-13 02:30:18 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "t3",
|
|
|
|
body: function() {
|
|
|
|
const item = getContext()
|
|
|
|
.getRequest()
|
|
|
|
.getBody();
|
|
|
|
item.id = item.id.toLowerCase() + "t3";
|
|
|
|
getContext()
|
|
|
|
.getRequest()
|
|
|
|
.setBody(item);
|
|
|
|
},
|
2018-12-14 16:12:55 +03:00
|
|
|
triggerType: TriggerType.Pre,
|
|
|
|
triggerOperation: TriggerOperation.All
|
2018-07-13 02:30:18 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "response1",
|
|
|
|
body: function() {
|
|
|
|
const prebody = getContext()
|
|
|
|
.getRequest()
|
|
|
|
.getBody();
|
|
|
|
if (prebody.id !== "TESTING POST TRIGGERt1") throw "name mismatch";
|
|
|
|
const postbody = getContext()
|
|
|
|
.getResponse()
|
|
|
|
.getBody();
|
|
|
|
if (postbody.id !== "TESTING POST TRIGGERt1") throw "name mismatch";
|
|
|
|
},
|
2018-12-14 16:12:55 +03:00
|
|
|
triggerType: TriggerType.Post,
|
|
|
|
triggerOperation: TriggerOperation.All
|
2018-07-13 02:30:18 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "triggerOpType",
|
|
|
|
body: "function() { }",
|
2018-12-14 16:12:55 +03:00
|
|
|
triggerType: TriggerType.Post,
|
|
|
|
triggerOperation: TriggerOperation.Delete
|
2018-07-13 02:30:18 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
// tslint:enable:no-var-keyword
|
|
|
|
// tslint:enable:prefer-const
|
|
|
|
// tslint:enable:curly
|
|
|
|
// tslint:enable:no-string-throw
|
|
|
|
// tslint:enable:object-literal-shorthand
|
|
|
|
|
|
|
|
it("should do trigger operations successfully with create", async function() {
|
|
|
|
for (const trigger of triggers) {
|
2019-04-04 01:07:52 +03:00
|
|
|
await container.scripts.triggers.create(trigger);
|
2018-07-13 02:30:18 +03:00
|
|
|
}
|
|
|
|
// create document
|
2019-03-07 23:32:56 +03:00
|
|
|
const { resource: document } = await container.items.create(
|
2018-07-13 02:30:18 +03:00
|
|
|
{ id: "doc1", key: "value" },
|
|
|
|
{ preTriggerInclude: "t1" }
|
|
|
|
);
|
|
|
|
assert.equal(document.id, "DOC1t1", "name should be capitalized");
|
2019-03-07 23:32:56 +03:00
|
|
|
const { resource: document2 } = await container.items.create(
|
2018-07-13 02:30:18 +03:00
|
|
|
{ id: "doc2", key2: "value2" },
|
|
|
|
{ preTriggerInclude: "t2" }
|
|
|
|
);
|
|
|
|
assert.equal(document2.id, "doc2", "name shouldn't change");
|
2019-03-07 23:32:56 +03:00
|
|
|
const { resource: document3 } = await container.items.create(
|
2018-07-13 02:30:18 +03:00
|
|
|
{ id: "Doc3", prop: "empty" },
|
|
|
|
{ preTriggerInclude: "t3" }
|
|
|
|
);
|
|
|
|
assert.equal(document3.id, "doc3t3");
|
2019-03-07 23:32:56 +03:00
|
|
|
const { resource: document4 } = await container.items.create(
|
2018-07-13 02:30:18 +03:00
|
|
|
{ id: "testing post trigger" },
|
|
|
|
{ postTriggerInclude: "response1", preTriggerInclude: "t1" }
|
|
|
|
);
|
|
|
|
assert.equal(document4.id, "TESTING POST TRIGGERt1");
|
2019-03-07 23:32:56 +03:00
|
|
|
const { resource: document5, headers } = await container.items.create(
|
2018-07-13 02:30:18 +03:00
|
|
|
{ id: "responseheaders" },
|
|
|
|
{ preTriggerInclude: "t1" }
|
|
|
|
);
|
|
|
|
assert.equal(document5.id, "RESPONSEHEADERSt1");
|
|
|
|
try {
|
|
|
|
await container.items.create({ id: "Docoptype" }, { postTriggerInclude: "triggerOpType" });
|
|
|
|
assert.fail("Must fail");
|
|
|
|
} catch (err) {
|
|
|
|
assert.equal(err.code, 400, "Must throw when using a DELETE trigger on a CREATE operation");
|
|
|
|
}
|
2018-06-05 00:03:49 +03:00
|
|
|
});
|
2018-07-13 02:30:18 +03:00
|
|
|
});
|
2018-06-05 00:03:49 +03:00
|
|
|
});
|