+
+ # trying to subscribe when already subscribed
+ post :subscribe,
+ :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
+ :session => { :user => other_user }
+ assert_no_difference "diary_entry.subscribers.count" do
+ post :subscribe,
+ :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
+ :session => { :user => other_user }
+ end
+ end
+
+ def test_unsubscribe_success
+ user = create(:user)
+ other_user = create(:user)
+
+ diary_entry = create(:diary_entry, :user => user)
+
+ post :subscribe,
+ :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
+ :session => { :user => other_user }
+ assert_difference "diary_entry.subscribers.count", -1 do
+ post :unsubscribe,
+ :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
+ :session => { :user => other_user }
+ end
+ assert_response :redirect
+ end
+
+ def test_unsubscribe_fail
+ user = create(:user)
+ other_user = create(:user)
+
+ diary_entry = create(:diary_entry, :user => user)
+
+ # not signed in
+ assert_no_difference "diary_entry.subscribers.count" do
+ post :unsubscribe,
+ :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name }
+ end
+ assert_response :forbidden
+
+ # bad diary id
+ post :unsubscribe,
+ :params => { :id => 999111, :display_name => "username" },
+ :session => { :user => other_user }
+ assert_response :not_found
+
+ # trying to unsubscribe when not subscribed
+ assert_no_difference "diary_entry.subscribers.count" do
+ post :unsubscribe,
+ :params => { :id => diary_entry.id, :display_name => diary_entry.user.display_name },
+ :session => { :user => other_user }
+ end