Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

106 lines (78 sloc) 3.209 kb

contextually

Description:

Contextually is a gem for running Rails controller tests under different user contexts in a convenient way. So you can test your authorization without much effort.

Synopsis:

Somewhere sensible (like your spec helper file), define your contexts like this:

Contextually.define do
  roles :user, :visitor, :monkey

  group :user, :monkey, :as => :member
  group :visitor, :monkey, :as => :idiot

  before :user do
    controller.stub!(:current_user).and_return(:user)
  end
  before :visitor do
    controller.stub!(:current_user).and_return(nil)
  end
  before :monkey do
    controller.stub!(:current_user).and_return(:monkey)
  end

  deny_access_to :visitor do
    it("should deny access") { should redirect_to(new_session_url) }
  end
  deny_access do
    it("should deny access") { should redirect_to(root_url) }
  end
end

Then use them in your controller specs like this:

describe TestsController do
  as :user, :get => :index do
    it { should respond_with(:success) }
  end

  as :visitor, :get => :index do
    it("should deny access") { should redirect_to(new_session_url) }
  end

  as :visitor, :monkey, :user, :get => :show do
    it { should respond_with(:success) }
  end

  as :visitor, :monkey, :user, "GET /test" do
    describe :get => :show do
      it { should respond_with(:success) }
    end
  end

  as :visitor, :monkey, :user do
    describe :get => :show do
      it { should respond_with(:success) }
    end
  end

  only_as :user, :get => :index do
    it { should respond_with(:success) }
  end

  deny_access_to :monkey, :visitor, :get => :index

  deny_access_to :idiot, :get => :index

  only_as :member, :get => :foo do
    it { should respond_with(:success) }
  end
end

Install:

Contextually is hosted on Gemcutter.org only. Add this to your Rails test environment:

config.gem "contextually"

License:

(The MIT License)

Copyright © 2009 Jonas Nicklas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Jump to Line
Something went wrong with that request. Please try again.