Feature: User Notification Objects (#752)

Co-authored-by: Pranav Raj S <pranavrajs@gmail.com>
This commit is contained in:
Sojan Jose
2020-05-01 14:53:43 +05:30
committed by GitHub
parent 66aace7c13
commit 96da27f1f6
35 changed files with 461 additions and 110 deletions

View File

@@ -56,10 +56,10 @@ RSpec.describe 'Contacts API', type: :request do
let(:valid_params) { { contact: { account_id: account.id } } }
context 'when it is an unauthenticated user' do
it 'creates the contact' do
expect { post "/api/v1/accounts/#{account.id}/contacts", params: valid_params }.to change(Contact, :count).by(1)
it 'returns unauthorized' do
expect { post "/api/v1/accounts/#{account.id}/contacts", params: valid_params }.to change(Contact, :count).by(0)
expect(response).to have_http_status(:success)
expect(response).to have_http_status(:unauthorized)
end
end

View File

@@ -42,7 +42,7 @@ RSpec.describe 'Notification Settings API', type: :request do
it 'updates the email related notification flags' do
put "/api/v1/accounts/#{account.id}/notification_settings",
params: { notification_settings: { selected_email_flags: ['conversation_assignment'] } },
params: { notification_settings: { selected_email_flags: ['email_conversation_assignment'] } },
headers: agent.create_new_auth_token,
as: :json
@@ -51,7 +51,7 @@ RSpec.describe 'Notification Settings API', type: :request do
agent.reload
expect(json_response['user_id']).to eq(agent.id)
expect(json_response['account_id']).to eq(account.id)
expect(json_response['selected_email_flags']).to eq(['conversation_assignment'])
expect(json_response['selected_email_flags']).to eq(['email_conversation_assignment'])
end
end
end

View File

@@ -0,0 +1,57 @@
require 'rails_helper'
RSpec.describe 'Notifications API', type: :request do
let(:account) { create(:account) }
describe 'GET /api/v1/accounts/{account.id}/notifications' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/notifications"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) }
let!(:notification) { create(:notification, account: account, user: admin) }
it 'returns all notifications' do
get "/api/v1/accounts/#{account.id}/notifications",
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(response.body).to include(notification.notification_type)
end
end
end
describe 'PATCH /api/v1/accounts/{account.id}/notifications/:id' do
let(:admin) { create(:user, account: account, role: :administrator) }
let!(:notification) { create(:notification, account: account, user: admin) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
put "/api/v1/accounts/#{account.id}/notifications/#{notification.id}",
params: { read_at: true }
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) }
it 'updates the notification read at' do
patch "/api/v1/accounts/#{account.id}/notifications/#{notification.id}",
headers: admin.create_new_auth_token,
params: { read_at: true },
as: :json
expect(response).to have_http_status(:success)
expect(notification.reload.read_at).not_to eq('')
end
end
end
end

View File

@@ -0,0 +1,31 @@
require 'rails_helper'
RSpec.describe 'Notifications Subscriptions API', type: :request do
let(:account) { create(:account) }
describe 'POST /api/v1/notification_subscriptions' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
post '/api/v1/notification_subscriptions'
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
it 'creates a notification subscriptions' do
post '/api/v1/notification_subscriptions',
params: { notification_subscription: { subscription_type: 'browser_push', 'subscription_attributes': { test: 'test' } } },
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['subscription_type']).to eq('browser_push')
expect(json_response['subscription_attributes']).to eq({ 'test' => 'test' })
end
end
end
end

View File

@@ -0,0 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :notification do
primary_actor { create(:conversation, account: account) }
notification_type { 'conversation_assignment' }
user
account
end
end

View File

@@ -6,11 +6,6 @@ describe ActionCableListener do
let!(:inbox) { create(:inbox, account: account) }
let!(:agent) { create(:user, account: account, role: :agent) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: agent) }
let!(:message) do
create(:message, message_type: 'outgoing',
account: account, inbox: inbox, conversation: conversation)
end
let!(:event) { Events::Base.new(event_name, Time.zone.now, message: message) }
before do
create(:inbox_member, inbox: inbox, user: agent)
@@ -18,13 +13,18 @@ describe ActionCableListener do
describe '#message_created' do
let(:event_name) { :'message.created' }
let!(:message) do
create(:message, message_type: 'outgoing',
account: account, inbox: inbox, conversation: conversation)
end
let!(:event) { Events::Base.new(event_name, Time.zone.now, message: message) }
it 'sends message to account admins, inbox agents and the contact' do
# HACK: to reload conversation inbox members
expect(conversation.inbox.reload.inbox_members.count).to eq(1)
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[agent.pubsub_token, admin.pubsub_token], 'message.created', message.push_event_data
)
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[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
)
listener.message_created(event)
end

View File

