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
.activate(['identity']);
59 chromeMocks
.identity
.mock
$setToken('fake_token');
60 client
= new remoting
.gcd
.Client({
61 apiBaseUrl
: 'https://fake.api',
65 teardown: function() {
66 chromeMocks
.restore();
68 queuedResponse
= null;
69 remoting
.identity
= null;
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
) {
85 fakeXhr
.respond(status
, headers
, body
);
89 QUnit
.test('insertRegistrationTicket', function(assert
) {
91 200, {'Content-type': 'application/json'},
92 JSON
.stringify(FAKE_REGISTRATION_TICKET
),
94 assert
.equal(fakeXhr
.method
, 'POST');
95 assert
.equal(fakeXhr
.url
, 'https://fake.api/registrationTickets');
96 assert
.equal(fakeXhr
.requestHeaders
['Authorization'],
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
) {
109 200, {'Content-type': 'application/json'},
110 JSON
.stringify(FAKE_REGISTRATION_TICKET
),
112 assert
.equal(fakeXhr
.method
, 'PATCH');
115 'https://fake.api/registrationTickets/fake_ticket_id?key=fake_key');
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
) {
131 200, {'Content-type': 'application/json'},
132 JSON
.stringify(FAKE_REGISTRATION_TICKET
),
134 assert
.equal(fakeXhr
.method
, 'POST');
137 'https://fake.api/registrationTickets/fake_ticket_id/finalize' +
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
) {
149 200, {'Content-type': 'application/json'},
150 JSON
.stringify(FAKE_DEVICE_LIST
),
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
) {
164 200, {'Content-type': 'application/json'},
165 JSON
.stringify(FAKE_DEVICE
),
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');
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
);