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.
9 /** @type {sinon.FakeXhr} */
12 /** @type {remoting.gcd.Client} */
16 var FAKE_REGISTRATION_TICKET
= {
17 kind
: 'clouddevices#registrationTicket',
19 robotAccountEmail
: 'fake@robotaccounts.com',
20 robotAccountAuthorizationCode
: 'fake_robot_auth_code',
28 kind
: 'clouddevices#device',
33 var FAKE_DEVICE_PATCH
= {
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', {
48 sinon
.useFakeXMLHttpRequest().onCreate
=
49 function(/** sinon.FakeXhr */ xhr
) {
51 xhr
.addEventListener('loadstart', function() {
57 remoting
.identity
= new remoting
.Identity();
58 chromeMocks
.identity
.mock
$setToken('fake_token');
59 client
= new remoting
.gcd
.Client({
60 apiBaseUrl
: 'https://fake.api',
64 teardown: function() {
66 queuedResponse
= null;
67 remoting
.identity
= null;
72 * @param {number} status
73 * @param {!Object<string>} headers
74 * @param {string} body
75 * @param {function():void=} opt_preconditions
77 function queueResponse(status
, headers
, body
, opt_preconditions
) {
78 console
.assert(queuedResponse
== null, '|queuedResponse| is null.');
79 queuedResponse = function() {
80 if (opt_preconditions
) {
83 fakeXhr
.respond(status
, headers
, body
);
87 QUnit
.test('insertRegistrationTicket', function(assert
) {
89 200, {'Content-type': 'application/json'},
90 JSON
.stringify(FAKE_REGISTRATION_TICKET
),
92 assert
.equal(fakeXhr
.method
, 'POST');
93 assert
.equal(fakeXhr
.url
, 'https://fake.api/registrationTickets');
94 assert
.equal(fakeXhr
.requestHeaders
['Authorization'],
97 JSON
.parse(fakeXhr
.requestBody
|| ''),
100 return client
.insertRegistrationTicket().then(function(ticket
) {
101 assert
.deepEqual(ticket
, FAKE_REGISTRATION_TICKET
);
105 QUnit
.test('patchRegistrationTicket', function(assert
) {
107 200, {'Content-type': 'application/json'},
108 JSON
.stringify(FAKE_REGISTRATION_TICKET
),
110 assert
.equal(fakeXhr
.method
, 'PATCH');
113 'https://fake.api/registrationTickets/fake_ticket_id?key=fake_key');
115 JSON
.parse(fakeXhr
.requestBody
|| ''), {
116 deviceDraft
: { 'fake_device_draft': true },
117 oauthClientId
: 'fake_client_id'
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
);
127 QUnit
.test('finalizeRegistrationTicket', function(assert
) {
129 200, {'Content-type': 'application/json'},
130 JSON
.stringify(FAKE_REGISTRATION_TICKET
),
132 assert
.equal(fakeXhr
.method
, 'POST');
135 'https://fake.api/registrationTickets/fake_ticket_id/finalize' +
137 assert
.equal(fakeXhr
.requestBody
, null);
139 return client
.finalizeRegistrationTicket('fake_ticket_id').
140 then(function(ticket
) {
141 assert
.deepEqual(ticket
, FAKE_REGISTRATION_TICKET
);
145 QUnit
.test('listDevices', function(assert
) {
147 200, {'Content-type': 'application/json'},
148 JSON
.stringify(FAKE_DEVICE_LIST
),
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');
155 return client
.listDevices().then(function(devices
) {
156 assert
.deepEqual(devices
, [FAKE_DEVICE
]);
160 QUnit
.test('patchDevice', function(assert
) {
162 200, {'Content-type': 'application/json'},
163 JSON
.stringify(FAKE_DEVICE
),
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');
170 JSON
.parse(fakeXhr
.requestBody
|| ''), FAKE_DEVICE_PATCH
);
172 return client
.patchDevice('fake_device_id', FAKE_DEVICE_PATCH
).
173 then(function(device
) {
174 assert
.deepEqual(device
, FAKE_DEVICE
);