diff --git a/app/models/ticket/article/adds_metadata_general.rb b/app/models/ticket/article/adds_metadata_general.rb index 954e4618c..704e9abd6 100644 --- a/app/models/ticket/article/adds_metadata_general.rb +++ b/app/models/ticket/article/adds_metadata_general.rb @@ -55,7 +55,9 @@ module Ticket::Article::AddsMetadataGeneral return true if user_id.blank? user = User.find(user_id) - if type.name == 'web' || type.name == 'phone' + is_customer = !TicketPolicy.new(user, ticket).agent_read_access? + + if (type.name == 'web' || type.name == 'phone') && is_customer self.from = "#{user.firstname} #{user.lastname} <#{user.email}>" return end diff --git a/spec/factories/ticket/article.rb b/spec/factories/ticket/article.rb index aa6134052..1ab1726c0 100644 --- a/spec/factories/ticket/article.rb +++ b/spec/factories/ticket/article.rb @@ -66,6 +66,13 @@ FactoryBot.define do end end + trait :outbound_web do + transient do + type_name { 'web' } + sender_name { 'Agent' } + end + end + factory :twitter_article do transient do type_name { 'twitter status' } diff --git a/spec/models/ticket/article/adds_metadata_general_spec.rb b/spec/models/ticket/article/adds_metadata_general_spec.rb index 8ae784366..c5627466d 100644 --- a/spec/models/ticket/article/adds_metadata_general_spec.rb +++ b/spec/models/ticket/article/adds_metadata_general_spec.rb @@ -2,17 +2,33 @@ require 'rails_helper' -RSpec.describe Ticket::Article::AddsMetadataGeneral, current_user_id: -> { agent.id } do +RSpec.describe Ticket::Article::AddsMetadataGeneral do let(:agent) { create(:agent) } - context 'when customer is agent' do - let(:customer) { create(:agent) } + context 'when Agent creates Article' do + shared_examples 'not including email in from' do |factory| + subject(:article) { create(:ticket_article, factory, ticket: ticket, created_by_id: agent.id, updated_by_id: agent.id ) } - it 'show customer agent details in from field' do - ticket = create(:ticket, customer_id: customer.id) - article = create(:ticket_article, :inbound_phone, ticket: ticket) + let(:ticket) { create(:ticket) } + let!(:agent) { create(:agent, groups: [ticket.group]) } - expect(article.from).to include customer.email + it "doesn't include email in from" do + expect(article.from).not_to include agent.email + end + end + + it_behaves_like 'not including email in from', :outbound_phone + it_behaves_like 'not including email in from', :outbound_web + + context 'when as Customer' do + subject(:article) { create(:ticket_article, :inbound_phone, ticket: ticket) } + + let(:customer) { agent } + let(:ticket) { create(:ticket, customer_id: customer.id) } + + it 'includes email in from' do + expect(article.from).not_to include agent.email + end end end end