Forum: Ruby on Rails Testing Problems - Acts as authenticated

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1a09a03a0f5091eb071f177a198f8506?d=identicon&s=25 Rails Nut (railsnut)
on 2005-11-28 12:25
Has anyone successfully run the unit and functional tests that are
pre-generated with the 'acts as authenticated' plugin ?

I'm having huge amounts of problems and it's not helped by the fact that
I'm a n00b to Rails and can't tell if I've made some fundamental error
setting things up.

Sequence of events was

1) Generate users scaffold
2) Generate authenticate controller using plugin
3) alter application.rb so that almost everything requires a logon
4) Run rake

I quickly discovered that there was no way that
'test_should_not_rehash_password' would run successfully in the unit
tests, so I abandoned that (commented it out).

On a re-run I found so many errors in other parts of of the app (it's a
team project) that I had to run just the 'users' functional test on its
own.

Virtually every test failed.

I thought my mods to application.rb might be the root of the problem,
but commenting them out got me no further, and replicating that
functionality within the 'setup' method of the functional test didn't
help either. I also took a few guesses at things that might be missing
from the artificial test environment (I used a few includes in other
words) but again no luck.

I think there is probably something simple I'm missing that will make
these tests run out of the box, but I'm at a loss to know what it is.

Can anyone help ?

Cheers,   Andy
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 james.adam (Guest)
on 2005-11-28 12:38
(Received via mailing list)
Can you post up some of the actual errors you got? They will help
track the issue down...

- james
1a09a03a0f5091eb071f177a198f8506?d=identicon&s=25 Rails Nut (railsnut)
on 2005-11-28 13:01
Okay ... here goes ... I know that the redirects are happening because
of my changes to application.rb , but I don't know how to modify the
test or test environment to replicate a logged on user.

-------------------------------------

D:\svn\DontBinIt\Application>ruby
test\functional\users_controller_test.rb
D:/svn/DontBinIt/Application/config/../app/models/item.rb:38: warning:
found = in conditional, should be ==
Loaded suite test/functional/users_controller_test
Started
FFFFFFFF
Finished in 0.172 seconds.

  1) Failure:
test_create(UsersControllerTest)
[test/functional/users_controller_test.rb:70]:
response is not a redirection to all of the options supplied
(redirection is <{:
controller=>"/authenticate", :action=>"login"}>)

  2) Failure:
test_destroy(UsersControllerTest)
[test/functional/users_controller_test.rb:96]:

response is not a redirection to all of the options supplied
(redirection is <{:
controller=>"/authenticate", :action=>"login"}>)

  3) Failure:
test_edit(UsersControllerTest)
[test/functional/users_controller_test.rb:78]:
Expected response to be a <:success>, but was <302>

  4) Failure:
test_index(UsersControllerTest)
[test/functional/users_controller_test.rb:32]:
Expected response to be a <:success>, but was <302>

  5) Failure:
test_list(UsersControllerTest)
[test/functional/users_controller_test.rb:39]:
Expected response to be a <:success>, but was <302>

  6) Failure:
test_new(UsersControllerTest)
[test/functional/users_controller_test.rb:58]:
Expected response to be a <:success>, but was <302>

  7) Failure:
test_show(UsersControllerTest)
[test/functional/users_controller_test.rb:48]:
Expected response to be a <:success>, but was <302>

  8) Failure:
test_update(UsersControllerTest)
[test/functional/users_controller_test.rb:88]:
response is not a redirection to all of the options supplied
(redirection is <{:
controller=>"/authenticate", :action=>"login"}>)

8 tests, 15 assertions, 8 failures, 0 errors
1a09a03a0f5091eb071f177a198f8506?d=identicon&s=25 Rails Nut (railsnut)
on 2005-11-28 13:08
And here are the mods to application.rb ...

  include AuthenticatedSystem
  before_filter :login_required
  before_filter :get_login_if_any

  def get_login_if_any
    current_user
  end
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 technoweenie (Guest)
on 2005-11-28 13:47
(Received via mailing list)
On 11/28/05, rails nut <rails_nut@hotpop.com> wrote:
> And here are the mods to application.rb ...
>
>   include AuthenticatedSystem
>   before_filter :login_required
>   before_filter :get_login_if_any
>
>   def get_login_if_any
>     current_user
>   end

By putting those lines in your controller, all your actions will
require a login for the tests.  The generator generates a test helper
for this purpose in lib/authenticated_test_helper.rb:

http://techno-weenie.net/svn/projects/plugins/acts...

Be sure to include the helper in test/test_helper.rb

class Test::Unit::TestCase
  include AuthenticatedTestHelper

  self.use_transactional_fixtures = true
  self.use_instantiated_fixtures  = false

Now in your test:

  login_as :bob # loads users(:bob) into the session

I apologize, I thought for sure I had better docs with this thing.
I'll be sure to clarify this part.  Do let me know if there are other
issues I missed :)

--
rick
http://techno-weenie.net
1a09a03a0f5091eb071f177a198f8506?d=identicon&s=25 Rails Nut (railsnut)
on 2005-11-29 12:01
technoweenie wrote:
>
> By putting those lines in your controller, ....


Thanks for that... looks like just what I need.

Busy with something else right now, but I'll be giving that a shot soon.

Cheers,   Andy
1a09a03a0f5091eb071f177a198f8506?d=identicon&s=25 Rails Nut (railsnut)
on 2005-12-01 19:16
> technoweenie wrote:
>>
>> By putting those lines in your controller, ....
>
>

Yep, that DOES seem to have solved most of the problems.  One or two
remain, though. Theres this one from the functional tests (with
associated code)  ...

-----------

  1) Failure:
test_create(UsersControllerTest)
[test/functional/users_controller_test.rb:101]:

Expected response to be a <:redirect>, but was <200>

-----------

  def test_create
	  login_as(:quentin)
    num_users = User.count

    post :create, :user => {}

    assert_response :redirect
    assert_redirected_to :action => 'list'

    assert_equal num_users + 1, User.count
  end

-----------

I'm wondering if the test is correct in this case ... it looks as though
the test is creating an 'null' user (no name etc) and (I guess) the
create action is  rendering the form with an error message.

Any thoughts ?

Andy
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 technoweenie (Guest)
on 2005-12-01 23:07
(Received via mailing list)
>   end
Yup, I copied that from the scaffolding generator.  Obviously the
scaffolding generator has no clue how your models work.  Perhaps I
could the required login feilds by default though for
acts_as_authenticated.  Good catch.  I'm used to filling those fields
manually :)

In your case, just make the tweak yourself.  No sense on waiting for
me to fix it so you can overwrite any changes you made.

--
rick
http://techno-weenie.net
This topic is locked and can not be replied to.