Feature: Ability to switch between multiple accounts (#881)
* Feature: Ability to switch between multiple accounts * Fix rubocop * Fix assigned inboxes * fix auth json * Add account switcher in UI * fix ordering on administrate * Add switch accounts to sidebar * add account id * Fix schema.rb timestamp * Revert "add account id" This reverts commit 27570f50ef584cb9a5f69454f43f630b318c8807. * Add a check for account Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
@@ -107,6 +107,7 @@ RSpec.describe 'Accounts API', type: :request do
|
||||
describe 'GET /api/v1/accounts/{account.id}' do
|
||||
let(:account) { create(:account) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
let(:user_without_access) { create(:user) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
@@ -119,9 +120,9 @@ RSpec.describe 'Accounts API', type: :request do
|
||||
context 'when it is an unauthorized user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}",
|
||||
headers: agent.create_new_auth_token
|
||||
headers: user_without_access.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -185,4 +186,29 @@ RSpec.describe 'Accounts API', type: :request do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/update_active_at' do
|
||||
let(:account) { create(:account) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post "/api/v1/accounts/#{account.id}/update_active_at"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
it 'modifies an account' do
|
||||
expect(agent.account_users.first.active_at).to eq(nil)
|
||||
post "/api/v1/accounts/#{account.id}/update_active_at",
|
||||
params: {},
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(agent.account_users.first.active_at).not_to eq(nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,6 +15,7 @@ describe ::ConversationFinder do
|
||||
create(:conversation, account: account, inbox: inbox, assignee: user_1)
|
||||
create(:conversation, account: account, inbox: inbox, assignee: user_1, status: 'resolved')
|
||||
create(:conversation, account: account, inbox: inbox, assignee: user_2)
|
||||
Current.account = account
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
|
||||
@@ -24,7 +24,9 @@ describe ActionCableListener do
|
||||
expect(conversation.inbox.reload.inbox_members.count).to eq(1)
|
||||
|
||||
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
|
||||
[agent.pubsub_token, admin.pubsub_token, conversation.contact.pubsub_token], 'message.created', message.push_event_data
|
||||
[agent.pubsub_token, admin.pubsub_token, conversation.contact.pubsub_token],
|
||||
'message.created',
|
||||
message.push_event_data.merge(account_id: account.id)
|
||||
)
|
||||
listener.message_created(event)
|
||||
end
|
||||
@@ -40,7 +42,8 @@ describe ActionCableListener do
|
||||
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
|
||||
[admin.pubsub_token, conversation.contact.pubsub_token],
|
||||
'conversation.typing_on', conversation: conversation.push_event_data,
|
||||
user: agent.push_event_data
|
||||
user: agent.push_event_data,
|
||||
account_id: account.id
|
||||
)
|
||||
listener.conversation_typing_on(event)
|
||||
end
|
||||
@@ -56,7 +59,8 @@ describe ActionCableListener do
|
||||
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
|
||||
[admin.pubsub_token, conversation.contact.pubsub_token],
|
||||
'conversation.typing_off', conversation: conversation.push_event_data,
|
||||
user: agent.push_event_data
|
||||
user: agent.push_event_data,
|
||||
account_id: account.id
|
||||
)
|
||||
listener.conversation_typing_off(event)
|
||||
end
|
||||
|
||||
@@ -27,6 +27,7 @@ RSpec.describe 'Confirmation Instructions', type: :mailer do
|
||||
let(:inviter_val) { create(:user, :administrator, skip_confirmation: true, account: account) }
|
||||
|
||||
it 'refers to the inviter and their account' do
|
||||
Current.account = account
|
||||
expect(mail.body).to match(
|
||||
"#{CGI.escapeHTML(inviter_val.name)}, with #{CGI.escapeHTML(inviter_val.account.name)}, has invited you to try out Chatwoot!"
|
||||
)
|
||||
|
||||
@@ -16,7 +16,6 @@ RSpec.describe User do
|
||||
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) }
|
||||
it { is_expected.to have_many(:assigned_inboxes).through(:inbox_members) }
|
||||
it { is_expected.to have_many(:messages) }
|
||||
it { is_expected.to have_many(:events) }
|
||||
end
|
||||
|
||||
@@ -11,23 +11,26 @@ RSpec.describe ContactPolicy, type: :policy do
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:contact) { create(:contact) }
|
||||
|
||||
let(:administrator_context) { { user: administrator, account: account, account_user: account.account_users.first } }
|
||||
let(:agent_context) { { user: agent, account: account, account_user: account.account_users.first } }
|
||||
|
||||
permissions :index?, :show?, :update? do
|
||||
context 'when administrator' do
|
||||
it { expect(contact_policy).to permit(administrator, contact) }
|
||||
it { expect(contact_policy).to permit(administrator_context, contact) }
|
||||
end
|
||||
|
||||
context 'when agent' do
|
||||
it { expect(contact_policy).not_to permit(agent, contact) }
|
||||
it { expect(contact_policy).to permit(agent_context, contact) }
|
||||
end
|
||||
end
|
||||
|
||||
permissions :create? do
|
||||
context 'when administrator' do
|
||||
it { expect(contact_policy).to permit(administrator, contact) }
|
||||
it { expect(contact_policy).to permit(administrator_context, contact) }
|
||||
end
|
||||
|
||||
context 'when agent' do
|
||||
it { expect(contact_policy).to permit(agent, contact) }
|
||||
it { expect(contact_policy).to permit(agent_context, contact) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,24 +10,26 @@ RSpec.describe InboxPolicy, type: :policy do
|
||||
let(:administrator) { create(:user, :administrator, account: account) }
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:inbox) { create(:inbox) }
|
||||
let(:administrator_context) { { user: administrator, account: account, account_user: account.account_users.first } }
|
||||
let(:agent_context) { { user: agent, account: account, account_user: account.account_users.first } }
|
||||
|
||||
permissions :create?, :destroy?, :update?, :set_agent_bot? do
|
||||
context 'when administrator' do
|
||||
it { expect(inbox_policy).to permit(administrator, inbox) }
|
||||
it { expect(inbox_policy).to permit(administrator_context, inbox) }
|
||||
end
|
||||
|
||||
context 'when agent' do
|
||||
it { expect(inbox_policy).not_to permit(agent, inbox) }
|
||||
it { expect(inbox_policy).not_to permit(agent_context, inbox) }
|
||||
end
|
||||
end
|
||||
|
||||
permissions :index? do
|
||||
context 'when administrator' do
|
||||
it { expect(inbox_policy).to permit(administrator, inbox) }
|
||||
it { expect(inbox_policy).to permit(administrator_context, inbox) }
|
||||
end
|
||||
|
||||
context 'when agent' do
|
||||
it { expect(inbox_policy).to permit(agent, inbox) }
|
||||
it { expect(inbox_policy).to permit(agent_context, inbox) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,24 +10,26 @@ RSpec.describe UserPolicy, type: :policy do
|
||||
let(:administrator) { create(:user, :administrator, account: account) }
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:user) { create(:user, account: account) }
|
||||
let(:administrator_context) { { user: administrator, account: account, account_user: account.account_users.first } }
|
||||
let(:agent_context) { { user: agent, account: account, account_user: account.account_users.first } }
|
||||
|
||||
permissions :create?, :update?, :destroy? do
|
||||
context 'when administrator' do
|
||||
it { expect(user_policy).to permit(administrator, user) }
|
||||
it { expect(user_policy).to permit(administrator_context, user) }
|
||||
end
|
||||
|
||||
context 'when agent' do
|
||||
it { expect(user_policy).not_to permit(agent, user) }
|
||||
it { expect(user_policy).not_to permit(agent_context, user) }
|
||||
end
|
||||
end
|
||||
|
||||
permissions :index? do
|
||||
context 'when administrator' do
|
||||
it { expect(user_policy).to permit(administrator, user) }
|
||||
it { expect(user_policy).to permit(administrator_context, user) }
|
||||
end
|
||||
|
||||
context 'when agent' do
|
||||
it { expect(user_policy).to permit(agent, user) }
|
||||
it { expect(user_policy).to permit(agent_context, user) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user