| <!DOCTYPE html> |
| <!-- |
| Copyright (C) 2016 The Android Open Source Project |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes"> |
| <title>gr-error-manager</title> |
| |
| <script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script> |
| <script src="../../../bower_components/web-component-tester/browser.js"></script> |
| |
| <link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html"> |
| <link rel="import" href="gr-error-manager.html"> |
| |
| <script>void(0);</script> |
| |
| <test-fixture id="basic"> |
| <template> |
| <gr-error-manager></gr-error-manager> |
| </template> |
| </test-fixture> |
| |
| <script> |
| suite('gr-error-manager tests', function() { |
| var element; |
| var sandbox; |
| |
| setup(function() { |
| sandbox = sinon.sandbox.create(); |
| stub('gr-rest-api-interface', { |
| getLoggedIn: function() { return Promise.resolve(true); }, |
| }); |
| element = fixture('basic'); |
| }); |
| |
| teardown(function() { |
| sandbox.restore(); |
| }); |
| |
| test('show auth error', function(done) { |
| var showAuthErrorStub = sandbox.stub(element, '_showAuthErrorAlert'); |
| element.fire('server-error', {response: {status: 403}}); |
| element.$.restAPI.getLoggedIn.lastCall.returnValue.then(function() { |
| assert.isTrue(showAuthErrorStub.calledOnce); |
| done(); |
| }); |
| }); |
| |
| test('show normal server error', function(done) { |
| var showAlertStub = sandbox.stub(element, '_showAlert'); |
| var textSpy = sandbox.spy(function() { return Promise.resolve('ZOMG'); }); |
| element.fire('server-error', {response: {status: 500, text: textSpy}}); |
| |
| assert.isTrue(textSpy.called); |
| textSpy.lastCall.returnValue.then(function() { |
| assert.isTrue(showAlertStub.calledOnce); |
| assert.isTrue(showAlertStub.lastCall.calledWithExactly( |
| 'Server error: ZOMG')); |
| done(); |
| }); |
| }); |
| |
| test('suppress TOO_MANY_FILES error', function(done) { |
| var showAlertStub = sandbox.stub(element, '_showAlert'); |
| var textSpy = sandbox.spy(function() { |
| return Promise.resolve('too many files to find conflicts'); |
| }); |
| element.fire('server-error', {response: {status: 500, text: textSpy}}); |
| |
| assert.isTrue(textSpy.called); |
| textSpy.lastCall.returnValue.then(function() { |
| assert.isFalse(showAlertStub.called); |
| done(); |
| }); |
| }); |
| |
| test('show network error', function(done) { |
| var consoleErrorStub = sandbox.stub(console, 'error'); |
| var showAlertStub = sandbox.stub(element, '_showAlert'); |
| element.fire('network-error', {error: new Error('ZOMG')}); |
| flush(function() { |
| assert.isTrue(showAlertStub.calledOnce); |
| assert.isTrue(showAlertStub.lastCall.calledWithExactly( |
| 'Server unavailable')); |
| assert.isTrue(consoleErrorStub.calledOnce); |
| assert.isTrue(consoleErrorStub.lastCall.calledWithExactly('ZOMG')); |
| done(); |
| }); |
| }); |
| |
| test('show auth refresh toast', function(done) { |
| var refreshStub = sandbox.stub(element.$.restAPI, 'checkCredentials', |
| function() { return Promise.resolve(true); }); |
| var toastSpy = sandbox.spy(element, '_createToastAlert'); |
| var windowOpen = sandbox.stub(window, 'open'); |
| element.fire('server-error', {response: {status: 403}}); |
| element.$.restAPI.getLoggedIn.lastCall.returnValue.then(function() { |
| assert.isTrue(toastSpy.called); |
| var toast = toastSpy.lastCall.returnValue; |
| assert.isOk(toast); |
| assert.include( |
| Polymer.dom(toast.root).textContent, 'Auth error'); |
| assert.include( |
| Polymer.dom(toast.root).textContent, 'Refresh credentials.'); |
| |
| assert.isFalse(windowOpen.called); |
| toast.fire('action'); |
| assert.isTrue(windowOpen.called); |
| |
| // @see Issue 5822: noopener breaks closeAfterLogin |
| assert.equal(windowOpen.lastCall.args[2].indexOf('noopener=yes'), |
| -1); |
| |
| var hideToastSpy = sandbox.spy(toast, 'hide'); |
| |
| assert.isTrue(refreshStub.called); |
| element.flushDebouncer('checkLoggedIn'); |
| flush(function() { |
| assert.isTrue(refreshStub.called); |
| assert.isTrue(hideToastSpy.called); |
| |
| assert.notStrictEqual(toastSpy.lastCall.returnValue, toast); |
| toast = toastSpy.lastCall.returnValue; |
| assert.isOk(toast); |
| assert.include( |
| Polymer.dom(toast.root).textContent, 'Credentials refreshed'); |
| done(); |
| }); |
| }); |
| }); |
| |
| test('show alert', function() { |
| sandbox.stub(element, '_showAlert'); |
| element.fire('show-alert', {message: 'foo'}); |
| assert.isTrue(element._showAlert.calledOnce); |
| assert.isTrue(element._showAlert.lastCall.calledWithExactly('foo')); |
| }); |
| |
| test('checks stale credentials on visibility change', function() { |
| var refreshStub = sandbox.stub(element.$.restAPI, |
| 'checkCredentials'); |
| sandbox.stub(Date, 'now').returns(999999); |
| element._lastCredentialCheck = 0; |
| element._handleVisibilityChange(); |
| |
| // Since there is no known account, it should not test credentials. |
| assert.isFalse(refreshStub.called); |
| assert.equal(element._lastCredentialCheck, 0); |
| |
| element.knownAccountId = 123; |
| element._handleVisibilityChange(); |
| |
| // Should test credentials, since there is a known account. |
| assert.isTrue(refreshStub.called); |
| assert.equal(element._lastCredentialCheck, 999999); |
| }); |
| |
| test('refresh loop continues on credential fail', function(done) { |
| var accountPromise = Promise.resolve(null); |
| sandbox.stub(element.$.restAPI, 'checkCredentials') |
| .returns(accountPromise); |
| var requestCheckStub = sandbox.stub(element, '_requestCheckLoggedIn'); |
| var handleRefreshStub = sandbox.stub(element, |
| '_handleCredentialRefreshed'); |
| var reloadStub = sandbox.stub(element, '_reloadPage'); |
| |
| element._refreshingCredentials = true; |
| element._checkSignedIn(); |
| |
| accountPromise.then(function() { |
| assert.isTrue(requestCheckStub.called); |
| assert.isFalse(handleRefreshStub.called); |
| assert.isFalse(reloadStub.called); |
| done(); |
| }); |
| }); |
| |
| test('refreshes with same credentials', function(done) { |
| var accountPromise = Promise.resolve({_account_id: 1234}); |
| sandbox.stub(element.$.restAPI, 'checkCredentials') |
| .returns(accountPromise); |
| var requestCheckStub = sandbox.stub(element, '_requestCheckLoggedIn'); |
| var handleRefreshStub = sandbox.stub(element, |
| '_handleCredentialRefreshed'); |
| var reloadStub = sandbox.stub(element, '_reloadPage'); |
| |
| element.knownAccountId = 1234; |
| element._refreshingCredentials = true; |
| element._checkSignedIn(); |
| |
| accountPromise.then(function() { |
| assert.isFalse(requestCheckStub.called); |
| assert.isTrue(handleRefreshStub.called); |
| assert.isFalse(reloadStub.called); |
| done(); |
| }); |
| }); |
| |
| test('reloads when refreshed credentials differ', function(done) { |
| var accountPromise = Promise.resolve({_account_id: 1234}); |
| sandbox.stub(element.$.restAPI, 'checkCredentials') |
| .returns(accountPromise); |
| var requestCheckStub = sandbox.stub(element, '_requestCheckLoggedIn'); |
| var handleRefreshStub = sandbox.stub(element, |
| '_handleCredentialRefreshed'); |
| var reloadStub = sandbox.stub(element, '_reloadPage'); |
| |
| element.knownAccountId = 4321; // Different from 1234 |
| element._refreshingCredentials = true; |
| element._checkSignedIn(); |
| |
| accountPromise.then(function() { |
| assert.isFalse(requestCheckStub.called); |
| assert.isFalse(handleRefreshStub.called); |
| assert.isTrue(reloadStub.called); |
| done(); |
| }); |
| }); |
| }); |
| </script> |