OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 (function() { |
| 6 |
| 7 'use strict'; |
| 8 |
| 9 var consentDialog = null; |
| 10 var promptForConsent = null; |
| 11 var getAuthToken = null; |
| 12 var identity = null; |
| 13 |
| 14 /** |
| 15 * @constructor |
| 16 * @implements {remoting.Identity.ConsentDialog} |
| 17 */ |
| 18 var MockConsent = function() { |
| 19 /** @type {boolean} */ |
| 20 this.grantConsent = true; |
| 21 /** @type {Array<string> | undefined} */ |
| 22 this.scopes = undefined; |
| 23 }; |
| 24 |
| 25 MockConsent.prototype.show = function() { |
| 26 // The consent dialog should only be shown if a previous call to getAuthToken |
| 27 // with {interactive: false} failed, and it should occur before any call with |
| 28 // {interactive: true}. |
| 29 ok(getAuthToken.calledOnce); |
| 30 ok(getAuthToken.calledWith({'interactive': false})); |
| 31 getAuthToken.reset(); |
| 32 |
| 33 if (this.grantConsent) { |
| 34 chromeMocks.identity.mock$setToken('token'); |
| 35 } |
| 36 return Promise.resolve(); |
| 37 }; |
| 38 |
| 39 module('Identity', { |
| 40 setup: function() { |
| 41 chromeMocks.identity.mock$clearToken(); |
| 42 chromeMocks.activate(['identity', 'runtime']); |
| 43 consentDialog = new MockConsent(); |
| 44 promptForConsent = sinon.spy(consentDialog, 'show'); |
| 45 identity = new remoting.Identity(consentDialog); |
| 46 getAuthToken = sinon.spy(chromeMocks.identity, 'getAuthToken'); |
| 47 }, |
| 48 teardown: function() { |
| 49 chromeMocks.restore(); |
| 50 chromeMocks.identity.getAuthToken.restore(); |
| 51 } |
| 52 }); |
| 53 |
| 54 test('consent is requested only on first invocation', function() { |
| 55 ok(!promptForConsent.called); |
| 56 return identity.getToken().then( |
| 57 function(/** string */ token) { |
| 58 ok(promptForConsent.called); |
| 59 ok(getAuthToken.calledOnce); |
| 60 ok(getAuthToken.calledWith({'interactive': true})); |
| 61 |
| 62 // Request another token. |
| 63 promptForConsent.reset(); |
| 64 getAuthToken.reset(); |
| 65 return identity.getToken(); |
| 66 |
| 67 }).then(function(/** string */ token) { |
| 68 ok(!promptForConsent.called); |
| 69 ok(getAuthToken.calledOnce); |
| 70 ok(getAuthToken.calledWith({'interactive': true})); |
| 71 equal(token, 'token'); |
| 72 }); |
| 73 }); |
| 74 |
| 75 test('cancellations are reported correctly', function() { |
| 76 consentDialog.grantConsent = false; |
| 77 chromeMocks.runtime.lastError.message = 'The user did not approve access.'; |
| 78 return identity.getToken().then( |
| 79 function(/** string */ token) { |
| 80 fail('expected getToken() to fail'); |
| 81 }).catch(function(/** remoting.Error */ error) { |
| 82 equal(error.getTag(), remoting.Error.Tag.CANCELLED); |
| 83 }); |
| 84 }); |
| 85 |
| 86 |
| 87 test('other errors are reported correctly', function() { |
| 88 consentDialog.grantConsent = false; |
| 89 chromeMocks.runtime.lastError.message = '<some other error message>'; |
| 90 return identity.getToken().then( |
| 91 function(/** string */ token) { |
| 92 fail('expected getToken() to fail'); |
| 93 }).catch(function(/** remoting.Error */ error) { |
| 94 equal(error.getTag(), remoting.Error.Tag.NOT_AUTHENTICATED); |
| 95 }); |
| 96 }); |
| 97 |
| 98 }()); |
OLD | NEW |