scube-server/spec/models/user_spec.rb
2015-05-01 15:57:26 +00:00

48 lines
1.3 KiB
Ruby

describe User do
subject(:user) { build :user }
it { is_expected.to be_valid }
it { is_expected.to have_many :playlists }
it { is_expected.to validate_presence_of :email }
it { is_expected.to validate_presence_of :password }
it { is_expected.to validate_presence_of :password_hash }
context 'when a user with the same email address already exists' do
let(:old_user) { create :user, email: 'unique@example.net' }
subject(:user) { build :user, email: old_user.email }
it { is_expected.not_to be_valid }
it 'has an error on email attribute' do
user.valid?
expect(user.errors[:email].size).to eq 1
end
end
context 'when password_confirmation does not match password' do
before { user.password_confirmation = user.password + 'INVALID' }
it { is_expected.not_to be_valid }
end
describe '#password=' do
it 'stores a bcrypt hash of the password in password_hash' do
expect(BCrypt::Password.new(user.password_hash)).to eq user.password
end
end
describe '#authenticate?' do
context 'with a valid password' do
it 'returns true' do
expect(user.authenticate? user.password).to be true
end
end
context 'with an invalid password' do
it 'returns false' do
expect(user.authenticate? user.password + '_INVALID').to be false
end
end
end
end