Rails: Shoulda Matchers and validates username on update - ruby-on-rails

Model Validation:
validates :username, uniqueness: true, format: { with: /\A[a-zA-Z0-9_.-#]+\Z/i, message: "must contain only letters, numbers or _*-#" }, on: :update, :if => :username_changed?
Rspec:
require 'spec_helper'
describe User, "references" do
it { should have_and_belong_to_many(:roles) }
it { should belong_to(:account_type) }
it { should belong_to(:primary_sport).class_name("Sport") }
it { should belong_to(:school) }
it { should belong_to(:city) }
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:email) }
it { should allow_value("test#test.com").for(:email) }
it { should_not allow_value("test.com").for(:email) }
describe "validation of username", focus: true do
before(:each) do
#user = User.new(email: Faker::Internet.email, password: "password", password_confirmation: "password", username: "test123", agreed_to_age_requirements: true)
end
it "should be valid" do
#user.save
#user.should be_valid
end
it "should not be valid with incorrect characters in username" do
#user.username = "test###!!!"
#user.should_not be_valid
end
end
end
FactoryGirl:
FactoryGirl.define do
factory :user do
email Faker::Internet.email
password "password"
password_confirmation "password"
agreed_to_age_requirements true
username Faker::Internet.user_name
end
end
I am basically just trying to test against the custom validation for the uniqueness of and with the specified format of the username

Related

Rails Tutorial Authenticate Issue

