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