Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Unified Diff: remoting/webapp/crd/js/identity_unittest.js

Issue 1013633003: remoting.Identity unit-tests and fixes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/webapp/crd/js/identity_unittest.js
diff --git a/remoting/webapp/crd/js/identity_unittest.js b/remoting/webapp/crd/js/identity_unittest.js
new file mode 100644
index 0000000000000000000000000000000000000000..6e927470e42ba701a7709cca83c6c27de797a3d1
--- /dev/null
+++ b/remoting/webapp/crd/js/identity_unittest.js
@@ -0,0 +1,98 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+(function() {
+
+'use strict';
+
+var consentDialog = null;
+var promptForConsent = null;
+var getAuthToken = null;
+var identity = null;
+
+/**
+ * @constructor
+ * @implements {remoting.Identity.ConsentDialog}
+ */
+var MockConsent = function() {
+ /** @type {boolean} */
+ this.grantConsent = true;
+ /** @type {Array<string> | undefined} */
+ this.scopes = undefined;
+};
+
+MockConsent.prototype.show = function() {
+ // The consent dialog should only be shown if a previous call to getAuthToken
+ // with {interactive: false} failed, and it should occur before any call with
+ // {interactive: true}.
+ ok(getAuthToken.calledOnce);
+ ok(getAuthToken.calledWith({'interactive': false}));
+ getAuthToken.reset();
+
+ if (this.grantConsent) {
+ chromeMocks.identity.mock$setToken('token');
+ }
+ return Promise.resolve();
+};
+
+module('Identity', {
+ setup: function() {
+ chromeMocks.identity.mock$clearToken();
+ chromeMocks.activate(['identity', 'runtime']);
+ consentDialog = new MockConsent();
+ promptForConsent = sinon.spy(consentDialog, 'show');
+ identity = new remoting.Identity(consentDialog);
+ getAuthToken = sinon.spy(chromeMocks.identity, 'getAuthToken');
+ },
+ teardown: function() {
+ chromeMocks.restore();
+ chromeMocks.identity.getAuthToken.restore();
+ }
+});
+
+test('consent is requested only on first invocation', function() {
+ ok(!promptForConsent.called);
+ return identity.getToken().then(
+ function(/** string */ token) {
+ ok(promptForConsent.called);
+ ok(getAuthToken.calledOnce);
+ ok(getAuthToken.calledWith({'interactive': true}));
+
+ // Request another token.
+ promptForConsent.reset();
+ getAuthToken.reset();
+ return identity.getToken();
+
+ }).then(function(/** string */ token) {
+ ok(!promptForConsent.called);
+ ok(getAuthToken.calledOnce);
+ ok(getAuthToken.calledWith({'interactive': true}));
+ equal(token, 'token');
+ });
+});
+
+test('cancellations are reported correctly', function() {
+ consentDialog.grantConsent = false;
+ chromeMocks.runtime.lastError.message = 'The user did not approve access.';
+ return identity.getToken().then(
+ function(/** string */ token) {
+ fail('expected getToken() to fail');
+ }).catch(function(/** remoting.Error */ error) {
+ equal(error.getTag(), remoting.Error.Tag.CANCELLED);
+ });
+});
+
+
+test('other errors are reported correctly', function() {
+ consentDialog.grantConsent = false;
+ chromeMocks.runtime.lastError.message = '<some other error message>';
+ return identity.getToken().then(
+ function(/** string */ token) {
+ fail('expected getToken() to fail');
+ }).catch(function(/** remoting.Error */ error) {
+ equal(error.getTag(), remoting.Error.Tag.NOT_AUTHENTICATED);
+ });
+});
+
+}());

Powered by Google App Engine
This is Rietveld 408576698