2012-03-04 14:26:05 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-12-01 20:31:59 +00:00
|
|
|
class FakesController < ApplicationController
|
2012-03-04 14:26:05 +00:00
|
|
|
|
|
|
|
def foo
|
|
|
|
raise Unauthorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def bar
|
2012-03-06 20:27:17 +00:00
|
|
|
raise Forbidden
|
2012-03-04 14:26:05 +00:00
|
|
|
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
|
2012-03-04 14:26:05 +00:00
|
|
|
end
|
|
|
|
|
2012-12-01 20:31:59 +00:00
|
|
|
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
|
2012-03-04 14:26:05 +00:00
|
|
|
|
|
|
|
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) }
|
2012-03-04 14:26:05 +00:00
|
|
|
|
|
|
|
it "response with 401" do
|
|
|
|
get :foo
|
|
|
|
|
|
|
|
response.status.should == 401
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when typical request" do
|
|
|
|
|
|
|
|
it "redirects to login_path" do
|
2012-03-06 20:29:07 +00:00
|
|
|
@controller.should_receive(:store_location)
|
|
|
|
|
2012-03-04 14:26:05 +00:00
|
|
|
get :foo
|
|
|
|
|
2012-07-29 17:43:03 +00:00
|
|
|
flash[:notice].should == "Please login first"
|
2012-03-04 14:26:05 +00:00
|
|
|
should redirect_to(login_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-06 20:27:17 +00:00
|
|
|
context "when action raise forbidden" do
|
2012-03-04 14:26:05 +00:00
|
|
|
context "when xhr" do
|
2012-03-08 14:00:10 +00:00
|
|
|
before { request.stub(:xhr?).and_return(true) }
|
2012-03-04 14:26:05 +00:00
|
|
|
|
|
|
|
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"
|
2012-03-04 14:26:05 +00:00
|
|
|
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
|
2012-12-01 20:31:59 +00:00
|
|
|
get :store
|
2012-03-06 20:28:32 +00:00
|
|
|
get :retrieve
|
2012-12-01 20:31:59 +00:00
|
|
|
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
|
2013-08-08 09:44:37 +00:00
|
|
|
@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
|
2012-03-04 14:26:05 +00:00
|
|
|
end
|