Add Forbiden Exception and refactor existing actions
parent
4f47f0a7fc
commit
5d52be2bae
@ -0,0 +1,69 @@
|
||||
require 'spec_helper'
|
||||
|
||||
class FakeController < ApplicationController
|
||||
|
||||
def foo
|
||||
raise Unauthorized
|
||||
end
|
||||
|
||||
def bar
|
||||
raise Forbiden
|
||||
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
|
||||
|
||||
context "when action raise forbiden" do
|
||||
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
|
||||
|
||||
flash[:alert].should == "This action is forbiden"
|
||||
should redirect_to(root_path)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue