Commit 00ae92ab authored by Tristan Cavelier's avatar Tristan Cavelier

jslint xwikistorage tests

parent 91ddd1e8
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, jIO, jio_tests, test, ok, deepEqual, sinon */ /*global define, jIO, jio_tests, test, ok, sinon */
// define([module_name], [dependencies], module); // define([module_name], [dependencies], module);
(function (dependencies, module) { (function (dependencies, module) {
...@@ -28,294 +28,283 @@ ...@@ -28,294 +28,283 @@
return o; return o;
} }
(function() { module('XWikiStorage');
module ('XWikiStorage');
var setUp = function(that, liveTest) { function setUp(that, liveTest) {
var o = generateTools(that); var o = generateTools(that);
o.server = sinon.fakeServer.create(); o.server = sinon.fakeServer.create();
o.jio = jIO.newJio({type:'xwiki',formTokenPath:'form_token'}); o.jio = jIO.newJio({type: 'xwiki', formTokenPath: 'form_token'});
o.addFakeServerResponse("xwiki", "GET", "form_token", 200, o.addFakeServerResponse("xwiki", "GET", "form_token", 200,
'<meta name="form_token" content="OMGHAX"/>'); '<meta name="form_token" content="OMGHAX"/>');
o._addFakeServerResponse = o.addFakeServerResponse; o._addFakeServerResponse = o.addFakeServerResponse;
o.expectedRequests = []; o.expectedRequests = [];
o.addFakeServerResponse = function(a,b,c,d,e) { o.addFakeServerResponse = function (a, b, c, d, e) {
o._addFakeServerResponse(a,b,c,d,e); o._addFakeServerResponse(a, b, c, d, e);
o.expectedRequests.push([b,c]); o.expectedRequests.push([b, c]);
}; };
o.assertReqs = function(count, message) { o.assertReqs = function (count, message) {
o.requests = (o.requests || 0) + count; var i, j, req, expected, ex;
ok(o.server.requests.length === o.requests, o.requests = (o.requests || 0) + count;
message + "[expected [" + count + "] got [" + ok(o.server.requests.length === o.requests,
(o.server.requests.length - (o.requests - count)) + "]]"); message + "[expected [" + count + "] got [" +
for (var i = 1; i <= count; i++) { (o.server.requests.length - (o.requests - count)) + "]]");
var req = o.server.requests[o.server.requests.length - i]; for (i = 1; i <= count; i += 1) {
if (!req) { req = o.server.requests[o.server.requests.length - i];
break; if (!req) {
} break;
for (var j = o.expectedRequests.length - 1; j >= 0; --j) { }
var expected = o.expectedRequests[j]; for (j = o.expectedRequests.length - 1; j >= 0; j -= 1) {
if (req.method === expected[0] && expected = o.expectedRequests[j];
req.url.indexOf(expected[1]) !== 0) if (req.method === expected[0] &&
{ req.url.indexOf(expected[1]) !== 0) {
o.expectedRequests.splice(j, 1); o.expectedRequests.splice(j, 1);
}
}
}
var ex = o.expectedRequests.pop();
if (ex) {
ok(0, "expected [" + ex[0] + "] request for [" + ex[1] + "]");
} }
}; }
return o; }
}; ex = o.expectedRequests.pop();
if (ex) {
test ("Post", function () { ok(0, "expected [" + ex[0] + "] request for [" + ex[1] + "]");
}
var o = setUp(this); };
return o;
// post without id }
o.spy (o, "status", 405, "Post without id");
o.jio.post({}, o.f); test("Post", function () {
o.clock.tick(5000);
o.assertReqs(0, "no id -> no request"); var o = setUp(this);
// post non empty document // post without id
o.addFakeServerResponse("xwiki", "POST", "myFile", 201, "HTML RESPONSE"); o.spy(o, "status", 405, "Post without id");
o.spy(o, "value", {"id": "myFile", "ok": true}, o.jio.post({}, o.f);
"Create = POST non empty document"); o.clock.tick(5000);
o.jio.post({"_id": "myFile", "title": "hello there"}, o.f); o.assertReqs(0, "no id -> no request");
o.clock.tick(5000);
o.server.respond(); // post non empty document
o.assertReqs(3, "put -> 1 request to get csrf token, 1 to get doc and 1 to post data"); o.addFakeServerResponse("xwiki", "POST", "myFile", 201, "HTML RESPONSE");
o.spy(o, "value", {"id": "myFile", "ok": true},
// post but document already exists (post = error!, put = ok) "Create = POST non empty document");
o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + o.jio.post({"_id": "myFile", "title": "hello there"}, o.f);
'<page xmlns="http://www.xwiki.org"><title>hello there</title></page>'; o.clock.tick(5000);
o.addFakeServerResponse("xwiki", "GET", "myFile2", 200, o.answer); o.server.respond();
o.spy (o, "status", 409, "Post but document already exists"); o.assertReqs(
o.jio.post({"_id": "myFile2", "title": "hello again"}, o.f); 3,
o.clock.tick(5000); "put -> 1 request to get csrf token, 1 to get doc and 1 to post data"
o.server.respond(); );
o.assertReqs(1, "post w/ existing doc -> 1 request to get doc then fail");
// post but document already exists (post = error!, put = ok)
util.closeAndcleanUpJio(o.jio); o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<page xmlns="http://www.xwiki.org"><title>hello there</title></page>';
o.addFakeServerResponse("xwiki", "GET", "myFile2", 200, o.answer);
o.spy(o, "status", 409, "Post but document already exists");
o.jio.post({"_id": "myFile2", "title": "hello again"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.assertReqs(1, "post w/ existing doc -> 1 request to get doc then fail");
util.closeAndcleanUpJio(o.jio);
}); });
test ("Put", function(){ test("Put", function () {
var o = setUp(this); var o = setUp(this);
// put without id => id required // put without id => id required
o.spy (o, "status", 20, "Put without id"); o.spy(o, "status", 20, "Put without id");
o.jio.put({}, o.f); o.jio.put({}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.assertReqs(0, "put w/o id -> 0 requests"); o.assertReqs(0, "put w/o id -> 0 requests");
// put non empty document // put non empty document
o.addFakeServerResponse("xwiki", "POST", "put1", 201, "HTML RESPONSE"); o.addFakeServerResponse("xwiki", "POST", "put1", 201, "HTML RESPONSE");
o.spy (o, "value", {"ok": true, "id": "put1"}, o.spy(o, "value", {"ok": true, "id": "put1"},
"Create = PUT non empty document"); "Create = PUT non empty document");
o.jio.put({"_id": "put1", "title": "myPut1"}, o.f); o.jio.put({"_id": "put1", "title": "myPut1"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(3, "put normal doc -> 1 req to get doc, 1 for csrf token, 1 to post"); o.assertReqs(
3,
// put but document already exists = update "put normal doc -> 1 req to get doc, 1 for csrf token, 1 to post"
o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + );
'<page xmlns="http://www.xwiki.org"><title>mtPut1</title></page>';
o.addFakeServerResponse("xwiki", "GET", "put2", 200, o.answer); // put but document already exists = update
o.addFakeServerResponse("xwiki", "POST", "put2", 201, "HTML RESPONSE"); o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
o.spy (o, "value", {"ok": true, "id": "put2"}, "Updated the document"); '<page xmlns="http://www.xwiki.org"><title>mtPut1</title></page>';
o.jio.put({"_id": "put2", "title": "myPut2abcdedg"}, o.f); o.addFakeServerResponse("xwiki", "GET", "put2", 200, o.answer);
o.clock.tick(5000); o.addFakeServerResponse("xwiki", "POST", "put2", 201, "HTML RESPONSE");
o.server.respond(); o.spy(o, "value", {"ok": true, "id": "put2"}, "Updated the document");
o.assertReqs(3, "put update doc -> 1 req to get doc, 1 for csrf token, 1 to post"); o.jio.put({"_id": "put2", "title": "myPut2abcdedg"}, o.f);
o.clock.tick(5000);
util.closeAndcleanUpJio(o.jio); o.server.respond();
o.assertReqs(
3,
"put update doc -> 1 req to get doc, 1 for csrf token, 1 to post"
);
util.closeAndcleanUpJio(o.jio);
}); });
test ("PutAttachment", function(){ test("PutAttachment", function () {
var o = setUp(this); var o = setUp(this);
// putAttachment without doc id => id required // putAttachment without doc id => id required
o.spy(o, "status", 20, "PutAttachment without doc id"); o.spy(o, "status", 20, "PutAttachment without doc id");
o.jio.putAttachment({}, o.f); o.jio.putAttachment({}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.assertReqs(0, "put attach w/o doc id -> 0 requests"); o.assertReqs(0, "put attach w/o doc id -> 0 requests");
// putAttachment without attachment id => attachment id required // putAttachment without attachment id => attachment id required
o.spy(o, "status", 22, "PutAttachment without attachment id"); o.spy(o, "status", 22, "PutAttachment without attachment id");
o.jio.putAttachment({"_id": "putattmt1"}, o.f); o.jio.putAttachment({"_id": "putattmt1"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.assertReqs(0, "put attach w/o attach id -> 0 requests"); o.assertReqs(0, "put attach w/o attach id -> 0 requests");
// putAttachment without underlying document => not found // putAttachment without underlying document => not found
o.addFakeServerResponse("xwiki", "GET", "putattmtx", 404, "HTML RESPONSE"); o.addFakeServerResponse("xwiki", "GET", "putattmtx", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "PutAttachment without document"); o.spy(o, "status", 404, "PutAttachment without document");
o.jio.putAttachment({"_id": "putattmtx", "_attachment": "putattmt2"}, o.f); o.jio.putAttachment({"_id": "putattmtx", "_attachment": "putattmt2"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(1, "put attach w/o existing document -> 1 request to get doc"); o.assertReqs(1, "put attach w/o existing document -> 1 request to get doc");
// putAttachment with document without data // putAttachment with document without data
o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<page xmlns="http://www.xwiki.org"><title>myPutAttm</title></page>'; '<page xmlns="http://www.xwiki.org"><title>myPutAttm</title></page>';
o.addFakeServerResponse("xwiki", "GET", "putattmt1", 200, o.answer); o.addFakeServerResponse("xwiki", "GET", "putattmt1", 200, o.answer);
o.addFakeServerResponse("xwiki", "POST", "putattmt1/putattmt2", 201,"HTML"+ o.addFakeServerResponse(
+ "RESPONSE"); "xwiki",
o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"}, "POST",
"PutAttachment with document, without data"); "putattmt1/putattmt2",
o.jio.putAttachment({"_id": "putattmt1", "_attachment": "putattmt2"}, o.f); 201,
o.clock.tick(5000); "HTML RESPONSE"
o.server.respond(); );
o.assertReqs(3, "put attach -> 1 request to get document, 1 to put " + o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"},
"attach, 1 to get csrf token"); "PutAttachment with document, without data");
o.jio.putAttachment({"_id": "putattmt1", "_attachment": "putattmt2"}, o.f);
util.closeAndcleanUpJio(o.jio); o.clock.tick(5000);
o.server.respond();
o.assertReqs(3, "put attach -> 1 request to get document, 1 to put " +
"attach, 1 to get csrf token");
util.closeAndcleanUpJio(o.jio);
}); });
test ("Get", function(){ test("Get", function () {
var o = setUp(this); var o = setUp(this);
// get inexistent document // get inexistent document
o.spy(o, "status", 404, "Get non existing document"); o.spy(o, "status", 404, "Get non existing document");
o.jio.get("get1", o.f); o.jio.get("get1", o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(1, "try to get nonexistent doc -> 1 request"); o.assertReqs(1, "try to get nonexistent doc -> 1 request");
// get inexistent attachment // get inexistent attachment
o.spy(o, "status", 404, "Get non existing attachment"); o.spy(o, "status", 404, "Get non existing attachment");
o.jio.get("get1/get2", o.f); o.jio.get("get1/get2", o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(1, "try to get nonexistent attach -> 1 request"); o.assertReqs(1, "try to get nonexistent attach -> 1 request");
// get document // get document
o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<page xmlns="http://www.xwiki.org"><title>some title</title></page>'; '<page xmlns="http://www.xwiki.org"><title>some title</title></page>';
o.addFakeServerResponse("xwiki", "GET", "get3", 200, o.answer); o.addFakeServerResponse("xwiki", "GET", "get3", 200, o.answer);
o.spy(o, "value", {"_id": "get3", "title": "some title"}, "Get document"); o.spy(o, "value", {"_id": "get3", "title": "some title"}, "Get document");
o.jio.get("get3", o.f); o.jio.get("get3", o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(1, "get document -> 1 request"); o.assertReqs(1, "get document -> 1 request");
// get inexistent attachment (document exists) // get inexistent attachment (document exists)
o.spy(o, "status", 404, "Get non existing attachment (doc exists)"); o.spy(o, "status", 404, "Get non existing attachment (doc exists)");
o.jio.get({"_id": "get3", "_attachment": "getx"}, o.f); o.jio.get({"_id": "get3", "_attachment": "getx"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(1, "get nonexistant attachment -> 1 request"); o.assertReqs(1, "get nonexistant attachment -> 1 request");
// get attachment // get attachment
o.answer = JSON.stringify({"_id": "get4", "title": "some attachment"}); o.answer = JSON.stringify({"_id": "get4", "title": "some attachment"});
o.addFakeServerResponse("xwiki", "GET", "get3/get4", 200, o.answer); o.addFakeServerResponse("xwiki", "GET", "get3/get4", 200, o.answer);
o.spy(o, "value", {"_id": "get4", "title": "some attachment"}, o.spy(o, "value", {"_id": "get4", "title": "some attachment"},
"Get attachment"); "Get attachment");
o.jio.get({"_id": "get3", "_attachment": "get4"}, o.f); o.jio.get({"_id": "get3", "_attachment": "get4"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(1, "get attachment -> 1 request"); o.assertReqs(1, "get attachment -> 1 request");
util.closeAndcleanUpJio(o.jio); util.closeAndcleanUpJio(o.jio);
}); });
test ("Remove", function(){ test("Remove", function () {
var o = setUp(this); var o = setUp(this);
// remove inexistent document // remove inexistent document
o.addFakeServerResponse("xwiki", "GET", "remove1", 404, "HTML RESPONSE"); o.addFakeServerResponse("xwiki", "GET", "remove1", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Remove non existening document"); o.spy(o, "status", 404, "Remove non existening document");
o.jio.remove({"_id": "remove1"}, o.f); o.jio.remove({"_id": "remove1"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
o.assertReqs(2, "remove nonexistent doc -> 1 request for csrf and 1 for doc"); o.assertReqs(
2,
// remove inexistent document/attachment "remove nonexistent doc -> 1 request for csrf and 1 for doc"
o.addFakeServerResponse("xwiki", "GET", "remove1/remove2", 404, "HTML" + );
"RESPONSE");
o.spy(o, "status", 404, "Remove inexistent document/attachment"); // remove inexistent document/attachment
o.jio.removeAttachment({"_id": "remove1", "_attachment": "remove2"}, o.f); o.addFakeServerResponse("xwiki", "GET", "remove1/remove2", 404, "HTML" +
o.clock.tick(5000); "RESPONSE");
o.server.respond(); o.spy(o, "status", 404, "Remove inexistent document/attachment");
o.assertReqs(2, "remove nonexistant attach -> 1 request for csrf and 1 for doc"); o.jio.removeAttachment({"_id": "remove1", "_attachment": "remove2"}, o.f);
o.clock.tick(5000);
// remove document o.server.respond();
//o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + o.assertReqs(
// '<page xmlns="http://www.xwiki.org"><title>some doc</title></page>'; 2,
//o.addFakeServerResponse("xwiki", "GET", "remove3", 200, o.answer); "remove nonexistant attach -> 1 request for csrf and 1 for doc"
o.addFakeServerResponse("xwiki", "POST", "bin/delete/Main/remove3", );
200, "HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "remove3"}, "Remove document"); // remove document
o.jio.remove({"_id": "remove3"}, o.f); //o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
o.clock.tick(5000); // '<page xmlns="http://www.xwiki.org"><title>some doc</title></page>';
o.server.respond(); //o.addFakeServerResponse("xwiki", "GET", "remove3", 200, o.answer);
o.assertReqs(2, "remove document -> 1 request for csrf and 1 for deleting doc"); o.addFakeServerResponse("xwiki", "POST", "bin/delete/Main/remove3",
200, "HTML RESPONSE");
o.answer = JSON.stringify({ o.spy(o, "value", {"ok": true, "id": "remove3"}, "Remove document");
"_id": "remove4", o.jio.remove({"_id": "remove3"}, o.f);
"title": "some doc", o.clock.tick(5000);
"_attachments": { o.server.respond();
"remove5": { o.assertReqs(
"length": 4, 2,
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e" "remove document -> 1 request for csrf and 1 for deleting doc"
} );
o.answer = JSON.stringify({
"_id": "remove4",
"title": "some doc",
"_attachments": {
"remove5": {
"length": 4,
"digest": "md5-d41d8cd98f00b204e9800998ecf8427e"
} }
}); }
// remove attachment });
o.addFakeServerResponse("xwiki", "POST", "delattachment/Main/remove4/remove5", // remove attachment
200, "HTML RESPONSE"); o.addFakeServerResponse(
o.spy(o, "value", {"ok": true, "id": "remove4/remove5"}, "xwiki",
"Remove attachment"); "POST",
o.jio.removeAttachment({"_id": "remove4", "_attachment": "remove5"}, o.f); "delattachment/Main/remove4/remove5",
o.clock.tick(5000); 200,
o.server.respond(); "HTML RESPONSE"
o.assertReqs(2, "remove attach -> 1 request for csrf and 1 for deletion"); );
o.spy(o, "value", {"ok": true, "id": "remove4/remove5"},
util.closeAndcleanUpJio(o.jio); "Remove attachment");
o.jio.removeAttachment({"_id": "remove4", "_attachment": "remove5"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.assertReqs(2, "remove attach -> 1 request for csrf and 1 for deletion");
util.closeAndcleanUpJio(o.jio);
}); });
var nThen = function(next) {
var funcs = [];
var calls = 0;
var waitFor = function(func) {
calls++;
return function() {
if (func) {
func.apply(null, arguments);
}
calls = (calls || 1) - 1;
while (!calls && funcs.length) {
funcs.shift()(waitFor);
}
};
};
next(waitFor);
var ret = {
nThen: function(next) {
funcs.push(next);
return ret;
},
orTimeout: function(func, milliseconds) {
var cto;
var timeout = setTimeout(function() {
while (funcs.shift() !== cto) ;
func(waitFor);
calls = (calls || 1) - 1;
while (!calls && funcs.length) {
//console.log("call");
funcs.shift()(waitFor);
}
}, milliseconds);
funcs.push(cto = function() { clearTimeout(timeout); });
return ret;
}
};
return ret;
};
})); }));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment