From 2025fae420976c60cfbb95e155cafc9153c31f21 Mon Sep 17 00:00:00 2001
From: Steffen van Bergerem <svbergerem@online.de>
Date: Sun, 13 Dec 2015 02:02:57 +0100
Subject: [PATCH] Disable hovercards for logged out users and prevent redirect
 to sign in page

closes #6587
---
 Changelog.md                                  |  3 +-
 .../javascripts/app/views/hovercard_view.js   |  6 +--
 .../app/views/hovercard_view_spec.js          | 53 +++++++++++++++++--
 3 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 51974a7db5..4148a081d0 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -6,7 +6,8 @@
 ## Bug fixes
 * Fix mention autocomplete when pasting the username [#6510](https://github.com/diaspora/diaspora/pull/6510)
 * Use and update updated\_at for notifications [#6573](https://github.com/diaspora/diaspora/pull/6573)
-* Ensure the author signature is checked when receiving a relayable [#6539](https://github.com/diaspora/diaspora/pull/6539) 
+* Ensure the author signature is checked when receiving a relayable [#6539](https://github.com/diaspora/diaspora/pull/6539)
+* Do not try to display hovercards when logged out [#6587](https://github.com/diaspora/diaspora/pull/6587)
 
 ## Features
 
diff --git a/app/assets/javascripts/app/views/hovercard_view.js b/app/assets/javascripts/app/views/hovercard_view.js
index 643fb771cf..0829849311 100644
--- a/app/assets/javascripts/app/views/hovercard_view.js
+++ b/app/assets/javascripts/app/views/hovercard_view.js
@@ -25,7 +25,7 @@ app.views.Hovercard = app.views.Base.extend({
     this.hashtags = this.$('.hashtags');
     this.person_link = this.$('a.person');
     this.person_handle = this.$('div.handle');
-    this.active = true;
+    this.active = app.currentUser.authenticated();
   },
 
   postRenderTemplate: function() {
@@ -97,7 +97,7 @@ app.views.Hovercard = app.views.Base.extend({
     href += "/hovercard.json";
 
     var self = this;
-    $.get(href, function(person){
+    $.ajax(href, {preventGlobalErrorHandling: true}).done(function(person){
       if( !person || person.length === 0 ) {
         throw new Error("received data is not a person object");
       }
@@ -130,7 +130,7 @@ app.views.Hovercard = app.views.Base.extend({
     // TODO render me client side!!!
     var href = this.href();
     href += "/aspect_membership_button";
-    $.get(href, function(response) {
+    $.ajax(href, {preventGlobalErrorHandling: true}).done(function(response){
       self.dropdown_container.html(response);
     });
     new app.views.AspectMembership({el: self.dropdown_container});
diff --git a/spec/javascripts/app/views/hovercard_view_spec.js b/spec/javascripts/app/views/hovercard_view_spec.js
index a003e24b25..2e8cddee5e 100644
--- a/spec/javascripts/app/views/hovercard_view_spec.js
+++ b/spec/javascripts/app/views/hovercard_view_spec.js
@@ -1,11 +1,54 @@
 describe("app.views.Hovercard", function() {
-  beforeEach(function() {
-    this.view = new app.views.Hovercard();
+  context("user not signed in", function() {
+    beforeEach(function() {
+      logout();
+      this.view = new app.views.Hovercard();
+    });
+
+    describe("initialize", function() {
+      it("deactivates hovercards", function() {
+        expect(this.view.active).toBeFalsy();
+      });
+    });
   });
 
-  describe("mouseIsOverElement", function() {
-    it("returns false if the element is undefined", function() {
-      expect(this.view.mouseIsOverElement(undefined, $.Event())).toBeFalsy();
+  context("user signed in", function() {
+    beforeEach(function() {
+      loginAs(factory.userAttrs());
+      this.view = new app.views.Hovercard();
+    });
+
+    describe("initialize", function() {
+      it("activates hovercards", function() {
+        expect(this.view.active).toBeTruthy();
+      });
+    });
+
+    describe("mouseIsOverElement", function() {
+      it("returns false if the element is undefined", function() {
+        expect(this.view.mouseIsOverElement(undefined, $.Event())).toBeFalsy();
+      });
+    });
+
+    describe("_populateHovercard", function() {
+      it("prevents global error handling for the ajax call", function() {
+        spyOn(jQuery, "ajax").and.callThrough();
+        this.view.parent = spec.content();
+        this.view._populateHovercard();
+        expect(jQuery.ajax).toHaveBeenCalledWith("undefined/hovercard.json", {preventGlobalErrorHandling: true});
+      });
+    });
+
+    describe("_populateHovercardWith", function() {
+      it("prevents global error handling for the ajax call", function() {
+        spyOn(jQuery, "ajax").and.callThrough();
+        this.view.parent = spec.content();
+        this.view._populateHovercardWith({});
+        expect(jQuery.ajax).toHaveBeenCalledWith(
+          "undefined/aspect_membership_button",
+          {preventGlobalErrorHandling: true}
+        );
+      });
     });
   });
 });
-- 
GitLab