Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # spec/models/user/authentication.rb
- require 'spec_helper'
- describe User::Authentication do
- subject { User.new }
- describe ".authenticate" do
- it "tries to find the user" do
- User.should_receive(:find_by_username_or_email)
- User.authenticate('blah@blah.com', 'blahblah')
- end
- it "checks if the password is correct" do
- user = double(:user)
- User.stub(:find_by_username_or_email).and_return user
- user.should_receive(:correct_password?)
- User.authenticate('blah@blah.com', 'blahblah')
- end
- it "returns the found user if authenticated" do
- user = double(:user)
- User.stub(:find_by_username_or_email).and_return user
- user.stub(:correct_password?).and_return true
- User.authenticate('blah@blah.com', 'blahblah')
- end
- it "returns nil if the user doesn't exists" do
- User.stub(:find_by_username_or_email).and_return nil
- User.authenticate('blah@blah.com', 'blahblah')
- end
- it "returns nil if the password is incorrect" do
- user = double(:user)
- User.stub(:find_by_username_or_email).and_return user
- user.stub(:correct_password?).and_return false
- User.authenticate('blah@blah.com', 'blahblah')
- end
- end
- describe ".find_by_username_or_email" do
- before :all do
- @user = FactoryGirl.create :user, email: 'kramer@vulume.com', username: 'cosmo'
- end
- it "finds a user by username case insensitively" do
- User.find_by_username_or_email('cosmo').should be == @user
- User.find_by_username_or_email('COSMO').should be == @user
- end
- it "finds a user by email case insensitively" do
- User.find_by_username_or_email('kramer@vulume.com').should be == @user
- User.find_by_username_or_email('KRAMER@VULUME.com').should be == @user
- end
- it "is nil if nothing is found" do
- User.find_by_username_or_email('blahblahblah').should be_nil
- end
- it "does not query the database if a blank value is passed in" do
- method = :find_by_username_or_email
- User.should_not query_the_database.when_calling(method).with('')
- User.should_not query_the_database.when_calling(method).with(nil)
- end
- end
- describe "#correct_password" do
- it "is true if the passed in password matches the password digest" do
- subject.password = 'blah'
- subject.correct_password?('blah').should be_true
- end
- it "false if the password does not match" do
- subject.password = 'omfg'
- subject.correct_password?('notthesame').should be_false
- end
- end
- describe "#password=" do
- it "sets the password attribute" do
- subject.password = 'blah'
- subject.password.should be == 'blah'
- end
- it "sets the password digest" do
- subject.should_receive(:password_digest=)
- subject.password = 'blah'
- end
- it "does not set the password digest if the unencrypted password is blank" do
- subject.should_not_receive(:password_digest=)
- subject.password = ''
- subject.password = nil
- end
- end
- end
Add Comment
Please, Sign In to add comment