Adds the HTTP Credentials section to PolyGerrit settings

Introduces the gr-http-password element, which is added to the settings
screen. The new element displays whether the user has an HTTP password
and allows the user to do any of the following actions:
- View the password,
- Generate a new password,
- Clear the password.

Bug: Issue 3911
Change-Id: I8b2dc328efd7577eb32f40472f7bb8a7c95ff87a
diff --git a/polygerrit-ui/app/elements/change/gr-related-changes-list/gr-related-changes-list.js b/polygerrit-ui/app/elements/change/gr-related-changes-list/gr-related-changes-list.js
index a113477..6ec04da 100644
--- a/polygerrit-ui/app/elements/change/gr-related-changes-list/gr-related-changes-list.js
+++ b/polygerrit-ui/app/elements/change/gr-related-changes-list/gr-related-changes-list.js
@@ -121,7 +121,7 @@
       if (relatedChange.change_id === currentChange.change_id) {
         classes.push('thisChange');
       }
-      return classes.join(' ');;
+      return classes.join(' ');
     },
 
     _computeLinkClass: function(change) {
diff --git a/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password.html b/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password.html
new file mode 100644
index 0000000..e58f1f2
--- /dev/null
+++ b/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password.html
@@ -0,0 +1,63 @@
+<!--
+Copyright (C) 2016 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<link rel="import" href="../../../bower_components/polymer/polymer.html">
+<link rel="import" href="../../shared/gr-button/gr-button.html">
+<link rel="import" href="../../shared/gr-rest-api-interface/gr-rest-api-interface.html">
+
+<dom-module id="gr-http-password">
+  <template>
+    <style>
+      .password {
+        font-family: var(--monospace-font-family);
+      }
+      .noPassword {
+        color: #777;
+        font-style: italic;
+      }
+    </style>
+    <style include="gr-settings-styles"></style>
+    <div class="gr-settings-styles">
+      <section>
+        <span class="title">Username</span>
+        <span class="value">[[_username]]</span>
+      </section>
+      <section>
+        <span class="title">Password</span>
+        <span hidden$="[[!_hasPassword]]">
+          <span class="value" hidden$="[[_passwordVisible]]">
+            <gr-button
+                link
+                on-tap="_handleViewPasswordTap">Click to view</gr-button>
+          </span>
+          <span
+              class="value password"
+              hidden$="[[!_passwordVisible]]">[[_password]]</span>
+        </span>
+        <span class="value noPassword" hidden$="[[_hasPassword]]">(None)</span>
+      </section>
+      <gr-button
+          id="generateButton"
+          on-tap="_handleGenerateTap">Generate New Password</gr-button>
+      <gr-button
+          id="clearButton"
+          on-tap="_handleClearTap"
+          disabled="[[!_hasPassword]]">Clear Password</gr-button>
+    </div>
+    <gr-rest-api-interface id="restAPI"></gr-rest-api-interface>
+  </template>
+  <script src="gr-http-password.js"></script>
+</dom-module>
diff --git a/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password.js b/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password.js
new file mode 100644
index 0000000..9248632
--- /dev/null
+++ b/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password.js
@@ -0,0 +1,81 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+(function() {
+  'use strict';
+
+  Polymer({
+    is: 'gr-http-password',
+
+    /**
+     * Fired when getting the password fails with non-404.
+     *
+     * @event network-error
+     */
+
+    properties: {
+      _serverConfig: Object,
+      _username: String,
+      _password: String,
+      _passwordVisible: {
+        type: Boolean,
+        value: false,
+      },
+      _hasPassword: Boolean,
+    },
+
+    loadData: function() {
+      var promises = [];
+
+      promises.push(this.$.restAPI.getAccount().then(function(account) {
+        this._username = account.username;
+      }.bind(this)));
+
+      promises.push(this.$.restAPI
+          .getAccountHttpPassword(this._handleGetPasswordError.bind(this))
+          .then(function(pass) {
+            this._password = pass;
+            this._hasPassword = !!pass;
+          }.bind(this)));
+
+      return Promise.all(promises);
+    },
+
+    _handleGetPasswordError: function(response) {
+      if (response.status === 404) {
+        this._hasPassword = false;
+      } else {
+        this.fire('network-error', {response: response});
+      }
+    },
+
+    _handleViewPasswordTap: function() {
+      this._passwordVisible = true;
+    },
+
+    _handleGenerateTap: function() {
+      this.$.restAPI.generateAccountHttpPassword().then(function(newPassword) {
+        this._hasPassword = true;
+        this._passwordVisible = true;
+        this._password = newPassword;
+      }.bind(this));
+    },
+
+    _handleClearTap: function() {
+      this.$.restAPI.deleteAccountHttpPassword().then(function() {
+        this._password = '';
+        this._hasPassword = false;
+      }.bind(this));
+    },
+  });
+})();
diff --git a/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password_test.html b/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password_test.html
new file mode 100644
index 0000000..44bffe5
--- /dev/null
+++ b/polygerrit-ui/app/elements/settings/gr-http-password/gr-http-password_test.html
@@ -0,0 +1,157 @@
+<!DOCTYPE html>
+<!--
+Copyright (C) 2016 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
+<title>gr-settings-view</title>
+
+<script src="../../../bower_components/webcomponentsjs/webcomponents.min.js"></script>
+<script src="../../../bower_components/web-component-tester/browser.js"></script>
+
+<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
+<link rel="import" href="gr-http-password.html">
+
+<test-fixture id="basic">
+  <template>
+    <gr-http-password></gr-http-password>
+  </template>
+</test-fixture>
+
+<script>
+  suite('gr-http-password tests (already has password)', function() {
+    var element;
+    var account;
+    var password;
+
+    setup(function(done) {
+      account = {username: 'user name'};
+      password = 'the password';
+
+      stub('gr-rest-api-interface', {
+        getAccount: function() { return Promise.resolve(account); },
+        getAccountHttpPassword: function() {
+          return Promise.resolve(password);
+        },
+      });
+
+      element = fixture('basic');
+      element.loadData().then(function() { flush(done) });
+    });
+
+    test('loads data', function() {
+      assert.equal(element._username, 'user name');
+      assert.equal(element._password, 'the password');
+      assert.isFalse(element._passwordVisible);
+      assert.isTrue(element._hasPassword);
+    });
+
+    test('view password', function() {
+      var button = element.$$('.value gr-button');
+      assert.isFalse(element._passwordVisible);
+      MockInteractions.tap(button);
+      assert.isTrue(element._passwordVisible);
+    });
+
+    test('generate password', function() {
+      var button = element.$.generateButton;
+      var nextPassword = 'the new password';
+      var generateStub = sinon.stub(element.$.restAPI,
+          'generateAccountHttpPassword', function() {
+            return Promise.resolve(nextPassword);
+          });
+
+      assert.isTrue(element._hasPassword);
+      assert.isFalse(element._passwordVisible);
+      assert.equal(element._password, 'the password');
+
+      MockInteractions.tap(button);
+
+      assert.isTrue(generateStub.called);
+      generateStub.lastCall.returnValue.then(function() {
+        assert.isTrue(element._passwordVisible);
+        assert.isTrue(element._hasPassword);
+        assert.equal(element._password, 'the new password');
+      });
+    });
+
+    test('clear password', function() {
+      var button = element.$.clearButton;
+      var clearStub = sinon.stub(element.$.restAPI, 'deleteAccountHttpPassword',
+          function() { return Promise.resolve(); });
+
+      assert.isTrue(element._hasPassword);
+      assert.equal(element._password, 'the password');
+
+      MockInteractions.tap(button);
+
+      assert.isTrue(clearStub.called);
+      clearStub.lastCall.returnValue.then(function() {
+        assert.isFalse(element._hasPassword);
+        assert.equal(element._password, '');
+      });
+    });
+  });
+
+  suite('gr-http-password tests (has no password)', function() {
+    var element;
+    var account;
+
+    setup(function(done) {
+      account = {username: 'user name'};
+      password = 'the password';
+
+      stub('gr-rest-api-interface', {
+        getAccount: function() { return Promise.resolve(account); },
+        getAccountHttpPassword: function(errFn) {
+          errFn({status: 404});
+          return Promise.resolve('');
+        },
+      });
+
+      element = fixture('basic');
+      element.loadData().then(function() { flush(done) });
+    });
+
+    test('loads data', function() {
+      assert.equal(element._username, 'user name');
+      assert.isNotOk(element._password);
+      assert.isFalse(element._passwordVisible);
+      assert.isFalse(element._hasPassword);
+    });
+
+    test('generate password', function() {
+      var button = element.$.generateButton;
+      var nextPassword = 'the new password';
+      var generateStub = sinon.stub(element.$.restAPI,
+          'generateAccountHttpPassword', function() {
+            return Promise.resolve(nextPassword);
+          });
+
+      assert.isFalse(element._hasPassword);
+      assert.isFalse(element._passwordVisible);
+      assert.isNotOk(element._password);
+
+      MockInteractions.tap(button);
+
+      assert.isTrue(generateStub.called);
+      generateStub.lastCall.returnValue.then(function() {
+        assert.isTrue(element._passwordVisible);
+        assert.isOk(element._hasPassword);
+        assert.equal(element._password, 'the new password');
+      });
+    });
+  });
+</script>
diff --git a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.html b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.html
index e5f8bee..9e5a63b 100644
--- a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.html
+++ b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.html
@@ -19,6 +19,7 @@
 <link rel="import" href="../gr-account-info/gr-account-info.html">
 <link rel="import" href="../gr-email-editor/gr-email-editor.html">
 <link rel="import" href="../gr-group-list/gr-group-list.html">
+<link rel="import" href="../gr-http-password/gr-http-password.html">
 <link rel="import" href="../gr-menu-editor/gr-menu-editor.html">
 <link rel="import" href="../gr-watched-projects-editor/gr-watched-projects-editor.html">
 <link rel="import" href="../../shared/gr-button/gr-button.html">
@@ -256,6 +257,10 @@
               disabled="[[!_computeAddEmailButtonEnabled(_newEmail, _addingEmail)]]"
               on-tap="_handleAddEmailButton">Send Verification</gr-button>
         </fieldset>
+        <h2>HTTP Credentials</h2>
+        <fieldset>
+          <gr-http-password id="httpPass"></gr-http-password>
+        </fieldset>
         <h2>Groups</h2>
         <fieldset>
           <gr-group-list id="groupList"></gr-group-list>
diff --git a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.js b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.js
index d952556..51a994e 100644
--- a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.js
+++ b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.js
@@ -112,6 +112,8 @@
 
       promises.push(this.$.groupList.loadData());
 
+      promises.push(this.$.httpPass.loadData());
+
       this._loadingPromise = Promise.all(promises).then(function() {
         this._loading = false;
       }.bind(this));
diff --git a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view_test.html b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view_test.html
index 70b7c1c..1935838 100644
--- a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view_test.html
+++ b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view_test.html
@@ -119,6 +119,7 @@
         getAccountEmails: function() { return Promise.resolve(); },
         getConfig: function() { return Promise.resolve(config); },
         getAccountGroups: function() { return Promise.resolve([]); },
+        getAccountHttpPassword: function() { return Promise.resolve(''); },
       });
       element = fixture('basic');
 
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
index 410f7db..abf5f94 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
@@ -835,5 +835,19 @@
       return this.send('PUT', '/changes/' + encodeURIComponent(changeNum) +
           '/topic', {topic: topic});
     },
+
+    getAccountHttpPassword: function(opt_errFn) {
+      return this._fetchSharedCacheURL('/accounts/self/password.http',
+          opt_errFn);
+    },
+
+    deleteAccountHttpPassword: function() {
+      return this.send('DELETE', '/accounts/self/password.http');
+    },
+
+    generateAccountHttpPassword: function() {
+      return this.send('PUT', '/accounts/self/password.http', {generate: true})
+          .then(this.getResponseObject);
+    },
   });
 })();
diff --git a/polygerrit-ui/app/test/index.html b/polygerrit-ui/app/test/index.html
index 949c454..e9f0031 100644
--- a/polygerrit-ui/app/test/index.html
+++ b/polygerrit-ui/app/test/index.html
@@ -57,6 +57,7 @@
     'settings/gr-account-info/gr-account-info_test.html',
     'settings/gr-email-editor/gr-email-editor_test.html',
     'settings/gr-group-list/gr-group-list_test.html',
+    'settings/gr-http-password/gr-http-password_test.html',
     'settings/gr-menu-editor/gr-menu-editor_test.html',
     'settings/gr-settings-view/gr-settings-view_test.html',
     'settings/gr-watched-projects-editor/gr-watched-projects-editor_test.html',