Skip to content

Commit e791903

Browse files
author
Ryan Bigg
committed
Section 6.1: Make a User model with password
1 parent 7d39e0c commit e791903

File tree

6 files changed

+74
-2
lines changed

6 files changed

+74
-2
lines changed

ticketee/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gem 'font-awesome-rails', '~> 4.2'
2828
gem 'simple_form', '3.1.0.rc2'
2929

3030
# Use ActiveModel has_secure_password
31-
# gem 'bcrypt', '~> 3.1.7'
31+
gem 'bcrypt', '~> 3.1.9'
3232

3333
# Use Unicorn as the app server
3434
# gem 'unicorn'

ticketee/Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ GEM
3737
thread_safe (~> 0.1)
3838
tzinfo (~> 1.1)
3939
arel (6.0.0.beta2)
40+
bcrypt (3.1.9)
4041
binding_of_caller (0.7.3.pre1)
4142
debug_inspector (>= 0.0.1)
4243
bootstrap-sass (3.3.0.1)
@@ -186,6 +187,7 @@ PLATFORMS
186187
ruby
187188

188189
DEPENDENCIES
190+
bcrypt (~> 3.1.9)
189191
bootstrap-sass (~> 3.3)
190192
byebug
191193
capybara (~> 2.4)

ticketee/app/models/user.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class User < ActiveRecord::Base
2+
has_secure_password
3+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateUsers < ActiveRecord::Migration
2+
def change
3+
create_table :users do |t|
4+
t.string :name
5+
t.string :email
6+
t.string :password_digest
7+
8+
t.timestamps null: false
9+
end
10+
end
11+
end

ticketee/db/schema.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20141112001034) do
14+
ActiveRecord::Schema.define(version: 20141112021638) do
1515

1616
create_table "projects", force: true do |t|
1717
t.string "name"
@@ -30,4 +30,12 @@
3030

3131
add_index "tickets", ["project_id"], name: "index_tickets_on_project_id"
3232

33+
create_table "users", force: true do |t|
34+
t.string "name"
35+
t.string "email"
36+
t.string "password_digest"
37+
t.datetime "created_at", null: false
38+
t.datetime "updated_at", null: false
39+
end
40+
3341
end

ticketee/spec/models/user_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require "rails_helper"
2+
3+
describe User do
4+
describe "passwords" do
5+
it "needs a password and confirmation to save" do
6+
user = User.new(email: "[email protected]")
7+
8+
user.save
9+
expect(user).to_not be_valid
10+
11+
user.password = "password"
12+
user.password_confirmation = ""
13+
user.save
14+
expect(user).to_not be_valid
15+
16+
user.password_confirmation = "password"
17+
user.save
18+
expect(user).to be_valid
19+
end
20+
21+
it "needs password and confirmation to match" do
22+
user = User.create(
23+
24+
password: "hunter2",
25+
password_confirmation: "hunter")
26+
27+
expect(user).to_not be_valid
28+
end
29+
end
30+
31+
describe "authentication" do
32+
let(:user) do
33+
User.create(
34+
35+
password: "hunter2",
36+
password_confirmation: "hunter2"
37+
)
38+
end
39+
40+
it "authenticates with a correct password" do
41+
expect(user.authenticate("hunter2")).to be
42+
end
43+
44+
it "does not authenticate with an incorrect password" do
45+
expect(user.authenticate("hunter1")).to_not be
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)