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