/
user_test.rb
81 lines (68 loc) · 2.13 KB
/
user_test.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
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should "validate presence of email" do
user = User.new
user.save
assert_match /empty/, user.errors.on(:email)
user.email = 'foo@bar.com'
user.save
assert_nil user.errors.on(:email)
end
should "validate length of email" do
user = User.new
user.email = 'a@a'
user.save
assert user.errors.on(:email)
end
should "always store email as lower case" do
user = User.new
user.email = 'F@FOOBAR.COM'
user.save
assert_equal 'f@foobar.com', user.email
end
should "be able to set user's reset password code" do
user = Factory(:user)
assert_nil user.reset_password_code
assert_nil user.reset_password_code_until
user.set_password_code!
user.reset_password_code
assert user.reset_password_code_until.is_a?(Time)
end
context "Authentication" do
should 'work with existing email and correct password' do
user = Factory(:user)
assert_equal user, User.authenticate(user.email, 'testing')
end
should 'work with existing email (case insensitive) and password' do
user = Factory(:user)
assert_equal user, User.authenticate(user.email.upcase, 'testing')
end
should 'not work with existing email and incorrect password' do
assert_nil User.authenticate('john@doe.com', 'foobar')
end
should 'not work with non-existant email' do
assert_nil User.authenticate('foo@bar.com', 'foobar')
end
end
context "password" do
should 'be required if crypted password is blank' do
user = User.new
user.save
assert user.errors.on(:password)
end
should 'not be required if crypted password is present' do
user = User.new
user.crypted_password = BCrypt::Password.create('foobar')
user.save
assert_nil user.errors.on(:password)
end
should "validate the length of password" do
user = User.new
user.password = '1234'
user.save
assert user.errors.on(:password)
user.password = '123456'
assert_equal "is invalid", user.errors.on(:password)
end
end
end