Overwrite article from based on current user.

This commit is contained in:
Martin Edenhofer 2016-08-24 20:29:56 +02:00
parent 2078a079cd
commit 2f6eb168a2
4 changed files with 35 additions and 13 deletions

View file

@ -35,7 +35,7 @@ class Observer::Ticket::Article::FillupFromEmail < ActiveRecord::Observer
# generate message id, force it in prodution, in test allow to set it for testing reasons
if !record.message_id || Rails.env.production?
fqdn = Setting.get('fqdn')
record.message_id = '<' + DateTime.current.to_s(:number) + '.' + record.ticket_id.to_s + '.' + rand(999_999).to_s() + '@' + fqdn + '>'
record.message_id = "<#{DateTime.current.to_s(:number)}.#{record.ticket_id}.#{rand(999_999)}@#{fqdn}>"
end
# generate message_id_md5

View file

@ -12,14 +12,10 @@ class Observer::Ticket::Article::FillupFromGeneral < ActiveRecord::Observer
# if article and sender type is set via *.postmaster)
return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster'
# if sender is customer, do not change anything
return if !record.sender_id
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
return if sender.nil?
return if sender['name'] == 'Customer'
# set from if not given
return if record.from
# set from on all article types excluding email
return if !record.type_id
type = Ticket::Article::Type.lookup(id: record.type_id)
return if type['name'] == 'email'
return if !record.created_by_id
user = User.find(record.created_by_id)

View file

@ -246,6 +246,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Agent', result['from'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -258,6 +259,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
assert_response(200)
params = {
from: 'something which should not be changed on server side',
ticket_id: ticket.id,
subject: 'some subject',
body: 'some body',
@ -269,6 +271,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Agent via Zammad <zammad@localhost>', result['from'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -285,6 +288,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Agent via Zammad <zammad@localhost>', result['from'])
assert_equal('new subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -342,6 +346,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
assert_equal(1, result['created_by_id'])
params = {
from: 'something which should not be changed on server side',
ticket_id: ticket.id,
subject: 'some subject',
body: 'some body',
@ -351,6 +356,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Admin', result['from'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -368,6 +374,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Admin', result['from'])
assert_equal('new subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -390,6 +397,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Admin via Zammad <zammad@localhost>', result['from'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -530,6 +538,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Customer1', result['from'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -555,6 +564,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Customer1', result['from'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])
@ -569,6 +579,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
assert_equal('Not authorized (admin permission required)!', result['error'])
params = {
from: 'something which should not be changed on server side',
ticket_id: ticket.id,
subject: 'some subject',
body: 'some body',
@ -582,6 +593,7 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
result = JSON.parse(@response.body)
assert_equal(Hash, result.class)
assert_equal(ticket.id, result['ticket_id'])
assert_equal('Tickets Customer1', result['from'])
assert_equal('some subject', result['subject'])
assert_equal('some body', result['body'])
assert_equal('text/plain', result['content_type'])

View file

@ -53,7 +53,7 @@ class TicketTriggerTest < ActiveSupport::TestCase
ticket1 = Ticket.create(
title: "some <b>title</b>\n äöüß",
group: Group.lookup(name: 'Users'),
customer_id: 2,
customer: User.lookup(email: 'nicole.braun@zammad.org'),
state: Ticket::State.lookup(name: 'new'),
priority: Ticket::Priority.lookup(name: '2 normal'),
updated_by_id: 1,
@ -78,6 +78,8 @@ class TicketTriggerTest < ActiveSupport::TestCase
assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
assert_equal(%w(aa kk), Tag.tag_list(object: 'Ticket', o_id: ticket1.id))
article1 = ticket1.articles.last
assert_match('Zammad <zammad@localhost>', article1.from)
assert_match('nicole.braun@zammad.org', article1.to)
assert_match('Thanks for your inquiry (some <b>title</b> äöüß)!', article1.subject)
assert_match('Braun<br>some &lt;b&gt;title&lt;/b&gt;', article1.body)
assert_equal('text/html', article1.content_type)
@ -118,11 +120,17 @@ class TicketTriggerTest < ActiveSupport::TestCase
assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
assert_equal(%w(aa kk), Tag.tag_list(object: 'Ticket', o_id: ticket1.id))
article1 = ticket1.articles.last
assert_match('Zammad <zammad@localhost>', article1.from)
assert_match('nicole.braun@zammad.org', article1.to)
assert_match('Thanks for your inquiry (some <b>title</b> äöüß)!', article1.subject)
assert_match('Braun<br>some &lt;b&gt;title&lt;/b&gt;', article1.body)
assert_equal('text/html', article1.content_type)
ticket2 = Ticket.create(
title: "some title\n äöüß",
group: Group.lookup(name: 'Users'),
customer_id: 2,
customer: User.lookup(email: 'nicole.braun@zammad.org'),
state: Ticket::State.lookup(name: 'open'),
priority: Ticket::Priority.lookup(name: '2 normal'),
updated_by_id: 1,
@ -182,7 +190,7 @@ class TicketTriggerTest < ActiveSupport::TestCase
ticket1 = Ticket.create(
title: "some title\n äöüß",
group: Group.lookup(name: 'Users'),
customer_id: 2,
customer: User.lookup(email: 'nicole.braun@zammad.org'),
state: Ticket::State.lookup(name: 'new'),
priority: Ticket::Priority.lookup(name: '2 normal'),
updated_by_id: 1,
@ -204,6 +212,12 @@ class TicketTriggerTest < ActiveSupport::TestCase
assert_equal('new', ticket1.state.name, 'ticket1.state verify')
assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
article1 = ticket1.articles.last
assert_match('Zammad <zammad@localhost>', article1.from)
assert_match('nicole.braun@zammad.org', article1.to)
assert_match('asdasdas', article1.subject)
assert_match('dasdasdasd', article1.body)
assert_equal('text/html', article1.content_type)
ticket1.priority = Ticket::Priority.lookup(name: '2 normal')
ticket1.save
@ -274,7 +288,7 @@ class TicketTriggerTest < ActiveSupport::TestCase
ticket1 = Ticket.create(
title: "some title\n äöüß",
group: Group.lookup(name: 'Users'),
customer_id: 2,
customer: User.lookup(email: 'nicole.braun@zammad.org'),
state: Ticket::State.lookup(name: 'new'),
priority: Ticket::Priority.lookup(name: '2 normal'),
updated_by_id: 1,