2018-12-01 21:03:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Account do
|
2018-12-09 22:36:09 -05:00
|
|
|
subject { create :personal_account }
|
|
|
|
|
|
|
|
it { is_expected.to belong_to(:person).optional }
|
2018-12-01 21:03:19 -05:00
|
|
|
|
2018-12-01 21:50:10 -05:00
|
|
|
it do
|
|
|
|
is_expected.to \
|
|
|
|
have_one(:user)
|
|
|
|
.dependent(:restrict_with_exception)
|
|
|
|
end
|
|
|
|
|
2019-02-01 22:48:23 -05:00
|
|
|
it do
|
|
|
|
is_expected.to \
|
|
|
|
have_many(:account_roles)
|
2019-02-01 23:27:42 -05:00
|
|
|
.inverse_of(:account)
|
2019-02-01 22:48:23 -05:00
|
|
|
.dependent(:restrict_with_exception)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to \
|
|
|
|
have_many(:roles)
|
|
|
|
.through(:account_roles)
|
|
|
|
end
|
|
|
|
|
2018-12-11 19:15:40 -05:00
|
|
|
it do
|
|
|
|
is_expected.to \
|
|
|
|
have_many(:account_telegram_contacts)
|
|
|
|
.dependent(:restrict_with_exception)
|
|
|
|
end
|
|
|
|
|
2018-12-02 05:54:56 -05:00
|
|
|
it do
|
|
|
|
is_expected.to \
|
2018-12-13 00:36:00 -05:00
|
|
|
have_one(:own_membership_app)
|
2018-12-06 17:39:11 -05:00
|
|
|
.class_name('MembershipApp')
|
2018-12-02 05:54:56 -05:00
|
|
|
.dependent(:restrict_with_exception)
|
|
|
|
end
|
|
|
|
|
2018-12-01 21:28:34 -05:00
|
|
|
it do
|
|
|
|
is_expected.to \
|
|
|
|
have_many(:passport_confirmations)
|
|
|
|
.dependent(:restrict_with_exception)
|
|
|
|
end
|
2018-12-01 21:50:10 -05:00
|
|
|
|
2018-12-09 22:36:09 -05:00
|
|
|
it { is_expected.not_to validate_presence_of :person }
|
2018-12-01 21:50:10 -05:00
|
|
|
it { is_expected.not_to validate_presence_of :user }
|
2018-12-04 20:49:26 -05:00
|
|
|
|
|
|
|
pending '.guests'
|
|
|
|
pending '#guest?'
|
2018-12-11 22:15:03 -05:00
|
|
|
pending '#can_access_sidekiq_web_interface?'
|
2018-12-05 20:55:35 -05:00
|
|
|
|
2019-02-01 16:36:10 -05:00
|
|
|
describe '#to_param' do
|
|
|
|
specify do
|
|
|
|
expect(subject.to_param).to eq subject.username
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-31 19:47:44 -05:00
|
|
|
describe '#username' do
|
|
|
|
def allow_value(*)
|
|
|
|
super.for :username
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to validate_presence_of :username }
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to validate_length_of(:username).is_at_least(3).is_at_most(36)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to validate_uniqueness_of(:username).case_insensitive }
|
|
|
|
|
|
|
|
it { is_expected.not_to allow_value nil }
|
|
|
|
it { is_expected.not_to allow_value '' }
|
|
|
|
it { is_expected.not_to allow_value ' ' * 3 }
|
|
|
|
|
|
|
|
it { is_expected.to allow_value Faker::Internet.username(3..36, %w[_]) }
|
|
|
|
it { is_expected.to allow_value 'foo_bar' }
|
|
|
|
it { is_expected.to allow_value 'foo123' }
|
|
|
|
|
2019-01-31 20:33:08 -05:00
|
|
|
it do
|
|
|
|
is_expected.not_to \
|
|
|
|
allow_value Faker::Internet.username(3..36, %w[_]).upcase
|
|
|
|
end
|
|
|
|
|
2019-01-31 19:47:44 -05:00
|
|
|
it { is_expected.not_to allow_value Faker::Internet.email }
|
|
|
|
it { is_expected.not_to allow_value '_foo' }
|
|
|
|
it { is_expected.not_to allow_value 'bar_' }
|
|
|
|
it { is_expected.not_to allow_value '1foo' }
|
|
|
|
end
|
|
|
|
|
2019-02-01 16:46:57 -05:00
|
|
|
describe '#public_name' do
|
|
|
|
def allow_value(*)
|
|
|
|
super.for :public_name
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to allow_value nil }
|
|
|
|
|
|
|
|
it { is_expected.not_to allow_value '' }
|
|
|
|
it { is_expected.not_to allow_value ' ' }
|
|
|
|
|
|
|
|
it { is_expected.to allow_value Faker::Name.name }
|
|
|
|
it { is_expected.to allow_value Faker::Name.first_name }
|
|
|
|
it { is_expected.to allow_value 'Foo Bar' }
|
|
|
|
end
|
|
|
|
|
2019-01-31 23:02:51 -05:00
|
|
|
describe '#biography' do
|
|
|
|
it { is_expected.not_to validate_presence_of :biography }
|
|
|
|
it { is_expected.to validate_length_of(:biography).is_at_most(10_000) }
|
|
|
|
end
|
|
|
|
|
2019-02-01 23:24:12 -05:00
|
|
|
describe '#has_role?' do
|
|
|
|
subject { create :usual_account }
|
|
|
|
|
|
|
|
let(:role) { subject.add_role :superuser }
|
|
|
|
|
|
|
|
let!(:account_role) { role.account_roles.last }
|
|
|
|
|
|
|
|
let(:result) { subject.has_role? :superuser }
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(result).to eq true
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'after role is removed' do
|
|
|
|
before do
|
|
|
|
subject.remove_role :superuser
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(result).to eq false
|
|
|
|
end
|
|
|
|
end
|
2019-02-08 01:42:32 -05:00
|
|
|
|
|
|
|
context 'before role expires' do
|
|
|
|
before do
|
|
|
|
account_role.update! expires_at: 2.seconds.from_now
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(result).to eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'before role expires' do
|
|
|
|
before do
|
|
|
|
account_role.update! expires_at: Faker::Time.forward
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(result).to eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'after role expires' do
|
|
|
|
before do
|
|
|
|
account_role.update! expires_at: 1.second.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(result).to eq false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'after role expires' do
|
|
|
|
before do
|
|
|
|
account_role.update! expires_at: Faker::Time.backward
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(result).to eq false
|
|
|
|
end
|
|
|
|
end
|
2019-02-01 23:24:12 -05:00
|
|
|
end
|
|
|
|
|
2018-12-05 20:55:35 -05:00
|
|
|
describe '#add_role' do
|
2019-02-08 02:01:34 -05:00
|
|
|
subject { create :usual_account }
|
|
|
|
|
|
|
|
let(:result) { subject.add_role :superuser }
|
|
|
|
|
|
|
|
let(:account_role) { result; AccountRole.last }
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { subject.roles.reload.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { subject.account_roles.reload.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { Role.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { AccountRole.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(account_role.account).to eq subject
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(account_role.role).to eq result
|
|
|
|
end
|
|
|
|
|
2018-12-05 20:55:35 -05:00
|
|
|
context 'to guest account' do
|
2018-12-05 20:56:52 -05:00
|
|
|
subject { create :guest_account }
|
2018-12-05 20:55:35 -05:00
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to \
|
|
|
|
raise_error RuntimeError, 'can not add role to guest account'
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
2018-12-05 20:56:52 -05:00
|
|
|
expect { result rescue nil }.not_to(
|
|
|
|
change { subject.roles.reload.count },
|
|
|
|
)
|
2018-12-05 20:55:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-01 23:04:58 -05:00
|
|
|
|
|
|
|
describe '#remove_role' do
|
|
|
|
subject { create :usual_account }
|
|
|
|
|
2019-02-01 23:18:22 -05:00
|
|
|
let(:role) { subject.add_role :superuser }
|
|
|
|
|
|
|
|
let!(:account_role) { role.account_roles.last }
|
2019-02-01 23:04:58 -05:00
|
|
|
|
|
|
|
let(:result) { subject.remove_role :superuser }
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { subject.roles.reload.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { subject.account_roles.reload.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
2019-02-01 23:18:22 -05:00
|
|
|
specify do
|
|
|
|
expect { result }.to change { role.accounts.reload.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { role.account_roles.reload.count }.by(-1)
|
|
|
|
end
|
|
|
|
|
2019-02-01 23:04:58 -05:00
|
|
|
specify do
|
2019-02-01 23:27:42 -05:00
|
|
|
expect { result }.not_to(change { Role.count })
|
2019-02-01 23:04:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
2019-02-01 23:27:42 -05:00
|
|
|
expect { result }.not_to(change { AccountRole.count })
|
2019-02-01 23:18:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.not_to \
|
|
|
|
change { account_role.reload.account }.from(subject)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.not_to change { account_role.reload.role }.from(role)
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect { result }.to change { account_role.reload.deleted_at }.from(nil)
|
|
|
|
expect(account_role.deleted_at).to be_within(10).of(Time.zone.now)
|
2019-02-01 23:04:58 -05:00
|
|
|
end
|
|
|
|
end
|
2018-12-01 21:03:19 -05:00
|
|
|
end
|