Commit d005d845 authored by lucas.parsy's avatar lucas.parsy Committed by Romain Courteaud

GdriveStorage: add a new google drive storage

Squashed commit of the following:

commit 2283732c2e3649d66c3b83f00ac2a9d846f50a35
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Dec 4 11:02:50 2015 +0100

    modified gdrivestorage's allDocs to return only the documents ids.
    modified gdrivestorage test file to fit the above changes.
    refactored an URI template in gdrivestorage.

commit eaf750820f349ff1add6ef5172779a119dc06597
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Nov 27 17:16:16 2015 +0100

    added allAttachments method to gdriveStorage with tests.
    corrected bug in get method returning a string instead of an object.

commit 212e4d6b41382c9d8ad2aaa9743671850eac245d
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Nov 27 15:49:05 2015 +0100

    various fixes on gdriveStorage and his tests.

    removed the removeAttachment method as it was useless.
    removed error catching when user was doing request with invalid credentials.
    throw error when doing a putAttachment on a folder.

    changed varaiables names in test file to respect naming convention.
    added/ removed tests following the changes on gdriveStorage file.

    added tests verifying that the user can only put "enclosure"
    as attachment name in putAttachment and getAttachment.

commit 5150c9420e30ab7869c4d4c2cedd5eddeb1e52de
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Tue Nov 24 11:01:33 2015 +0100

    followed naming convention in gdrivestorage test file.
    post and put method now return the id of the docment they created/modified.
    corrected bug in getAttachment allowing the method to pass the test,
    but not to work on real utilisation.

commit 0195dc37eeac4ae8f009f483e566180c81c857b1
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Tue Nov 3 16:44:20 2015 +0100

    added test for gdrivestorage for allDocs() method
    when method needs to do multiple calls to google Drive API.
    added gdrivestorage.tests.js to tests.html file.

commit 204acb98616267a5598b099f8375c3fe38d4e268
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Oct 30 12:27:49 2015 +0100

    refactored gdrivestorage.js
    Improved gdrive allDocs test to check return value.

commit 23f6ec71de45d161c36ca148d5fa53d475a51d89
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Thu Oct 29 14:21:36 2015 +0100

    corrected 2 jslint errors in gdrivestorage test file

commit b472700451e72659fecfacd07305cddb48ece323
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Thu Oct 29 13:40:08 2015 +0100

    completed google drive storage and added test file

commit 841a5fb5d2388cd822ed53dec1984b0aafb3accd
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Mon Oct 19 16:12:56 2015 +0200

    changed names of variables and checked user input in putAttachment method

commit 991cd0a70ce3a9da61c724f6bf50336684776535
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Fri Sep 25 15:31:57 2015 +0200

    refactoring of gdrivestorage.js
    various conditions optimized
    added compliance with future tests.

    added error message when giving non-boolean value
    for "trashed" value at creation of gdrive storage.

commit 8d82e3e51634d774685bc39c804a9a383dc46cad
Author: Lucas Parsy <lucas.parsy@nexedi.com>
Date:   Thu Sep 24 17:33:00 2015 +0200

    added google drive storage to jio (tests to be done)
