Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty passwords #1

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
class User < ApplicationRecord
has_secure_password
has_secure_password validations: false

before_validation :downcase_email, on: [:create, :update]

validates :email, presence: true, uniqueness: { case_sensitive: false }, format: { with: /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/ }
validates :first_name, :last_name, presence: true
validates :password, length: { minimum: PASSWORD_MIN_LENGTH }
validates :password, length: { minimum: PASSWORD_MIN_LENGTH },
confirmation: true,
allow_nil: true

scope :admins, -> { where(admin: true) }
scope :regular, -> { where(admin: false) }
Expand All @@ -18,6 +20,10 @@ def full_name
[first_name, last_name].join(" ")
end

def authenticate(given_password)
password_digest.present? && super
end

private

def downcase_email
Expand Down
17 changes: 14 additions & 3 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "#authenticate will not authenticate user without password" do
user = FactoryBot.create(:user, password: nil, password_digest: nil)
refute user.authenticate("secret")
end

test "#authenticate will authenticate user with correct password" do
user = FactoryBot.create(:user)
assert user.authenticate("secret")
end

test "#authenticate will not authenticate when given wrong password" do
user = FactoryBot.create(:user)
refute user.authenticate("password")
end
end