"Permission denied" in rspec but working fine in development

671 views
Skip to first unread message

udo.eisenbarth

unread,
Feb 26, 2012, 7:35:25 AM2/26/12
to declarative_authorization
Hi,

I tried to test my application using rspec but ran in some trouble
with "Permission denied" errors although the resource should have been
accessible even with the :guest role. In the real application (in
development env) everything works as expected.

The test logfile shows the error message:

Processing by PagesController#start as HTML
Permission denied: No matching rules found for start for
#<Authorization::AnonymousUser:0xb7739a8 @role_symbols=[:guest]>
(roles [:guest], privileges [:start], context :pages).
Rendered text template (0.0ms)
Completed 403 Forbidden in 333ms (Views: 325.4ms | ActiveRecord:
0.0ms)

Of course the test failes.... If I uncomment the
Authorization.ignore_access_control(true) then everything works of
course....


BTW: I'm using Rails 3.1.3, declarative_authorization 0.5.5, rspec
2.8.0


Here is the (somewhat abbreviated) code:
-------
pages_controller_spec.rb

require 'spec_helper'

describe PagesController do
render_views
before (:each) do
activate_authlogic
# Authorization.ignore_access_control(true)
end
describe "GET 'start'" do
it "should be successful" do
get :start
response.should be_success
end
end
-------
pages_controller.rb

class PagesController < ApplicationController
filter_access_to :all

def start
@pageTitle=""
end

-------
authorization_rules.rb

authorization do
role :admin do
has_omnipotence
includes :operator
end
role :operator do
has_permission_on :experiments, :to => :manage
has_permission_on :statistics, :to => [:overview, :calendar]
includes :guest
end
role :guest do
has_permission_on :pages, :to => [:start,:about,:changelog]
end
end


Best regards,

Udo

Steffen Bartsch

unread,
Mar 5, 2012, 4:53:24 PM3/5/12
to declarative_...@googlegroups.com
Am 26.02.2012 13:35, schrieb udo.eisenbarth:
> The test logfile shows the error message:
>
> Processing by PagesController#start as HTML
> Permission denied: No matching rules found for start for
> #<Authorization::AnonymousUser:0xb7739a8 @role_symbols=[:guest]>
> (roles [:guest], privileges [:start], context :pages).
> Rendered text template (0.0ms)
> Completed 403 Forbidden in 333ms (Views: 325.4ms | ActiveRecord:
> 0.0ms)
>
> Of course the test failes.... If I uncomment the
> Authorization.ignore_access_control(true) then everything works of
> course....
>
>...

> role :guest do
> has_permission_on :pages, :to => [:start,:about,:changelog]
> end
> end

This is really strange. From the error log and the rules I'd say it
should work. Are you sure these rules are actually used in the tests?
There needs to be something else fishy here...

Steffen

udo.eisenbarth

unread,
Mar 8, 2012, 10:21:08 AM3/8/12
to declarative_...@googlegroups.com
Hi,

After a long search I found out what went wrong although I still don't fully understand it.

If I start the unit test from the main directory as "rspec spec/controllers/pages_controller.spec.rb" everything works fine. If I first go to the directory <App>/spec/controllers/ and start it as
"rspec pages_controller.spec.rb" the test fails. To me it looks like that in this case decl_authorization cannot find the rules file. Could this be the reason? Since there is no error message this is really hard to find out which cost me almost two days....Well that's life :-)

Udo

David Morton

unread,
Jun 23, 2012, 5:52:51 PM6/23/12
to declarative_...@googlegroups.com
ARG, I just posted a similar question.   I see this too.  (Now that I have tried backing up a directory)   I've wasted a day trying to track this down and never went back to Rails.root.

It appears that when authorization.rb is loaded, Rails.root has not yet been set.

Possibly related to https://rails.lighthouseapp.com/projects/8994/tickets/4016-railsroot-returns-nil-before-app-initialization-silently-breaking-some-gems
Reply all
Reply to author
Forward
0 new messages