Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:27:05+00:00 2026-06-12T18:27:05+00:00

I feel like this should be a simple question, yet I’ve struggled to find

  • 0

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:

  1. Login with email (all lower) and password is valid
  2. Login with email (all upper) and password is valid
  3. Login with username and password is valid
  4. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:27:06+00:00Added an answer on June 12, 2026 at 6:27 pm

    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:

    context "finding a user" do
      let(:user) { FactoryGirl.create(:user) }
    
      it "can find by lower email" do
        User.find_for_database_authentication( {login: user.email.downcase} ).should eq(user)
      end
    
      it "can find by upper email" do
        User.find_for_database_authentication( {login: user.email.upcase} ).should eq(user)
      end
    
      it "can find by jumbled username" do
        scrambled_username = user.username.downcase.chars.map{|c| rand() > 0.5 ? c.capitalize : c}.join
        User.find_for_database_authentication( {login: username} ).should eq(user)
      end
    end
    
    context "authenticating a user" do
      let(:user) { FactoryGirl.create(:user, password: "password123", password_confirmation: "password123") }
    
      it "will validate a correct password" do
        user.valid_password?("password123").should be_true
      end
      it "will not validate an incorrect password" do
        user.valid_password?("bad-password").should be_false
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like the answer to this question is really simple, but I really
I have a rather simple question for you.. I feel like I should have
This may be a super dumb question, that I feel like I should know
Okay, I feel like I should be able to do this, but I have
I feel like this should be a no brainer, but clearly I'm missing something...
I feel like this should be obvious to me, but for some reason I
We're using EF 5RC, code first with migrations. I feel like this should be
I feel like this is something I should already know, but I'm just not
I feel like this is a stupid question, but I can't think of a
I feel like this is quite delicate, I have various folders whith projects I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.