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