From a6b9c6c05765525aa16ac479678a72f68155cbcf Mon Sep 17 00:00:00 2001
From: Steffen van Bergerem <svbergerem@online.de>
Date: Sat, 4 Apr 2015 03:46:27 +0200
Subject: [PATCH] Return 406 on people/guid/contacts, format json

---
 app/controllers/people_controller.rb       | 40 +++++++++++++---------
 spec/controllers/people_controller_spec.rb |  5 +++
 2 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb
index 57c18fa7ba..a4693a90ad 100644
--- a/app/controllers/people_controller.rb
+++ b/app/controllers/people_controller.rb
@@ -135,24 +135,30 @@ class PeopleController < ApplicationController
   end
 
   def contacts
-    @person = Person.find_by_guid(params[:person_id])
+    respond_to do |format|
+      format.json { render nothing: true, status: 406 }
 
-    if @person
-      @contact = current_user.contact_for(@person)
-      @contacts_of_contact = Contact.contact_contacts_for(current_user, @person)
-      gon.preloads[:person] = PersonPresenter.new(@person, current_user).full_hash_with_profile
-      gon.preloads[:photos] = {
-        count: photos_from(@person, :all).count(:all)
-      }
-      gon.preloads[:contacts] = {
-        count: @contacts_of_contact.count(:all),
-      }
-      @contacts_of_contact = @contacts_of_contact.paginate(:page => params[:page], :per_page => (params[:limit] || 15))
-      @hashes = hashes_for_people @contacts_of_contact, @aspects
-      respond_with @person, layout: "with_header"
-    else
-      flash[:error] = I18n.t 'people.show.does_not_exist'
-      redirect_to people_path
+      format.any do
+        @person = Person.find_by_guid(params[:person_id])
+
+        if @person
+          @contact = current_user.contact_for(@person)
+          @contacts_of_contact = Contact.contact_contacts_for(current_user, @person)
+          gon.preloads[:person] = PersonPresenter.new(@person, current_user).full_hash_with_profile
+          gon.preloads[:photos] = {
+            count: photos_from(@person, :all).count(:all)
+          }
+          gon.preloads[:contacts] = {
+            count: @contacts_of_contact.count(:all),
+          }
+          @contacts_of_contact = @contacts_of_contact.paginate(page: params[:page], per_page: (params[:limit] || 15))
+          @hashes = hashes_for_people @contacts_of_contact, @aspects
+          respond_with @person, layout: "with_header"
+        else
+          flash[:error] = I18n.t "people.show.does_not_exist"
+          redirect_to people_path
+        end
+      end
     end
   end
 
diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb
index 74610562eb..841e3d6166 100644
--- a/spec/controllers/people_controller_spec.rb
+++ b/spec/controllers/people_controller_spec.rb
@@ -497,6 +497,11 @@ describe PeopleController, :type => :controller do
       get :contacts, :person_id => eve.person.to_param
       expect(response.body).to include '"photos":{"count":16}' # eve is not sharing with alice
     end
+
+    it "returns a 406 for json format" do
+      get :contacts, person_id: "foo", format: :json
+      expect(response.code).to eq("406")
+    end
   end
 
   describe '#diaspora_id?' do
-- 
GitLab