3 class FriendshipsControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/username/make_friend", :method => :get },
9 { :controller => "friendships", :action => "make_friend", :display_name => "username" }
12 { :path => "/user/username/make_friend", :method => :post },
13 { :controller => "friendships", :action => "make_friend", :display_name => "username" }
16 { :path => "/user/username/remove_friend", :method => :get },
17 { :controller => "friendships", :action => "remove_friend", :display_name => "username" }
20 { :path => "/user/username/remove_friend", :method => :post },
21 { :controller => "friendships", :action => "remove_friend", :display_name => "username" }
26 # Get users to work with
28 friend = create(:user)
30 # Check that the users aren't already friends
31 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
33 # When not logged in a GET should ask us to login
34 get make_friend_path(friend)
35 assert_redirected_to login_path(:referer => make_friend_path(:display_name => friend.display_name))
37 # When not logged in a POST should error
38 post make_friend_path(friend)
39 assert_response :forbidden
40 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
44 # When logged in a GET should get a confirmation page
45 get make_friend_path(friend)
46 assert_response :success
47 assert_template :make_friend
48 assert_select "form" do
49 assert_select "input[type='hidden'][name='referer']", 0
50 assert_select "input[type='submit']", 1
52 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
54 # When logged in a POST should add the friendship
55 assert_difference "ActionMailer::Base.deliveries.size", 1 do
56 perform_enqueued_jobs do
57 post make_friend_path(friend)
60 assert_redirected_to user_path(friend)
61 assert_match(/is now your friend/, flash[:notice])
62 assert Friendship.find_by(:befriender => user, :befriendee => friend)
63 email = ActionMailer::Base.deliveries.first
64 assert_equal 1, email.to.count
65 assert_equal friend.email, email.to.first
66 ActionMailer::Base.deliveries.clear
68 # A second POST should report that the friendship already exists
69 assert_no_difference "ActionMailer::Base.deliveries.size" do
70 perform_enqueued_jobs do
71 post make_friend_path(friend)
74 assert_redirected_to user_path(friend)
75 assert_match(/You are already friends with/, flash[:warning])
76 assert Friendship.find_by(:befriender => user, :befriendee => friend)
79 def test_make_friend_with_referer
80 # Get users to work with
82 friend = create(:user)
85 # Check that the users aren't already friends
86 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
88 # The GET should preserve any referer
89 get make_friend_path(friend), :params => { :referer => "/test" }
90 assert_response :success
91 assert_template :make_friend
92 assert_select "form" do
93 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
94 assert_select "input[type='submit']", 1
96 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
98 # When logged in a POST should add the friendship and refer us
99 assert_difference "ActionMailer::Base.deliveries.size", 1 do
100 perform_enqueued_jobs do
101 post make_friend_path(friend), :params => { :referer => "/test" }
104 assert_redirected_to "/test"
105 assert_match(/is now your friend/, flash[:notice])
106 assert Friendship.find_by(:befriender => user, :befriendee => friend)
107 email = ActionMailer::Base.deliveries.first
108 assert_equal 1, email.to.count
109 assert_equal friend.email, email.to.first
110 ActionMailer::Base.deliveries.clear
113 def test_make_friend_unknown_user
114 # Should error when a bogus user is specified
115 session_for(create(:user))
116 get make_friend_path(:display_name => "No Such User")
117 assert_response :not_found
118 assert_template :no_such_user
121 def test_remove_friend
122 # Get users to work with
124 friend = create(:user)
125 create(:friendship, :befriender => user, :befriendee => friend)
127 # Check that the users are friends
128 assert Friendship.find_by(:befriender => user, :befriendee => friend)
130 # When not logged in a GET should ask us to login
131 get remove_friend_path(friend)
132 assert_redirected_to login_path(:referer => remove_friend_path(:display_name => friend.display_name))
134 # When not logged in a POST should error
135 post remove_friend_path, :params => { :display_name => friend.display_name }
136 assert_response :forbidden
137 assert Friendship.find_by(:befriender => user, :befriendee => friend)
141 # When logged in a GET should get a confirmation page
142 get remove_friend_path(friend)
143 assert_response :success
144 assert_template :remove_friend
145 assert_select "form" do
146 assert_select "input[type='hidden'][name='referer']", 0
147 assert_select "input[type='submit']", 1
149 assert Friendship.find_by(:befriender => user, :befriendee => friend)
151 # When logged in a POST should remove the friendship
152 post remove_friend_path(friend)
153 assert_redirected_to user_path(friend)
154 assert_match(/was removed from your friends/, flash[:notice])
155 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
157 # A second POST should report that the friendship does not exist
158 post remove_friend_path(friend)
159 assert_redirected_to user_path(friend)
160 assert_match(/is not one of your friends/, flash[:error])
161 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
164 def test_remove_friend_with_referer
165 # Get users to work with
167 friend = create(:user)
168 create(:friendship, :befriender => user, :befriendee => friend)
171 # Check that the users are friends
172 assert Friendship.find_by(:befriender => user, :befriendee => friend)
174 # The GET should preserve any referer
175 get remove_friend_path(friend), :params => { :referer => "/test" }
176 assert_response :success
177 assert_template :remove_friend
178 assert_select "form" do
179 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
180 assert_select "input[type='submit']", 1
182 assert Friendship.find_by(:befriender => user, :befriendee => friend)
184 # When logged in a POST should remove the friendship and refer
185 post remove_friend_path(friend), :params => { :referer => "/test" }
186 assert_redirected_to "/test"
187 assert_match(/was removed from your friends/, flash[:notice])
188 assert_nil Friendship.find_by(:befriender => user, :befriendee => friend)
191 def test_remove_friend_unknown_user
192 # Should error when a bogus user is specified
193 session_for(create(:user))
194 get remove_friend_path(:display_name => "No Such User")
195 assert_response :not_found
196 assert_template :no_such_user