commit eb400587e8b53811de43157e30aab5d66ed36ce6
parent 55c60a69ac1fe88ed374886140ab28d532a28dc4
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 13 May 2016 14:59:46 -0400
Fix various bugs saving from connector and add test
Diffstat:
3 files changed, 112 insertions(+), 6 deletions(-)
diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js
@@ -354,8 +354,13 @@ Zotero.Server.Connector.SaveItem.prototype = {
}
// save items
- var itemSaver = new Zotero.Translate.ItemSaver(libraryID,
- Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD, 1, undefined, cookieSandbox);
+ var itemSaver = new Zotero.Translate.ItemSaver({
+ libraryID,
+ collections: collection ? [collection.id] : undefined,
+ attachmentMode: Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD,
+ forceTagType: 1,
+ cookieSandbox
+ });
itemSaver.saveItems(data.items, function(returnValue, items) {
if(returnValue) {
try {
@@ -369,10 +374,6 @@ Zotero.Server.Connector.SaveItem.prototype = {
}
}
- for(var i=0; i<items.length; i++) {
- if(collection) collection.addItem(items[i].id);
- }
-
sendResponseCallback(201, "application/json", JSON.stringify({"items":data.items}));
} catch(e) {
Zotero.logError(e);
diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js
@@ -619,6 +619,10 @@ Zotero.Translate.ItemSaver.prototype = {
let newTags = [];
for(let i=0; i<tags.length; i++) {
let tag = tags[i];
+ // Convert raw string to object with 'tag' property
+ if (typeof tag == 'string') {
+ tag = { tag };
+ }
tag.type = this._forceTagType || tag.type || 0;
newTags.push(tag);
}
diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js
@@ -0,0 +1,101 @@
+"use strict";
+
+describe("Connector Server", function () {
+ Components.utils.import("resource://zotero-unit/httpd.js");
+ var win, connectorServerPath, testServerPath, httpd;
+ var testServerPort = 16213;
+
+ before(function* () {
+ Zotero.Prefs.set("httpServer.enabled", true);
+ yield resetDB({
+ thisArg: this,
+ skipBundledFiles: true
+ });
+
+ win = yield loadZoteroPane();
+ connectorServerPath = 'http://127.0.0.1:' + Zotero.Prefs.get('httpServer.port');
+ testServerPath = 'http://127.0.0.1:' + testServerPort;
+ });
+
+ beforeEach(function () {
+ httpd = new HttpServer();
+ httpd.start(testServerPort);
+ });
+
+ afterEach(function* () {
+ var defer = new Zotero.Promise.defer();
+ httpd.stop(() => defer.resolve());
+ yield defer.promise;
+ });
+
+ describe("/connector/saveItems", function () {
+ // TODO: Test cookies
+ it("should save an item to the current selected collection", function* () {
+ var collection = yield createDataObject('collection');
+ yield waitForItemsLoad(win);
+
+ var body = {
+ items: [
+ {
+ itemType: "newspaperArticle",
+ title: "Title",
+ creators: [
+ {
+ firstName: "First",
+ lastName: "Last",
+ creatorType: "author"
+ }
+ ],
+ attachments: [
+ {
+ title: "Attachment",
+ url: `${testServerPath}/attachment`,
+ mimeType: "text/html"
+ }
+ ]
+ }
+ ],
+ uri: "http://example.com"
+ };
+
+ httpd.registerPathHandler(
+ "/attachment",
+ {
+ handle: function (request, response) {
+ response.setStatusLine(null, 200, "OK");
+ response.write("<html><head><title>Title</title><body>Body</body></html>");
+ }
+ }
+ );
+
+ var promise = waitForItemEvent('add');
+ var req = yield Zotero.HTTP.request(
+ 'POST',
+ connectorServerPath + "/connector/saveItems",
+ {
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: JSON.stringify(body)
+ }
+ );
+
+ // Check parent item
+ var ids = yield promise;
+ assert.lengthOf(ids, 1);
+ var item = Zotero.Items.get(ids[0]);
+ assert.equal(Zotero.ItemTypes.getName(item.itemTypeID), 'newspaperArticle');
+ assert.isTrue(collection.hasItem(item.id));
+
+ // Check attachment
+ promise = waitForItemEvent('add');
+ ids = yield promise;
+ assert.lengthOf(ids, 1);
+ item = Zotero.Items.get(ids[0]);
+ assert.isTrue(item.isImportedAttachment());
+
+ // Wait until indexing is done
+ yield waitForItemEvent('refresh');
+ });
+ });
+});