I'm trying to restrict a few controllers in my app to require a login before any action. I know how to implement it, but not how to write nice tests in rspec for it.
For example, if I want to restrict every action of my users controller to require login, I could have a test like:
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the index page" do
before { visit users_path }
it { should have_selector('title', text: 'Log In') }
end
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_selector('title', text: 'Log In') }
end
describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(login_path) }
end
describe "submitting a DELETE request to the Users#destroy action" do
before { delete user_path(user) }
specify { response.should redirect_to(root_path) }
end
....etc.....
end
end
Do I need to specify all 7 of my restful routes for every controller I want to test? Seems pretty inefficient. Is there some way to say "before visit any users routes response should redirect to login_path"?