3 class FollowsControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/username/follow", :method => :get },
9 { :controller => "follows", :action => "show", :display_name => "username" }
12 { :path => "/user/username/follow", :method => :post },
13 { :controller => "follows", :action => "create", :display_name => "username" }
16 { :path => "/user/username/follow", :method => :delete },
17 { :controller => "follows", :action => "destroy", :display_name => "username" }
22 # Get users to work with
24 follow = create(:user)
26 # Check that the users aren't already friends
27 assert_nil Follow.find_by(:follower => user, :following => follow)
29 # When not logged in a GET should ask us to login
30 get follow_path(follow)
31 assert_redirected_to login_path(:referer => follow_path(follow))
33 # When not logged in a POST should error
34 post follow_path(follow)
35 assert_response :forbidden
36 assert_nil Follow.find_by(:follower => user, :following => follow)
40 # When logged in a GET should get a confirmation page
41 get follow_path(follow)
42 assert_response :success
44 assert_select "a[href*='test']", 0
45 assert_nil Follow.find_by(:follower => user, :following => follow)
47 # When logged in a POST should add the follow
48 assert_difference "ActionMailer::Base.deliveries.size", 1 do
49 perform_enqueued_jobs do
50 post follow_path(follow)
53 assert_redirected_to user_path(follow)
54 assert_match(/You are now following/, flash[:notice])
55 assert Follow.find_by(:follower => user, :following => follow)
56 email = ActionMailer::Base.deliveries.first
57 assert_equal 1, email.to.count
58 assert_equal follow.email, email.to.first
59 ActionMailer::Base.deliveries.clear
61 # A second POST should report that the follow already exists
62 assert_no_difference "ActionMailer::Base.deliveries.size" do
63 perform_enqueued_jobs do
64 post follow_path(follow)
67 assert_redirected_to user_path(follow)
68 assert_match(/You already follow/, flash[:warning])
69 assert Follow.find_by(:follower => user, :following => follow)
72 def test_follow_with_referer
73 # Get users to work with
75 follow = create(:user)
78 # Check that the users aren't already friends
79 assert_nil Follow.find_by(:follower => user, :following => follow)
81 # The GET should preserve any referer
82 get follow_path(follow), :params => { :referer => "/test" }
83 assert_response :success
85 assert_select "a[href*='test']"
86 assert_nil Follow.find_by(:follower => user, :following => follow)
88 # When logged in a POST should add the follow and refer us
89 assert_difference "ActionMailer::Base.deliveries.size", 1 do
90 perform_enqueued_jobs do
91 post follow_path(follow), :params => { :referer => "/test" }
94 assert_redirected_to "/test"
95 assert_match(/You are now following/, flash[:notice])
96 assert Follow.find_by(:follower => user, :following => follow)
97 email = ActionMailer::Base.deliveries.first
98 assert_equal 1, email.to.count
99 assert_equal follow.email, email.to.first
100 ActionMailer::Base.deliveries.clear
103 def test_follow_unknown_user
104 # Should error when a bogus user is specified
105 session_for(create(:user))
106 get follow_path("No Such User")
107 assert_response :not_found
108 assert_template :no_such_user
112 # Get users to work with
114 follow = create(:user)
115 create(:follow, :follower => user, :following => follow)
117 # Check that the users are friends
118 assert Follow.find_by(:follower => user, :following => follow)
120 # When not logged in a GET should ask us to login
121 get follow_path(follow)
122 assert_redirected_to login_path(:referer => follow_path(follow))
124 # When not logged in a POST should error
125 delete follow_path, :params => { :display_name => follow.display_name }
126 assert_response :forbidden
127 assert Follow.find_by(:follower => user, :following => follow)
131 # When logged in a GET should get a confirmation page
132 get follow_path(follow)
133 assert_response :success
134 assert_template :show
135 assert_select "a[href*='test']", 0
136 assert Follow.find_by(:follower => user, :following => follow)
138 # When logged in a DELETE should remove the follow
139 delete follow_path(follow)
140 assert_redirected_to user_path(follow)
141 assert_match(/You successfully unfollowed/, flash[:notice])
142 assert_nil Follow.find_by(:follower => user, :following => follow)
144 # A second DELETE should report that the follow does not exist
145 delete follow_path(follow)
146 assert_redirected_to user_path(follow)
147 assert_match(/You are not following/, flash[:error])
148 assert_nil Follow.find_by(:follower => user, :following => follow)
151 def test_unfollow_with_referer
152 # Get users to work with
154 follow = create(:user)
155 create(:follow, :follower => user, :following => follow)
158 # Check that the users are friends
159 assert Follow.find_by(:follower => user, :following => follow)
161 # The GET should preserve any referer
162 get follow_path(follow), :params => { :referer => "/test" }
163 assert_response :success
164 assert_template :show
165 assert_select "a[href*='test']"
166 assert Follow.find_by(:follower => user, :following => follow)
168 # When logged in a POST should remove the follow and refer
169 delete follow_path(follow), :params => { :referer => "/test" }
170 assert_redirected_to "/test"
171 assert_match(/You successfully unfollowed/, flash[:notice])
172 assert_nil Follow.find_by(:follower => user, :following => follow)
175 def test_unfollow_unknown_user
176 # Should error when a bogus user is specified
177 session_for(create(:user))
178 get follow_path("No Such User")
179 assert_response :not_found
180 assert_template :no_such_user