Merge "Handle email confirmation links in PolyGerrit"
diff --git a/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager.js b/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager.js
index 479f389..4c8c64f 100644
--- a/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager.js
+++ b/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager.js
@@ -31,6 +31,7 @@
     attached: function() {
       this.listen(document, 'server-error', '_handleServerError');
       this.listen(document, 'network-error', '_handleNetworkError');
+      this.listen(document, 'show-alert', '_handleShowAlert');
     },
 
     detached: function() {
@@ -61,6 +62,10 @@
       }
     },
 
+    _handleShowAlert: function(e) {
+      this._showAlert(e.detail.message);
+    },
+
     _handleNetworkError: function(e) {
       this._showAlert('Server unavailable');
       console.error(e.detail.error.message);
diff --git a/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html b/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html
index 44cbde0..e2cacf9 100644
--- a/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html
+++ b/polygerrit-ui/app/elements/core/gr-error-manager/gr-error-manager_test.html
@@ -134,5 +134,12 @@
         });
       });
     });
+
+    test('show alert', function() {
+      sandbox.stub(element, '_showAlert');
+      element.fire('show-alert', {message: 'foo'});
+      assert.isTrue(element._showAlert.calledOnce);
+      assert.isTrue(element._showAlert.lastCall.calledWithExactly('foo'));
+    });
   });
 </script>
diff --git a/polygerrit-ui/app/elements/core/gr-router/gr-router.js b/polygerrit-ui/app/elements/core/gr-router/gr-router.js
index 74344a7..3819bfe 100644
--- a/polygerrit-ui/app/elements/core/gr-router/gr-router.js
+++ b/polygerrit-ui/app/elements/core/gr-router/gr-router.js
@@ -64,7 +64,11 @@
         if (data.hash[0] !== '/') {
           data.hash = '/' + data.hash;
         }
-        page.redirect(data.hash);
+        var newUrl = data.hash;
+        if (newUrl.indexOf('/VE/') === 0) {
+          newUrl = '/settings' + data.hash;
+        }
+        page.redirect(newUrl);
         return;
       }
       restAPI.getLoggedIn().then(function(loggedIn) {
@@ -171,6 +175,19 @@
       app.params = params;
     });
 
