I feel like this should be a simple question, yet I’ve struggled to find the answer. I have set up devise for authentication in my Rails project, and it’s working great. I’ve also customized the password validation and the login requirements. Specifically, one should be able to login with either their username or email, and the email should not be case sensitive.
How to I test this in my model specs? Specifically testing:
- Login with email (all lower) and password is valid
- Login with email (all upper) and password is valid
- Login with username and password is valid
- Login with username (jumbled case) and password is invalid
Basically, I just need one function that takes in the login details and tell me whether or not devise will authenticate it. But I can’t find such a function in any examples or any way to construct such a function in the devise documentation.
I am confident that it is actually working, and CAN test it in my request specs, but as it is defined in the model it feels like their ought to be a model test as well.
The only devise testing I’ve regularly found is in the controller, which doesn’t help as it just automatically signs in the user without requiring the login details.
Well, there are two distinct components here:
1) Finding a user
2) Validating the password for the user
Finding a user is handled by
find_for_database_authentication(info on having username and email handled by “login”)The validating of a password is handled by the
valid_password?method (info)So, you’d want to break this test up into: