rails 4.2.0+devise+minitest

767 views
Skip to first unread message

Aramis Fernandes

unread,
Feb 5, 2015, 12:06:53 PM2/5/15
to plataforma...@googlegroups.com
Hello there,

I'm writing some tests to verify user registration and user sign in. I've read the documentation on github but I still haven't found the answers, so here are some questions:

1) Are there methods provided by Devise that would execute actions like logging a user in? Something like:

@user = users(:valid_user)
log_in @user


2) What is the best approach to set the encrypted_password field on the fixtures? I have something like this:

user:
  email:    "us...@host.com"
  encrypted_password: <%= BCrypt::Password.create('password') %>


And I'd like to test if I can log in with such user using a test like this:

test "should be able to login with username" do
    @user = users(:valid_user)
    post user_session_path, user:  { username:          @user.username,
                                                  password:          'password',
                                                  password_confirmation: 'password' }

    assigns(:user)
    assert user_signed_in?
end


Thanks in advance,
Aramis.

Aramis

unread,
Feb 5, 2015, 12:17:53 PM2/5/15
to plataforma...@googlegroups.com
Code is wrong, sorry.

Correction for question 2:

valid_user:
email: "us...@host.com"
username: "user_name"
encrypted_password: <%= BCrypt::Password.create('password') %>

Lucas Mazza

unread,
Feb 5, 2015, 1:51:50 PM2/5/15
to plataforma...@googlegroups.com

2): you can use <%= Devise.bcrypt User, 'password' %> in your fixtures, so the password will be encrypted with the proper streches and the pepper if necessary.

--

---
You received this message because you are subscribed to the Google Groups "Devise" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aramis

unread,
Feb 5, 2015, 3:29:29 PM2/5/15
to plataforma...@googlegroups.com
Hi Lucas,

Thanks a lot for your answers.


> 1) We have a few pages in the Wiki regarding controller testing that you
> might find useful
> https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)

I've read this one, but it seems not to cover minitest. Or maybe I'm
doing something wrong: I'm writing an integration test when this
documentation mentios controller tests only.
I want to test if a user can log into the application using devise
authentication. Should I use something different from an integration
test?
I tried many things [1], but I still can't do it.


> and
> https://github.com/plataformatec/devise/wiki/How-To:-Sign-in-from-a-controller

I'm not sure I asked correctly, but I trying to test if a page is show
for a user that is signed in. If its not, then another page should be
rendered


> 2): you can use <%= Devise.bcrypt User, 'password' %> in your fixtures, so
> the password will be encrypted with the proper streches and the pepper if
> necessary.

That is exactly the line I was looking for! :D



[1] These are the "many" things I tried:


- I tried including the line

8<----------------------------------------------
include Devise::TestHelpers
---------------------------------------------->8

into the following files (one at time):

8<----------------------------------------------
test/test_helper.rb
test/integration/users_access_test.rb
---------------------------------------------->8

- I also tried adding those 2 lines inside my setup method:

8<----------------------------------------------
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in @user
---------------------------------------------->8

Then I discovered that @request was nil. And I do not seem able to
create one request by:

8<----------------------------------------------
@request = ActionDispatch::TestRequest.new
(what should I include into helpers to make it work?)
---------------------------------------------->8


- I also tried adding:

8<----------------------------------------------
class ActionController::TestCase
include Devise::TestHelpers
end
---------------------------------------------->8

to my test/test_helper.rb file

But no success at all :(

Aramis

unread,
Feb 7, 2015, 7:12:50 AM2/7/15
to plataforma...@googlegroups.com
After some failures, I verified that the sign_in method is only
available for controler tests, but not for integration tests.
But I managed to test login interface using post <path> { info }.

Problem solved. Thanks.

Aramis

unread,
Feb 9, 2015, 2:50:46 PM2/9/15
to plataforma...@googlegroups.com
Sorry, I've discovered I was wrong, the problem is not solved.
Here is my code for the integration test, I'd like to test if
redirections are working.

# test/integration/user_login_test.rb
require 'test_helper'

class UserLoginTest < ActionDispatch::IntegrationTest

def setup
@admin = users(:admin)
@user = users(:user)
end

test "should get index only for admin" do
# Guest
get users_path
assert_redirected_to root_path # Ok until here

# Regular user (non-admin)
login(@user) # Error here, on login method
get users_path
assert_redirected_to user_root_path

# Admin
login(@admin)
get users_path
assert_response :success
end

private

def login(u)
open_session do |sess|
sess.https!
sess.post user_session_path, username: u.username, password: 'password'
assert_equal user_root_path, sess.path ## -> Error on this line [1]
sess.https!(false)
end
end
end



On test log, I get this message:

[1] FAIL["test_should_get_index_only_for_admin", UserLoginTest, 1.679411424]
test_should_get_index_only_for_admin#UserLoginTest (1.68s)
Expected: "/profile"
Actual: "/users/sign_in"



I'm assuming Devise is not allowing that login to be performed. Is
this correct? In that case, how could I sign a user in inside an
integration test?

Thanks in advance.

--
Aramis.

Katarzyna Siwek

unread,
Jun 25, 2015, 10:40:53 AM6/25/15
to plataforma...@googlegroups.com
Did you figure it out? I am in the same place you were before. I am about to check if I can adjust code [https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs] to make it work even if only in controllers.

Aramis

unread,
Jun 25, 2015, 3:02:37 PM6/25/15
to plataforma...@googlegroups.com
Hi Katarzyna,

> Did you figure it out? I am in the same place you were before. I am about to
> check if I can adjust code
> [https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs]
> to make it work even if only in controllers.

I couldn't make it work on integration tests, so I had to split
authentication and other functionalities across some files.
But yes, the way is to mock an authentication. Unfortunately it has
been a while since I stopped working on that code. And by that time I
didn't tried mocking any further.

Sorry I can't help you any more than this.


Best Regards,
Aramis.
Reply all
Reply to author
Forward
0 new messages