Fix attachment-download test
This commit is contained in:
parent
065bac3b8a
commit
13cd84e1be
1 changed files with 71 additions and 61 deletions
|
@ -147,6 +147,7 @@ describe("ZoteroPane", function() {
|
||||||
var baseURL = `http://localhost:${port}/`;
|
var baseURL = `http://localhost:${port}/`;
|
||||||
var server;
|
var server;
|
||||||
var responses = {};
|
var responses = {};
|
||||||
|
var httpd;
|
||||||
|
|
||||||
var setup = Zotero.Promise.coroutine(function* (options = {}) {
|
var setup = Zotero.Promise.coroutine(function* (options = {}) {
|
||||||
server = sinon.fakeServer.create();
|
server = sinon.fakeServer.create();
|
||||||
|
@ -157,44 +158,20 @@ describe("ZoteroPane", function() {
|
||||||
setHTTPResponse(server, baseURL, response, responses);
|
setHTTPResponse(server, baseURL, response, responses);
|
||||||
}
|
}
|
||||||
|
|
||||||
before(function () {
|
async function downloadOnDemand() {
|
||||||
Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
|
|
||||||
|
|
||||||
Zotero.Prefs.set("api.url", baseURL);
|
|
||||||
Zotero.Sync.Runner.apiKey = apiKey;
|
|
||||||
})
|
|
||||||
beforeEach(function* () {
|
|
||||||
this.httpd = new HttpServer();
|
|
||||||
this.httpd.start(port);
|
|
||||||
|
|
||||||
yield Zotero.Users.setCurrentUserID(1);
|
|
||||||
yield Zotero.Users.setCurrentUsername("testuser");
|
|
||||||
})
|
|
||||||
afterEach(function* () {
|
|
||||||
var defer = new Zotero.Promise.defer();
|
|
||||||
this.httpd.stop(() => defer.resolve());
|
|
||||||
yield defer.promise;
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should download an attachment on-demand", function* () {
|
|
||||||
for (let fn of ['downloadAsNeeded', 'downloadOnSync']) {
|
|
||||||
yield setup();
|
|
||||||
|
|
||||||
Zotero.Sync.Storage.Local[fn](Zotero.Libraries.userLibraryID, true);
|
|
||||||
|
|
||||||
var item = new Zotero.Item("attachment");
|
var item = new Zotero.Item("attachment");
|
||||||
item.attachmentLinkMode = 'imported_file';
|
item.attachmentLinkMode = 'imported_file';
|
||||||
item.attachmentPath = 'storage:test.txt';
|
item.attachmentPath = 'storage:test.txt';
|
||||||
// TODO: Test binary data
|
// TODO: Test binary data
|
||||||
var text = Zotero.Utilities.randomString();
|
var text = Zotero.Utilities.randomString();
|
||||||
item.attachmentSyncState = "to_download";
|
item.attachmentSyncState = "to_download";
|
||||||
yield item.saveTx();
|
await item.saveTx();
|
||||||
|
|
||||||
var mtime = "1441252524000";
|
var mtime = "1441252524000";
|
||||||
var md5 = Zotero.Utilities.Internal.md5(text)
|
var md5 = Zotero.Utilities.Internal.md5(text)
|
||||||
|
|
||||||
var s3Path = `pretend-s3/${item.key}`;
|
var s3Path = `pretend-s3/${item.key}`;
|
||||||
this.httpd.registerPathHandler(
|
httpd.registerPathHandler(
|
||||||
`/users/1/items/${item.key}/file`,
|
`/users/1/items/${item.key}/file`,
|
||||||
{
|
{
|
||||||
handle: function (request, response) {
|
handle: function (request, response) {
|
||||||
|
@ -206,7 +183,7 @@ describe("ZoteroPane", function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
this.httpd.registerPathHandler(
|
httpd.registerPathHandler(
|
||||||
"/" + s3Path,
|
"/" + s3Path,
|
||||||
{
|
{
|
||||||
handle: function (request, response) {
|
handle: function (request, response) {
|
||||||
|
@ -219,18 +196,51 @@ describe("ZoteroPane", function() {
|
||||||
// Disable loadURI() so viewAttachment() doesn't trigger translator loading
|
// Disable loadURI() so viewAttachment() doesn't trigger translator loading
|
||||||
var stub = sinon.stub(zp, "loadURI");
|
var stub = sinon.stub(zp, "loadURI");
|
||||||
|
|
||||||
yield zp.viewAttachment(item.id);
|
await zp.viewAttachment(item.id);
|
||||||
|
|
||||||
assert.ok(stub.calledOnce);
|
assert.ok(stub.calledOnce);
|
||||||
assert.ok(stub.calledWith(OS.Path.toFileURI(item.getFilePath())));
|
assert.ok(stub.calledWith(OS.Path.toFileURI(item.getFilePath())));
|
||||||
stub.restore();
|
stub.restore();
|
||||||
|
|
||||||
assert.equal((yield item.attachmentHash), md5);
|
assert.equal(await item.attachmentHash, md5);
|
||||||
assert.equal((yield item.attachmentModificationTime), mtime);
|
assert.equal(await item.attachmentModificationTime, mtime);
|
||||||
var path = yield item.getFilePathAsync();
|
var path = await item.getFilePathAsync();
|
||||||
assert.equal((yield Zotero.File.getContentsAsync(path)), text);
|
assert.equal(await Zotero.File.getContentsAsync(path), text);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
before(function () {
|
||||||
|
Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
|
||||||
})
|
})
|
||||||
|
beforeEach(function* () {
|
||||||
|
Zotero.Prefs.set("api.url", baseURL);
|
||||||
|
Zotero.Sync.Runner.apiKey = apiKey;
|
||||||
|
|
||||||
|
httpd = new HttpServer();
|
||||||
|
httpd.start(port);
|
||||||
|
|
||||||
|
yield Zotero.Users.setCurrentUserID(1);
|
||||||
|
yield Zotero.Users.setCurrentUsername("testuser");
|
||||||
|
})
|
||||||
|
afterEach(function* () {
|
||||||
|
var defer = new Zotero.Promise.defer();
|
||||||
|
httpd.stop(() => defer.resolve());
|
||||||
|
yield defer.promise;
|
||||||
|
})
|
||||||
|
after(function () {
|
||||||
|
Zotero.HTTP.mock = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should download an attachment on-demand in as-needed mode", function* () {
|
||||||
|
yield setup();
|
||||||
|
Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true);
|
||||||
|
yield downloadOnDemand();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should download an attachment on-demand in at-sync-time mode", function* () {
|
||||||
|
yield setup();
|
||||||
|
Zotero.Sync.Storage.Local.downloadOnSync(Zotero.Libraries.userLibraryID, true);
|
||||||
|
yield downloadOnDemand();
|
||||||
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue