Skip to content

Commit

Permalink
Merge pull request #4 from hostedgpt/feat/secure-password
Browse files Browse the repository at this point in the history
feat: secure password
  • Loading branch information
al3rez authored Dec 19, 2023
2 parents 389873c + 2a2caad commit c277c87
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ gem "redis", ">= 4.0.1"
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[windows jruby]
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ GEM
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bcrypt (3.1.20)
bigdecimal (3.1.4)
bindex (0.8.1)
bootsnap (1.17.0)
Expand Down Expand Up @@ -311,6 +312,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
byebug
capybara
Expand Down
1 change: 1 addition & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Person < ApplicationRecord
delegated_type :personable, types: %w[User Tombstone]
validates_associated :personable
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class User < ApplicationRecord
include Personable

has_secure_password

validates :password, presence: true
validates :password_confirmation, presence: true
end
2 changes: 1 addition & 1 deletion db/migrate/20231219014005_create_users.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateUsers < ActiveRecord::Migration[7.1]
def change
create_table :users do |t|
t.string :password
t.string :password_digest
t.datetime :registered_at, default: -> { "CURRENT_TIMESTAMP" }
end
end
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
end

create_table "users", force: :cascade do |t|
t.string "password"
t.string "password_digest"
t.datetime "registered_at", default: -> { "CURRENT_TIMESTAMP" }
end

Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/people.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

one:
email: MyString
type:

two:
email: MyString
type:
4 changes: 2 additions & 2 deletions test/fixtures/tombstones.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

one:
person: one
deleted_at: 2023-12-19 08:40:05
erected_at: 2023-12-19 08:40:05

two:
person: two
deleted_at: 2023-12-19 08:40:05
erected_at: 2023-12-19 08:40:05
20 changes: 17 additions & 3 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
require "test_helper"

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "should not save user without password" do
user = User.new
person = Person.new(email: "example@gmail.com", personable: user)
assert_raises(ActiveRecord::RecordInvalid) { person.save! }
end
j
test "should not save user without password confirmation" do
user = User.new(password: "password")
person = Person.new(email: "example@gmail.com", personable: user)
assert_raises(ActiveRecord::RecordInvalid) { person.save! }
end

test "should save user with password" do
user = User.new(password: "password", password_confirmation: "password")
person = Person.new(email: "exmaple@gmail.com", personable: user)
assert person.save!
end
end
3 changes: 0 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)

# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all

# Add more helper methods to be used by all tests here...
end
end

0 comments on commit c277c87

Please sign in to comment.