Dirk Breuer
2006-Feb-16 10:28 UTC
[Rails] Problems to test controllers which require login
Hi there,
I have a quite stupid problem with my testing environment. I''m trying
to
test my controllers but every controller with a login rewuirement will
fail (because there is no logged in user in the session). I realized the
conceptual problem behind this, but I''m not able to solve it.
What I have done so far:
* Login in the test class as described in the Agile Rails Book. This
works perfect for the LoginControllerTest (even for sites, that need
logged in users).
* Extracted the login functionallity in the test_helper.rb file (again
as described in the book). But unfortunatly this works only for the
LoginControllerTest. In every other Controller test I got an error while
performing this method. (I tried this with the original sources from the
book, and it doesn''t worked, too).
* The last thing I''ve tried was to send the session information every
time I request a controller action that requires an authorized user (I
know this is ugly but was my last hope). My code looks something like
this:
get :index, :session => {:user => User.find(1)}
Again this doesn''t worked out.
Now this list is all I can ask for help. If anybody could help me out
with this problem.
thanks a lot.
cheers
Dirk
--
Posted via http://www.ruby-forum.com/.
Just throw this in your test''s setup method def setup ... @session = @request.session @session[:user] = users(:first) end Pat On 2/16/06, Dirk Breuer <dirk.breuer@gmail.com> wrote:> Hi there, > > I have a quite stupid problem with my testing environment. I''m trying to > test my controllers but every controller with a login rewuirement will > fail (because there is no logged in user in the session). I realized the > conceptual problem behind this, but I''m not able to solve it. > > What I have done so far: > > * Login in the test class as described in the Agile Rails Book. This > works perfect for the LoginControllerTest (even for sites, that need > logged in users). > * Extracted the login functionallity in the test_helper.rb file (again > as described in the book). But unfortunatly this works only for the > LoginControllerTest. In every other Controller test I got an error while > performing this method. (I tried this with the original sources from the > book, and it doesn''t worked, too). > * The last thing I''ve tried was to send the session information every > time I request a controller action that requires an authorized user (I > know this is ugly but was my last hope). My code looks something like > this: > > get :index, :session => {:user => User.find(1)} > > Again this doesn''t worked out. > > > Now this list is all I can ask for help. If anybody could help me out > with this problem. > > > thanks a lot. > > cheers > > Dirk > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Dirk Breuer
2006-Feb-16 10:46 UTC
[Rails] Re: Problems to test controllers which require login
Thanks for the fast response Pat. Your adivce works perfectly! Thanks a lot Dirk -- Posted via http://www.ruby-forum.com/.
Tom Mornini
2006-Feb-16 16:21 UTC
[Rails] Problems to test controllers which require login
get and post take up to 3 arguments:
action, parameters = nil, session = nil, flash = nil
so
get :action_that_requires_login, {}, { :user_id => 1 }
will get the page, with no parameters, and a session where session
[:user_id] == 1
This allow a lot more flexibility in testing...for instance testing
what happens
if a user that isn''t logged in tries to access an action.
--
-- Tom Mornini
On Feb 16, 2006, at 2:28 AM, Dirk Breuer wrote:
> Hi there,
>
> I have a quite stupid problem with my testing environment. I''m
> trying to
> test my controllers but every controller with a login rewuirement will
> fail (because there is no logged in user in the session). I
> realized the
> conceptual problem behind this, but I''m not able to solve it.
>
> What I have done so far:
>
> * Login in the test class as described in the Agile Rails Book. This
> works perfect for the LoginControllerTest (even for sites, that need
> logged in users).
> * Extracted the login functionallity in the test_helper.rb file (again
> as described in the book). But unfortunatly this works only for the
> LoginControllerTest. In every other Controller test I got an error
> while
> performing this method. (I tried this with the original sources
> from the
> book, and it doesn''t worked, too).
> * The last thing I''ve tried was to send the session information
every
> time I request a controller action that requires an authorized user (I
> know this is ugly but was my last hope). My code looks something like
> this:
>
> get :index, :session => {:user => User.find(1)}
>
> Again this doesn''t worked out.
>
>
> Now this list is all I can ask for help. If anybody could help me out
> with this problem.
>
>
> thanks a lot.
>
> cheers
>
> Dirk
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
Enrico Teotti
2006-Feb-17 11:11 UTC
[Rails] Problems to test controllers which require login
I''m using the salted login generator with happyness, exept now
I''d
like to test my controllers behind the salted login. I''ve read on the
mailing list about this issue, and I''ve used the
pass_the_session_to_the_action but without success.
This is a line of the controller tested, I''m passing a param
(level_id) and then I pass the user session as in the salted login
generator tests
get :show_level, {:level_id => 1 }, { :user => @bob }
it gets redirected to the userController... so... this is definitly
bad I think it means the before_filter hasn''t recognized me.
@bob is coming from the :users fixture correctly included in the
current test suite.
?
Thanks,
Enrico
--
"The only thing necessary for the triumph of evil
is for good men to do nothing"
Edmund Burke