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