2012-03-04 14:26:05 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
class FakeController < ApplicationController
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe FakeController do
|
|
|
|
|
|
|
|
describe "#ensure_authenticated!" do
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "action raise unauthorized" do
|
|
|
|
|
|
|
|
context "when xhr" do
|
|
|
|
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
|
|
|
|
get :foo
|
|
|
|
|
|
|
|
flash[:notice].should == "Please login"
|
|
|
|
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
|
|
|
|
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"
|
2012-03-04 14:26:05 +00:00
|
|
|
should redirect_to(root_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|