From 5ce8cede0ffa79eed8e4c21edb2ecf39f3a926ab Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Thu, 3 Nov 2016 11:40:12 +0100 Subject: [PATCH 01/23] Rewrite throws to rejects on bad responses --- package.json | 1 + scenarios/get/1.js | 6 ++++-- scenarios/get/2.js | 6 ++++-- scenarios/get/3.js | 6 ++++-- scenarios/post/1.js | 3 ++- scenarios/post/2.js | 3 ++- scenarios/post/3.js | 3 ++- 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index a1aec66..361e63d 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "homepage": "https://github.com/sealcode/sealious-integration-tests#readme", "dependencies": { + "auto-load": "^2.1.0", "bluebird": "^3.4.6", "node-uuid": "^1.4.7", "request": "^2.74.0", diff --git a/scenarios/get/1.js b/scenarios/get/1.js index 42b7e62..2e5f34e 100644 --- a/scenarios/get/1.js +++ b/scenarios/get/1.js @@ -19,10 +19,12 @@ module.exports = function() { resolveWithFullResponse: true }).then((res) => { if (res.statusCode === 404) return res - else throw new Error('incorrect status code, received ' + res.statusCode) + // else throw new Error('incorrect status code, received ' + res.statusCode) + else reject('incorrect status code, received ' + res.statusCode) }).then((res) => { if (res.body.type === "not_found") return true - else throw new Error('incorrect type of response') + // else throw new Error('incorrect type of response') + else reject('incorrect type of response') }).then(() => { console.log("succcess!"); }); diff --git a/scenarios/get/2.js b/scenarios/get/2.js index e6e05fb..d3d86c8 100644 --- a/scenarios/get/2.js +++ b/scenarios/get/2.js @@ -19,10 +19,12 @@ module.exports = function() { resolveWithFullResponse: true }).then((res) => { if (res.statusCode === 404) return res - else throw new Error('incorrect status code, received ' + res.statusCode) + // else throw new Error('incorrect status code, received ' + res.statusCode) + else reject('incorrect status code, received ' + res.statusCode) }).then((res) => { if (res.body.type === "bad_subject") return true - else throw new Error('incorrect type of response') + // else throw new Error('incorrect type of response') + else reject('incorrect type of response') }).then(() => { console.log("succcess!"); }); diff --git a/scenarios/get/3.js b/scenarios/get/3.js index 60233c6..a0af1c4 100644 --- a/scenarios/get/3.js +++ b/scenarios/get/3.js @@ -19,10 +19,12 @@ module.exports = function() { resolveWithFullResponse: true }).then((res) => { if (res.statusCode === 200) return res - else throw new Error('incorrect status code, received ' + res.statusCode) + // else throw new Error('incorrect status code, received ' + res.statusCode) + else reject('incorrect status code, received ' + res.statusCode) }).then((res) => { if (res.body.length === 0) return true - else throw new Error('incorrect body') + // else throw new Error('incorrect body') + else reject('incorrect body') }).then(() => { console.log("succcess!"); }); diff --git a/scenarios/post/1.js b/scenarios/post/1.js index f193c68..3e10ce0 100644 --- a/scenarios/post/1.js +++ b/scenarios/post/1.js @@ -36,7 +36,8 @@ module.exports = function() { resolveWithFullResponse: true }).then((res) => { if (res.statusCode === 201) return res - else throw new Error('incorrect status code, received ' + res.statusCode) + // else throw new Error('incorrect status code, received ' + res.statusCode) + else reject('incorrect status code, received ' + res.statusCode) }).then((res) => { verify(res.body) }).then(() => { diff --git a/scenarios/post/2.js b/scenarios/post/2.js index be98d54..f51fe1c 100644 --- a/scenarios/post/2.js +++ b/scenarios/post/2.js @@ -27,7 +27,8 @@ module.exports = function() { resolveWithFullResponse: true }).then((res) => { if (res.statusCode === 400) return res - else throw new Error('incorrect status code, received ' + res.statusCode) + // else throw new Error('incorrect status code, received ' + res.statusCode) + else reject('incorrect status code, received ' + res.statusCode) }).then(() => { console.log("succcess!"); }); diff --git a/scenarios/post/3.js b/scenarios/post/3.js index bb490e6..190c2a7 100644 --- a/scenarios/post/3.js +++ b/scenarios/post/3.js @@ -22,7 +22,8 @@ module.exports = function() { resolveWithFullResponse: true }).then((res) => { if (res.statusCode === 400) return res - else throw new Error('incorrect status code, received ' + res.statusCode) + // else throw new Error('incorrect status code, received ' + res.statusCode) + else reject('incorrect status code, received ' + res.statusCode) }).then((res) => { console.log(res.body) // "error":"Bad Request","message":"Invalid multipart payload format" From 79f1e551df91a30f05422c534fb1cd7dbfef3995 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Thu, 3 Nov 2016 12:33:24 +0100 Subject: [PATCH 02/23] Fix all then and catch chains --- index.js | 32 +++++++++++++++++++++----------- package.json | 3 +++ pre-test.js | 19 +++++++++++++++++-- scenarios/get/1.js | 32 ++++++++++++++++++-------------- scenarios/get/2.js | 30 ++++++++++++++++-------------- scenarios/get/3.js | 27 +++++++++++++++++---------- scenarios/login.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ scenarios/post/1.js | 18 ++++++++++-------- scenarios/post/2.js | 16 +++++++++------- scenarios/post/3.js | 26 +++++++++++++++----------- 10 files changed, 170 insertions(+), 77 deletions(-) create mode 100644 scenarios/login.js diff --git a/index.js b/index.js index 0f9503f..38346a3 100644 --- a/index.js +++ b/index.js @@ -1,20 +1,30 @@ "use strict"; const Promise = require("bluebird"); -var scenarios = require('auto-load')('scenarios') +const pre = require("./pre-test.js"); +var scenarios = require('auto-load')('scenarios'); -require("./pre-test.js")() -.then(() => { - // scenarios/[method]/[scenario (key)] - // e.g. scenarios/post/1.js +pre() +.then(function(){ return Promise.each( Object.keys(scenarios), (method) => { - for (var key in scenarios[method]) { - return scenarios[method][key]().then(() => console.log('→ scenario done')) + if(scenarios[method] instanceof Function){ + return scenarios[method](); + }else{ + const promises = {}; + for (var key in scenarios[method]) { + promises[key] = scenarios[method][key]().then(() => console.log('→ scenario done')); + } + return Promise.props(promises); } } - ) -}) -.then(() => { - console.log("Tests run complete. Exiting with status 0."); + ); +}).then(()=> { + console.log("\n\n\t✓ Success! Tests run complete. Exiting with status 0.\n\n"); process.exit(0); }) +.catch((err) => { + console.error(err); + console.log(err.stack); + console.log("\n\n\t✗ Tests failed. Exiting with status 1. See the above output for details.\n\n"); + process.exit(1); +}); diff --git a/package.json b/package.json index 361e63d..33cf937 100644 --- a/package.json +++ b/package.json @@ -29,5 +29,8 @@ "sealious": "github:sealcode/sealious#alpha", "sealious-datastore-mongo": "sealcode/sealious-datastore-mongo#alpha", "sealious-www-server": "github:sealcode/sealious-www-server#alpha" + }, + "devDependencies": { + "request-debug": "^0.2.0" } } diff --git a/pre-test.js b/pre-test.js index ff09dfe..9a8570d 100644 --- a/pre-test.js +++ b/pre-test.js @@ -19,7 +19,22 @@ module.exports = function(){ fields: [ {name: "number", type: "int"} ] - }) + }); - return App.start(); + App.createCollection({ + name: "restricted", + fields: [], + access_strategy: { + default: "logged_in" + } + }); + + return App.start() + .then(function(){ + const datastore = App.ChipManager.get_datastore_chip(); + return datastore.remove("users", {}); + //.then(() => datastore.remove("sessions", {})); + }).then(function(){ + return App; + }); }; diff --git a/scenarios/get/1.js b/scenarios/get/1.js index 2e5f34e..f8622df 100644 --- a/scenarios/get/1.js +++ b/scenarios/get/1.js @@ -8,24 +8,28 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); -var uri = (path) => "http://localhost:8080/api/v1/" + path; +var uri = (path) => "http://localhost:8081/api/v1/" + path; module.exports = function() { - console.log('scenario #1') - return rp.get({ url: uri("collections/people/" + "_"), json: true, resolveWithFullResponse: true - }).then((res) => { - if (res.statusCode === 404) return res - // else throw new Error('incorrect status code, received ' + res.statusCode) - else reject('incorrect status code, received ' + res.statusCode) - }).then((res) => { - if (res.body.type === "not_found") return true - // else throw new Error('incorrect type of response') - else reject('incorrect type of response') - }).then(() => { - console.log("succcess!"); - }); + }) + // .then((res) => { + // if (res.body.type === "not_found") return res + // }).catch((res) => { + // throw new Error("Incorrect type of body, it should be `not_found`, received: "+res.body.type) + // }) + + .then((res) => { + throw new Error("Should have thrown a 404 error!"); + }) + .catch((res) => { + if(res.statusCode !== 404 || res.error.message.type !== "not_found"){ + throw new Error("should have thrown a 404 not_found error"); + } + }) + + .then(() => console.log("succcess!")) }; diff --git a/scenarios/get/2.js b/scenarios/get/2.js index d3d86c8..d074941 100644 --- a/scenarios/get/2.js +++ b/scenarios/get/2.js @@ -8,24 +8,26 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); -var uri = (path) => "http://localhost:8080/api/v1/" + path; +var uri = (path) => "http://localhost:8081/api/v1/" + path; module.exports = function() { - console.log('scenario #2') - return rp.get({ url: uri("collections/peopl"), //`peopl` instead of `people` json: true, resolveWithFullResponse: true - }).then((res) => { - if (res.statusCode === 404) return res - // else throw new Error('incorrect status code, received ' + res.statusCode) - else reject('incorrect status code, received ' + res.statusCode) - }).then((res) => { - if (res.body.type === "bad_subject") return true - // else throw new Error('incorrect type of response') - else reject('incorrect type of response') - }).then(() => { - console.log("succcess!"); - }); + }) + .then(() => { + throw new Error("Should have thrown a 404 error!"); + }) + .catch((res) => { + if (res.statusCode === 404){ + if (res.error.message.type === "bad_subject"){ + return true; + } else { + throw new Error('incorrect status code, received ' + res.statusCode); + } + } + }) + + .then(() => console.log("succcess!")) }; diff --git a/scenarios/get/3.js b/scenarios/get/3.js index a0af1c4..22b116b 100644 --- a/scenarios/get/3.js +++ b/scenarios/get/3.js @@ -8,24 +8,31 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); -var uri = (path) => "http://localhost:8080/api/v1/" + path; +var uri = (path) => "http://localhost:8081/api/v1/" + path; module.exports = function() { - console.log('scenario #3') - return rp.get({ url: uri("collections/empty"), json: true, resolveWithFullResponse: true - }).then((res) => { + }) + .then((res) => { if (res.statusCode === 200) return res - // else throw new Error('incorrect status code, received ' + res.statusCode) - else reject('incorrect status code, received ' + res.statusCode) - }).then((res) => { + }) + .catch((res) => { + if (res.statusCode !== 200) { + throw new Error('incorrect status code, received ' + res.statusCode) + } + }) + + .then((res) => { if (res.body.length === 0) return true - // else throw new Error('incorrect body') - else reject('incorrect body') - }).then(() => { + }) + .catch((res) => { + throw new Error('incorrect body') + }) + + .then(() => { console.log("succcess!"); }); }; diff --git a/scenarios/login.js b/scenarios/login.js new file mode 100644 index 0000000..2c35be0 --- /dev/null +++ b/scenarios/login.js @@ -0,0 +1,44 @@ +"use strict"; +var rp = require("request-promise"); +//require('request-debug')(rp); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function(){ + const jar = rp.jar(); + const request = rp.defaults({jar: jar}); + + const user = {username: "user", password: "password"}; + + return request.post({ + url: uri("collections/restricted"), + }) + .catch((response) => { + assert.equal(response.statusCode, 401); + }) + .then(function(){ + //register + return request.post({ + url: uri("users"), + formData: user, + }); + }) + .then(function(){ + return request.post({ + url: uri("sessions"), + formData: user, + }); + }) + .then(function(){ + return request.get({ + url: uri("users/me"), + }); + }) + .then(function(){ + return request.post({ + url: uri("collections/restricted"), + }); + }); +}; diff --git a/scenarios/post/1.js b/scenarios/post/1.js index 3e10ce0..a382aaa 100644 --- a/scenarios/post/1.js +++ b/scenarios/post/1.js @@ -9,11 +9,9 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); -var uri = (path) => "http://localhost:8080/api/v1/" + path; +var uri = (path) => "http://localhost:8081/api/v1/" + path; module.exports = function() { - console.log('scenario #1') - var prepared_body = { name: "Element", age: 20, @@ -34,13 +32,17 @@ module.exports = function() { formData: prepared_body, json: true, resolveWithFullResponse: true - }).then((res) => { + }) + .then((res) => { if (res.statusCode === 201) return res - // else throw new Error('incorrect status code, received ' + res.statusCode) - else reject('incorrect status code, received ' + res.statusCode) - }).then((res) => { + }) + .catch((res) => { + throw new Error('incorrect status code, received ' + res.statusCode) + }) + .then((res) => { verify(res.body) - }).then(() => { + }) + .then(() => { console.log("succcess!"); }); }; diff --git a/scenarios/post/2.js b/scenarios/post/2.js index f51fe1c..5246eab 100644 --- a/scenarios/post/2.js +++ b/scenarios/post/2.js @@ -9,11 +9,9 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); -var uri = (path) => "http://localhost:8080/api/v1/" + path; +var uri = (path) => "http://localhost:8081/api/v1/" + path; module.exports = function() { - console.log('scenario #2') - var prepared_body = { age: 20, description: "Sealious proposes an application architecture that enables creating applications in a highly declarative way." @@ -25,11 +23,15 @@ module.exports = function() { formData: prepared_body, json: true, resolveWithFullResponse: true - }).then((res) => { + }) + .then((res) => { if (res.statusCode === 400) return res - // else throw new Error('incorrect status code, received ' + res.statusCode) - else reject('incorrect status code, received ' + res.statusCode) - }).then(() => { + }) + .catch((res) => { + throw new Error('incorrect status code, received ' + res.statusCode) + }) + + .then(() => { console.log("succcess!"); }); }; diff --git a/scenarios/post/3.js b/scenarios/post/3.js index 190c2a7..273d8e0 100644 --- a/scenarios/post/3.js +++ b/scenarios/post/3.js @@ -9,25 +9,29 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); -var uri = (path) => "http://localhost:8080/api/v1/" + path; +var uri = (path) => "http://localhost:8081/api/v1/" + path; module.exports = function() { - console.log('scenario #3') - - prepared_body = {} return rp.post({ url: uri('collections/people'), formData: {}, json: true, resolveWithFullResponse: true - }).then((res) => { + }) + .then((res) => { if (res.statusCode === 400) return res - // else throw new Error('incorrect status code, received ' + res.statusCode) - else reject('incorrect status code, received ' + res.statusCode) - }).then((res) => { - console.log(res.body) - // "error":"Bad Request","message":"Invalid multipart payload format" - }).then((res) => { + }) + .catch((res) => { + throw new Error('incorrect status code, received ' + res.statusCode) + }) + + .then((res) => { + if (res.body.error === "Bad Request") return true + }) + .catch((res) => { + throw new Error('incorrect type of response, received ' + res.body.error) + }) + .then(() => { console.log("succcess!"); }); }; From 22d216dee95bc2d4057335343f4894d6c4b389b4 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Thu, 3 Nov 2016 12:53:20 +0100 Subject: [PATCH 03/23] Rewrite all test --- index.js | 8 ++------ pre-test.js | 2 ++ scenarios/post/1.js | 13 ++++++------- scenarios/post/2.js | 13 +++++-------- scenarios/post/3.js | 22 +++++++--------------- 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 38346a3..7f9fe47 100644 --- a/index.js +++ b/index.js @@ -10,11 +10,7 @@ pre() if(scenarios[method] instanceof Function){ return scenarios[method](); }else{ - const promises = {}; - for (var key in scenarios[method]) { - promises[key] = scenarios[method][key]().then(() => console.log('→ scenario done')); - } - return Promise.props(promises); + return Promise.each(Object.keys(scenarios[method]), key => scenarios[method][key]()); } } ); @@ -23,7 +19,7 @@ pre() process.exit(0); }) .catch((err) => { - console.error(err); + console.error(err); console.log(err.stack); console.log("\n\n\t✗ Tests failed. Exiting with status 1. See the above output for details.\n\n"); process.exit(1); diff --git a/pre-test.js b/pre-test.js index 9a8570d..9aaaeec 100644 --- a/pre-test.js +++ b/pre-test.js @@ -29,6 +29,8 @@ module.exports = function(){ } }); + App.Logger.error = () => {} + return App.start() .then(function(){ const datastore = App.ChipManager.get_datastore_chip(); diff --git a/scenarios/post/1.js b/scenarios/post/1.js index a382aaa..1100f05 100644 --- a/scenarios/post/1.js +++ b/scenarios/post/1.js @@ -34,13 +34,12 @@ module.exports = function() { resolveWithFullResponse: true }) .then((res) => { - if (res.statusCode === 201) return res - }) - .catch((res) => { - throw new Error('incorrect status code, received ' + res.statusCode) - }) - .then((res) => { - verify(res.body) + console.log(res.statusCode) + if (res.statusCode !== 201){ + throw new Error('incorrect status code, received ' + res.statusCode) + } else { + verify(res.body) + } }) .then(() => { console.log("succcess!"); diff --git a/scenarios/post/2.js b/scenarios/post/2.js index 5246eab..0f4540a 100644 --- a/scenarios/post/2.js +++ b/scenarios/post/2.js @@ -25,13 +25,10 @@ module.exports = function() { resolveWithFullResponse: true }) .then((res) => { - if (res.statusCode === 400) return res + if (res.statusCode !== 400){ + throw new Error('incorrect status code, received ' + res.statusCode) + } else { + console.log("succcess!"); + } }) - .catch((res) => { - throw new Error('incorrect status code, received ' + res.statusCode) - }) - - .then(() => { - console.log("succcess!"); - }); }; diff --git a/scenarios/post/3.js b/scenarios/post/3.js index 273d8e0..f4f49f1 100644 --- a/scenarios/post/3.js +++ b/scenarios/post/3.js @@ -19,19 +19,11 @@ module.exports = function() { resolveWithFullResponse: true }) .then((res) => { - if (res.statusCode === 400) return res - }) - .catch((res) => { - throw new Error('incorrect status code, received ' + res.statusCode) - }) - - .then((res) => { - if (res.body.error === "Bad Request") return true - }) - .catch((res) => { - throw new Error('incorrect type of response, received ' + res.body.error) - }) - .then(() => { - console.log("succcess!"); - }); + if (res.statusCode !== 400){ + throw new Error('incorrect status code, received ' + res.statusCode) + } else { + if (res.body.error !== "Bad Request") throw new Error('incorrect type of response, received ' + res.body.error) + else console.log("succcess!"); + } + }) }; From 8ab44352f3998dde9a6c716a8d82fe963732cb59 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Fri, 4 Nov 2016 16:40:24 +0100 Subject: [PATCH 04/23] Rewrite then catch chains in get/3.js --- scenarios/get/3.js | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/scenarios/get/3.js b/scenarios/get/3.js index 22b116b..575e4e4 100644 --- a/scenarios/get/3.js +++ b/scenarios/get/3.js @@ -17,22 +17,11 @@ module.exports = function() { resolveWithFullResponse: true }) .then((res) => { - if (res.statusCode === 200) return res - }) - .catch((res) => { - if (res.statusCode !== 200) { + if (res.statusCode !== 200){ throw new Error('incorrect status code, received ' + res.statusCode) + } else { + if (res.body.length !== 0) throw new Error('incorrect body') + else console.log("success!") } }) - - .then((res) => { - if (res.body.length === 0) return true - }) - .catch((res) => { - throw new Error('incorrect body') - }) - - .then(() => { - console.log("succcess!"); - }); }; From 2d689a65ded05754002d26ff186b67b2426145b1 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Fri, 4 Nov 2016 22:11:25 +0100 Subject: [PATCH 05/23] Add new test - Get all elements in accordance with query param --- package.json | 2 +- scenarios/get/4.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 scenarios/get/4.js diff --git a/package.json b/package.json index 33cf937..37824b6 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "node-uuid": "^1.4.7", "request": "^2.74.0", "request-promise": "^4.1.1", - "sealious": "github:sealcode/sealious#alpha", + "sealious": "github:sealcode/sealious#default_config_delegation", "sealious-datastore-mongo": "sealcode/sealious-datastore-mongo#alpha", "sealious-www-server": "github:sealcode/sealious-www-server#alpha" }, diff --git a/scenarios/get/4.js b/scenarios/get/4.js new file mode 100644 index 0000000..ca474eb --- /dev/null +++ b/scenarios/get/4.js @@ -0,0 +1,44 @@ +// scenario #4 +// TODO: Get all elements in accordance with query param + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var prepared_body = { + name: "Some title", + description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit." + } + return rp.post({ + url: uri('collections/people'), + formData: prepared_body, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + if (res.statusCode !== 201) { + throw new Error('incorrect status code, received ' + res.statusCode) + } else { + return rp.get({ + url: uri("collections/people?search=Lorem"), + json: true, + resolveWithFullResponse: true + }) + } + }) + .then((res) => { + if (res.body.length !== 0) { + if (res.body[0].description.original.search('Lorem') !== -1) { + console.log("success!") + } else { + throw new Error(`the searched element don't fit to search keyword`) + } + } else { + throw new Error(`the body in response on search keyword 'Lorem' doesn't have search results`) + } + }) +}; From 384843269208f1dd6342308b2468765c5bac34ce Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Fri, 4 Nov 2016 22:25:26 +0100 Subject: [PATCH 06/23] Add new test - Post new element without some fields (check non-existent fields) --- scenarios/post/4.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 scenarios/post/4.js diff --git a/scenarios/post/4.js b/scenarios/post/4.js new file mode 100644 index 0000000..5312084 --- /dev/null +++ b/scenarios/post/4.js @@ -0,0 +1,25 @@ +// scenario #4 +// TODO: Post new element without some fields (check non-existent fields) + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var prepared_body = { + name: "Some title" + } + return rp.post({ + url: uri('collections/people'), + formData: prepared_body, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + if (res.body.age !== undefined) throw new Error(`the body contains fields with 'undefined' value, which were not added`) + else console.log("success!") + }) +}; From 9509e1910a9ef134fe6d2908221cf7cffdd0c1fe Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Fri, 4 Nov 2016 22:30:01 +0100 Subject: [PATCH 07/23] Add new collection --- pre-test.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pre-test.js b/pre-test.js index 9aaaeec..0936cc9 100644 --- a/pre-test.js +++ b/pre-test.js @@ -14,11 +14,14 @@ module.exports = function(){ ] }); + App.createCollection({ + name: "without_required_values", + fields: [{ name: "number", type: "float" }] + }); + App.createCollection({ name: "empty", - fields: [ - {name: "number", type: "int"} - ] + fields: [{ name: "number", type: "int" }] }); App.createCollection({ From 7d5472326190ab39a2e6f81e068195b2cdd2ba58 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Fri, 4 Nov 2016 22:43:34 +0100 Subject: [PATCH 08/23] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e849603..290dcd4 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ uploaded_files *~ ~* \#* +_scenarios\* From 0b99e13567313e06824f29904419841c773fd5a0 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Fri, 4 Nov 2016 22:50:23 +0100 Subject: [PATCH 09/23] Fix typo in .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 290dcd4..596cc72 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,4 @@ uploaded_files *~ ~* \#* -_scenarios\* +_scenarios From d9c6ede3f53fa5fd57625f7ec7b40b5f07d22baf Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Sat, 5 Nov 2016 22:59:14 +0100 Subject: [PATCH 10/23] Add new test - Post new element without fields to collection without requried values --- scenarios/post/5.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 scenarios/post/5.js diff --git a/scenarios/post/5.js b/scenarios/post/5.js new file mode 100644 index 0000000..28dd1b9 --- /dev/null +++ b/scenarios/post/5.js @@ -0,0 +1,29 @@ +// scenario #5 +// TODO: Post new element without fields to collection without requried values + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + return rp.post({ + url: uri('collections/without_required_values'), + data: {}, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + if (res.statusCode !== 201){ + throw new Error('incorrect status code, received ' + res.statusCode) + } else { + if (Object.keys(res.body.body).length !== 0) { + throw new Error('the body has unforeseen properties') + } else { + console.log("success!") + } + } + }) +}; From 524dcdf94f26815b50637a7dd615050e8c8bb1aa Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Sat, 5 Nov 2016 23:01:39 +0100 Subject: [PATCH 11/23] Add description for two test which sending empty objects --- scenarios/post/3.js | 6 +++--- scenarios/post/5.js | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scenarios/post/3.js b/scenarios/post/3.js index f4f49f1..29a0fee 100644 --- a/scenarios/post/3.js +++ b/scenarios/post/3.js @@ -1,6 +1,6 @@ // scenario #3 -// TODO: Post new element without body -// - send empty object as formData / JSON-encoded +// TODO: Post new element without body (formData) +// - send empty object as formData // - check status // - check body @@ -25,5 +25,5 @@ module.exports = function() { if (res.body.error !== "Bad Request") throw new Error('incorrect type of response, received ' + res.body.error) else console.log("succcess!"); } - }) + }) }; diff --git a/scenarios/post/5.js b/scenarios/post/5.js index 28dd1b9..e8aae1c 100644 --- a/scenarios/post/5.js +++ b/scenarios/post/5.js @@ -1,5 +1,6 @@ // scenario #5 // TODO: Post new element without fields to collection without requried values +// - send empty object as json data "use strict"; var rp = require("request-promise"); From d10c5449c285d0f4134694b86f6109f7b9e83d86 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Mon, 7 Nov 2016 12:49:29 +0100 Subject: [PATCH 12/23] Add new test - Post new element with fields which aren't declared in definition of collection --- scenarios/post/6.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 scenarios/post/6.js diff --git a/scenarios/post/6.js b/scenarios/post/6.js new file mode 100644 index 0000000..3382ea5 --- /dev/null +++ b/scenarios/post/6.js @@ -0,0 +1,26 @@ +// scenario #6 +// TODO: Post new element with fields which aren't declared in definition of collection + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var prepared_body = { + foo: 20, + bar: "Sealious proposes an application architecture that enables creating applications in a highly declarative way." + } + return rp.post({ + url: uri('collections/people'), + formData: prepared_body, + json: true, + resolveWithFullResponse: true + }) + .catch((response) => { + assert.equal(response.statusCode, 403); + console.log("success!") + }) +}; From c4b875fc210dd1ff36f4e73d703bfbea64b35cf4 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Mon, 7 Nov 2016 14:03:54 +0100 Subject: [PATCH 13/23] Add new test - Delete posted element from collection --- scenarios/delete/1.js | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scenarios/delete/1.js diff --git a/scenarios/delete/1.js b/scenarios/delete/1.js new file mode 100644 index 0000000..59d1531 --- /dev/null +++ b/scenarios/delete/1.js @@ -0,0 +1,46 @@ +// scenario #1 +// TODO: Delete posted element from collection + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var prepared_body = { + name: "Some title", + description: "Lorem ipsum dolor sit amet, consectetur adipisicing elit." + } + return rp.post({ + url: uri('collections/people'), + formData: prepared_body, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + if (res.statusCode !== 201) { + throw new Error('incorrect status code, received ' + res.statusCode) + } else { + var id = res.body.id + return rp.delete({ + url: uri(`collections/people/${id}`), + json: true, + resolveWithFullResponse: true + }) + } + }) + .then((res) => { + if (res.body !== undefined) { + if (res.statusCode !== 204) { + throw new Error('incorrect status code, received ' + res.statusCode) + // http://stackoverflow.com/a/2342589 + } else { + console.log("success!") + } + } else { + throw new Error(`the response has body`) + } + }) +}; From d7d1d81d17d1325d4ebaf065d3c51570d34a3108 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Mon, 7 Nov 2016 14:04:17 +0100 Subject: [PATCH 14/23] Add new test - Delete non-existing element from collection --- scenarios/delete/2.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 scenarios/delete/2.js diff --git a/scenarios/delete/2.js b/scenarios/delete/2.js new file mode 100644 index 0000000..ffd459a --- /dev/null +++ b/scenarios/delete/2.js @@ -0,0 +1,25 @@ +// scenario #2 +// TODO: Delete non-existing element from collection + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var id = Math.random() + return rp.delete({ + url: uri(`collections/people/${id}`), + json: true, + resolveWithFullResponse: true + }) + .catch((res) => { + if (res.statusCode !== 404) { + throw new Error('incorrect status code, received ' + res.statusCode) + } else { + console.log("success!") + } + }) +}; From 41a3c4f756ec805b49730ab1184b164be9c6e889 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Mon, 7 Nov 2016 14:28:47 +0100 Subject: [PATCH 15/23] Add red color text to error in console --- package.json | 1 + scenarios/delete/1.js | 7 ++++--- scenarios/delete/2.js | 3 ++- scenarios/get/1.js | 5 +++-- scenarios/get/2.js | 5 +++-- scenarios/get/3.js | 5 +++-- scenarios/get/4.js | 7 ++++--- scenarios/post/1.js | 3 ++- scenarios/post/2.js | 3 ++- scenarios/post/3.js | 5 +++-- scenarios/post/4.js | 3 ++- scenarios/post/5.js | 5 +++-- scenarios/post/6.js | 1 + 13 files changed, 33 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 37824b6..b1784c6 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "sealious-www-server": "github:sealcode/sealious-www-server#alpha" }, "devDependencies": { + "cli-color": "^1.1.0", "request-debug": "^0.2.0" } } diff --git a/scenarios/delete/1.js b/scenarios/delete/1.js index 59d1531..3e08063 100644 --- a/scenarios/delete/1.js +++ b/scenarios/delete/1.js @@ -5,6 +5,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -21,7 +22,7 @@ module.exports = function() { }) .then((res) => { if (res.statusCode !== 201) { - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { var id = res.body.id return rp.delete({ @@ -34,13 +35,13 @@ module.exports = function() { .then((res) => { if (res.body !== undefined) { if (res.statusCode !== 204) { - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) // http://stackoverflow.com/a/2342589 } else { console.log("success!") } } else { - throw new Error(`the response has body`) + throw new Error(clc.red(`the response has body`)) } }) }; diff --git a/scenarios/delete/2.js b/scenarios/delete/2.js index ffd459a..19ef723 100644 --- a/scenarios/delete/2.js +++ b/scenarios/delete/2.js @@ -5,6 +5,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -17,7 +18,7 @@ module.exports = function() { }) .catch((res) => { if (res.statusCode !== 404) { - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { console.log("success!") } diff --git a/scenarios/get/1.js b/scenarios/get/1.js index f8622df..245b4ae 100644 --- a/scenarios/get/1.js +++ b/scenarios/get/1.js @@ -7,6 +7,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -23,11 +24,11 @@ module.exports = function() { // }) .then((res) => { - throw new Error("Should have thrown a 404 error!"); + throw new Error(clc.red("Should have thrown a 404 error!")); }) .catch((res) => { if(res.statusCode !== 404 || res.error.message.type !== "not_found"){ - throw new Error("should have thrown a 404 not_found error"); + throw new Error(clc.red("should have thrown a 404 not_found error")); } }) diff --git a/scenarios/get/2.js b/scenarios/get/2.js index d074941..15697d9 100644 --- a/scenarios/get/2.js +++ b/scenarios/get/2.js @@ -7,6 +7,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -17,14 +18,14 @@ module.exports = function() { resolveWithFullResponse: true }) .then(() => { - throw new Error("Should have thrown a 404 error!"); + throw new Error(clc.red("Should have thrown a 404 error!")); }) .catch((res) => { if (res.statusCode === 404){ if (res.error.message.type === "bad_subject"){ return true; } else { - throw new Error('incorrect status code, received ' + res.statusCode); + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)); } } }) diff --git a/scenarios/get/3.js b/scenarios/get/3.js index 575e4e4..5b2f17e 100644 --- a/scenarios/get/3.js +++ b/scenarios/get/3.js @@ -7,6 +7,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -18,9 +19,9 @@ module.exports = function() { }) .then((res) => { if (res.statusCode !== 200){ - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { - if (res.body.length !== 0) throw new Error('incorrect body') + if (res.body.length !== 0) throw new Error(clc.red('incorrect body')) else console.log("success!") } }) diff --git a/scenarios/get/4.js b/scenarios/get/4.js index ca474eb..9a744f0 100644 --- a/scenarios/get/4.js +++ b/scenarios/get/4.js @@ -5,6 +5,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -21,7 +22,7 @@ module.exports = function() { }) .then((res) => { if (res.statusCode !== 201) { - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { return rp.get({ url: uri("collections/people?search=Lorem"), @@ -35,10 +36,10 @@ module.exports = function() { if (res.body[0].description.original.search('Lorem') !== -1) { console.log("success!") } else { - throw new Error(`the searched element don't fit to search keyword`) + throw new Error(clc.red(`the searched element don't fit to search keyword`)) } } else { - throw new Error(`the body in response on search keyword 'Lorem' doesn't have search results`) + throw new Error(clc.red(`the body in response on search keyword 'Lorem' doesn't have search results`)) } }) }; diff --git a/scenarios/post/1.js b/scenarios/post/1.js index 1100f05..16c14f9 100644 --- a/scenarios/post/1.js +++ b/scenarios/post/1.js @@ -8,6 +8,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -36,7 +37,7 @@ module.exports = function() { .then((res) => { console.log(res.statusCode) if (res.statusCode !== 201){ - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { verify(res.body) } diff --git a/scenarios/post/2.js b/scenarios/post/2.js index 0f4540a..6785a7b 100644 --- a/scenarios/post/2.js +++ b/scenarios/post/2.js @@ -8,6 +8,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -26,7 +27,7 @@ module.exports = function() { }) .then((res) => { if (res.statusCode !== 400){ - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { console.log("succcess!"); } diff --git a/scenarios/post/3.js b/scenarios/post/3.js index 29a0fee..4ed199d 100644 --- a/scenarios/post/3.js +++ b/scenarios/post/3.js @@ -8,6 +8,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -20,9 +21,9 @@ module.exports = function() { }) .then((res) => { if (res.statusCode !== 400){ - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { - if (res.body.error !== "Bad Request") throw new Error('incorrect type of response, received ' + res.body.error) + if (res.body.error !== "Bad Request") throw new Error(clc.red('incorrect type of response, received ' + res.body.error)) else console.log("succcess!"); } }) diff --git a/scenarios/post/4.js b/scenarios/post/4.js index 5312084..5394044 100644 --- a/scenarios/post/4.js +++ b/scenarios/post/4.js @@ -5,6 +5,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -19,7 +20,7 @@ module.exports = function() { resolveWithFullResponse: true }) .then((res) => { - if (res.body.age !== undefined) throw new Error(`the body contains fields with 'undefined' value, which were not added`) + if (res.body.age !== undefined) throw new Error(clc.red(`the body contains fields with 'undefined' value, which were not added`)) else console.log("success!") }) }; diff --git a/scenarios/post/5.js b/scenarios/post/5.js index e8aae1c..a7a207a 100644 --- a/scenarios/post/5.js +++ b/scenarios/post/5.js @@ -6,6 +6,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; @@ -18,10 +19,10 @@ module.exports = function() { }) .then((res) => { if (res.statusCode !== 201){ - throw new Error('incorrect status code, received ' + res.statusCode) + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) } else { if (Object.keys(res.body.body).length !== 0) { - throw new Error('the body has unforeseen properties') + throw new Error(clc.red('the body has unforeseen properties')) } else { console.log("success!") } diff --git a/scenarios/post/6.js b/scenarios/post/6.js index 3382ea5..f1252c7 100644 --- a/scenarios/post/6.js +++ b/scenarios/post/6.js @@ -5,6 +5,7 @@ var rp = require("request-promise"); var fs = require("fs"); var assert = require("assert"); +var clc = require('cli-color'); var uri = (path) => "http://localhost:8081/api/v1/" + path; From 187d8971306c5de5130b85537857db15fa00934f Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Mon, 7 Nov 2016 14:29:14 +0100 Subject: [PATCH 16/23] Add new test - Get with formData from collection --- scenarios/get/5.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 scenarios/get/5.js diff --git a/scenarios/get/5.js b/scenarios/get/5.js new file mode 100644 index 0000000..65dfe46 --- /dev/null +++ b/scenarios/get/5.js @@ -0,0 +1,26 @@ +// scenario #5 +// TODO: Get with formData from collection + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); +var clc = require('cli-color'); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + return rp.get({ + url: uri("collections/people"), + data: {name: "Some title2"}, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + throw new Error(clc.red("Should have thrown a error which is related to a useable body in GET request")) + }) + .catch((res) => { + assert.equal(response.statusCode, 400); + console.log("succcess!") + }); +}; From eee7054eeaf2acb02a24f85edd8cb7ac2c16c110 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Mon, 7 Nov 2016 14:52:02 +0100 Subject: [PATCH 17/23] Add new test - Login with incorrect credentials --- scenarios/login-with-incorrect-credentials.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 scenarios/login-with-incorrect-credentials.js diff --git a/scenarios/login-with-incorrect-credentials.js b/scenarios/login-with-incorrect-credentials.js new file mode 100644 index 0000000..ff46c72 --- /dev/null +++ b/scenarios/login-with-incorrect-credentials.js @@ -0,0 +1,29 @@ +"use strict"; +var rp = require("request-promise"); +//require('request-debug')(rp); +var fs = require("fs"); +var assert = require("assert"); + +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function(){ + const user = {username: "user", password: "password"}; + const incorrect_user = {username: "u", password: "p"}; + + //register + return rp.post({ + url: uri("users"), + formData: user + }) + .then(() => { + return rp.post({ + url: uri("sessions"), + formData: incorrect_user + }) + }) + .catch((res) => { + assert.equal(res.statusCode, 401); + console.log("success!") + }) + +}; From 027866000e8b5bc83123fda1c375978c0cfffc68 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Mon, 7 Nov 2016 17:36:52 +0100 Subject: [PATCH 18/23] Fix if statement for checking res.body --- scenarios/delete/1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scenarios/delete/1.js b/scenarios/delete/1.js index 3e08063..86b8913 100644 --- a/scenarios/delete/1.js +++ b/scenarios/delete/1.js @@ -33,7 +33,7 @@ module.exports = function() { } }) .then((res) => { - if (res.body !== undefined) { + if (res.body === undefined) { if (res.statusCode !== 204) { throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) // http://stackoverflow.com/a/2342589 From 02084faab9ddc4859d2d9fe54cf3f244343eb5cf Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Tue, 8 Nov 2016 19:14:35 +0100 Subject: [PATCH 19/23] Cleanup get/1.js --- scenarios/get/1.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/scenarios/get/1.js b/scenarios/get/1.js index 245b4ae..49da059 100644 --- a/scenarios/get/1.js +++ b/scenarios/get/1.js @@ -17,12 +17,6 @@ module.exports = function() { json: true, resolveWithFullResponse: true }) - // .then((res) => { - // if (res.body.type === "not_found") return res - // }).catch((res) => { - // throw new Error("Incorrect type of body, it should be `not_found`, received: "+res.body.type) - // }) - .then((res) => { throw new Error(clc.red("Should have thrown a 404 error!")); }) From faf55b0b04b141dbf72b2cb076700d4e67b2589b Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Tue, 8 Nov 2016 20:33:46 +0100 Subject: [PATCH 20/23] Add new test for PUT method - Update element of collection --- scenarios/put/1.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 scenarios/put/1.js diff --git a/scenarios/put/1.js b/scenarios/put/1.js new file mode 100644 index 0000000..f16b2e2 --- /dev/null +++ b/scenarios/put/1.js @@ -0,0 +1,52 @@ +// scenario #1 +// TODO: Update element of collection by PUT method +// the updated element should has integrally new body + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); +var clc = require('cli-color'); +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var initial_body = {name: "Title-1", age: 21} + var updated_body = {name: "Title-2"} + + var verify = function(res){ + var a = assert.equal(res.body.age, undefined); + assert.deepEqual(res.body.name,{ + original: updated_body.name, + safe: updated_body.name + }); + } + + return rp.post({ + url: uri('collections/people'), + formData: initial_body, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + if (res.statusCode !== 201) { + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) + } else { + var id = res.body.id; + return rp.put({ + url: uri(`collections/people/${id}`), + formData: updated_body, + json: true, + resolveWithFullResponse: true + }) + } + }) + .then((res) => { + if (res.body.created_context.timestamp !== res.body.last_modified_context.timestamp) { + if (res.statusCode !== 200) throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) + else verify(res.body) + } else { + throw new Error(clc.red(`the timestamps aren't different`)) + } + }) + .then(() => console.log("succcess!")); +}; From 05296d966abf639c7c7dc6f22702de6fb15c2c46 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Thu, 10 Nov 2016 22:40:57 +0100 Subject: [PATCH 21/23] Add new test - Update element of collection by PUT method with incorrect fields --- scenarios/put/2.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 scenarios/put/2.js diff --git a/scenarios/put/2.js b/scenarios/put/2.js new file mode 100644 index 0000000..166a6e0 --- /dev/null +++ b/scenarios/put/2.js @@ -0,0 +1,38 @@ +// scenario #2 +// TODO: Update element of collection by PUT method with incorrect fields + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); +var clc = require('cli-color'); +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var initial_body = {name: "Title-1", age: 21} + var updated_body = {name: "Title-2", incorrect_value: 23} + + return rp.post({ + url: uri('collections/people'), + formData: initial_body, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + if (res.statusCode !== 201) { + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) + } else { + var id = res.body.id; + return rp.put({ + url: uri(`collections/people/${id}`), + formData: updated_body, + json: true, + resolveWithFullResponse: true + }) + } + }) + .catch((res) => { + if (res.statusCode !== 403) throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) + }) + .then(() => console.log("succcess!")); +}; From f39e07b8eba32d9d510c4fb9b387bac6612cd6cf Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Thu, 10 Nov 2016 22:47:28 +0100 Subject: [PATCH 22/23] Add then chain in put/2.js --- scenarios/put/2.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scenarios/put/2.js b/scenarios/put/2.js index 166a6e0..3589a10 100644 --- a/scenarios/put/2.js +++ b/scenarios/put/2.js @@ -31,6 +31,9 @@ module.exports = function() { }) } }) + .then((res) => { + throw new Error(clc.red("Should have thrown a error which is related to incorrect status code")) + }) .catch((res) => { if (res.statusCode !== 403) throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) }) From e70a88e82cdf9078089234d1813327bacb7e5a60 Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Thu, 10 Nov 2016 22:48:04 +0100 Subject: [PATCH 23/23] Add new test - Update element of collection by PUT method without required field --- scenarios/put/3.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 scenarios/put/3.js diff --git a/scenarios/put/3.js b/scenarios/put/3.js new file mode 100644 index 0000000..a5062e6 --- /dev/null +++ b/scenarios/put/3.js @@ -0,0 +1,41 @@ +// scenario #3 +// TODO: Update element of collection by PUT method without required field + +"use strict"; +var rp = require("request-promise"); +var fs = require("fs"); +var assert = require("assert"); +var clc = require('cli-color'); +var uri = (path) => "http://localhost:8081/api/v1/" + path; + +module.exports = function() { + var initial_body = {name: "Title-1", age: 21} + var updated_body = {age: 23} + + return rp.post({ + url: uri('collections/people'), + formData: initial_body, + json: true, + resolveWithFullResponse: true + }) + .then((res) => { + if (res.statusCode !== 201) { + throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) + } else { + var id = res.body.id; + return rp.put({ + url: uri(`collections/people/${id}`), + formData: updated_body, + json: true, + resolveWithFullResponse: true + }) + } + }) + .then((res) => { + throw new Error(clc.red("Should have thrown a error which is related to incorrect status code")) + }) + .catch((res) => { + if (res.statusCode !== 403) throw new Error(clc.red('incorrect status code, received ' + res.statusCode)) + }) + .then(() => console.log("succcess!")); +};