asciinema.org/spec/controllers/application_controller_spec.rb

124 lines
2.5 KiB
Ruby
Raw Normal View History

require 'spec_helper'
class FakesController < ApplicationController
def foo
raise Unauthorized
end
def bar
2012-03-06 20:27:17 +00:00
raise Forbidden
end
2012-03-06 20:28:32 +00:00
def store
store_location
render :nothing => true
end
def retrieve
@location = get_stored_location
@location_again = get_stored_location || 'NOWAI!'
render :nothing => true
end
end
describe FakesController do
before do
@orig_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
@routes.draw do
resource :fake do
get :foo
get :bar
get :store
get :retrieve
end
end
end
after do
@routes, @orig_routes = @orig_routes, nil
end
describe "#ensure_authenticated!" do
end
describe "action raise unauthorized" do
context "when xhr" do
2012-03-08 14:00:10 +00:00
before { request.stub(:xhr?).and_return(true) }
it "response with 401" do
get :foo
response.status.should == 401
end
end
context "when typical request" do
it "redirects to login_path" do
@controller.should_receive(:store_location)
get :foo
2012-07-29 17:43:03 +00:00
flash[:notice].should == "Please login first"
should redirect_to(login_path)
end
end
end
2012-03-06 20:27:17 +00:00
context "when action raise forbidden" do
context "when xhr" do
2012-03-08 14:00:10 +00:00
before { request.stub(:xhr?).and_return(true) }
it "response with 401" do
get :bar
response.status.should == 403
end
end
context "when typical request" do
it "redirects to root_path" do
get :bar
2012-03-06 20:27:17 +00:00
flash[:alert].should == "This action is forbidden"
should redirect_to(root_path)
end
end
end
2012-03-06 20:28:32 +00:00
describe '#store_location / #get_stored_location' do
it 'stores current request path to be later retrieved' do
get :store
2012-03-06 20:28:32 +00:00
get :retrieve
assigns[:location].should == '/fake/store'
2012-03-06 20:28:32 +00:00
assigns[:location_again].should == 'NOWAI!'
end
end
2012-03-06 21:03:12 +00:00
describe '#redirect_back_or_to' do
context 'when there is no stored location' do
it 'redirects to given location' do
path = double
@controller.should_receive(:redirect_to).with(path)
@controller.send(:redirect_back_or_to, path)
end
end
context 'when there is stored location' do
it 'redirects to stored location' do
stored_path = double
path = double
@controller.stub(:get_stored_location => stored_path)
2012-03-06 21:03:12 +00:00
@controller.should_receive(:redirect_to).with(stored_path)
@controller.send(:redirect_back_or_to, path)
end
end
end
end