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,
|
2021-03-01 15:27:26 +01:00
|
|
|
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
|
2022-02-16 18:13:08 +01:00
|
|
|
let(:user) { create :user, password: 'blahblahblah' }
|
2013-07-12 20:06:49 +02:00
|
|
|
|
|
|
|
it 'can authenticate with correct password' do
|
2022-02-16 18:13:08 +01:00
|
|
|
expect(User.authenticate(user.nick, 'blahblahblah')).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
|
2019-11-11 11:07:52 +01:00
|
|
|
it 'can not authenticate with nil nick' do
|
2022-02-16 18:13:08 +01:00
|
|
|
expect(User.authenticate(nil, 'blahblahblah')).to be_nil
|
2019-11-11 11:07:52 +01:00
|
|
|
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
|
2022-02-16 18:13:08 +01:00
|
|
|
user.password = 'abcdefghijkl'
|
|
|
|
user.password_confirmation = 'foobaruvwxyz'
|
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
|
2022-02-16 18:13:08 +01:00
|
|
|
user.password = 'abcdefghijkl'
|
|
|
|
user.password_confirmation = 'abcdefghijkl'
|
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
|
2013-09-20 22:39:19 +02:00
|
|
|
|
|
|
|
it 'can authenticate using email address' do
|
2022-02-16 18:13:08 +01:00
|
|
|
expect(User.authenticate(user.email, 'blahblahblah')).to be_truthy
|
2013-09-20 22:39:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can authenticate when there is no nick' do
|
|
|
|
user.nick = nil
|
|
|
|
expect(user).to be_valid
|
2022-02-16 18:13:08 +01:00
|
|
|
expect(User.authenticate(user.email, 'blahblahblah')).to be_truthy
|
2013-09-20 22:39:19 +02:00
|
|
|
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
|