Fix attachment-download test

This commit is contained in:
Dan Stillman 2017-08-16 01:42:21 +02:00
parent 065bac3b8a
commit 13cd84e1be

View file

@ -147,6 +147,7 @@ describe("ZoteroPane", function() {
var baseURL = `http://localhost:${port}/`;
var server;
var responses = {};
var httpd;
var setup = Zotero.Promise.coroutine(function* (options = {}) {
server = sinon.fakeServer.create();
@ -157,44 +158,20 @@ describe("ZoteroPane", function() {
setHTTPResponse(server, baseURL, response, responses);
}
before(function () {
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);
async function downloadOnDemand() {
var item = new Zotero.Item("attachment");
item.attachmentLinkMode = 'imported_file';
item.attachmentPath = 'storage:test.txt';
// TODO: Test binary data
var text = Zotero.Utilities.randomString();
item.attachmentSyncState = "to_download";
yield item.saveTx();
await item.saveTx();
var mtime = "1441252524000";
var md5 = Zotero.Utilities.Internal.md5(text)
var s3Path = `pretend-s3/${item.key}`;
this.httpd.registerPathHandler(
httpd.registerPathHandler(
`/users/1/items/${item.key}/file`,
{
handle: function (request, response) {
@ -206,7 +183,7 @@ describe("ZoteroPane", function() {
}
}
);
this.httpd.registerPathHandler(
httpd.registerPathHandler(
"/" + s3Path,
{
handle: function (request, response) {
@ -219,18 +196,51 @@ describe("ZoteroPane", function() {
// Disable loadURI() so viewAttachment() doesn't trigger translator loading
var stub = sinon.stub(zp, "loadURI");
yield zp.viewAttachment(item.id);
await zp.viewAttachment(item.id);
assert.ok(stub.calledOnce);
assert.ok(stub.calledWith(OS.Path.toFileURI(item.getFilePath())));
stub.restore();
assert.equal((yield item.attachmentHash), md5);
assert.equal((yield item.attachmentModificationTime), mtime);
var path = yield item.getFilePathAsync();
assert.equal((yield Zotero.File.getContentsAsync(path)), text);
}
assert.equal(await item.attachmentHash, md5);
assert.equal(await item.attachmentModificationTime, mtime);
var path = await item.getFilePathAsync();
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();
});
})