public
Description: Nice user contexts in your RSpec-Rails controller specs
Homepage:
Clone URL: git://github.com/jnicklas/contextually.git
name age message
file .gitignore Fri Oct 16 13:44:34 -0700 2009 Intitial commit [jnicklas]
file History.txt Loading commit data...
file Manifest.txt Sun Oct 18 02:44:06 -0700 2009 Manifest [jnicklas]
file README.rdoc
file Rakefile
directory lib/ Sun Oct 18 03:34:32 -0700 2009 Tagged 0.1 [jnicklas]
directory script/
directory spec/
README.rdoc

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.