diff --git a/chrome/content/zotero/preferences/preferences_sync.js b/chrome/content/zotero/preferences/preferences_sync.js index 7f83bd289..8c6ad507e 100644 --- a/chrome/content/zotero/preferences/preferences_sync.js +++ b/chrome/content/zotero/preferences/preferences_sync.js @@ -32,7 +32,7 @@ Zotero_Preferences.Sync = { this.updateStorageSettingsUI(); var username = Zotero.Users.getCurrentUsername() || Zotero.Prefs.get('sync.server.username') || " "; - var apiKey = Zotero.Sync.Data.Local.getAPIKey(); + var apiKey = yield Zotero.Sync.Data.Local.getAPIKey(); this.displayFields(apiKey ? username : ""); var pass = Zotero.Sync.Runner.getStorageController('webdav').password; @@ -286,7 +286,7 @@ Zotero_Preferences.Sync = { var loadingLabel = Zotero.getString("zotero.preferences.sync.librariesToSync.loadingLibraries"); addRow(loadingLabel, "loading", false, false); - var apiKey = Zotero.Sync.Data.Local.getAPIKey(); + var apiKey = yield Zotero.Sync.Data.Local.getAPIKey(); var client = Zotero.Sync.Runner.getAPIClient({apiKey}); var groups = []; try { diff --git a/chrome/content/zotero/xpcom/sync/syncLocal.js b/chrome/content/zotero/xpcom/sync/syncLocal.js index a684dd14c..e934d23fe 100644 --- a/chrome/content/zotero/xpcom/sync/syncLocal.js +++ b/chrome/content/zotero/xpcom/sync/syncLocal.js @@ -42,13 +42,16 @@ Zotero.Sync.Data.Local = { }), - getAPIKey: function () { + /** + * @return {Promise} + */ + getAPIKey: Zotero.Promise.method(function () { var login = this._getAPIKeyLoginInfo(); return login ? login.password // Fallback to old username/password : this._getAPIKeyFromLogin(); - }, + }), setAPIKey: function (apiKey) { diff --git a/chrome/content/zotero/xpcom/sync/syncRunner.js b/chrome/content/zotero/xpcom/sync/syncRunner.js index f8efc4ef4..44f4c2161 100644 --- a/chrome/content/zotero/xpcom/sync/syncRunner.js +++ b/chrome/content/zotero/xpcom/sync/syncRunner.js @@ -33,7 +33,13 @@ if (!Zotero.Sync) { Zotero.Sync.Runner_Module = function (options = {}) { const stopOnError = false; - Zotero.defineProperty(this, 'enabled', { get: () => _apiKey || Zotero.Sync.Data.Local.getAPIKey() }); + Zotero.defineProperty(this, 'enabled', { + get: () => { + if (_apiKey) return true; + var username = Zotero.Prefs.get('sync.server.username'); + return username && Zotero.Sync.Data.Local.getLegacyPassword(username); + } + }); Zotero.defineProperty(this, 'syncInProgress', { get: () => _syncInProgress }); Zotero.defineProperty(this, 'lastSyncStatus', { get: () => _lastSyncStatus }); @@ -1267,7 +1273,7 @@ Zotero.Sync.Runner_Module = function (options = {}) { this.deleteAPIKey = Zotero.Promise.coroutine(function* (){ - var apiKey = Zotero.Sync.Data.Local.getAPIKey(); + var apiKey = yield Zotero.Sync.Data.Local.getAPIKey(); var client = this.getAPIClient({apiKey}); Zotero.Sync.Data.Local.setAPIKey(); yield client.deleteAPIKey(); @@ -1313,7 +1319,7 @@ Zotero.Sync.Runner_Module = function (options = {}) { } - var _getAPIKey = Zotero.Promise.coroutine(function* () { + var _getAPIKey = Zotero.Promise.method(function () { // Set as .apiKey on Runner in tests or set in login manager return _apiKey || Zotero.Sync.Data.Local.getAPIKey() }) diff --git a/test/tests/preferences_syncTest.js b/test/tests/preferences_syncTest.js index 4d7f987e1..7c8dbe591 100644 --- a/test/tests/preferences_syncTest.js +++ b/test/tests/preferences_syncTest.js @@ -69,7 +69,7 @@ describe("Sync Preferences", function () { getAPIKeyFromCredentialsStub.resolves(apiResponse); yield setCredentials("Username", "correctPassword"); - assert.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); + yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); assert.equal(doc.getElementById('sync-unauthorized').getAttribute('hidden'), 'true'); }); @@ -79,7 +79,7 @@ describe("Sync Preferences", function () { yield setCredentials("Username", "incorrectPassword"); assert.isTrue(Zotero.alert.called); - assert.equal(Zotero.Sync.Data.Local.getAPIKey(), ""); + yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), ""); assert.equal(doc.getElementById('sync-authorized').getAttribute('hidden'), 'true'); }); @@ -87,12 +87,12 @@ describe("Sync Preferences", function () { it("should delete API key and display auth form when 'Unlink Account' clicked", function* () { getAPIKeyFromCredentialsStub.resolves(apiResponse); yield setCredentials("Username", "correctPassword"); - assert.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); + yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); yield win.Zotero_Preferences.Sync.unlinkAccount(false); assert.isTrue(deleteAPIKey.called); - assert.equal(Zotero.Sync.Data.Local.getAPIKey(), ""); + yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), ""); assert.equal(doc.getElementById('sync-authorized').getAttribute('hidden'), 'true'); }); @@ -103,7 +103,7 @@ describe("Sync Preferences", function () { waitForDialog(null, 'cancel'); yield win.Zotero_Preferences.Sync.unlinkAccount(); - assert.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); + yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); assert.equal(doc.getElementById('sync-unauthorized').getAttribute('hidden'), 'true'); }); diff --git a/test/tests/syncLocalTest.js b/test/tests/syncLocalTest.js index e3404961e..02202655e 100644 --- a/test/tests/syncLocalTest.js +++ b/test/tests/syncLocalTest.js @@ -6,9 +6,9 @@ describe("Zotero.Sync.Data.Local", function() { var apiKey1 = Zotero.Utilities.randomString(24); var apiKey2 = Zotero.Utilities.randomString(24); Zotero.Sync.Data.Local.setAPIKey(apiKey1); - assert.equal(Zotero.Sync.Data.Local.getAPIKey(apiKey1), apiKey1); + yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(apiKey1), apiKey1); Zotero.Sync.Data.Local.setAPIKey(apiKey2); - assert.equal(Zotero.Sync.Data.Local.getAPIKey(apiKey2), apiKey2); + yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(apiKey2), apiKey2); }) @@ -16,7 +16,7 @@ describe("Zotero.Sync.Data.Local", function() { var apiKey = Zotero.Utilities.randomString(24); Zotero.Sync.Data.Local.setAPIKey(apiKey); Zotero.Sync.Data.Local.setAPIKey(""); - assert.strictEqual(Zotero.Sync.Data.Local.getAPIKey(apiKey), ""); + yield assert.eventually.strictEqual(Zotero.Sync.Data.Local.getAPIKey(apiKey), ""); }) })