summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/base/js/identity_unittest.js
blob: 55ce092eafe601708162a8a0e5931bd6d36ff4b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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';

/** @type {MockConsent} */
var consentDialog = null;
/** @type {sinon.Spy | Function} */
var promptForConsent = null;
/** @type {sinon.Spy | Function} */
var getAuthToken = null;
/** @type {remoting.Identity} */
var identity = null;

/**
 * @param {QUnit.Assert} assert
 * @constructor
 * @implements {remoting.Identity.ConsentDialog}
 */
var MockConsent = function(assert) {
  /** @type {boolean} */
  this.grantConsent = true;
  /** @private {QUnit.Assert} */
  this.assert_ = assert;
};

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}.
  this.assert_.ok(getAuthToken.calledOnce);
  this.assert_.ok(getAuthToken.calledWith(
      {'interactive': false, scopes: undefined}));
  getAuthToken.reset();

  if (this.grantConsent) {
    chromeMocks.identity.mock$setToken('token');
  }
  return Promise.resolve();
};

QUnit.module('Identity', {
  beforeEach: function(/** QUnit.Assert*/ assert) {
    chromeMocks.identity.mock$clearToken();
    consentDialog = new MockConsent(assert);
    promptForConsent = sinon.spy(consentDialog, 'show');
    identity = new remoting.Identity(consentDialog);
    getAuthToken = sinon.spy(chromeMocks.identity, 'getAuthToken');
  },
  afterEach: function() {
    getAuthToken.restore();
  }
});

QUnit.test('consent is requested only on first invocation', function(assert) {
  assert.ok(!promptForConsent.called);
  return identity.getToken().then(
      function(/** string */ token) {
        assert.ok(promptForConsent.called);
        assert.ok(getAuthToken.calledOnce);
        assert.ok(getAuthToken.calledWith(
            {'interactive': true, 'scopes': undefined}));

        // Request another token.
        promptForConsent.reset();
        getAuthToken.reset();
        return identity.getToken();

      }).then(function(/** string */ token) {
        assert.ok(!promptForConsent.called);
        assert.ok(getAuthToken.calledOnce);
        assert.ok(getAuthToken.calledWith({
          'interactive': true, 'scopes': undefined}));
        assert.equal(token, 'token');
      });
});

QUnit.test('requesting an explicit scope works', function(assert) {
  assert.ok(!promptForConsent.called);
  return identity.getToken().then(
      function() {
        // Request a token with an explicit scope.
        promptForConsent.reset();
        getAuthToken.reset();
        return identity.getToken(['scope']);

      }).then(function(/** string */ token) {
        assert.ok(!promptForConsent.called);
        assert.ok(getAuthToken.calledOnce);
        assert.ok(getAuthToken.calledWith({
          'interactive': true, 'scopes': ['scope']}));
        assert.equal(token, 'token["scope"]');
      });
});

QUnit.test('multiple concurrent outstanding requests are handled correctly',
           function(assert) {
  assert.ok(!promptForConsent.called);
  return identity.getToken().then(
      function() {
        // Request a token with an explicit scope and another without.
        promptForConsent.reset();
        getAuthToken.reset();
        var withScope = identity.getToken(['scope']);
        var withoutScope = identity.getToken();
        return Promise.all([withScope, withoutScope]);

      }).then(function(/** Array<string> */ tokens) {
        assert.ok(!promptForConsent.called);
        assert.ok(getAuthToken.calledTwice);
        assert.ok(getAuthToken.calledWith({
          'interactive': true, 'scopes': ['scope']}));
        assert.ok(getAuthToken.calledWith({
          'interactive': true, 'scopes': undefined}));
        assert.equal(tokens.length, 2);
        assert.equal(tokens[0], 'token["scope"]');
        assert.equal(tokens[1], 'token');
      });
});

QUnit.test('cancellations are reported correctly', function(assert) {
  consentDialog.grantConsent = false;
  chromeMocks.runtime.lastError.message = 'The user did not approve access.';
  return identity.getToken().then(
      function(/** string */ token) {
        assert.ok(false, 'expected getToken() to fail');
      }).catch(function(/** remoting.Error */ error) {
        assert.equal(error.getTag(), remoting.Error.Tag.CANCELLED);
      });
});


QUnit.test('other errors are reported correctly', function(assert) {
  consentDialog.grantConsent = false;
  chromeMocks.runtime.lastError.message = '<some other error message>';
  return identity.getToken().then(
      function(/** string */ token) {
        assert.ok(false, 'expected getToken() to fail');
      }).catch(function(/** remoting.Error */ error) {
        assert.equal(error.getTag(), remoting.Error.Tag.NOT_AUTHENTICATED);
      });
});

}());