Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / crd / js / gcd_client_unittest.js
blob0276d83f1b97289b9b3acb8b8dc921df1208904c
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'
23   }
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();
54             }
55           });
56         };
57     remoting.identity = new remoting.Identity();
58     chromeMocks.identity.mock$setToken('fake_token');
59     client = new remoting.gcd.Client({
60       apiBaseUrl: 'https://fake.api',
61       apiKey: 'fake_key'
62     });
63   },
64   teardown: function() {
65     fakeXhr = null;
66     queuedResponse = null;
67     remoting.identity = null;
68   }
69 });
71 /**
72  * @param {number} status
73  * @param {!Object<string>} headers
74  * @param {string} body
75  * @param {function():void=} opt_preconditions
76  */
77 function queueResponse(status, headers, body, opt_preconditions) {
78   console.assert(queuedResponse == null, '|queuedResponse| is null.');
79   queuedResponse = function() {
80     if (opt_preconditions) {
81       opt_preconditions();
82     }
83     fakeXhr.respond(status, headers, body);
84   };
87 QUnit.test('insertRegistrationTicket', function(assert) {
88   queueResponse(
89       200, {'Content-type': 'application/json'},
90       JSON.stringify(FAKE_REGISTRATION_TICKET),
91       function() {
92         assert.equal(fakeXhr.method, 'POST');
93         assert.equal(fakeXhr.url, 'https://fake.api/registrationTickets');
94         assert.equal(fakeXhr.requestHeaders['Authorization'],
95                      'Bearer fake_token');
96         assert.deepEqual(
97             JSON.parse(fakeXhr.requestBody || ''),
98             { userEmail: 'me' });
99       });
100   return client.insertRegistrationTicket().then(function(ticket) {
101     assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
102   });
105 QUnit.test('patchRegistrationTicket', function(assert) {
106   queueResponse(
107       200, {'Content-type': 'application/json'},
108       JSON.stringify(FAKE_REGISTRATION_TICKET),
109       function() {
110         assert.equal(fakeXhr.method, 'PATCH');
111         assert.equal(
112             fakeXhr.url,
113             'https://fake.api/registrationTickets/fake_ticket_id?key=fake_key');
114         assert.deepEqual(
115             JSON.parse(fakeXhr.requestBody || ''), {
116               deviceDraft: { 'fake_device_draft': true },
117               oauthClientId: 'fake_client_id'
118             });
119       });
120   return client.patchRegistrationTicket('fake_ticket_id', {
121     'fake_device_draft': true
122   }, 'fake_client_id').then(function(ticket) {
123     assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
124   });
127 QUnit.test('finalizeRegistrationTicket', function(assert) {
128   queueResponse(
129       200, {'Content-type': 'application/json'},
130       JSON.stringify(FAKE_REGISTRATION_TICKET),
131       function() {
132         assert.equal(fakeXhr.method, 'POST');
133         assert.equal(
134             fakeXhr.url,
135             'https://fake.api/registrationTickets/fake_ticket_id/finalize' +
136               '?key=fake_key');
137         assert.equal(fakeXhr.requestBody, null);
138       });
139   return client.finalizeRegistrationTicket('fake_ticket_id').
140       then(function(ticket) {
141         assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
142       });
145 QUnit.test('listDevices', function(assert) {
146   queueResponse(
147       200, {'Content-type': 'application/json'},
148       JSON.stringify(FAKE_DEVICE_LIST),
149       function() {
150         assert.equal(fakeXhr.method, 'GET');
151         assert.equal(fakeXhr.url, 'https://fake.api/devices');
152         assert.equal(fakeXhr.requestHeaders['Authorization'],
153                      'Bearer fake_token');
154       });
155   return client.listDevices().then(function(devices) {
156     assert.deepEqual(devices, [FAKE_DEVICE]);
157   });
160 QUnit.test('patchDevice', function(assert) {
161   queueResponse(
162       200, {'Content-type': 'application/json'},
163       JSON.stringify(FAKE_DEVICE),
164       function() {
165         assert.equal(fakeXhr.method, 'PATCH');
166         assert.equal(fakeXhr.url, 'https://fake.api/devices/fake_device_id');
167         assert.equal(fakeXhr.requestHeaders['Authorization'],
168                      'Bearer fake_token');
169         assert.deepEqual(
170             JSON.parse(fakeXhr.requestBody || ''), FAKE_DEVICE_PATCH);
171       });
172   return client.patchDevice('fake_device_id', FAKE_DEVICE_PATCH).
173       then(function(device) {
174         assert.deepEqual(device, FAKE_DEVICE);
175       });
178 })();