Feature: Conversation creation email notifications (#576)

* Clean up the mailers

* Disable assignment mailer if setting is turned off

* Email notifications on conversation create

* Specs
This commit is contained in:
Sojan Jose
2020-03-01 19:06:13 +05:30
committed by GitHub
parent d6237dfc59
commit cda65ea339
17 changed files with 160 additions and 31 deletions

View File

@@ -0,0 +1,52 @@
require 'rails_helper'
describe EmailNotificationListener do
let(:listener) { described_class.instance }
let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
let!(:agent_with_notification) { create(:user, account: account) }
let!(:agent_with_out_notification) { create(:user, account: account) }
let!(:inbox) { create(:inbox, account: account) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: user) }
describe 'conversation_created' do
let(:event_name) { :'conversation.created' }
before do
creation_mailer = double
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_created).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.save!
create(:inbox_member, user: agent_with_notification, inbox: inbox)
conversation.reload
event = Events::Base.new(event_name, Time.zone.now, conversation: conversation)
listener.conversation_created(event)
expect(AgentNotifications::ConversationNotificationsMailer).to have_received(:conversation_created)
.with(conversation, agent_with_notification)
end
it 'does not send and email to inbox members who have notifications turned off' do
notification_setting = agent_with_notification.notification_settings.first
notification_setting.unselect_all_email_flags
notification_setting.save!
create(:inbox_member, user: agent_with_out_notification, inbox: inbox)
conversation.reload
event = Events::Base.new(event_name, Time.zone.now, conversation: conversation)
listener.conversation_created(event)
expect(AgentNotifications::ConversationNotificationsMailer).not_to have_received(:conversation_created)
.with(conversation, agent_with_out_notification)
end
end
end
end

View File

@@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe AgentNotifications::ConversationNotificationsMailer, type: :mailer do
let(:class_instance) { described_class.new }
let(:agent) { create(:user, email: 'agent1@example.com') }
let(:conversation) { create(:conversation, assignee: agent) }
before do
allow(described_class).to receive(:new).and_return(class_instance)
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 }
it 'renders the subject' do
expect(mail.subject).to eq("#{agent.name}, A new conversation [ID - #{conversation
.display_id}] has been created in #{conversation.inbox&.name}.")
end
it 'renders the receiver email' do
expect(mail.to).to eq([agent.email])
end
end
describe 'conversation_assigned' do
let(:mail) { described_class.conversation_assigned(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.")
end
it 'renders the receiver email' do
expect(mail.to).to eq([agent.email])
end
end
end

View File

@@ -2,12 +2,12 @@
require 'rails_helper'
RSpec.describe ConversationMailer, type: :mailer do
describe 'new_message' do
RSpec.describe ConversationReplyMailer, type: :mailer do
describe 'reply_with_summary' do
let(:agent) { create(:user, email: 'agent1@example.com') }
let(:conversation) { create(:conversation, assignee: agent) }
let(:message) { create(:message, conversation: conversation) }
let(:mail) { described_class.new_message(message.conversation, Time.zone.now).deliver_now }
let(:mail) { described_class.reply_with_summary(message.conversation, Time.zone.now).deliver_now }
let(:class_instance) { described_class.new }
before do

View File

@@ -34,7 +34,7 @@ RSpec.describe Conversation, type: :model do
new_assignee
allow(Rails.configuration.dispatcher).to receive(:dispatch)
allow(AssignmentMailer).to receive(:conversation_assigned).and_return(assignment_mailer)
allow(AgentNotifications::ConversationNotificationsMailer).to receive(:conversation_assigned).and_return(assignment_mailer)
allow(assignment_mailer).to receive(:deliver_later)
Current.user = old_assignee
@@ -58,7 +58,7 @@ RSpec.describe Conversation, type: :model do
.with(described_class::ASSIGNEE_CHANGED, kind_of(Time), conversation: conversation)
# send_email_notification_to_assignee
expect(AssignmentMailer).to have_received(:conversation_assigned).with(conversation, new_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
@@ -117,11 +117,23 @@ RSpec.describe Conversation, type: :model do
let(:agent) do
create(:user, email: 'agent@example.com', account: conversation.account, role: :agent)
end
let(:assignment_mailer) { double(deliver: true) }
it 'assigns the agent to conversation' do
expect(update_assignee).to eq(true)
expect(conversation.reload.assignee).to eq(agent)
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)
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)
end
end
describe '#toggle_status' do

View File

@@ -1,17 +1,17 @@
require 'rails_helper'
Sidekiq::Testing.fake!
RSpec.describe ConversationEmailWorker, type: :worker do
RSpec.describe ConversationReplyEmailWorker, type: :worker do
let(:perform_at) { (Time.zone.today + 6.hours).to_datetime }
let(:scheduled_job) { described_class.perform_at(perform_at, 1, Time.zone.now) }
let(:conversation) { build(:conversation, display_id: nil) }
describe 'testing ConversationEmailWorker' do
describe 'testing ConversationSummaryEmailWorker' do
before do
conversation.save!
allow(Conversation).to receive(:find).and_return(conversation)
mailer = double
allow(ConversationMailer).to receive(:new_message).and_return(mailer)
allow(ConversationReplyMailer).to receive(:reply_with_summary).and_return(mailer)
allow(mailer).to receive(:deliver_later).and_return(true)
end
@@ -28,9 +28,9 @@ RSpec.describe ConversationEmailWorker, type: :worker do
end
context 'with actions performed by the worker' do
it 'calls ConversationMailer' do
it 'calls ConversationSummaryMailer' do
described_class.new.perform(1, Time.zone.now)
expect(ConversationMailer).to have_received(:new_message)
expect(ConversationReplyMailer).to have_received(:reply_with_summary)
end
end
end