Fix various bugs saving from connector and add test

This commit is contained in:
Dan Stillman 2016-05-13 14:59:46 -04:00
parent 55c60a69ac
commit eb400587e8
3 changed files with 112 additions and 6 deletions

View file

@ -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);

View file

@ -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);
}

View file

@ -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');
});
});
});