Fix various bugs saving from connector and add test
This commit is contained in:
parent
55c60a69ac
commit
eb400587e8
|
@ -354,8 +354,13 @@ Zotero.Server.Connector.SaveItem.prototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
// save items
|
// save items
|
||||||
var itemSaver = new Zotero.Translate.ItemSaver(libraryID,
|
var itemSaver = new Zotero.Translate.ItemSaver({
|
||||||
Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD, 1, undefined, cookieSandbox);
|
libraryID,
|
||||||
|
collections: collection ? [collection.id] : undefined,
|
||||||
|
attachmentMode: Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD,
|
||||||
|
forceTagType: 1,
|
||||||
|
cookieSandbox
|
||||||
|
});
|
||||||
itemSaver.saveItems(data.items, function(returnValue, items) {
|
itemSaver.saveItems(data.items, function(returnValue, items) {
|
||||||
if(returnValue) {
|
if(returnValue) {
|
||||||
try {
|
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}));
|
sendResponseCallback(201, "application/json", JSON.stringify({"items":data.items}));
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
Zotero.logError(e);
|
Zotero.logError(e);
|
||||||
|
|
|
@ -619,6 +619,10 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
let newTags = [];
|
let newTags = [];
|
||||||
for(let i=0; i<tags.length; i++) {
|
for(let i=0; i<tags.length; i++) {
|
||||||
let tag = tags[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;
|
tag.type = this._forceTagType || tag.type || 0;
|
||||||
newTags.push(tag);
|
newTags.push(tag);
|
||||||
}
|
}
|
||||||
|
|
101
test/tests/server_connectorTest.js
Normal file
101
test/tests/server_connectorTest.js
Normal 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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user