Commit 1a2d6a19 by Quang Vinh Nguyen

Make a basic User model (including secure password)

parent 60205d57
......@@ -57,6 +57,7 @@
source 'https://rubygems.org'
gem 'rails', '5.1.4'
gem 'bcrypt', '3.1.12'
gem 'bootstrap-sass', '3.3.7'
gem 'puma', '3.9.1'
gem 'sass-rails', '5.0.6'
......
......@@ -42,6 +42,7 @@ GEM
arel (8.0.0)
autoprefixer-rails (8.5.1)
execjs
bcrypt (3.1.12)
bindex (0.5.0)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
......@@ -202,6 +203,7 @@ PLATFORMS
ruby
DEPENDENCIES
bcrypt (= 3.1.12)
bootstrap-sass (= 3.3.7)
byebug (= 9.0.6)
coffee-rails (= 4.2.2)
......
class User < ApplicationRecord
before_save { self.email.downcase! }
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, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: {minimum: 6}
has_secure_password
end
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
class AddIndexToUsersEmail < ActiveRecord::Migration[5.1]
def change
add_index :users, :email, unique: true
end
end
class AddPasswordDigestToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :password_digest, :string
end
end
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180529074848) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.index ["email"], name: "index_users_on_email", unique: true
end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: 'Example User', email: 'user@example.com',
password: 'foobar',
password_confirmation: 'foobar')
end
test 'should be valid' do
assert @user.valid?
end
test 'name should be present' do
@user.name = ' '
assert_not @user.valid?
end
test 'email should be present' do
@user.email = ' '
assert_not @user.valid?
end
test 'name should not be too long' do
@user.name = 'a' * 51
assert_not @user.valid?
end
test 'email should not be too long' do
@user.email = 'a' * 244 + '@example.com'
assert_not @user.valid?
end
test 'email validation should accept valid address' do
valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org
first.last@foo.jp alice+bob@baz.cn]
valid_addresses.each do |valid_address|
@user.email = valid_address
assert @user.valid?, "#{valid_address.inspect} should be valid"
end
end
test 'email addresses should be unique' do
duplicate_user = @user.dup
@user.save
assert_not duplicate_user.valid?
end
test 'email address should be saved ass lower-case' do
mixed_case_email = 'Foo@exAMPle.com'
@user.email = mixed_case_email
@user.save
assert_equal mixed_case_email.downcase, @user.reload.email
end
test 'password should be present (nonblank)' do
@user.password = @user.password_confirmation = ' ' * 6
assert_not @user.valid?
end
test 'password should have minimum length' do
@user.password = @user.password_confirmation = 'a' * 5
assert_not @user.valid?
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment