Chore: Enable Users to create multiple accounts (#440)
Addresses: #402 - migrations to split roles and other attributes from users table - make changes in code to accommodate this change Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
9
spec/factories/account_users.rb
Normal file
9
spec/factories/account_users.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :account_user do
|
||||
account
|
||||
user
|
||||
role { 'agent' }
|
||||
end
|
||||
end
|
||||
@@ -4,6 +4,9 @@ FactoryBot.define do
|
||||
factory :user do
|
||||
transient do
|
||||
skip_confirmation { true }
|
||||
role { 'agent' }
|
||||
account { nil }
|
||||
inviter { nil }
|
||||
end
|
||||
|
||||
provider { 'email' }
|
||||
@@ -11,12 +14,11 @@ FactoryBot.define do
|
||||
name { Faker::Name.name }
|
||||
nickname { Faker::Name.first_name }
|
||||
email { nickname + '@example.com' }
|
||||
role { 'agent' }
|
||||
password { 'password' }
|
||||
account
|
||||
|
||||
after(:build) do |user, evaluator|
|
||||
user.skip_confirmation! if evaluator.skip_confirmation
|
||||
create(:account_user, user: user, account: evaluator.account, role: evaluator.role, inviter: evaluator.inviter) if evaluator.account
|
||||
end
|
||||
|
||||
trait :with_avatar do
|
||||
|
||||
@@ -4,7 +4,8 @@ require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Confirmation Instructions', type: :mailer do
|
||||
describe :notify do
|
||||
let(:confirmable_user) { FactoryBot.build(:user, inviter: inviter_val) }
|
||||
let(:account) { create(:account) }
|
||||
let(:confirmable_user) { build(:user, inviter: inviter_val, account: account) }
|
||||
let(:inviter_val) { nil }
|
||||
let(:mail) { Devise::Mailer.confirmation_instructions(confirmable_user, nil, {}) }
|
||||
|
||||
@@ -23,9 +24,7 @@ RSpec.describe 'Confirmation Instructions', type: :mailer do
|
||||
end
|
||||
|
||||
context 'when there is an inviter' do
|
||||
let(:inviter_val) do
|
||||
FactoryBot.create(:user, role: :administrator, skip_confirmation: true)
|
||||
end
|
||||
let(:inviter_val) { create(:user, :administrator, skip_confirmation: true, account: account) }
|
||||
|
||||
it 'refers to the inviter and their account' do
|
||||
expect(mail.body).to match(
|
||||
|
||||
@@ -5,7 +5,8 @@ require 'rails_helper'
|
||||
RSpec.describe Account do
|
||||
it { is_expected.to validate_presence_of(:name) }
|
||||
|
||||
it { is_expected.to have_many(:users).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:users).through(:account_users) }
|
||||
it { is_expected.to have_many(:account_users) }
|
||||
it { is_expected.to have_many(:inboxes).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:conversations).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:contacts).dependent(:destroy) }
|
||||
|
||||
16
spec/models/account_user_spec.rb
Normal file
16
spec/models/account_user_spec.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe User do
|
||||
let!(:account_user) { create(:account_user) }
|
||||
|
||||
describe 'notification_settings' do
|
||||
it 'gets created with the right default settings' do
|
||||
expect(account_user.user.notification_settings).not_to eq(nil)
|
||||
|
||||
expect(account_user.user.notification_settings.first.conversation_creation?).to eq(false)
|
||||
expect(account_user.user.notification_settings.first.conversation_assignment?).to eq(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -8,12 +8,11 @@ RSpec.describe User do
|
||||
context 'validations' do
|
||||
it { is_expected.to validate_presence_of(:email) }
|
||||
it { is_expected.to validate_presence_of(:name) }
|
||||
it { is_expected.to validate_presence_of(:account_id) }
|
||||
end
|
||||
|
||||
context 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to belong_to(:inviter).class_name('User').required(false) }
|
||||
it { is_expected.to have_many(:accounts).through(:account_users) }
|
||||
it { is_expected.to have_many(:account_users) }
|
||||
it { is_expected.to have_many(:assigned_conversations).class_name('Conversation').dependent(:nullify) }
|
||||
it { is_expected.to have_many(:inbox_members).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:notification_settings).dependent(:destroy) }
|
||||
@@ -27,13 +26,4 @@ RSpec.describe User do
|
||||
it { expect(user.pubsub_token).not_to eq(nil) }
|
||||
it { expect(user.saved_changes.keys).not_to eq('pubsub_token') }
|
||||
end
|
||||
|
||||
describe 'notification_settings' do
|
||||
it 'gets created with the right default settings' do
|
||||
expect(user.notification_settings).not_to eq(nil)
|
||||
|
||||
expect(user.notification_settings.first.conversation_creation?).to eq(false)
|
||||
expect(user.notification_settings.first.conversation_assignment?).to eq(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,8 +5,10 @@ require 'rails_helper'
|
||||
RSpec.describe ContactPolicy, type: :policy do
|
||||
subject(:contact_policy) { described_class }
|
||||
|
||||
let(:administrator) { create(:user, :administrator) }
|
||||
let(:agent) { create(:user) }
|
||||
let(:account) { create(:account) }
|
||||
|
||||
let(:administrator) { create(:user, :administrator, account: account) }
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:contact) { create(:contact) }
|
||||
|
||||
permissions :index?, :show?, :update? do
|
||||
|
||||
@@ -5,8 +5,10 @@ require 'rails_helper'
|
||||
RSpec.describe InboxPolicy, type: :policy do
|
||||
subject(:inbox_policy) { described_class }
|
||||
|
||||
let(:administrator) { create(:user, :administrator) }
|
||||
let(:agent) { create(:user) }
|
||||
let(:account) { create(:account) }
|
||||
|
||||
let(:administrator) { create(:user, :administrator, account: account) }
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:inbox) { create(:inbox) }
|
||||
|
||||
permissions :create?, :destroy? do
|
||||
|
||||
@@ -5,9 +5,11 @@ require 'rails_helper'
|
||||
RSpec.describe UserPolicy, type: :policy do
|
||||
subject(:user_policy) { described_class }
|
||||
|
||||
let(:administrator) { create(:user, :administrator) }
|
||||
let(:agent) { create(:user) }
|
||||
let(:user) { create(:user) }
|
||||
let(:account) { create(:account) }
|
||||
|
||||
let(:administrator) { create(:user, :administrator, account: account) }
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:user) { create(:user, account: account) }
|
||||
|
||||
permissions :create?, :update?, :destroy? do
|
||||
context 'when administrator' do
|
||||
|
||||
Reference in New Issue
Block a user