Skip to content

Instantly share code, notes, and snippets.

@maxinspace
Created May 3, 2017 12:22
Show Gist options
  • Select an option

  • Save maxinspace/2c9a4b2f52d0c08a76e3b9f94b2c9d27 to your computer and use it in GitHub Desktop.

Select an option

Save maxinspace/2c9a4b2f52d0c08a76e3b9f94b2c9d27 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Account do
describe "#validations" do
subject { Account.new }
it { is_expected.to belong_to(:account_type)}
it { is_expected.to belong_to(:account_class)}
it { is_expected.to belong_to(:vendor)}
it { is_expected.to belong_to(:manager)}
it { is_expected.to have_many(:orders) }
it { is_expected.to have_many(:subscriptions).dependent(:restrict_with_exception) }
it { is_expected.to have_many(:balance_transactions).dependent(:restrict_with_exception) }
it { is_expected.to have_many(:payments).dependent(:restrict_with_exception) }
it { is_expected.to have_many(:guaranteed_payments).dependent(:restrict_with_exception) }
it { is_expected.to have_many(:charges).through(:subscriptions) }
it { is_expected.to have_many(:account_accesses).dependent(:destroy) }
it { is_expected.to have_many(:users).through(:account_accesses) }
it { is_expected.to have_many(:applications).through(:subscriptions) }
it { is_expected.to have_many(:user_entry_points).dependent(:destroy) }
it { is_expected.to have_many(:dns_domains).dependent(:destroy) }
it { is_expected.to have_many(:notes).dependent(:destroy) }
it { is_expected.to have_many(:user_invites).dependent(:destroy) }
it { is_expected.to have_many(:account_discounts).dependent(:destroy) }
it { is_expected.to have_many(:account_notification_categories).dependent(:destroy) }
it { is_expected.to have_many(:notification_categories) }
it { is_expected.to respond_to(:vendor_name) }
it { is_expected.to respond_to(:manager_name) }
it { is_expected.to respond_to(:account_class_key) }
it { is_expected.to respond_to(:account_class_name) }
it { is_expected.to respond_to(:account_type_key) }
it { is_expected.to respond_to(:account_type_name) }
it { is_expected.to respond_to(:credit_limit) }
it { is_expected.to respond_to(:admin_account_accesses) }
it { is_expected.to respond_to(:owner_email) }
it { is_expected.to respond_to(:location_attributes) }
it { is_expected.to respond_to(:subscriptions_count) }
it { is_expected.to respond_to(:guaranteed_payment_period) }
it { is_expected.to respond_to(:guaranteed_payment_limit) }
it { is_expected.to validate_numericality_of(:balance) }
it { is_expected.to validate_presence_of(:account_class) }
it { is_expected.to validate_presence_of(:account_type) }
it { is_expected.to validate_presence_of(:vendor) }
it { is_expected.to validate_presence_of(:owner_account_access) }
it { is_expected.to validate_numericality_of(:balance)}
it { is_expected.to validate_numericality_of(:balance_limit) }
its(:status) { is_expected.to eq('active') }
end
let(:account) { create(:account) }
it_behaves_like 'with switchable'
it_behaves_like 'with location'
it_behaves_like 'trackable', :account
it_behaves_like 'has custom attributes'
subject { account }
describe '#primary_name validation' do
before do
allow(account).to receive(:account_type).and_return(double('AccountType', marked_for_destruction?: false))
allow(account).to receive(:account_type_primary_name).and_return(true)
end
it { is_expected.to validate_presence_of(:primary_name) }
context "when primary name set to false" do
before do
allow(account).to receive(:account_type_primary_name).and_return(false)
end
it { is_expected.not_to validate_presence_of(:primary_name) }
end
end
describe '#name' do
context 'when account type is business' do
subject { create :account, account_type: create(:account_type, :business) }
its(:name) { is_expected.to eq(subject.primary_name) }
end
context 'when account type is personal' do
subject { create :account, account_type: create(:account_type, :personal) }
its(:name) { is_expected.to eq("IP #{subject.first_name} #{subject.last_name} #{subject.middle_name}") }
end
end
context 'deactivation' do
let(:active_account) { create(:account, status: 'active') }
let(:inactive_account) { create(:account, status: 'inactive') }
context 'with subscription in provisioning' do
let!(:subscription1) { create(:subscription, account: active_account, status: :installing) }
let!(:subscription2) { create(:subscription, account: inactive_account, status: :installing) }
it 'is prohibitted for active account' do
expect { active_account.to_inactive }.to change(active_account.errors, :count).to(1)
end
it 'is prohibitted for inactive account' do
expect { inactive_account.to_inactive }.to change(inactive_account.errors, :count).to(1)
end
end
context 'without subscriptions in provisioning' do
let!(:subscription1) { create(:subscription, account: active_account, status: :active) }
let!(:subscription2) { create(:subscription, account: inactive_account, status: :active) }
it 'is allowed for active account' do
expect { active_account.to_inactive }.to_not change(active_account.errors, :count).from(0)
end
it 'is prohibitted for inactive account' do
expect { inactive_account.to_inactive }.to change(inactive_account.errors, :count).to(1)
end
end
end
describe 'activity tracking', callbacks: true do
it 'does not track balance change' do
expect { account.update_attribute(:balance, 100) }.to_not change(account.activities, :count)
end
context 'when custom attributes changed' do
let(:vendor) { create(:vendor) }
let(:custom_attribute) { create(:custom_attribute, vendor: vendor) }
let(:value) { create(:custom_attribute_value, custom_attribute: custom_attribute) }
let(:account) { create(:account, vendor: vendor, custom_attributes: [value]) }
it "tracks changes in activity" do
expect {
account.update(custom_attributes_attributes: {id: value.id, value: 'Gotham'})
}.to change { account.activities(true).count }.by(1)
end
end
context 'when location changed' do
let(:account) { create(:account_with_location) }
let(:location) { account.location }
it "tracks changes in activity" do
expect {
account.update(location_attributes: {id: location.id, city: 'Gotham'})
}.to change { account.activities(true).count }.by(1)
end
end
end
describe '#owner_account_access' do
before do
account.owner_account_access.destroy
account.reload
end
it 'creates new owner account access instance on update' do
expect(account).to_not be_valid
account.update_attribute(:new_owner_id, User.first.id)
expect(account.owner_account_access.user_id).to eq User.first.id
end
end
context '#role_for_user' do
let(:account) { create(:account) }
let(:user) { account.owner }
it "creates corresponding role for users" do
expect(account.role_for_user(user).text).to eq 'Owner'
expect(account.role_for_user(create(:user)).text).to be_nil
end
end
describe '#subscriptions_to_watch_count' do
let!(:graced) { create(:subscription, status: 'graced', account: account) }
let!(:stopped) { create(:subscription, status: 'stopped', account: account) }
let!(:ordered) { create(:subscription, status: 'ordered', account: account) }
let!(:deleted) { create(:subscription, status: 'deleted', account: account) }
its(:subscriptions_to_watch_count) { is_expected.to eq 2 }
end
describe '#payments_to_watch_count' do
let!(:new_payment) { create(:payment, status: 'waiting_for_payment', account: account) }
let!(:cancelled_payment) { create(:payment, status: 'cancelled', account: account) }
let!(:closed_payment) { create(:payment, status: 'completed', account: account) }
its(:payments_to_watch_count) { is_expected.to eq 1 }
end
describe '#available_account_classes' do
let(:vendor) { account.vendor }
before do
create(:account_class, vendor: vendor, post_pay: false)
create(:account_class, vendor: vendor, post_pay: true)
end
context 'when account have active subscriptions' do
let(:only_prepay_account_classes) { vendor.account_classes.where(post_pay: false) }
before { create(:subscription, :active, account: account) }
it 'returns account classes only for current account class type (post/pre pay)' do
expect(account.available_account_classes).to contain_exactly(*only_prepay_account_classes)
end
end
context 'when account have only deleted subscriptions' do
let(:all_account_classes) { vendor.account_classes }
before { create(:subscription, :deleted, account: account) }
it { expect(account.available_account_classes).to contain_exactly(*all_account_classes) }
end
end
describe '#short_name' do
let(:account) { build(:account, first_name: 'Tom', last_name: 'Pupkin') }
it { expect(account.short_name).to eq 'Tom Pupkin' }
end
describe '#users_to_detach?' do
it 'finds no users to detach' do
expect(account.decorate.users_to_detach?).to be_falsey
end
context 'when has other users besides owner' do
let!(:account_access) { create(:account_access, account: account) }
it 'returns true' do
expect(account.decorate.users_to_detach?).to be_truthy
end
end
end
describe '#users_to_attach?' do
it 'finds no users to attach' do
expect(account.decorate.users_to_attach?).to be_falsey
end
context 'when some users left to be attached' do
let!(:user) { create(:user, vendor: account.vendor) }
it 'finds accessible to attach users' do
expect(account.decorate.users_to_attach?).to be_truthy
end
end
end
describe '#set_default_tech_and_bill_users' do
let(:new_owner_user) { create(:user) }
let(:account) { create(:account, owner: new_owner_user, bill_user_id: nil) }
it "sets bill user as owner user of account" do
account.set_default_tech_and_bill_users
expect(account.bill_user_id).to eq new_owner_user.id
end
end
describe '#active_guaranteed_payment' do
subject { account.active_guaranteed_payment }
let!(:payment) { create(:guaranteed_payment, status: 'revoked', account: account) }
it { is_expected.to be_nil }
context 'when there are active payments' do
let!(:payment) { create(:guaranteed_payment, status: 'provided', account: account) }
it { is_expected.to eq payment }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment