foodsoft/spec/models/user_spec.rb

74 lines
2.5 KiB
Ruby
Raw Normal View History

2013-09-18 12:44:41 +02:00
require_relative '../spec_helper'
2013-07-12 20:06:49 +02:00
describe User do
it 'is correctly created' do
2013-09-18 12:44:41 +02:00
user = create :user,
nick: 'johnnydoe', first_name: 'Johnny', last_name: 'DoeBar',
email: 'johnnydoe@foodcoop.test', phone: '+1234567890'
2013-07-24 22:46:25 +02:00
expect(user.nick).to eq('johnnydoe')
expect(user.first_name).to eq('Johnny')
expect(user.last_name).to eq('DoeBar')
expect(user.name).to eq('Johnny DoeBar')
expect(user.email).to eq('johnnydoe@foodcoop.test')
expect(user.phone).to eq('+1234567890')
2013-07-12 20:06:49 +02:00
end
describe 'does not have the role' do
2013-09-18 12:44:41 +02:00
let(:user) { create :user }
2014-08-19 11:39:33 +02:00
it 'admin' do expect(user.role_admin?).to be_falsey end
it 'finance' do expect(user.role_finance?).to be_falsey end
it 'article_meta' do expect(user.role_article_meta?).to be_falsey end
it 'suppliers' do expect(user.role_suppliers?).to be_falsey end
it 'orders' do expect(user.role_orders?).to be_falsey end
2013-07-12 20:06:49 +02:00
end
describe do
2013-09-18 12:44:41 +02:00
let(:user) { create :user, password: 'blahblah' }
2013-07-12 20:06:49 +02:00
it 'can authenticate with correct password' do
2014-08-19 11:39:33 +02:00
expect(User.authenticate(user.nick, 'blahblah')).to be_truthy
2013-07-12 20:06:49 +02:00
end
it 'can not authenticate with incorrect password' do
2013-07-24 22:46:25 +02:00
expect(User.authenticate(user.nick, 'foobar')).to be_nil
2013-07-12 20:06:49 +02:00
end
it 'can not authenticate with nil nick' do
expect(User.authenticate(nil, 'blahblah')).to be_nil
end
it 'can not authenticate with nil password' do
expect(User.authenticate(user.nick, nil)).to be_nil
end
2013-07-12 20:06:49 +02:00
it 'can not set a password without matching confirmation' do
user.password = 'abcdefghij'
user.password_confirmation = 'foobarxyz'
2013-07-24 22:46:25 +02:00
expect(user).to be_invalid
2013-07-12 20:06:49 +02:00
end
it 'can set a password with matching confirmation' do
user.password = 'abcdefghij'
user.password_confirmation = 'abcdefghij'
2013-07-24 22:46:25 +02:00
expect(user).to be_valid
2013-07-12 20:06:49 +02:00
end
it 'has a unique nick' do
2013-09-18 12:44:41 +02:00
expect(build(:user, nick: user.nick, email: "x-#{user.email}")).to be_invalid
2013-07-12 20:06:49 +02:00
end
it 'has a unique email' do
2013-09-18 12:44:41 +02:00
expect(build(:user, email: "#{user.email}")).to be_invalid
2013-07-12 20:06:49 +02:00
end
it 'can authenticate using email address' do
2014-08-19 11:39:33 +02:00
expect(User.authenticate(user.email, 'blahblah')).to be_truthy
end
it 'can authenticate when there is no nick' do
user.nick = nil
expect(user).to be_valid
2014-08-19 11:39:33 +02:00
expect(User.authenticate(user.email, 'blahblah')).to be_truthy
end
2013-07-12 20:06:49 +02:00
end
describe 'admin' do
2013-09-18 12:44:41 +02:00
let(:user) { create :admin }
2014-08-19 11:39:33 +02:00
it 'default admin role' do expect(user.role_admin?).to be_truthy end
2013-07-12 20:06:49 +02:00
end
end