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

jslint xwikistorage tests

parent 91ddd1e8
/*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);
(function (dependencies, module) {
......@@ -28,53 +28,53 @@
return o;
}
(function() {
module ('XWikiStorage');
var setUp = function(that, liveTest) {
module('XWikiStorage');
function setUp(that, liveTest) {
var o = generateTools(that);
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,
'<meta name="form_token" content="OMGHAX"/>');
o._addFakeServerResponse = o.addFakeServerResponse;
o.expectedRequests = [];
o.addFakeServerResponse = function(a,b,c,d,e) {
o._addFakeServerResponse(a,b,c,d,e);
o.expectedRequests.push([b,c]);
o.addFakeServerResponse = function (a, b, c, d, e) {
o._addFakeServerResponse(a, b, c, d, e);
o.expectedRequests.push([b, c]);
};
o.assertReqs = function(count, message) {
o.assertReqs = function (count, message) {
var i, j, req, expected, ex;
o.requests = (o.requests || 0) + count;
ok(o.server.requests.length === o.requests,
message + "[expected [" + count + "] got [" +
(o.server.requests.length - (o.requests - count)) + "]]");
for (var i = 1; i <= count; i++) {
var req = o.server.requests[o.server.requests.length - i];
for (i = 1; i <= count; i += 1) {
req = o.server.requests[o.server.requests.length - i];
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) {
expected = o.expectedRequests[j];
if (req.method === expected[0] &&
req.url.indexOf(expected[1]) !== 0)
{
req.url.indexOf(expected[1]) !== 0) {
o.expectedRequests.splice(j, 1);
}
}
}
var ex = o.expectedRequests.pop();
ex = o.expectedRequests.pop();
if (ex) {
ok(0, "expected [" + ex[0] + "] request for [" + ex[1] + "]");
}
};
return o;
};
}
test ("Post", function () {
test("Post", function () {
var o = setUp(this);
// post without id
o.spy (o, "status", 405, "Post without id");
o.spy(o, "status", 405, "Post without id");
o.jio.post({}, o.f);
o.clock.tick(5000);
o.assertReqs(0, "no id -> no request");
......@@ -86,13 +86,16 @@
o.jio.post({"_id": "myFile", "title": "hello there"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.assertReqs(3, "put -> 1 request to get csrf token, 1 to get doc and 1 to post data");
o.assertReqs(
3,
"put -> 1 request to get csrf token, 1 to get doc and 1 to post data"
);
// post but document already exists (post = error!, put = ok)
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.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();
......@@ -101,40 +104,46 @@
util.closeAndcleanUpJio(o.jio);
});
test ("Put", function(){
test("Put", function () {
var o = setUp(this);
// 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.clock.tick(5000);
o.assertReqs(0, "put w/o id -> 0 requests");
// put non empty document
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");
o.jio.put({"_id": "put1", "title": "myPut1"}, o.f);
o.clock.tick(5000);
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 normal doc -> 1 req to get doc, 1 for csrf token, 1 to post"
);
// put but document already exists = update
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);
o.addFakeServerResponse("xwiki", "POST", "put2", 201, "HTML RESPONSE");
o.spy (o, "value", {"ok": true, "id": "put2"}, "Updated the document");
o.spy(o, "value", {"ok": true, "id": "put2"}, "Updated the document");
o.jio.put({"_id": "put2", "title": "myPut2abcdedg"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.assertReqs(3, "put update doc -> 1 req to get doc, 1 for csrf token, 1 to post");
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);
......@@ -162,8 +171,13 @@
o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<page xmlns="http://www.xwiki.org"><title>myPutAttm</title></page>';
o.addFakeServerResponse("xwiki", "GET", "putattmt1", 200, o.answer);
o.addFakeServerResponse("xwiki", "POST", "putattmt1/putattmt2", 201,"HTML"+
+ "RESPONSE");
o.addFakeServerResponse(
"xwiki",
"POST",
"putattmt1/putattmt2",
201,
"HTML RESPONSE"
);
o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"},
"PutAttachment with document, without data");
o.jio.putAttachment({"_id": "putattmt1", "_attachment": "putattmt2"}, o.f);
......@@ -175,7 +189,7 @@
util.closeAndcleanUpJio(o.jio);
});
test ("Get", function(){
test("Get", function () {
var o = setUp(this);
......@@ -223,7 +237,7 @@
util.closeAndcleanUpJio(o.jio);
});
test ("Remove", function(){
test("Remove", function () {
var o = setUp(this);
......@@ -233,7 +247,10 @@
o.jio.remove({"_id": "remove1"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.assertReqs(2, "remove nonexistent doc -> 1 request for csrf and 1 for doc");
o.assertReqs(
2,
"remove nonexistent doc -> 1 request for csrf and 1 for doc"
);
// remove inexistent document/attachment
o.addFakeServerResponse("xwiki", "GET", "remove1/remove2", 404, "HTML" +
......@@ -242,7 +259,10 @@
o.jio.removeAttachment({"_id": "remove1", "_attachment": "remove2"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.assertReqs(2, "remove nonexistant attach -> 1 request for csrf and 1 for doc");
o.assertReqs(
2,
"remove nonexistant attach -> 1 request for csrf and 1 for doc"
);
// remove document
//o.answer = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
......@@ -254,7 +274,10 @@
o.jio.remove({"_id": "remove3"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.assertReqs(2, "remove document -> 1 request for csrf and 1 for deleting doc");
o.assertReqs(
2,
"remove document -> 1 request for csrf and 1 for deleting doc"
);
o.answer = JSON.stringify({
"_id": "remove4",
......@@ -267,8 +290,13 @@
}
});
// remove attachment
o.addFakeServerResponse("xwiki", "POST", "delattachment/Main/remove4/remove5",
200, "HTML RESPONSE");
o.addFakeServerResponse(
"xwiki",
"POST",
"delattachment/Main/remove4/remove5",
200,
"HTML RESPONSE"
);
o.spy(o, "value", {"ok": true, "id": "remove4/remove5"},
"Remove attachment");
o.jio.removeAttachment({"_id": "remove4", "_attachment": "remove5"}, o.f);
......@@ -279,43 +307,4 @@
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