+    page(/^\/settings\/VE\/(\S+)/, function(data) {
+      restAPI.getLoggedIn().then(function(loggedIn) {
+        if (loggedIn) {
+          app.params = {
+            view: 'gr-settings-view',
+            emailToken: data.params[0],
+          };
+        } else {
+          page.show('/login/' + encodeURIComponent(data.canonicalPath));
+        }
+      });
+    });
+
     page(/^\/settings\/?/, function(data) {
       restAPI.getLoggedIn().then(function(loggedIn) {
         if (loggedIn) {
diff --git a/polygerrit-ui/app/elements/gr-app.html b/polygerrit-ui/app/elements/gr-app.html
index 83ceb57..269240a 100644
--- a/polygerrit-ui/app/elements/gr-app.html
+++ b/polygerrit-ui/app/elements/gr-app.html
@@ -117,6 +117,7 @@
       </template>
       <template is="dom-if" if="[[_showSettingsView]]" restamp="true">
         <gr-settings-view
+            params="[[params]]"
             on-account-detail-update="_handleAccountDetailUpdate">
         </gr-settings-view>
       </template>
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 5e20c9e..e12d434 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
@@ -32,11 +32,21 @@
      * @event title-change
      */
 
+    /**
+     * Fired with email confirmation text.
+     *
+     * @event show-alert
+     */
+
     properties: {
       prefs: {
         type: Object,
         value: function() { return {}; },
       },
+      params: {
+        type: Object,
+        value: function() { return {}; },
+      },
       _accountInfoMutable: Boolean,
       _accountInfoChanged: Boolean,
       _diffPrefs: Object,
@@ -116,7 +126,6 @@
       var promises = [
         this.$.accountInfo.loadData(),
         this.$.watchedProjectsEditor.loadData(),
-        this.$.emailEditor.loadData(),
         this.$.groupList.loadData(),
         this.$.httpPass.loadData(),
       ];
@@ -139,6 +148,18 @@
         }
       }.bind(this)));
 
+      if (this.params.emailToken) {
+        promises.push(this.$.restAPI.confirmEmail(this.params.emailToken).then(
+          function(message) {
+            if (message) {
+              this.fire('show-alert', {message: message});
+            }
+            this.$.emailEditor.loadData();
+          }.bind(this)));
+      } else {
+        promises.push(this.$.emailEditor.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 0b42da5..6a2af18 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
@@ -43,6 +43,7 @@
     var preferences;
     var diffPreferences;
     var config;
+    var sandbox;
 
     function valueOf(title, fieldsetid) {
       var sections = element.$[fieldsetid].querySelectorAll('section');
@@ -65,11 +66,12 @@
     }
 
     function stubAddAccountEmail(statusCode) {
-      return sinon.stub(element.$.restAPI, 'addAccountEmail',
+      return sandbox.stub(element.$.restAPI, 'addAccountEmail',
           function() { return Promise.resolve({status: statusCode}); });
     }
 
     setup(function(done) {
+      sandbox = sinon.sandbox.create();
       account = {
         _account_id: 123,
         name: 'user name',
@@ -129,8 +131,12 @@
       element._loadingPromise.then(done);
     });
 
+    teardown(function() {
+      sandbox.restore();
+    });
+
     test('calls the title-change event', function() {
-      var titleChangedStub = sinon.stub();
+      var titleChangedStub = sandbox.stub();
 
       // Create a new view.
       var newElement = document.createElement('gr-settings-view');
@@ -341,5 +347,52 @@
         done();
       });
     });
+
+    test('emails are loaded without emailToken', function() {
+      sandbox.stub(element.$.emailEditor, 'loadData');
+      element.params = {};
+      element.attached();
+      assert.isTrue(element.$.emailEditor.loadData.calledOnce);
+    });
+
+    suite('when email verification token is provided', function() {
+      var resolveConfirm;
+
+      setup(function() {
+        sandbox.stub(element.$.emailEditor, 'loadData');
+        sandbox.stub(element.$.restAPI, 'confirmEmail', function() {
+          return new Promise(function(resolve) { resolveConfirm = resolve; });
+        });
+        element.params = {emailToken: 'foo'};
+        element.attached();
+      });
+
+      test('it is used to confirm email via rest API', function() {
+        assert.isTrue(element.$.restAPI.confirmEmail.calledOnce);
+        assert.isTrue(element.$.restAPI.confirmEmail.calledWith('foo'));
+      });
+
+      test('emails are not loaded initially', function() {
+        assert.isFalse(element.$.emailEditor.loadData.called);
+      });
+
+      test('user emails are loaded after email confirmed', function(done) {
+        element._loadingPromise.then(function() {
+          assert.isTrue(element.$.emailEditor.loadData.calledOnce);
+          done();
+        });
+        resolveConfirm();
+      });
+
+      test('show-alert is fired when email is confirmed', function(done) {
+        sandbox.spy(element, 'fire');
+        element._loadingPromise.then(function() {
+          assert.isTrue(element.fire.calledWith('show-alert', {message: 'bar'}));
+          done();
+        });
+        resolveConfirm('bar');
+      });
+
+    });
   });
 </script>
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 3769a0b..4b52503 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
@@ -942,5 +942,15 @@
           this.getChangeActionURL(changeNum, patchNum, '/description'),
           {description: desc});
     },
+
+    confirmEmail: function(token) {
+      return this.send('PUT', '/config/server/email.confirm', {token: token})
+          .then(function(response) {
+            if (response.status === 204) {
+              return 'Email confirmed successfully.';
+            }
+            return null;
+          });
+    },
   });
 })();
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.html
index a9cea24..f43d62c 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface_test.html
@@ -333,10 +333,10 @@
 
     test('saveDiffPreferences invalidates cache line', function() {
       var cacheKey = '/accounts/self/preferences.diff';
-      var sendStub = sandbox.stub(element, 'send');
+      sandbox.stub(element, 'send');
       element._cache[cacheKey] = {tab_size: 4};
       element.saveDiffPreferences({tab_size: 8});
-      assert.isTrue(sendStub.called);
+      assert.isTrue(element.send.called);
       assert.notOk(element._cache[cacheKey]);
     });
 
@@ -414,10 +414,17 @@
     });
 
     test('savPreferences normalizes download scheme', function() {
-      var sendStub = sandbox.stub(element, 'send');
+      sandbox.stub(element, 'send');
       element.savePreferences({download_scheme: 'HTTP'});
-      assert.isTrue(sendStub.called);
-      assert.equal(sendStub.lastCall.args[2].download_scheme, 'http');
+      assert.isTrue(element.send.called);
+      assert.equal(element.send.lastCall.args[2].download_scheme, 'http');
+    });
+
+    test('confirmEmail', function() {
+      sandbox.spy(element, 'send');
+      element.confirmEmail('foo');
+      assert.isTrue(element.send.calledWith(
+          'PUT', '/config/server/email.confirm', {token: 'foo'}));
     });
   });
 </script>