-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor_spec.rb
88 lines (74 loc) · 2.66 KB
/
editor_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'spec_helper'
describe Editor do
describe 'attributes' do
it { should allow_mass_assignment_of(:email) }
it { should allow_mass_assignment_of(:name) }
it { should allow_mass_assignment_of(:password) }
it { should allow_mass_assignment_of(:password_confirmation) }
end
describe 'associations' do
it { should have_many(:posts) }
it { should have_many(:attachments) }
end
describe 'validations' do
subject { Factory(:editor) }
it { should be_valid }
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:email) }
it "validate confirmation of password" do
editor = Factory.build(:editor, :password => 'password',
:password_confirmation => 'password2')
editor.should_not be_valid
editor.errors[:password].should include("doesn't match confirmation")
end
it "should validate presence of password on create" do
editor = Factory.build(:editor, :password => nil)
editor.should_not be_valid
editor.errors[:password].should include("can't be blank")
end
it "should not validate presence of password on update" do
editor = Factory.create(:editor)
editor.update_attributes(:name => "New name")
editor.save
editor.should be_valid
end
end
describe "callbacks" do
describe "password encryption" do
it "encrypts password on save" do
BCrypt::Engine.should_receive(:generate_salt).and_return("$2a$10$SfbTYlR2xJX/x48XMxNXLu")
editor = Factory.build(:editor)
editor.password_hash.should be_nil
editor.save
editor.password_hash.should == "$2a$10$SfbTYlR2xJX/x48XMxNXLuim36E5WnPTcNReIz1lQfZVrIhVuBQsi"
end
end
end
describe "authenticate" do
it "should authenticate given valid email and password" do
editor = Factory.create(:editor, :email => 'pink.panter@gmail.com')
Editor.authenticate('pink.panter@gmail.com', 'password').should == editor
end
it "should not authenticate given invalid password" do
editor = Factory.create(:editor)
Editor.authenticate('pink.panter@gmail.com', 'password1').should == nil
end
it "should not authenticate given invalid email" do
editor = Factory.create(:editor)
Editor.authenticate('pink.panter@gmail.com1', 'password').should == nil
end
end
end
# == Schema Information
#
# Table name: editors
#
# id :integer(4) not null, primary key
# email :string(255)
# name :string(255)
# password_hash :string(255)
# password_salt :string(255)
# is_admin :boolean(1) default(FALSE)
# created_at :datetime
# updated_at :datetime
#