views:

370

answers:

3

Is there a way to test the declarative_authorization permissions with respect?

In the documentation it has some instructions to use test unit, but I can't seem to find a way to use rspec.

A: 

You can use all assertion in rspec, so you can use it or convert it in rpsec style.

shingara
I'm sorry. I didn't get that. Can you be a little more specific?
Victor Martins
+1  A: 

I've manage to use cucumber to test the roles.

Like this:

require 'machinist/active_record'
require 'spec/blueprints.rb'

def user  
  @user ||= User.make
end

Given /^I am an Administrator$/ do
  user
  @user.assignments.delete_all
  @user.roles.delete_all
  @user.assignments << Assignment.create(:user_id => @user.id,
                   :role_id =>  Role.find_by_name("admin").id)
  @user.roles.count.should == 1
  @user.roles << Role.find_by_name("admin")  
  Authorization.current_user = @user
end
Victor Martins
+1  A: 

Yes, you can test permissions. See here under Authorization::TestHelper -- require the helpers in your spec_helper file, and include Authorization::TestHelper. This pulls in methods that allow you to specify the current user and its privileges in rspec's before method.

Another tip: to make it easier to create factory objects, declarative_authorization has a without_access_control method that allows you to bypass authorization in your before sections.

zetetic