parent e10057fd
......@@ -173,6 +173,7 @@ module.exports = function (grunt) {
'src/jio.storage/cryptstorage.js',
'src/jio.storage/dropboxstorage.js',
'src/jio.storage/davstorage.js',
'src/jio.storage/gdrivestorage.js',
'src/jio.storage/unionstorage.js',
'src/jio.storage/erp5storage.js',
'src/jio.storage/querystorage.js',
......
......@@ -43,6 +43,7 @@ zip:
@cp src/jio.storage/replicatestorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/cryptstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/gdrivestorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/revisionstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/zipstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/replicaterevisionstorage.js $(TMPDIR)/jio/storage/
......@@ -76,6 +77,7 @@ zip:
@$(UGLIFY) src/jio.storage/replicatestorage.js >$(TMPDIR)/jio/storage/replicatestorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/cryptstorage.js >$(TMPDIR)/jio/storage/cryptstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/gdrivestorage.js >$(TMPDIR)/jio/storage/gdrivestorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/revisionstorage.js >$(TMPDIR)/jio/storage/revisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/replicaterevisionstorage.js >$(TMPDIR)/jio/storage/replicaterevisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/zipstorage.js >$(TMPDIR)/jio/storage/zipstorage.min.js 2>/dev/null
......
/*
* Copyright 2015, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/**
* JIO Google Drive Storage. Type = "gdrive".
* Google Drive "database" storage.
*/
/*global jIO, Blob, RSVP, UriTemplate, JSON*/
/*jslint nomen: true*/
(function (jIO, Blob, RSVP, UriTemplate, JSON) {
"use strict";
var UPLOAD_URL = "https://www.googleapis.com{/upload}/drive/v2/files{/id}" +
"{?uploadType,access_token}",
upload_template = UriTemplate.parse(UPLOAD_URL),
REMOVE_URL = "https://www.googleapis.com/drive/v2/" +
"files{/id,trash}{?access_token}",
remove_template = UriTemplate.parse(REMOVE_URL),
LIST_URL = "https://www.googleapis.com/drive/v2/files" +
"?prettyPrint=false{&pageToken}&q=trashed=false" +
"&fields=nextPageToken,items(id){&access_token}",
list_template = UriTemplate.parse(LIST_URL),
GET_URL = "https://www.googleapis.com/drive/v2/files{/id}{?alt}",
get_template = UriTemplate.parse(GET_URL);
function handleError(error, id) {
if (error.target.status === 404) {
throw new jIO.util.jIOError(
"Cannot find document: " + id,
404
);
}
throw error;
}
function listPage(result, token) {
var i,
obj;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": "GET",
"url": list_template.expand({
pageToken : (result.nextPageToken || ""),
access_token: token
})
});
})
.push(function (data) {
obj = JSON.parse(data.target.response || data.target.responseText);
for (i = 0; i < obj.items.length; i += 1) {
obj.items[i].value = {};
result.push(obj.items[i]);
}
result.nextPageToken = obj.nextPageToken;
return result;
}, handleError);
}
function checkName(name) {
if (name !== "enclosure") {
throw new jIO.util.jIOError("Only support 'enclosure' attachment", 400);
}
}
/**
* The JIO Google Drive Storage extension
*
* @class GdriveStorage
* @constructor
*/
function GdriveStorage(spec) {
if (spec === undefined || spec.access_token === undefined ||
typeof spec.access_token !== 'string') {
throw new TypeError("Access Token must be a string " +
"which contains more than one character.");
}
if (spec.trashing !== undefined &&
(spec.trashing !== true && spec.trashing !== false)) {
throw new TypeError("trashing parameter" +
" must be a boolean (true or false)");
}
this._trashing = spec.trashing || true;
this._access_token = spec.access_token;
return;
}
function recursiveAllDocs(result, accessToken) {
return new RSVP.Queue()
.push(function () {
return listPage(result, accessToken);
})
.push(function () {
if (result.nextPageToken) {
return recursiveAllDocs(result, accessToken);
}
return result;
});
}
GdriveStorage.prototype.hasCapacity = function (name) {
return (name === "list");
};
GdriveStorage.prototype.buildQuery = function () {
return recursiveAllDocs([], this._access_token);
};
function sendMetaData(id, param, token) {
var boundary = "-------314159265358979323846";
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": id ? "PUT" : "POST",
"url": upload_template.expand({
access_token: token,
id: id || [],
upload: id ? [] : "upload",
uploadType: "multipart"
}),
headers: {
"Content-Type" : 'multipart/related; boundary="' + boundary + '"'
},
data: '--' + boundary + '\n' +
'Content-Type: application/json; charset=UTF-8\n\n' +
JSON.stringify(param) + '\n\n--' + boundary + "--"
});
})
.push(function (result) {
var obj = JSON.parse(result.target.responseText);
return obj.id;
},
function (error) {handleError(error, id); });
}
GdriveStorage.prototype.put = function (id, param) {
return sendMetaData(id, param, this._access_token);
};
GdriveStorage.prototype.post = function (param) {
return sendMetaData(undefined, param, this._access_token);
};
function sendData(id, blob, token) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
"type": "PUT",
"url": upload_template.expand({
access_token: token,
upload: "upload",
id: id,
uploadType: "media"
}),
data: blob
});
})
.push(function (data) {
data = JSON.parse(data.target.responseText);
if (data.mimeType === "application/vnd.google-apps.folder") {
throw new jIO.util.jIOError("cannot put attachments to folder", 400);
}
return data;
}, function (error) {handleError(error, id); });
}
GdriveStorage.prototype.putAttachment = function (id, name, blob) {
checkName(name);
return sendData(id, blob, this._access_token);
};
GdriveStorage.prototype.remove = function (id) {
var that = this;
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: that._trashing ? "POST" : "DELETE",
url: remove_template.expand({
id : id,
access_token : that._access_token,
trash : that._trashing ? "trash" : []
})
});
})
.push(undefined, function (error) {handleError(error, id); });
};
function getData(id, attach, token) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "GET",
dataType: attach ? "blob" : "json",
url: get_template.expand({
id: id,
alt: attach ? "media" : [],
access_token: token
}),
headers: {
"Authorization" : "Bearer " + token
}
});
})
.push(function (evt) {
return evt.target.response ||
(attach ? new Blob([evt.target.responseText],
{"type" :
evt.target.responseHeaders["Content-Type"]}) :
JSON.parse(evt.target.responseText));
}, function (error) {handleError(error, id); });
}
GdriveStorage.prototype.get = function (id) {
return getData(id, false, this._access_token);
};
GdriveStorage.prototype.getAttachment = function (id, name) {
checkName(name);
return getData(id, true, this._access_token);
};
GdriveStorage.prototype.allAttachments = function (id) {
var token = this._access_token;
return new RSVP.Queue()
.push(function () {
return getData(id, false, token);
})
.push(function (data) {
if (data.mimeType === "application/vnd.google-apps.folder") {
return {};
}
return {"enclosure": {}};
});
};
jIO.addStorage('gdrive', GdriveStorage);
}(jIO, Blob, RSVP, UriTemplate, JSON));
/*jslint nomen: true */
/*global Blob, sinon*/
(function (jIO, QUnit, Blob, sinon) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module,
throws = QUnit.throws,
token = "sample_token",
domain = "https://www.googleapis.com",
boundary = "---------314159265358979323846",
list_url = domain + "/drive/v2/files" +
"?prettyPrint=false&pageToken=&q=trashed=false" +
"&fields=nextPageToken,items(id)" +
"&access_token=" + token,
sample_list = '{"items":[' +
'{"id":"0B4kh3jbjOf5LamRlX21MZlVCYXM"}]}',
part_sample1 = '{"nextPageToken": "nptkn01",' +
'"items":[{"id":"0B4kh3jbjOf5Lb2theE8xWHhvWXM"}]}',
part_sample2 = '{"items":[{"id":"0B4kh3jbjOf5LamRlX21MZ"}]}';
function error404Tester(fun, encl, blob) {
stop();
expect(3);
this.jio[fun]("inexistent", encl ? "enclosure" : undefined,
blob || undefined)
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find document: inexistent");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
}
/////////////////////////////////////////////////////////////////
// Google Drive Storage constructor
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.constructor");
test("create storage", function () {
var jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
equal(jio.__type, "gdrive");
deepEqual(jio.__storage._access_token, token);
});
test("reject invalid trashing parameter", function () {
throws(
function () {
jIO.createJIO({
type: "gdrive",
access_token: token,
trashing : "foobar"
});
},
function (error) {
ok(error instanceof TypeError);
equal(error.message,
"trashing parameter must be a boolean (true or false)");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.post
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.post", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("post document", function () {
var server = this.server,
put_url = domain + "/upload/drive/v2/files?uploadType" +
"=multipart&access_token=" + token,
body = boundary +
'\nContent-Type: application/json; charset=UTF-8' +
'\n\n{\"title\":\"metadata\"}\n\n' + boundary + "--",
res_text = '{"id": "sampleId"}';
this.server.respondWith("POST", put_url, [200, {
"Content-Type": "text/xml"
}, res_text]);
stop();
expect(7);
this.jio.post({title: "metadata"})
.then(function (obj) {
equal(obj, "sampleId");
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, put_url);
equal(server.requests[0].status, 200);
equal(server.requests[0].requestBody, body);
equal(server.requests[0].responseText, res_text);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.put
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.put", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("put document", function () {
var server = this.server,
put_url = domain + "/drive/v2/files/sampleId?uploadType" +
"=multipart&access_token=" + token,
body = boundary +
'\nContent-Type: application/json; charset=UTF-8' +
'\n\n{\"title\":\"metadata\"}\n\n' + boundary + "--",
res_text = '{"id": "sampleId"}';
this.server.respondWith("PUT", put_url, [200, {
"Content-Type": "text/xml"
}, res_text]);
stop();
expect(7);
this.jio.put("sampleId", {title: "metadata"})
.then(function (obj) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "PUT");
equal(server.requests[0].url, put_url);
equal(server.requests[0].status, 200);
equal(server.requests[0].requestBody, body);
equal(server.requests[0].responseText, res_text);
equal(obj, "sampleId");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put to inexistent document", function () {
var tester = error404Tester.bind(this);
tester("put");
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.remove
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.remove", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("remove document", function () {
var url_delete = domain +
"/drive/v2/files/sampleId/trash?access_token=" +
token,
server = this.server;
this.server.respondWith("POST", url_delete, [200, {
"Content-Type": "text/xml"
}, 'sampleId']);
stop();
expect(7);
this.jio.remove("sampleId")
.then(function () {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, url_delete);
equal(server.requests[0].status, 200);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].responseText, 'sampleId');
deepEqual(server.requests[0].requestHeaders, {
"Content-Type": "text/plain;charset=utf-8"
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("remove inexistent document", function () {
var tester = error404Tester.bind(this);
tester("remove");
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.get
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.get", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("get inexistent document", function () {
var tester = error404Tester.bind(this);
tester("get");
});
test("get document", function () {
var url = domain + "/drive/v2/files/sampleId?alt=",
body = '{"id": "sampleId", "mimeType":' +
'"application/vnd.google-apps.folder", "title": "folder1"}';
this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml"
}, body
]);
stop();
expect(1);
this.jio.get("sampleId")
.then(function (result) {
deepEqual(result,
{"id": "sampleId",
"mimeType": "application/vnd.google-apps.folder",
"title": "folder1"}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.allDocs
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.allDocs", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("get all docs", function () {
var object_result = {"data": {"rows": [{"id":
"0B4kh3jbjOf5LamRlX21MZlVCYXM",
"value": {}}],
"total_rows": 1}},
server = this.server;
this.server.respondWith("GET", list_url, [200, {
}, sample_list]);
stop();
expect(7);
this.jio.allDocs()
.then(function (res) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, list_url);
equal(server.requests[0].status, 200);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].responseText, sample_list);
deepEqual(res, object_result);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allDocs with multiple API requests (nextPageToken)", function () {
var object_result = {"data": {"rows": [], "total_rows": 2}},
server = this.server,
token_url = domain + "/drive/v2/files" +
"?prettyPrint=false&pageToken=nptkn01&q=trashed=false" +
"&fields=nextPageToken,items(id)" +
"&access_token=" + token;
object_result.data.rows.push(
{"id": "0B4kh3jbjOf5Lb2theE8xWHhvWXM", "value": {}}
);
object_result.data.rows.push(
{"id": "0B4kh3jbjOf5LamRlX21MZ", "value": {}}
);
this.server.respondWith("GET", list_url, [200, {
}, part_sample1]);
this.server.respondWith("GET", token_url, [200, {
}, part_sample2]);
stop();
expect(12);
this.jio.allDocs()
.then(function (res) {
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, list_url);
equal(server.requests[0].status, 200);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].responseText, part_sample1);
equal(server.requests[0].method, "GET");
equal(server.requests[1].url, token_url);
equal(server.requests[1].status, 200);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].responseText, part_sample2);
deepEqual(res, object_result);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.putAttachment
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.putAttachment", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("reject non enclosure attachment", function () {
stop();
expect(3);
this.jio.putAttachment(
"sampleId",
"not_enclosure",
new Blob()
)
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Only support 'enclosure' attachment");
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("putAttachment document", function () {
var blob = new Blob(["foo"]),
url_put_att = domain + "/upload/drive/v2/files/sampleId?" +
"uploadType=media&access_token=" + token,
server = this.server;
this.server.respondWith("PUT", url_put_att, [204, {
"Content-Type": "text/xml"
}, '{"mimeType": "text/xml"}']);
stop();
expect(7);
this.jio.putAttachment(
"sampleId",
"enclosure",
blob
)
.then(function () {
equal(server.requests.length, 1);
equal(server.requests[0].method, "PUT");
equal(server.requests[0].url, url_put_att);
equal(server.requests[0].status, 204);
equal(server.requests[0].responseText, "{\"mimeType\": \"text/xml\"}");
deepEqual(server.requests[0].requestHeaders, {
"Content-Type": "text/plain;charset=utf-8"
});
equal(server.requests[0].requestBody, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("putAttachment to inexistent document", function () {
var tester = error404Tester.bind(this);
tester("putAttachment", true, new Blob());
});
test("putAttachment to folder", function () {
var blob = new Blob([""]),
url_put_att = domain + "/upload/drive/v2/files/sampleId?" +
"uploadType=media&access_token=" + token,
server = this.server;
this.server.respondWith("PUT", url_put_att, [204, {
"Content-Type": "text/xml"
}, '{"mimeType": "application/vnd.google-apps.folder"}']);
stop();
expect(4);
this.jio.putAttachment(
"sampleId",
"enclosure",
blob
)
.fail(function (error) {
equal(server.requests[0].responseText,
"{\"mimeType\": \"application/vnd.google-apps.folder\"}");
ok(error instanceof jIO.util.jIOError);
equal(error.message, "cannot put attachments to folder");
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.getAttachment
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.getAttachment", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("reject non enclosure attachment", function () {
stop();
expect(3);
this.jio.getAttachment(
"sampleId",
"not_enclosure"
)
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Only support 'enclosure' attachment");
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("getAttachment document", function () {
var url = domain + "/drive/v2/files/" +
"sampleId?alt=media",
server = this.server;
this.server.respondWith("GET", url, [200, {
"Content-Type": "text/plain"
}, "foo\nbaré"]);
stop();
expect(8);
this.jio.getAttachment(
"sampleId",
"enclosure"
)
.then(function (result) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, url);
equal(server.requests[0].status, 200);
equal(server.requests[0].responseText, "foo\nbaré");
ok(result instanceof Blob, "Data is Blob");
deepEqual(result.type, "text/plain", "Check mimetype");
return jIO.util.readBlobAsText(result);
})
.then(function (result) {
equal(result.target.result, "foo\nbaré",
"Attachment correctly fetched");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get inexistent attachment", function () {
var tester = error404Tester.bind(this);
tester("getAttachment", true);
});
/////////////////////////////////////////////////////////////////
// Google Drive Storage.allAttachments
/////////////////////////////////////////////////////////////////
module("Google Drive Storage.allAttachments", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "gdrive",
access_token: token
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("allAttachments on file", function () {
var url = domain + "/drive/v2/files/sampleId?alt=",
body = '{"id": "sampleId", "mimeType":' +
'"text/xml", "title": "folder1"}';
this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml"
}, body
]);
stop();
expect(1);
this.jio.allAttachments("sampleId")
.then(function (result) {
deepEqual(result, {enclosure: {}}, "enclosure on file");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allAttachments on directory", function () {
var url = domain + "/drive/v2/files/sampleId?alt=",
body = '{"id": "sampleId", "mimeType":' +
'"application/vnd.google-apps.folder", "title": "folder1"}';
this.server.respondWith("GET", url, [200, {
"Content-Type": "text/xml"
}, body
]);
stop();
expect(1);
this.jio.allAttachments("sampleId")
.then(function (result) {
deepEqual(result, {}, "empty result on directory");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get inexistent attachment", function () {
var tester = error404Tester.bind(this);
tester("allAttachments");
});
}(jIO, QUnit, Blob, sinon));
......@@ -46,7 +46,7 @@
<script src="jio.storage/cryptstorage.tests.js"></script>
<script src="jio.storage/dropboxstorage.tests.js"></script>
<script src="jio.storage/zipstorage.tests.js"></script>
<script src="jio.storage/gdrivestorage.tests.js"></script>
<!--script src="../lib/jquery/jquery.min.js"></script>
<script src="../src/jio.storage/xwikistorage.js"></script>
<script src="jio.storage/xwikistorage.tests.js"></script-->
......
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