Commit ac7b4ae5 authored by Tristan Cavelier's avatar Tristan Cavelier

Merge remote-tracking branch 'cjdelisle/xwiki-phantomjs-test-failures'

Conflicts:
	src/jio.storage/xwikistorage.js
	test/jiotests.js
parents 3dbe62ac d24fe545
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
maxlen: 80, maxlen: 80,
sloppy: true, sloppy: true,
nomen: true, nomen: true,
vars: true,
plusplus: true plusplus: true
*/ */
/*global /*global
define: true,
jIO: true, jIO: true,
$: true, jQuery: true,
XMLHttpRequest: true, XMLHttpRequest: true,
Blob: true, Blob: true,
FormData: true, FormData: true,
...@@ -17,10 +17,13 @@ ...@@ -17,10 +17,13 @@
* JIO XWiki Storage. Type = 'xwiki'. * JIO XWiki Storage. Type = 'xwiki'.
* XWiki Document/Attachment storage. * XWiki Document/Attachment storage.
*/ */
jIO.addStorageType('xwiki', function (spec, my) { (function () {
var $, store;
store = function (spec, my) {
spec = spec || {}; spec = spec || {};
var that, priv, xwikistorage; var that, priv, xwikistorage;
that = my.basicStorage(spec, my); that = my.basicStorage(spec, my);
priv = {}; priv = {};
...@@ -30,7 +33,7 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -30,7 +33,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param id the document id. * @param id the document id.
* @return a map of { 'space':<Space>, 'page':<Page> } * @return a map of { 'space':<Space>, 'page':<Page> }
*/ */
var getParts = function (id) { priv.getParts = function (id) {
if (id.indexOf('/') === -1) { if (id.indexOf('/') === -1) {
return { return {
space: 'Main', space: 'Main',
...@@ -49,17 +52,18 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -49,17 +52,18 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param andThen function which is called with (formToken, err) * @param andThen function which is called with (formToken, err)
* as parameters. * as parameters.
*/ */
var doWithFormToken = function (andThen) { priv.doWithFormToken = function (andThen) {
$.ajax({ $.ajax({
url: priv.formTokenPath, url: priv.formTokenPath,
type: "GET", type: "GET",
async: true, async: true,
dataType: 'text', dataType: 'text',
success: function (html) { success: function (html) {
var m, token;
// this is unreliable // this is unreliable
//var token = $('meta[name=form_token]', html).attr("content"); //var token = $('meta[name=form_token]', html).attr("content");
var m = html.match(/<meta name="form_token" content="(\w*)"\/>/); m = html.match(/<meta name="form_token" content="(\w*)"\/>/);
var token = (m && m[1]) || null; token = (m && m[1]) || null;
if (!token) { if (!token) {
andThen(null, { andThen(null, {
"status": 404, "status": 404,
...@@ -92,8 +96,8 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -92,8 +96,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param docId the id of the document. * @param docId the id of the document.
* @return the REST URL for accessing this document. * @return the REST URL for accessing this document.
*/ */
var getDocRestURL = function (docId) { priv.getDocRestURL = function (docId) {
var parts = getParts(docId); var parts = priv.getParts(docId);
return priv.xwikiurl + '/rest/wikis/' return priv.xwikiurl + '/rest/wikis/'
+ priv.wiki + '/spaces/' + parts.space + '/pages/' + parts.page; + priv.wiki + '/spaces/' + parts.space + '/pages/' + parts.page;
}; };
...@@ -103,7 +107,7 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -103,7 +107,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
* Equivilant to the `new Blob()` constructor. * Equivilant to the `new Blob()` constructor.
* Will fall back on deprecated BlobBuilder if necessary. * Will fall back on deprecated BlobBuilder if necessary.
*/ */
var makeBlob = function (contentArray, options) { priv.makeBlob = function (contentArray, options) {
var i, bb, BB; var i, bb, BB;
try { try {
// use the constructor if possible. // use the constructor if possible.
...@@ -120,6 +124,11 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -120,6 +124,11 @@ jIO.addStorageType('xwiki', function (spec, my) {
} }
}; };
priv.isBlob = function (potentialBlob) {
return typeof (potentialBlob) !== 'undefined' &&
potentialBlob.toString() === "[object Blob]";
};
/* /*
* Wrapper for the xwikistorage based on localstorage JiO store. * Wrapper for the xwikistorage based on localstorage JiO store.
*/ */
...@@ -134,9 +143,10 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -134,9 +143,10 @@ jIO.addStorageType('xwiki', function (spec, my) {
getItem: function (docId, andThen) { getItem: function (docId, andThen) {
var success = function (jqxhr) { var success = function (jqxhr) {
var out = {}; var out, xd;
out = {};
try { try {
var xd = $(jqxhr.responseText); xd = $(jqxhr.responseText);
xd.find('modified').each(function () { xd.find('modified').each(function () {
out._last_modified = Date.parse($(this).text()); out._last_modified = Date.parse($(this).text());
}); });
...@@ -144,8 +154,12 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -144,8 +154,12 @@ jIO.addStorageType('xwiki', function (spec, my) {
out._creation_date = Date.parse($(this).text()); out._creation_date = Date.parse($(this).text());
}); });
xd.find('title').each(function () { out.title = $(this).text(); }); xd.find('title').each(function () { out.title = $(this).text(); });
xd.find('parent').each(function () { out.parent = $(this).text(); }); xd.find('parent').each(function () {
xd.find('syntax').each(function () { out.syntax = $(this).text(); }); out.parent = $(this).text();
});
xd.find('syntax').each(function () {
out.syntax = $(this).text();
});
xd.find('content').each(function () { xd.find('content').each(function () {
out.content = $(this).text(); out.content = $(this).text();
}); });
...@@ -163,7 +177,7 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -163,7 +177,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
}; };
$.ajax({ $.ajax({
url: getDocRestURL(docId), url: priv.getDocRestURL(docId),
type: "GET", type: "GET",
async: true, async: true,
dataType: 'xml', dataType: 'xml',
...@@ -198,13 +212,18 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -198,13 +212,18 @@ jIO.addStorageType('xwiki', function (spec, my) {
* attachment blob and err being the error if any. * attachment blob and err being the error if any.
*/ */
getAttachment: function (docId, fileName, andThen) { getAttachment: function (docId, fileName, andThen) {
var xhr, parts, url;
// need to do this manually, jquery doesn't support returning blobs. // need to do this manually, jquery doesn't support returning blobs.
var xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
var parts = getParts(docId); parts = priv.getParts(docId);
var url = priv.xwikiurl + '/bin/download/' + parts.space + url = priv.xwikiurl + '/bin/download/' + parts.space +
"/" + parts.page + "/" + fileName; "/" + parts.page + "/" + fileName + '?cb=' + Math.random();
xhr.open('GET', url, true); xhr.open('GET', url, true);
if (priv.useBlobs) {
xhr.responseType = 'blob'; xhr.responseType = 'blob';
} else {
xhr.responseType = 'text';
}
xhr.onload = function (e) { xhr.onload = function (e) {
if (xhr.status === 200) { if (xhr.status === 200) {
...@@ -212,11 +231,7 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -212,11 +231,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
if (contentType.indexOf(';') > -1) { if (contentType.indexOf(';') > -1) {
contentType = contentType.substring(0, contentType.indexOf(';')); contentType = contentType.substring(0, contentType.indexOf(';'));
} }
if (priv.useBlobs) {
andThen(makeBlob([xhr.response], {type: contentType}));
} else {
andThen(xhr.response); andThen(xhr.response);
}
} else { } else {
andThen(null, { andThen(null, {
"status": xhr.status, "status": xhr.status,
...@@ -241,14 +256,15 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -241,14 +256,15 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param andThen a callback taking (err), err being the error if any. * @param andThen a callback taking (err), err being the error if any.
*/ */
setItem: function (id, doc, andThen) { setItem: function (id, doc, andThen) {
doWithFormToken(function (formToken, err) { priv.doWithFormToken(function (formToken, err) {
if (err) { if (err) {
that.error(err); that.error(err);
return; return;
} }
var parts = getParts(id); var parts = priv.getParts(id);
$.ajax({ $.ajax({
url: priv.xwikiurl + "/bin/preview/" + parts.space + '/' + parts.page, url: priv.xwikiurl + "/bin/preview/" +
parts.space + '/' + parts.page,
type: "POST", type: "POST",
async: true, async: true,
dataType: 'text', dataType: 'text',
...@@ -257,8 +273,8 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -257,8 +273,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
title: doc.title || '', title: doc.title || '',
xredirect: '', xredirect: '',
language: 'en', language: 'en',
// RequiresHTMLConversion: 'content', // RequiresHTMLConversion: 'content',
// content_syntax: doc.syntax || 'xwiki/2.1', // content_syntax: doc.syntax || 'xwiki/2.1',
content: doc.content || '', content: doc.content || '',
xeditaction: 'edit', xeditaction: 'edit',
comment: 'Saved by JiO', comment: 'Saved by JiO',
...@@ -292,21 +308,23 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -292,21 +308,23 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param fileName the attachment file name. * @param fileName the attachment file name.
* @param mimeType the MIME type of the attachment content. * @param mimeType the MIME type of the attachment content.
* @param content the attachment content. * @param content the attachment content.
* @param andThen a callback taking one parameter which is the error if any. * @param andThen a callback taking one parameter, the error if any.
*/ */
setAttachment: function (docId, fileName, mimeType, content, andThen) { setAttachment: function (docId, fileName, mimeType, content, andThen) {
doWithFormToken(function (formToken, err) { priv.doWithFormToken(function (formToken, err) {
var parts, blob, fd, xhr;
if (err) { if (err) {
that.error(err); that.error(err);
return; return;
} }
var parts = getParts(docId); parts = priv.getParts(docId);
var blob = (content.constructor === "function Blob() { [native code] }") blob = priv.isBlob(content)
? content : makeBlob([content], {type: mimeType}); ? content
var fd = new FormData(); : priv.makeBlob([content], {type: mimeType});
fd = new FormData();
fd.append("filepath", blob, fileName); fd.append("filepath", blob, fileName);
fd.append("form_token", formToken); fd.append("form_token", formToken);
var xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open('POST', priv.xwikiurl + "/bin/upload/" + xhr.open('POST', priv.xwikiurl + "/bin/upload/" +
parts.space + '/' + parts.page, true); parts.space + '/' + parts.page, true);
xhr.onload = function (e) { xhr.onload = function (e) {
...@@ -328,14 +346,15 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -328,14 +346,15 @@ jIO.addStorageType('xwiki', function (spec, my) {
}, },
removeItem: function (id, andThen) { removeItem: function (id, andThen) {
doWithFormToken(function (formToken, err) { priv.doWithFormToken(function (formToken, err) {
if (err) { if (err) {
that.error(err); that.error(err);
return; return;
} }
var parts = getParts(id); var parts = priv.getParts(id);
$.ajax({ $.ajax({
url: priv.xwikiurl + "/bin/delete/" + parts.space + '/' + parts.page, url: priv.xwikiurl + "/bin/delete/" +
parts.space + '/' + parts.page,
type: "POST", type: "POST",
async: true, async: true,
dataType: 'text', dataType: 'text',
...@@ -360,8 +379,8 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -360,8 +379,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
}, },
removeAttachment: function (docId, fileName, andThen) { removeAttachment: function (docId, fileName, andThen) {
var parts = getParts(docId); var parts = priv.getParts(docId);
doWithFormToken(function (formToken, err) { priv.doWithFormToken(function (formToken, err) {
if (err) { if (err) {
that.error(err); that.error(err);
return; return;
...@@ -450,7 +469,8 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -450,7 +469,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
// URL location of the wiki, unused since // URL location of the wiki, unused since
// XWiki doesn't currently allow cross-domain requests. // XWiki doesn't currently allow cross-domain requests.
priv.xwikiurl = spec.xwikiurl || priv.xwikiurl = spec.xwikiurl ||
window.location.href.replace(/\/xwiki\/bin\//, '/xwiki\n').split('\n')[0]; window.location.href.replace(/\/xwiki\/bin\//, '/xwiki\n')
.split('\n')[0];
// should be: s@/xwiki/bin/.*$@/xwiki@ // should be: s@/xwiki/bin/.*$@/xwiki@
// but jslint gets in the way. // but jslint gets in the way.
...@@ -461,6 +481,10 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -461,6 +481,10 @@ jIO.addStorageType('xwiki', function (spec, my) {
// getAttachment() rather than strings. // getAttachment() rather than strings.
priv.useBlobs = spec.useBlobs || false; priv.useBlobs = spec.useBlobs || false;
// If true then Blob objects will be returned by
// getAttachment() rather than strings.
priv.useBlobs = spec.useBlobs || false;
that.specToStore = function () { that.specToStore = function () {
return { return {
...@@ -642,7 +666,8 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -642,7 +666,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.remove = that.removeAttachment = function (command) { that.remove = that.removeAttachment = function (command) {
var notFoundError = function (word) { var notFoundError, objId, complete;
notFoundError = function (word) {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -652,8 +677,8 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -652,8 +677,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
}); });
}; };
var objId = command.getDocId(); objId = command.getDocId();
var complete = function (err) { complete = function (err) {
if (err) { if (err) {
that.error(err); that.error(err);
} else { } else {
...@@ -691,4 +716,21 @@ jIO.addStorageType('xwiki', function (spec, my) { ...@@ -691,4 +716,21 @@ jIO.addStorageType('xwiki', function (spec, my) {
}; };
return that; return that;
}); };
if (typeof (define) === 'function' && define.amd) {
define(['jquery', 'jiobase', 'module'], function (jquery, j, mod) {
$ = jquery;
jIO.addStorageType('xwiki', store);
var conf = mod.config();
conf.type = 'xwiki';
return jIO.newJio(conf);
});
} else {
jIO.addStorageType('xwiki', store);
$ = jQuery;
}
}());
...@@ -6575,590 +6575,6 @@ test ("AllDocs", function () { ...@@ -6575,590 +6575,6 @@ test ("AllDocs", function () {
}); });
*/ */
module("Amazon S3 Storage");
/*
Post without id
Create = POST non empty document
Post but document already exists
*/
test ("Post", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"https://jiobucket.s3.amazonaws.com"
});
o.server = sinon.fakeServer.create();
//Post without ID (ID is generated by storage)
o.server.respondWith("GET",
"https://jiobucket.s3.amazonaws.com/",
[404, {}, "HTML Response"]
);
o.server.respondWith("POST",
"https://jiobucket.s3.amazonaws.com/",
[204, {}, "HTML Response"]
);
//o.spy (o, "status", 405, "Post without id");
o.spy(o, "jobstatus", "done", "Post without ID");
o.jio.post({}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
//Post with ID
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"https://jiobucket.s3.amazonaws.com/post1",
[404, {}, "HTML Response"]
);
o.server.respondWith("POST",
"https://jiobucket.s3.amazonaws.com/",
[204, {}, "HTML Response"]
);
o.spy(o, "value", {'ok':true,'id':'post1'}, "Post with ID");
o.jio.post({"_id": "post1", "title": "myPost1"}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
//Post but document already exists (update)
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/post2",
[200, {}, "HTML Response"]
);
o.spy (o, "status", 409, "Post but document already exists (update)");
//o.spy(o, "jobstatus", "done", "Post without ID");
o.jio.post({"_id": "post2", "title": "myPost2"}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
/*
Put without id
Create = PUT non empty document
Updated the document
*/
test("Put", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"http://s3.amazonaws.com/jiobucket/put1"
});
// put without id => id required
o.server = sinon.fakeServer.create();
o.spy (o, "status", 20, "Put without id");
o.jio.put({}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
//Put non empty document
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[404, {}, "HTML Response"]
);
o.server.respondWith("PUT",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200, {}, "HTML Response"]
);
o.spy (o, "value", {"ok": true, "id": "http://100%.json"},
"PUT non empty document");
o.jio.put({"_id": "http://100%.json", "title": "myPut1"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
//Put an existing document (update)
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200, {}, "HTML Response"]
);
o.server.respondWith("PUT",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200, {}, "HTML Response"]
);
o.spy (o, "value", {"ok": true, "id": "http://100%.json"},
"PUT non empty document");
o.jio.put({"_id": "http://100%.json", "title": "myPut1"}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
test ("PutAttachment", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"https://jiobucket.s3.amazonaws.com"
});
//PutAttachment without document ID (document ID is required)
o.server = sinon.fakeServer.create();
//PutAttachment without attachment ID (attachment ID is required)
o.spy(o, "status", 20, "PutAttachment without doc id -> 20");
o.jio.putAttachment({"_attachment": "putattmt1"}, o.f);
o.tick(o);
// putAttachment without attachment id => 22 Attachment Id Required
o.spy(o, "status", 22, "PutAttachment without attachment id -> 22");
o.jio.putAttachment({"_id": "http://100%.json"}, o.f);
o.tick(o);
//PutAttachment without underlying document (document is required)
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[404, {}, "HTML Response"]
);
o.spy(o, "status", 404, "PutAttachment without document -> 404");
o.jio.putAttachment({
"_id": "http://100%.json",
"_attachment": "putattmt2"
}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
//PutAttachment
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200,{"Content-Type": "text/plain"},'{"_id":"http://100%.json","title":"Hi There!"}']
);
o.server.respondWith("PUT",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200, {}, "HTML Response"]
);
o.server.respondWith("PUT",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json.body_.html",
[200, {}, "HTML Response"]
);
o.spy(o, "value", {
"ok": true,
"id": "http://100%.json",
"attachment": "body.html"
}, "PutAttachment");
o.jio.putAttachment({
"_id": "http://100%.json",
"_attachment": "body.html",
"_mimetype": "text/html",
"_data": "<h1>Hi There!!</h1><p>How are you?</p>"
}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
/*
Get non existing document
Get non existing attachment
Get document
Get non existing attachment (doc exists)
Get attachment
*/
test("Get", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"https://jiobucket.s3.amazonaws.com"
});
//Get non existing document
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"https://jiobucket.s3.amazonaws.com/doc",
[404, {}, "HTML Response"]
);
o.spy(o, "status", 404, "Get non existing document" );
o.jio.get("doc", {"max_retry":1}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
//Get document
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200,{"Content-Type": "text/plain"},'{"_id":"http://100%.json","title":"Hi There!"}']
);
o.spy(o, "value", {"_id":"http://100%.json","title":"Hi There!"}, "Get document" )
o.jio.get({"_id":"http://100%.json"}, {"max_retry":1}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
test("GetAttachment", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"https://jiobucket.s3.amazonaws.com"
});
//Get non existing attachment
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json.body_.html",
[404, {}, "HTML Response"]
);
o.spy(o, "status", 404, "Get non existing attachment" );
o.jio.getAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
//Get attachment
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json.body_.html",
[200, {"Content-Type": "text/plain"}, "My Attachment Content"]
);
o.spy(o, "value", "My Attachment Content", "Get attachment");
o.jio.getAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
//begins the remove tests
/*
Remove inexistent document
Remove inexistent document/attachment
Remove document
Check index file
Check if document has been removed
Remove one of multiple attachment
Check index file
Remove one document and attachment together
Check index file
Check if attachment has been removed
Check if document has been removed
*/
test ("Remove", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"https://jiobucket.s3.amazonaws.com/"
});
//Remove non-existing document
o.server = sinon.fakeServer.create();
o.server.respondWith("DELETE",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[404, {}, "HTML RESPONSE"]
);
o.spy(o, "status", 404, "Remove non existing document");
o.jio.remove({"_id": "http://100%.json"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.server.restore();
//Remove document
o.server = sinon.fakeServer.create();
o.server.respondWith("DELETE",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200, {"Content-Type": "text/plain"}, "My Attachment Content"]
);
o.spy(o, "value", {"ok":true, "id":"http://100%.json"}, "Remove document");
o.jio.remove({"_id": "http://100%.json"}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
//Remove document with multiple attachments
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[
200,
{"Content-Type": "text/html"},
JSON.stringify({
"_attachments": {
"body.html": {
"length": 32,
"digest": "md5-dontcare",
"content_type": "text/html"
},
"other": {
"length": 3,
"digest": "md5-dontcare-again",
"content_type": "text/plain"
}
}
})
]
);
o.server.respondWith("DELETE",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200, {"Content-Type": "text/plain"}, "<h1>Deleted</h1>"]
);
o.server.respondWith("DELETE",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json.body_.html",
[200, {"Content-Type": "text/plain"}, "<h1>Deleted</h1>"]
);
o.server.respondWith("DELETE",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json.other",
[200, {"Content-Type": "text/plain"}, "<h1>Deleted</h1>"]
);
o.spy(o, "value", {"ok": true, "id": "http://100%.json"},
"Remove document containing multiple attachments");
o.jio.remove({"_id": "http://100%.json"}, {"max_retry": 1}, o.f);
o.clock.tick(1000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
test ("RemoveAttachment", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"https://jiobucket.s3.amazonaws.com/"
});
//Remove non-existing attachment
o.server = sinon.fakeServer.create();
o.server.respondWith("DELETE",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json.body_.html",
[404, {}, "HTML RESPONSE"]
);
o.spy(o, "status", 404, "Remove non existing attachment");
o.jio.removeAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(5000);
o.server.respond();
o.server.restore();
//Remove attachment
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200,{"Content-Type": "text/plain"},'{"_id":"http://100%.json","title":"Hi There!"}']
);
o.server.respondWith("PUT",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json",
[200,{"Content-Type": "text/plain"},'{"_id":"http://100%.json","title":"Hi There!"}']
);
o.server.respondWith("DELETE",
"http://s3.amazonaws.com/jiobucket/http:%252F%252F100%2525_.json.body_.html",
[200, {"Content-Type": "text/plain"}, "My Attachment Content"]
);
o.spy(o, "value", {
"ok": true,
"id": "http://100%.json",
"attachment": "body.html"
}, "Remove attachment");
o.jio.removeAttachment({
"_id": "http://100%.json",
"_attachment": "body.html"
}, {"max_retry": 1}, o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
test ("AllDocs", function(){
var o = generateTools(this);
o.jio = JIO.newJio({
"type":"s3",
"AWSIdentifier":"dontcare",
"password":"dontcare",
"server":"jiobucket",
"url":"https://jiobucket.s3.amazonaws.com/"
});
//allDocs without option
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/",
[204, {}, '<?xml version="1.0" encoding="UTF-8"?><ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>jiobucket</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>documentONE</Key><LastModified>2013-05-03T15:32:01.000Z</LastModified><ETag>&quot;8a65389818768e1f5e6530a949233581&quot;</ETag><Size>163</Size><Owner><ID>5d09e586ab92acad85e9d053f769cce65f82178e218d9ac9b0473f3ce7f89606</ID><DisplayName>jonathan.rivalan</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>documentONE.1st_Attachment_manual</Key><LastModified>2013-05-03T15:32:02.000Z</LastModified><ETag>&quot;f391dec65366d2b470406bc7b9595dea&quot;</ETag><Size>35</Size><Owner><ID>5d09e586ab92acad85e9d053f769cce65f82178e218d9ac9b0473f3ce7f89606</ID><DisplayName>jonathan.rivalan</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>']
);
o.spy(o, "jobstatus", "done", "AllDocs without include docs");
o.jio.allDocs(o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
//allDocs with the include docs option
o.server = sinon.fakeServer.create();
o.server.respondWith("GET",
"http://s3.amazonaws.com/jiobucket/",
[204, {}, '<?xml version="1.0" encoding="UTF-8"?><ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>jiobucket</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>documentONE</Key><LastModified>2013-05-03T15:32:01.000Z</LastModified><ETag>&quot;8a65389818768e1f5e6530a949233581&quot;</ETag><Size>163</Size><Owner><ID>5d09e586ab92acad85e9d053f769cce65f82178e218d9ac9b0473f3ce7f89606</ID><DisplayName>jonathan.rivalan</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>documentONE.1st_Attachment_manual</Key><LastModified>2013-05-03T15:32:02.000Z</LastModified><ETag>&quot;f391dec65366d2b470406bc7b9595dea&quot;</ETag><Size>35</Size><Owner><ID>5d09e586ab92acad85e9d053f769cce65f82178e218d9ac9b0473f3ce7f89606</ID><DisplayName>jonathan.rivalan</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>']
);
o.server.respondWith("GET",
"https://jiobucket.s3.amazonaws.com/documentONE",
[
200,
{"Content-Type": "text/html"},
JSON.stringify({
"_attachments": {
"body.html": {
"length": 32,
"digest": "md5-dontcare",
"content_type": "text/html"
},
"other": {
"length": 3,
"digest": "md5-dontcare-again",
"content_type": "text/plain"
}
}
})
]
);
o.spy(o, "jobstatus", "done", "AllDocs with include docs");
o.jio.allDocs({"include_docs": true},o.f);
o.clock.tick(5000);
o.server.respond();
o.tick(o);
o.server.restore();
o.jio.stop();
});
var nThen = function(next) { var nThen = function(next) {
var funcs = []; var funcs = [];
var calls = 0; var calls = 0;
...@@ -7326,5 +6742,3 @@ if (window.requirejs) { ...@@ -7326,5 +6742,3 @@ if (window.requirejs) {
} }
}()); }());
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