I'm following the Rails Tutorial Modeling Users Chapter: http://www.railstutorial.org/book/modeling_users#cha-modeling_users.
My user.rb looks like:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
has_secure_password
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
end
and my user model spec looks like:
describe User, :type => :model do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
... (other methods are here)
describe "when password is not present" do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
it { should_not be_valid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by(email: #user.email) }
describe "with valid password" do
it { should eq found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
Which I'm pretty sure is exactly a duplication what the Rails Tutorial code is, but I'm getting the following failed test errors:
rspec ./spec/models/user_spec.rb:83 # User when password is not present should not be valid
rspec ./spec/models/user_spec.rb:108 # User return value of authenticate method with invalid password should be false
I checked this out by looking at the Ruby on Rails Tutorial Book's source code (Rails 4) at GitHub: spec/models/user_spec.rb. Based on the code there, it looks like your passwords are currently of acceptable type and that's why your test is failing. I mean your passwords are good. foobar is a valid password. Below an empty string is passed to the User model validation.
describe "when password is not present" do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
The second one I'm not sure, but would it help if you tried the Rails 3 spec/models/user_spec.rbrelated code for the same test:
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
It looks slightly different but it's testing the same thing. This is just suggestion, because I'm not sure what's going wrong.
It looks like a couple of methods are missing (if you're trying to match the tutorial exactly) Listing 6.25 & 6.28:
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end

Validation failed: Password digest can't be blank

I have seen other threads with similar problems but they don't seem to fix my problem. I am following along with with the tutorial on http://ruby.railstutorial.org/ to get a basic user sign up and login or a different project then that sample one.
Basically when I run rspec I keep getting this error. I have run all my migrations and test prepare. Really stumped with this.
1) User should reject duplicate email addresses
Failure/Error: User.create!(#attr)
ActiveRecord::RecordInvalid:
Validation failed: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
# ./spec/models/user_spec.rb:60:in `block (2 levels) in <top (required)>'
2) User should accept valid email addresses
Failure/Error: valid_email_user.should be_valid end
expected #<User id: nil, name: "Example User", email: "user#foo.com", created_at: nil, updated_at: nil, password_digest: nil> to be valid, but got errors: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
# ./spec/models/user_spec.rb:48:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:46:in `each'
# ./spec/models/user_spec.rb:46:in `block (2 levels) in <top (required)>'
3) User should create a new instance given valid attributes
Failure/Error: User.create!(#attr)
ActiveRecord::RecordInvalid:
Validation failed: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
# ./spec/models/user_spec.rb:25:in `block (2 levels) in <top (required)>'
4) User should reject email addresses identical up to case
Failure/Error: User.create!(#attr.merge(:email => upcased_email))
ActiveRecord::RecordInvalid:
Validation failed: Password digest can't be blank, Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
# ./spec/models/user_spec.rb:67:in `block (2 levels) in <top (required)>'
Here is my user_spec
require 'spec_helper'
describe User do
before(:each) do
#attr = { :name => "Example User", :email => "user#example.com" }
end
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
it "should create a new instance given valid attributes" do
User.create!(#attr)
end
it "should require a name" do
no_name_user = User.new(#attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email address" do
no_email_user = User.new(#attr.merge(:email => ""))
no_email_user.should_not be_valid
end
it "should reject names that are too long" do
long_name = "a" * 51
long_name_user = User.new(#attr.merge(:name => long_name))
long_name_user.should_not be_valid
end
it "should accept valid email addresses" do
addresses = %w[user#foo.com THE_USER#foo.bar.org first.last#foo.jp]
addresses.each do |address|
valid_email_user = User.new(#attr.merge(:email => address))
valid_email_user.should be_valid end
end
it "should reject invalid email addresses" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.]
addresses.each do |address|
invalid_email_user = User.new(#attr.merge(:email => address))
invalid_email_user.should_not be_valid end
end
it "should reject duplicate email addresses" do
# Put a user with given email address into the database.
User.create!(#attr)
user_with_duplicate_email = User.new(#attr)
user_with_duplicate_email.should_not be_valid
end
it "should reject email addresses identical up to case" do
upcased_email = #attr[:email].upcase
User.create!(#attr.merge(:email => upcased_email))
user_with_duplicate_email = User.new(#attr)
user_with_duplicate_email.should_not be_valid
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by_email(#user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
here is my user model
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
your validation is broken too, a password and password_confirmation should be required if the user is created but not thereafter
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
you will run into problems if you try to update the user later on, say changing its Name, because then it will check for both password and password_confirmation but the user already has a password set stored usually in the password_digist field encrypted by bcrypt but not stored along with password and confirmation in plain text.
I recommend this only to be validated for new records:
validates :password, presence: true, length: { minimum: 6 } if new?
validates :password_confirmation, presence: true if new?
In all the failing tests, you use #attr = { :name => "Example User", :email => "user#example.com" } to create a new user. But to create a new user, you MUST set password and password_confirmation. Add these attributes to the #attr hash. The password_digest field should then be set automatically and it should be possible to save the user.
delete this line
attr_accessible :name, :email, :password, :password_confirmation
It should work now.
This preventes the method rails method password= from being called. (also see secure_password.rb)

"undefined local variable or method" in the rails console when it encounters methods provided by has_secure_password

My problem is when I try to use the rails console to update a user email I get NoMethodError: undefined method 'update_attribute'
While working thru the problem I tried
current_user = user.authrnticate(foobar) and got back NameError: undefined local variable or method 'user' for main:Object
Which leads me to believe the "rails c" isn't using or seeing the user.rb file corectly.
I have tried restarting the rails server before and after performing [rake db:test:prepare] and [rake db:migrate].
My [rails c] will execute methods not defined in user.rb.
Rspec is running without a problem. All tests in the user_spec are passing. and when I comment out has_secure_password all tests fail.
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
describe "when name is to long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
#user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
#user.should be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation " do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
describe "when password is too short" do
before { #user.password = #user.password_confirmation = "a" * 5}
it { should_not be_valid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by_email(#user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
My user.rb is
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = user.email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: {minimum: 6 }
validates :password_confirmation, presence: true
end
and here is the entire project on github
I have looked exhaustively for a solution including a throe read of the ActiveRecord documentation and I'm stumped.
Any help is greatly appreciated.
Many of the helper methods are not available to be called in the console.
you can often access them by calling them via helper.helpername, but this doesn't always work
How are you getting the User from the database?
For example user = User.find_by_name('name')
This error:
NameError: undefined local variable or method 'user' for main:Object
Says that you didn't instantiate a variable user.
Did you fetch User from a database in to user variable?
Your irb session should look something like this:
To check if you have a user in database:
> User.all
If you have a valid user in database named "Jon"
> user = User.find_by_name("Jon")
> current_user = user.auhenticate("valid_password") #will work
If you don't have a user in database:
> User.create(name: "Jon", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
> user = User.find_by_name("Jon")
> current_user = user.auhenticate("valid_password") #will work
RSpec uses test database but rails c on default uses development one so make sure that you have a record in your development database when you want to work on it in rails c.
rails c command is loading all your project libraries.

Failing Rspec Test with MongoID , Custom Authentication, Password Digest

i have completed the rails tutorial http://ruby.railstutorial.org/, and trying to implement the user model, but in MongoDB, using MongoID.
I have the following code, which is almost exactly the same as the tutorial, yet it's failing on one of the test cases.
class User
include Mongoid::Document
include ActiveModel::SecurePassword
attr_accessible :name, :email, :password, :password_confirmation
field :name, type: String
field :email, type: String
field :password_digest, type: String
has_secure_password
before_save { |user| user.email = email.downcase }
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :name, presence: true, length: { maximum: 50}
validates :email, presence: true, format: { with: email_regex },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 5}
validates :password_confirmation, presence: true
end
and rspec tests:
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
describe 'without a name' do
before { #user.name = "" }
it { should_not be_valid }
end
describe 'without an email' do
before { #user.email = "" }
it { should_not be_valid }
end
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
#user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
#user.should be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by(email: #user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
The failure on the spec is:
........F..........
Failures:
1) User return value of authenticate method with valid password
Failure/Error: it { should == found_user.authenticate(#user.password) }
expected: #<User _id: 50e30a678bffa61a9e000001, _type: nil, name: "Example User", email: "user#example.com", password_digest: "$2a$10$C6klW7B9ok4OIHFopy5AfuncvwNYTZqHWTbqpdd8gXmruSLlwizPO">
got: #<User _id: 50e30f158bffa6df67000001, _type: nil, name: "Example User", email: "user#example.com", password_digest: "$2a$10$mDCz9JVPXGSvpGBwbyHIsuV/k.lJ/ux1TuvgRzxSLYlIna/CdztN2"> (using ==)
Diff:
## -1,2 +1,2 ##
-#<User _id: 50e30a678bffa61a9e000001, _type: nil, name: "Example User", email: "user#example.com", password_digest: "$2a$10$C6klW7B9ok4OIHFopy5AfuncvwNYTZqHWTbqpdd8gXmruSLlwizPO">
+#<User _id: 50e30f158bffa6df67000001, _type: nil, name: "Example User", email: "user#example.com", password_digest: "$2a$10$mDCz9JVPXGSvpGBwbyHIsuV/k.lJ/ux1TuvgRzxSLYlIna/CdztN2">
# ./spec/models/user_spec.rb:89:in `block (4 levels) in <top (required)>'
Finished in 1.79 seconds
19 examples, 1 failure
Update:
Did a bit more diggig, and querying the test database it seems that the data is not being cleared before each test case.
Updat2:
I have managed to fix this by including the following in my Spec Helper:
config.before :each do
Mongoid.purge!
end
But tests are noticeably slower now! any idea how to speed it up?
The problem is calling save, which recreates the password_digest
describe "return value of authenticate method" do
#before { #user.save } #comment this line
#....
end

NoMethodError undefined method `save' for nil:NilClass

What do I need to do to fix this? I am new to ruby on rails.
Error when rspec is ran
1) remember token should have a nonblank remember token
Failure/Error: before { #user.save }
NoMethodError:
undefined method `save' for nil:NilClass
# ./spec/models/user_spec.rb:125:in `block (2 levels) in <top (required)>'
user_spec.rb
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
.
.
.
it { should respond_to(:remember_token) }
.
.
.
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by_email(#user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
describe "remember token" do
before { #user.save }
it "should have a nonblank remember token" do
subject.remember_token.should_not be_blank
end
end
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
It looks like this block of code
describe "remember token" do
is outside the block
describe User do
...
end
If you move it inside the block, then it will have the before action fire that creates the #user object (which you then save in your own before block)
the #user variable will be not found, so
you should move describe "remember token" inside ->
describe User do
//current definitions
describe "remember token" do
before { #user.save }
it "should have a nonblank remember token" do
subject.remember_token.should_not be_blank
end
end
end

Resources