-
Notifications
You must be signed in to change notification settings - Fork 8
/
user_spec.rb
57 lines (43 loc) · 1.76 KB
/
user_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
require 'user'
require 'database_helpers'
describe User do
describe '.create' do
it 'creates a new user' do
user = User.create(email: '[email protected]', password: 'password123')
persisted_data = persisted_data(id: user.id, table: 'users')
expect(user).to be_a User
expect(user.id).to eq persisted_data.first['id']
expect(user.email).to eq '[email protected]'
end
it 'hashes the password using BCrypt' do
expect(BCrypt::Password).to receive(:create).with('password123')
User.create(email: '[email protected]', password: 'password123')
end
end
describe '.find' do
it 'returns the user' do
user = User.create(email: '[email protected]', password: 'password123')
result = User.find(id: user.id)
expect(result.id).to eq user.id
expect(result.email).to eq user.email
end
it 'returns nil if there is no ID given' do
expect(User.find(id: nil)).to eq nil
end
end
describe '.authenticate' do
it 'returns a user given a correct username and password, if one exists' do
user = User.create(email: '[email protected]', password: 'password123')
authenticated_user = User.authenticate(email: '[email protected]', password: 'password123')
expect(authenticated_user.id).to eq user.id
end
it 'returns nil given an incorrect email address' do
user = User.create(email: '[email protected]', password: 'password123')
expect(User.authenticate(email: '[email protected]', password: 'password123')).to be_nil
end
it 'returns nil given an incorrect password' do
user = User.create(email: '[email protected]', password: 'password123')
expect(User.authenticate(email: '[email protected]', password: 'wrongpassword')).to be_nil
end
end
end