From 4e17520d8373f43c6cd4ff14f0320ffe1970aba3 Mon Sep 17 00:00:00 2001 From: Arthur Del Esposte Date: Wed, 23 Jul 2014 18:55:34 +0000 Subject: [PATCH] Fix friend removal bug --- app/controllers/my_profile/friends_controller.rb | 2 +- app/models/friendship.rb | 5 +++++ test/functional/friends_controller_test.rb | 4 ++-- test/integration/manage_friendships_test.rb | 37 +++++++++++++++++++++++++++++++++++++ test/unit/friendship_test.rb | 14 ++++++++++++++ 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 test/integration/manage_friendships_test.rb diff --git a/app/controllers/my_profile/friends_controller.rb b/app/controllers/my_profile/friends_controller.rb index 21f812b..436c301 100644 --- a/app/controllers/my_profile/friends_controller.rb +++ b/app/controllers/my_profile/friends_controller.rb @@ -11,7 +11,7 @@ class FriendsController < MyProfileController def remove @friend = profile.friends.find(params[:id]) if request.post? && params[:confirmation] - profile.remove_friend(@friend) + Friendship.remove_friendship(profile, @friend) redirect_to :action => 'index' end end diff --git a/app/models/friendship.rb b/app/models/friendship.rb index 8e390dd..d7d2e9f 100644 --- a/app/models/friendship.rb +++ b/app/models/friendship.rb @@ -15,4 +15,9 @@ class Friendship < ActiveRecord::Base Friendship.update_cache_counter(:friends_count, friendship.person, -1) Friendship.update_cache_counter(:friends_count, friendship.friend, -1) end + + def self.remove_friendship(person1, person2) + person1.remove_friend(person2) + person2.remove_friend(person1) + end end diff --git a/test/functional/friends_controller_test.rb b/test/functional/friends_controller_test.rb index c692e85..09c1b34 100644 --- a/test/functional/friends_controller_test.rb +++ b/test/functional/friends_controller_test.rb @@ -36,12 +36,12 @@ class FriendsControllerTest < ActionController::TestCase should 'actually remove friend' do profile.add_friend(friend) + friend.add_friend(profile) - assert_difference 'Friendship.count', -1 do + assert_difference 'Friendship.count', -2 do post :remove, :id => friend.id, :confirmation => '1' assert_redirected_to :action => 'index' end - assert_equal friend, Profile.find(friend.id) end should 'display find people button' do diff --git a/test/integration/manage_friendships_test.rb b/test/integration/manage_friendships_test.rb new file mode 100644 index 0000000..70867db --- /dev/null +++ b/test/integration/manage_friendships_test.rb @@ -0,0 +1,37 @@ +require "#{File.dirname(__FILE__)}/../test_helper" + +class ManageFriendshipsTest < ActionController::IntegrationTest + + def setup + FriendsController.any_instance.stubs(:get_layout).returns('application') + ProfileController.any_instance.stubs(:get_layout).returns('application') + + Friendship.delete_all + Person.delete_all + @person = create_user("albert", :password => 'test', + :password_confirmation => 'test').person + @person.user.activate + + @friend = fast_create(Person, :identifier => "isaac") + + login(@person.identifier, 'test') + end + + should 'remove friendships' do + @person.add_friend(@friend) + @friend.add_friend(@person) + + get "/myprofile/#{@person.identifier}/friends/remove/#{@friend.id}" + assert_response :success + + post "/myprofile/#{@person.identifier}/friends/remove/#{@friend.id}", + :confirmation => '1' + assert_response :redirect + + follow_redirect! + + assert assigns(:friends).empty? + assert !@person.is_a_friend?(@friend) + assert !@friend.is_a_friend?(@person) + end +end diff --git a/test/unit/friendship_test.rb b/test/unit/friendship_test.rb index a829ace..3470a51 100644 --- a/test/unit/friendship_test.rb +++ b/test/unit/friendship_test.rb @@ -58,4 +58,18 @@ class FriendshipTest < ActiveSupport::TestCase assert_equal ['a'], ta.get_friend_name end + should 'remove friendships when a friend removal occurs' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + p1.add_friend(p2, 'friends') + p2.add_friend(p1, 'friends') + + assert_difference 'Friendship.count', -2 do + Friendship.remove_friendship(p1, p2) + end + + assert_not_includes p1.friends(true), p2 + assert_not_includes p2.friends(true), p1 + end + end -- libgit2 0.21.2