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 {remoting.gcd.Client} */
13 var FAKE_REGISTRATION_TICKET
= {
14 kind
: 'clouddevices#registrationTicket',
16 robotAccountEmail
: 'fake@robotaccounts.com',
17 robotAccountAuthorizationCode
: 'fake_robot_auth_code',
25 kind
: 'clouddevices#device',
30 var FAKE_DEVICE_PATCH
= {
35 var FAKE_DEVICE_LIST
= {
36 kind
: 'clouddevices#devicesListResponse',
37 devices
: [FAKE_DEVICE
]
40 QUnit
.module('gcd_client with mock_xhr', {
41 beforeEach: function() {
42 remoting
.MockXhr
.activate();
43 remoting
.identity
= new remoting
.Identity();
44 chromeMocks
.identity
.mock
$setToken('fake_token');
45 client
= new remoting
.gcd
.Client({
46 apiBaseUrl
: 'https://fake.api',
50 afterEach: function() {
51 remoting
.identity
= null;
52 remoting
.MockXhr
.restore();
56 QUnit
.test('insertRegistrationTicket', function(assert
) {
57 remoting
.MockXhr
.setResponseFor(
58 'POST', 'https://fake.api/registrationTickets',
59 function(/** remoting.MockXhr */ xhr
) {
60 assert
.equal(xhr
.params
.useIdentity
, true);
62 xhr
.params
.jsonContent
,
64 xhr
.setJsonResponse(200, FAKE_REGISTRATION_TICKET
);
66 return client
.insertRegistrationTicket().then(function(ticket
) {
67 assert
.deepEqual(ticket
, FAKE_REGISTRATION_TICKET
);
71 QUnit
.test('patchRegistrationTicket', function(assert
) {
72 remoting
.MockXhr
.setResponseFor(
73 'PATCH', 'https://fake.api/registrationTickets/fake_ticket_id',
74 function(/** remoting.MockXhr */ xhr
) {
75 assert
.equal(xhr
.params
.useIdentity
, false);
77 xhr
.params
.urlParams
, {
81 xhr
.params
.jsonContent
, {
82 deviceDraft
: { 'fake_device_draft': true },
83 oauthClientId
: 'fake_client_id'
85 xhr
.setJsonResponse(200, FAKE_REGISTRATION_TICKET
);
87 return client
.patchRegistrationTicket('fake_ticket_id', {
88 'fake_device_draft': true
89 }, 'fake_client_id').then(function(ticket
) {
90 assert
.deepEqual(ticket
, FAKE_REGISTRATION_TICKET
);
94 QUnit
.test('finalizeRegistrationTicket', function(assert
) {
95 remoting
.MockXhr
.setResponseFor(
96 'POST', 'https://fake.api/registrationTickets/fake_ticket_id/finalize',
97 function(/** remoting.MockXhr */ xhr
) {
98 assert
.equal(xhr
.params
.urlParams
['key'], 'fake_key');
99 assert
.equal(xhr
.params
.useIdentity
, false);
100 xhr
.setJsonResponse(200, FAKE_REGISTRATION_TICKET
);
102 return client
.finalizeRegistrationTicket('fake_ticket_id').
103 then(function(ticket
) {
104 assert
.deepEqual(ticket
, FAKE_REGISTRATION_TICKET
);
108 QUnit
.test('listDevices', function(assert
) {
109 remoting
.MockXhr
.setResponseFor(
110 'GET', 'https://fake.api/devices',
111 function(/** remoting.MockXhr */ xhr
) {
112 assert
.equal(xhr
.params
.useIdentity
, true);
113 xhr
.setJsonResponse(200, FAKE_DEVICE_LIST
);
115 return client
.listDevices().then(function(devices
) {
116 assert
.deepEqual(devices
, [FAKE_DEVICE
]);
120 QUnit
.test('patchDevice', function(assert
) {
121 remoting
.MockXhr
.setResponseFor(
122 'PATCH', 'https://fake.api/devices/fake_device_id',
123 function(/** remoting.MockXhr */ xhr
) {
124 assert
.equal(xhr
.params
.useIdentity
, true);
125 assert
.deepEqual(xhr
.params
.jsonContent
, FAKE_DEVICE_PATCH
);
126 xhr
.setJsonResponse(200, FAKE_DEVICE
);
128 return client
.patchDevice('fake_device_id', FAKE_DEVICE_PATCH
).
129 then(function(device
) {
130 assert
.deepEqual(device
, FAKE_DEVICE
);