Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / remoting / webapp / crd / js / gcd_client_unittest.js
blob38d9c0dd9c0c5f29a56cc2a6184e08a1016c3b1b
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.
5 (function() {
7 'use strict';
9 /** @type {sinon.FakeXhr} */
10 var fakeXhr;
12 /** @type {remoting.gcd.Client} */
13 var client;
15 /** @const */
16 var FAKE_REGISTRATION_TICKET = {
17 kind: 'clouddevices#registrationTicket',
18 id: 'fake_ticket_id',
19 robotAccountEmail: 'fake@robotaccounts.com',
20 robotAccountAuthorizationCode: 'fake_robot_auth_code',
21 deviceDraft: {
22 id: 'fake_device_id'
26 /** @const */
27 var FAKE_DEVICE = {
28 kind: 'clouddevices#device',
29 id: 'fake_device_id'
32 /** @const */
33 var FAKE_DEVICE_PATCH = {
34 fake_patch: true
37 /** @const */
38 var FAKE_DEVICE_LIST = {
39 kind: 'clouddevices#devicesListResponse',
40 devices: [FAKE_DEVICE]
43 /** @type {?function():void} */
44 var queuedResponse = null;
46 QUnit.module('gcd_client', {
47 setup: function() {
48 sinon.useFakeXMLHttpRequest().onCreate =
49 function(/** sinon.FakeXhr */ xhr) {
50 fakeXhr = xhr;
51 xhr.addEventListener('loadstart', function() {
52 if (queuedResponse) {
53 queuedResponse();
55 });
57 remoting.identity = new remoting.Identity();
58 chromeMocks.activate(['identity']);
59 chromeMocks.identity.mock$setToken('fake_token');
60 client = new remoting.gcd.Client({
61 apiBaseUrl: 'https://fake.api',
62 apiKey: 'fake_key'
63 });
65 teardown: function() {
66 chromeMocks.restore();
67 fakeXhr = null;
68 queuedResponse = null;
69 remoting.identity = null;
71 });
73 /**
74 * @param {number} status
75 * @param {!Object<string,string>} headers
76 * @param {string} body
77 * @param {function():void=} opt_preconditions
79 function queueResponse(status, headers, body, opt_preconditions) {
80 base.debug.assert(queuedResponse == null);
81 queuedResponse = function() {
82 if (opt_preconditions) {
83 opt_preconditions();
85 fakeXhr.respond(status, headers, body);
89 QUnit.test('insertRegistrationTicket', function(assert) {
90 queueResponse(
91 200, {'Content-type': 'application/json'},
92 JSON.stringify(FAKE_REGISTRATION_TICKET),
93 function() {
94 assert.equal(fakeXhr.method, 'POST');
95 assert.equal(fakeXhr.url, 'https://fake.api/registrationTickets');
96 assert.equal(fakeXhr.requestHeaders['Authorization'],
97 'Bearer fake_token');
98 assert.deepEqual(
99 JSON.parse(fakeXhr.requestBody || ''),
100 { userEmail: 'me' });
102 return client.insertRegistrationTicket().then(function(ticket) {
103 assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
107 QUnit.test('patchRegistrationTicket', function(assert) {
108 queueResponse(
109 200, {'Content-type': 'application/json'},
110 JSON.stringify(FAKE_REGISTRATION_TICKET),
111 function() {
112 assert.equal(fakeXhr.method, 'PATCH');
113 assert.equal(
114 fakeXhr.url,
115 'https://fake.api/registrationTickets/fake_ticket_id?key=fake_key');
116 assert.deepEqual(
117 JSON.parse(fakeXhr.requestBody || ''), {
118 deviceDraft: { 'fake_device_draft': true },
119 oauthClientId: 'fake_client_id'
122 return client.patchRegistrationTicket('fake_ticket_id', {
123 'fake_device_draft': true
124 }, 'fake_client_id').then(function(ticket) {
125 assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
129 QUnit.test('finalizeRegistrationTicket', function(assert) {
130 queueResponse(
131 200, {'Content-type': 'application/json'},
132 JSON.stringify(FAKE_REGISTRATION_TICKET),
133 function() {
134 assert.equal(fakeXhr.method, 'POST');
135 assert.equal(
136 fakeXhr.url,
137 'https://fake.api/registrationTickets/fake_ticket_id/finalize' +
138 '?key=fake_key');
139 assert.equal(fakeXhr.requestBody, null);
141 return client.finalizeRegistrationTicket('fake_ticket_id').
142 then(function(ticket) {
143 assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
147 QUnit.test('listDevices', function(assert) {
148 queueResponse(
149 200, {'Content-type': 'application/json'},
150 JSON.stringify(FAKE_DEVICE_LIST),
151 function() {
152 assert.equal(fakeXhr.method, 'GET');
153 assert.equal(fakeXhr.url, 'https://fake.api/devices');
154 assert.equal(fakeXhr.requestHeaders['Authorization'],
155 'Bearer fake_token');
157 return client.listDevices().then(function(devices) {
158 assert.deepEqual(devices, [FAKE_DEVICE]);
162 QUnit.test('patchDevice', function(assert) {
163 queueResponse(
164 200, {'Content-type': 'application/json'},
165 JSON.stringify(FAKE_DEVICE),
166 function() {
167 assert.equal(fakeXhr.method, 'PATCH');
168 assert.equal(fakeXhr.url, 'https://fake.api/devices/fake_device_id');
169 assert.equal(fakeXhr.requestHeaders['Authorization'],
170 'Bearer fake_token');
171 assert.deepEqual(
172 JSON.parse(fakeXhr.requestBody || ''), FAKE_DEVICE_PATCH);
174 return client.patchDevice('fake_device_id', FAKE_DEVICE_PATCH).
175 then(function(device) {
176 assert.deepEqual(device, FAKE_DEVICE);
180 })();