Follow-ups to getAPIKey() changes in 008321bb89

Addresses #1086
This commit is contained in:
Dan Stillman 2016-09-05 03:59:42 -04:00
parent 008321bb89
commit f68ee60524
5 changed files with 24 additions and 15 deletions

View File

@ -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 {

View File

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

View File

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

View File

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

View File

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