From eb400587e8b53811de43157e30aab5d66ed36ce6 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 13 May 2016 14:59:46 -0400 Subject: [PATCH] Fix various bugs saving from connector and add test --- .../content/zotero/xpcom/server_connector.js | 13 +-- .../xpcom/translation/translate_item.js | 4 + test/tests/server_connectorTest.js | 101 ++++++++++++++++++ 3 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 test/tests/server_connectorTest.js diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js index 47bc9f9b7..d7587397f 100644 --- 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 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("TitleBody"); + } + } + ); + + 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'); + }); + }); +});