chore: Fix emails being sent with the wrong translations (#2236)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Sojan Jose
2021-06-08 22:45:01 +05:30
committed by GitHub
parent 67ce6f5704
commit 1bf7227843
15 changed files with 75 additions and 38 deletions

View File

@@ -374,14 +374,24 @@ RSpec.describe 'Conversations API', type: :request do
let(:params) { { email: 'test@test.com' } }
it 'mutes conversation' do
allow(ConversationReplyMailer).to receive(:conversation_transcript)
mailer = double
allow(ConversationReplyMailer).to receive(:with).and_return(mailer)
allow(mailer).to receive(:conversation_transcript)
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/transcript",
headers: agent.create_new_auth_token,
params: params,
as: :json
expect(response).to have_http_status(:success)
expect(ConversationReplyMailer).to have_received(:conversation_transcript).with(conversation, 'test@test.com')
expect(mailer).to have_received(:conversation_transcript).with(conversation, 'test@test.com')
end
it 'renders error when parameter missing' do
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/transcript",
headers: agent.create_new_auth_token,
params: {},
as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

View File

@@ -104,7 +104,9 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do
describe 'POST /api/v1/widget/conversations/transcript' do
context 'with a conversation' do
it 'sends transcript email' do
allow(ConversationReplyMailer).to receive(:conversation_transcript)
mailer = double
allow(ConversationReplyMailer).to receive(:with).and_return(mailer)
allow(mailer).to receive(:conversation_transcript)
post '/api/v1/widget/conversations/transcript',
headers: { 'X-Auth-Token' => token },
@@ -112,7 +114,7 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do
as: :json
expect(response).to have_http_status(:success)
expect(ConversationReplyMailer).to have_received(:conversation_transcript).with(conversation, 'test@test.com')
expect(mailer).to have_received(:conversation_transcript).with(conversation, 'test@test.com')
end
end
end

View File

@@ -15,43 +15,43 @@ describe ::EmailTemplates::DbResolverService do
email_template = create(:email_template, name: 'test', body: 'test')
handler = ActionView::Template.registered_template_handler(:liquid)
template_details = {
locals: [],
format: Mime['html'].to_sym,
updated_at: email_template.updated_at,
virtual_path: 'test'
}
expect(
resolver.find_templates('test', '', false, []).first.to_json
resolver.find_templates('test', '', false, []).first.inspect
).to eq(
ActionView::Template.new(
email_template.body,
"DB Template - #{email_template.id}", handler, template_details
).to_json
"DB Template - #{email_template.id}", handler, **template_details
).inspect
)
end
end
context 'when account template exists in db' do
let(:account) { create(:account) }
let(:installation_template) { create(:email_template, name: 'test', body: 'test') }
let(:account_template) { create(:email_template, name: 'test', body: 'test2', account: account) }
let!(:installation_template) { create(:email_template, name: 'test', body: 'test') }
let!(:account_template) { create(:email_template, name: 'test', body: 'test2', account: account) }
it 'return account template for current account' do
Current.account = account
handler = ActionView::Template.registered_template_handler(:liquid)
template_details = {
locals: [],
format: Mime['html'].to_sym,
updated_at: account_template.updated_at,
virtual_path: 'test'
}
expect(
resolver.find_templates('test', '', false, []).first.to_json
resolver.find_templates('test', '', false, []).first.inspect
).to eq(
ActionView::Template.new(
account_template.body,
"DB Template - #{account_template.id}", handler, template_details
).to_json
"DB Template - #{account_template.id}", handler, **template_details
).inspect
)
Current.account = nil
end
@@ -60,18 +60,18 @@ describe ::EmailTemplates::DbResolverService do
Current.account = create(:account)
handler = ActionView::Template.registered_template_handler(:liquid)
template_details = {
locals: [],
format: Mime['html'].to_sym,
updated_at: installation_template.updated_at,
virtual_path: 'test'
}
expect(
resolver.find_templates('test', '', false, []).first.to_json
resolver.find_templates('test', '', false, []).first.inspect
).to eq(
ActionView::Template.new(
installation_template.body,
"DB Template - #{installation_template.id}", handler, template_details
).to_json
"DB Template - #{installation_template.id}", handler, **template_details
).inspect
)
Current.account = nil
end

View File

@@ -5,7 +5,7 @@ require 'rails_helper'
RSpec.describe 'Confirmation Instructions', type: :mailer do
describe :notify do
let(:account) { create(:account) }
let(:confirmable_user) { build(:user, inviter: inviter_val, account: account) }
let(:confirmable_user) { create(:user, inviter: inviter_val, account: account) }
let(:inviter_val) { nil }
let(:mail) { Devise::Mailer.confirmation_instructions(confirmable_user, nil, {}) }
@@ -27,11 +27,9 @@ 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!"
"#{CGI.escapeHTML(inviter_val.name)}, with #{CGI.escapeHTML(account.name)}, has invited you to try out Chatwoot!"
)
Current.account = nil
end
end
end

View File

@@ -6,15 +6,18 @@ RSpec.describe ConversationReplyEmailWorker, type: :worker do
let(:scheduled_job) { described_class.perform_at(perform_at, 1, Time.zone.now) }
let(:conversation) { build(:conversation, display_id: nil) }
let(:message) { build(:message, conversation: conversation, content_type: 'incoming_email', inbox: conversation.inbox) }
let(:mailer) { double }
let(:mailer_action) { double }
describe 'testing ConversationSummaryEmailWorker' do
before do
conversation.save!
allow(Conversation).to receive(:find).and_return(conversation)
mailer = double
allow(ConversationReplyMailer).to receive(:reply_with_summary).and_return(mailer)
allow(ConversationReplyMailer).to receive(:reply_without_summary).and_return(mailer)
allow(mailer).to receive(:deliver_later).and_return(true)
allow(ConversationReplyMailer).to receive(:with).and_return(mailer)
allow(ConversationReplyMailer).to receive(:with).and_return(mailer)
allow(mailer).to receive(:reply_with_summary).and_return(mailer_action)
allow(mailer).to receive(:reply_without_summary).and_return(mailer_action)
allow(mailer_action).to receive(:deliver_later).and_return(true)
end
it 'worker jobs are enqueued in the mailers queue' do
@@ -32,13 +35,13 @@ RSpec.describe ConversationReplyEmailWorker, type: :worker do
context 'with actions performed by the worker' do
it 'calls ConversationSummaryMailer#reply_with_summary when last incoming message was not email' do
described_class.new.perform(1, Time.zone.now)
expect(ConversationReplyMailer).to have_received(:reply_with_summary)
expect(mailer).to have_received(:reply_with_summary)
end
it 'calls ConversationSummaryMailer#reply_without_summary when last incoming message was from email' do
message.save
described_class.new.perform(1, Time.zone.now)
expect(ConversationReplyMailer).to have_received(:reply_without_summary)
expect(mailer).to have_received(:reply_without_summary)
end
end
end