preferences_syncTest.js (3727B)
1 describe("Sync Preferences", function () { 2 var win, doc; 3 before(function* () { 4 // Load prefs with sync pane 5 win = yield loadWindow("chrome://zotero/content/preferences/preferences.xul", { 6 pane: 'zotero-prefpane-sync', 7 tabIndex: 0 8 }); 9 doc = win.document; 10 let defer = Zotero.Promise.defer(); 11 let pane = doc.getElementById('zotero-prefpane-sync'); 12 if (!pane.loaded) { 13 pane.addEventListener('paneload', function () { 14 defer.resolve(); 15 }); 16 yield defer.promise; 17 } 18 }); 19 20 after(function() { 21 win.close(); 22 }); 23 24 describe("Settings", function () { 25 describe("Data Syncing", function () { 26 var getAPIKeyFromCredentialsStub, deleteAPIKey, indicatorElem; 27 28 var setCredentials = Zotero.Promise.coroutine(function* (username, password) { 29 let usernameElem = doc.getElementById('sync-username-textbox'); 30 let passwordElem = doc.getElementById('sync-password'); 31 usernameElem.value = username; 32 passwordElem.value = password; 33 34 // Triggered by `change` event for usernameElem and passwordElem; 35 yield win.Zotero_Preferences.Sync.linkAccount(); 36 }); 37 38 var apiKey = Zotero.Utilities.randomString(24); 39 40 var apiResponse = { 41 key: apiKey, 42 username: "Username", 43 userID: 1, 44 access: {} 45 }; 46 47 before(function* () { 48 getAPIKeyFromCredentialsStub = sinon.stub( 49 Zotero.Sync.APIClient.prototype, 'createAPIKeyFromCredentials'); 50 deleteAPIKey = sinon.stub(Zotero.Sync.APIClient.prototype, 'deleteAPIKey').resolves(); 51 indicatorElem = doc.getElementById('sync-status-indicator') 52 sinon.stub(Zotero, 'alert'); 53 }); 54 55 beforeEach(function* (){ 56 yield win.Zotero_Preferences.Sync.unlinkAccount(false); 57 deleteAPIKey.resetHistory(); 58 Zotero.alert.reset(); 59 }); 60 61 after(function() { 62 Zotero.HTTP.mock = null; 63 Zotero.alert.restore(); 64 getAPIKeyFromCredentialsStub.restore(); 65 deleteAPIKey.restore(); 66 }); 67 68 it("should set API key and display full controls with correct credentials", function* () { 69 getAPIKeyFromCredentialsStub.resolves(apiResponse); 70 yield setCredentials("Username", "correctPassword"); 71 72 yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); 73 assert.equal(doc.getElementById('sync-unauthorized').getAttribute('hidden'), 'true'); 74 }); 75 76 77 it("should display dialog when credentials incorrect", function* () { 78 getAPIKeyFromCredentialsStub.resolves(false); 79 yield setCredentials("Username", "incorrectPassword"); 80 81 assert.isTrue(Zotero.alert.called); 82 yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), ""); 83 assert.equal(doc.getElementById('sync-authorized').getAttribute('hidden'), 'true'); 84 }); 85 86 87 it("should delete API key and display auth form when 'Unlink Account' clicked", function* () { 88 getAPIKeyFromCredentialsStub.resolves(apiResponse); 89 yield setCredentials("Username", "correctPassword"); 90 yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); 91 92 yield win.Zotero_Preferences.Sync.unlinkAccount(false); 93 94 assert.isTrue(deleteAPIKey.called); 95 yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), ""); 96 assert.equal(doc.getElementById('sync-authorized').getAttribute('hidden'), 'true'); 97 }); 98 99 it("should not unlink on pressing cancel", function* () { 100 getAPIKeyFromCredentialsStub.resolves(apiResponse); 101 yield setCredentials("Username", "correctPassword"); 102 103 waitForDialog(null, 'cancel'); 104 105 yield win.Zotero_Preferences.Sync.unlinkAccount(); 106 yield assert.eventually.equal(Zotero.Sync.Data.Local.getAPIKey(), apiKey); 107 assert.equal(doc.getElementById('sync-unauthorized').getAttribute('hidden'), 'true'); 108 }); 109 110 }) 111 }) 112 }) 113