Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A Rails plugin to add a should_require_login Shoulda macro to your controller tests.
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
MIT-LICENSE
README.rdoc
Rakefile
init.rb
install.rb

README.rdoc

Should Require Login macro

----

Firstly, and as is true with Rails, this plugin is opinionated software It makes certain assumptions about the design of your application and the resources which are available. Please be sure to fully read and understand this document before complaining to the author. ;)

What's it do?

This plugin adds a method to your functional tests which is creatively named should_require_login. This assertion will verify that the given list of views (with optional request parameters) requires that a user be authenticated prior to successful access.

How's it do that?

To verify that authentication is required, the assertion will check that accessing each given view, when no user is signed in, will result in a redirection to your sign in page (oh, by the way, you are required to have a named route called login_url).

The encapsulation of the assertion logic is written for Shoulda, and so using Shoulda is required. (opinionated, remember?)

While I'm mentioning these nuances, I should also mention that you're required to also have the following methods available:

login_as(user/nil)

To set the user with whom the test should login. This method should also accept nil to log in as nobody.

Example

Since this plugin will really only work with Shoulda (you read that already, right?), here you go:

   class FoosControllerTest < ActionController::TestCase

     context 'FooController' do

       should_require_login :new, :edit
       should_require_login :create,     :method => :post,   :params => {:name => 'bar'}
       should_require_login :update,     :method => :put,    :params => { :id => 1 },    :with_user => lambda { users(:foo) }
       should_require_login :destroy,    :method => :delete

     end

   end

-----

Contribution

The source for this plugin is stored in git and can be found on GitHub. Feel free to fork and modify it as you see fit. I can't guarantee that I'll like what you do, or that I'll accept your changes into my repository, but that's the joy (and pain) of git and of Open Source. ;)

Copyright information and attributions

I can't take the full credit for this plugin, merely for the pain of writing the same tests over and over and over and over again. The true inspiration for this plugin is rooted in that grief and then coming across the Coulda Shoulda Woulda post by Ken Collins. I've extended it a bit to add support for request parameters and HTTP request methods.

Copyright © 2008 Nathaniel E. Bibler, released under the MIT license.

Something went wrong with that request. Please try again.