Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / crd / js / gcd_client_with_mock_xhr_unittest.js
blobbd145390ca0a5bbbe6adcd7183ef31f57b0c990a
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 {remoting.gcd.Client} */
10 var client;
12 /** @const */
13 var FAKE_REGISTRATION_TICKET = {
14   kind: 'clouddevices#registrationTicket',
15   id: 'fake_ticket_id',
16   robotAccountEmail: 'fake@robotaccounts.com',
17   robotAccountAuthorizationCode: 'fake_robot_auth_code',
18   deviceDraft: {
19     id: 'fake_device_id'
20   }
23 /** @const */
24 var FAKE_DEVICE = {
25   kind: 'clouddevices#device',
26   id: 'fake_device_id'
29 /** @const */
30 var FAKE_DEVICE_PATCH = {
31   fake_patch: true
34 /** @const */
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',
47       apiKey: 'fake_key'
48     });
49   },
50   afterEach: function() {
51     remoting.identity = null;
52     remoting.MockXhr.restore();
53   }
54 });
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);
61         assert.deepEqual(
62             xhr.params.jsonContent,
63             { userEmail: 'me' });
64         xhr.setJsonResponse(200, FAKE_REGISTRATION_TICKET);
65       });
66   return client.insertRegistrationTicket().then(function(ticket) {
67     assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
68   });
69 });
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);
76         assert.deepEqual(
77             xhr.params.urlParams, {
78               key: 'fake_key'
79             });
80         assert.deepEqual(
81             xhr.params.jsonContent, {
82               deviceDraft: { 'fake_device_draft': true },
83               oauthClientId: 'fake_client_id'
84             });
85         xhr.setJsonResponse(200, FAKE_REGISTRATION_TICKET);
86       });
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);
91   });
92 });
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);
101       });
102   return client.finalizeRegistrationTicket('fake_ticket_id').
103       then(function(ticket) {
104         assert.deepEqual(ticket, FAKE_REGISTRATION_TICKET);
105       });
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);
114       });
115   return client.listDevices().then(function(devices) {
116     assert.deepEqual(devices, [FAKE_DEVICE]);
117   });
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);
127       });
128   return client.patchDevice('fake_device_id', FAKE_DEVICE_PATCH).
129       then(function(device) {
130         assert.deepEqual(device, FAKE_DEVICE);
131       });
134 })();