Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / crd / js / legacy_host_list_api_unittest.js
blob045a77872819c0c46877cbda57e39fa78bda1703
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 /**
6 * @fileoverview
7 * Unit tests for host_controller.js.
8 */
10 (function() {
12 'use strict';
14 var FAKE_HOST_ID = '0bad0bad-0bad-0bad-0bad-0bad0bad0bad';
15 var FAKE_HOST_NAME = '<FAKE_HOST_NAME>';
16 var FAKE_PUBLIC_KEY = '<FAKE_PUBLIC_KEY>';
17 var FAKE_HOST_CLIENT_ID = '<FAKE_HOST_CLIENT_ID>';
18 var FAKE_AUTH_CODE = '<FAKE_AUTH_CODE>';
20 /** @type {sinon.TestStub} */
21 var generateUuidStub;
23 QUnit.module('host_list_api_impl', {
24 beforeEach: function(/** QUnit.Assert */ assert) {
25 remoting.settings = new remoting.Settings();
26 remoting.MockXhr.activate();
27 generateUuidStub = sinon.stub(base, 'generateUuid');
28 generateUuidStub.returns(FAKE_HOST_ID);
30 afterEach: function(/** QUnit.Assert */ assert) {
31 generateUuidStub.restore();
32 remoting.MockXhr.restore();
33 remoting.settings = null;
35 });
37 /**
38 * Install an HTTP response for requests to the registry.
39 * @param {QUnit.Assert} assert
41 function queueRegistryResponse(assert) {
42 var responseJson = {
43 data: {
44 authorizationCode: FAKE_AUTH_CODE
48 remoting.MockXhr.setResponseFor(
49 'POST', 'DIRECTORY_API_BASE_URL/@me/hosts',
50 function(/** remoting.MockXhr */ xhr) {
51 assert.deepEqual(
52 xhr.params.jsonContent,
53 { data: {
54 hostId: FAKE_HOST_ID,
55 hostName: FAKE_HOST_NAME,
56 publicKey: FAKE_PUBLIC_KEY
57 } });
58 xhr.setJsonResponse(200, responseJson);
59 });
62 QUnit.test('register', function(assert) {
63 var impl = new remoting.LegacyHostListApi();
64 queueRegistryResponse(assert);
65 return impl.register(
66 FAKE_HOST_NAME,
67 FAKE_PUBLIC_KEY,
68 FAKE_HOST_CLIENT_ID
69 ).then(function(regResult) {
70 assert.equal(regResult.authCode, FAKE_AUTH_CODE);
71 assert.equal(regResult.email, '');
72 });
73 });
75 QUnit.test('register failure', function(assert) {
76 var impl = new remoting.LegacyHostListApi();
77 remoting.MockXhr.setEmptyResponseFor(
78 'POST', 'DIRECTORY_API_BASE_URL/@me/hosts', 500);
79 return impl.register(
80 FAKE_HOST_NAME,
81 FAKE_PUBLIC_KEY,
82 FAKE_HOST_CLIENT_ID
83 ).then(function(authCode) {
84 throw 'test failed';
85 }, function(/** remoting.Error */ e) {
86 assert.equal(e.getTag(), remoting.Error.Tag.REGISTRATION_FAILED);
87 });
88 });
90 })();