@@ -1,5 +1,5 @@
require 'rails_helper'
describe EmailNotificationListener do
describe NotificationListener do
let(:listener) { described_class.instance }
let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
@@ -13,14 +13,14 @@ describe EmailNotificationListener do
before do
creation_mailer = double
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_created).and_return(creation_mailer)
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_creation).and_return(creation_mailer)
allow(creation_mailer).to receive(:deliver_later).and_return(true)
end
context 'when conversation is created' do
it 'sends email to inbox members who have notifications turned on' do
notification_setting = agent_with_notification.notification_settings.first
notification_setting.selected_email_flags = [:conversation_creation]
notification_setting.selected_email_flags = [:email_conversation_creation]
notification_setting.save!
create(:inbox_member, user: agent_with_notification, inbox: inbox)
@@ -29,7 +29,7 @@ describe EmailNotificationListener do
event = Events::Base.new(event_name, Time.zone.now, conversation: conversation)
listener.conversation_created(event)
expect(AgentNotifications::ConversationNotificationsMailer).to have_received(:conversation_created)
expect(AgentNotifications::ConversationNotificationsMailer).to have_received(:conversation_creation)
.with(conversation, agent_with_notification)
end
@@ -44,7 +44,7 @@ describe EmailNotificationListener do
event = Events::Base.new(event_name, Time.zone.now, conversation: conversation)
listener.conversation_created(event)
expect(AgentNotifications::ConversationNotificationsMailer).not_to have_received(:conversation_created)
expect(AgentNotifications::ConversationNotificationsMailer).not_to have_received(:conversation_creation)
.with(conversation, agent_with_out_notification)
end
end

View File

@@ -12,8 +12,8 @@ RSpec.describe AgentNotifications::ConversationNotificationsMailer, type: :maile
allow(class_instance).to receive(:smtp_config_set_or_development?).and_return(true)
end
describe 'conversation_created' do
let(:mail) { described_class.conversation_created(conversation, agent).deliver_now }
describe 'conversation_creation' do
let(:mail) { described_class.conversation_creation(conversation, agent).deliver_now }
it 'renders the subject' do
expect(mail.subject).to eq("#{agent.name}, A new conversation [ID - #{conversation
@@ -25,8 +25,8 @@ RSpec.describe AgentNotifications::ConversationNotificationsMailer, type: :maile
end
end
describe 'conversation_assigned' do
let(:mail) { described_class.conversation_assigned(conversation, agent).deliver_now }
describe 'conversation_assignment' do
let(:mail) { described_class.conversation_assignment(conversation, agent).deliver_now }
it 'renders the subject' do
expect(mail.subject).to eq("#{agent.name}, A new conversation [ID - #{conversation.display_id}] has been assigned to you.")

View File

@@ -9,8 +9,8 @@ RSpec.describe User 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)
expect(account_user.user.notification_settings.first.email_conversation_creation?).to eq(false)
expect(account_user.user.notification_settings.first.email_conversation_assignment?).to eq(true)
end
end
end

View File

@@ -39,8 +39,6 @@ RSpec.describe Conversation, type: :model do
new_assignee
allow(Rails.configuration.dispatcher).to receive(:dispatch)
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_assigned).and_return(assignment_mailer)
allow(assignment_mailer).to receive(:deliver_later)
Current.user = old_assignee
conversation.update(
@@ -61,11 +59,6 @@ RSpec.describe Conversation, type: :model do
.with(described_class::CONVERSATION_LOCK_TOGGLE, kind_of(Time), conversation: conversation)
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(described_class::ASSIGNEE_CHANGED, kind_of(Time), conversation: conversation)
# send_email_notification_to_assignee
expect(AgentNotifications::ConversationNotificationsMailer).to have_received(:conversation_assigned).with(conversation, new_assignee)
expect(assignment_mailer).to have_received(:deliver_later) if ENV.fetch('SMTP_ADDRESS', nil).present?
end
it 'creates conversation activities' do
@@ -129,15 +122,28 @@ RSpec.describe Conversation, type: :model do
expect(conversation.reload.assignee).to eq(agent)
end
it 'send assignment mailer' do
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_assignment).and_return(assignment_mailer)
allow(assignment_mailer).to receive(:deliver_later)
Current.user = conversation.assignee
expect(update_assignee).to eq(true)
# send_email_notification_to_assignee
expect(AgentNotifications::ConversationNotificationsMailer).to have_received(:conversation_assignment).with(conversation, agent)
expect(assignment_mailer).to have_received(:deliver_later) if ENV.fetch('SMTP_ADDRESS', nil).present?
end
it 'does not send assignment mailer if notification setting is turned off' do
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_assigned).and_return(assignment_mailer)
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_assignment).and_return(assignment_mailer)
notification_setting = agent.notification_settings.first
notification_setting.unselect_all_email_flags
notification_setting.save!
expect(update_assignee).to eq(true)
expect(AgentNotifications::ConversationNotificationsMailer).not_to have_received(:conversation_assigned).with(conversation, agent)
expect(AgentNotifications::ConversationNotificationsMailer).not_to have_received(:conversation_assignment).with(conversation, agent)
end
end