2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-06-25 06:54:25 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class EmailDeliverTest < ActiveSupport::TestCase
|
|
|
|
test 'basic check' do
|
2019-02-20 11:18:47 +00:00
|
|
|
travel_to DateTime.current
|
2016-06-25 06:54:25 +00:00
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
if ENV['MAIL_SERVER'].blank?
|
2016-06-25 06:54:25 +00:00
|
|
|
raise "Need MAIL_SERVER as ENV variable like export MAIL_SERVER='mx.example.com'"
|
|
|
|
end
|
2017-07-20 12:01:14 +00:00
|
|
|
if ENV['MAIL_SERVER_ACCOUNT'].blank?
|
2016-06-25 06:54:25 +00:00
|
|
|
raise "Need MAIL_SERVER_ACCOUNT as ENV variable like export MAIL_SERVER_ACCOUNT='user:somepass'"
|
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-09-09 12:45:44 +00:00
|
|
|
if ENV['MAIL_SERVER_EMAIL'].blank?
|
|
|
|
raise "Need MAIL_SERVER_EMAIL as ENV variable like export MAIL_SERVER_EMAIL='someunitest@example.com'"
|
|
|
|
end
|
|
|
|
|
2016-06-25 06:54:25 +00:00
|
|
|
server_login = ENV['MAIL_SERVER_ACCOUNT'].split(':')[0]
|
|
|
|
server_password = ENV['MAIL_SERVER_ACCOUNT'].split(':')[1]
|
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
email_address = EmailAddress.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
realname: 'me Helpdesk',
|
2019-09-09 12:45:44 +00:00
|
|
|
email: "some-zammad-#{ENV['MAIL_SERVER_EMAIL']}",
|
2016-06-25 06:54:25 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
group = Group.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'DeliverTest',
|
2016-06-25 06:54:25 +00:00
|
|
|
email_address_id: email_address.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2016-06-25 06:54:25 +00:00
|
|
|
)
|
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
channel = Channel.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
area: 'Email::Account',
|
|
|
|
group_id: group.id,
|
|
|
|
options: {
|
|
|
|
inbound: {
|
2016-06-25 06:54:25 +00:00
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: 'mx1.example.com',
|
|
|
|
user: 'example',
|
2016-06-25 06:54:25 +00:00
|
|
|
password: 'some_pw',
|
2018-12-19 17:31:51 +00:00
|
|
|
ssl: true,
|
2016-06-25 06:54:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'sendmail'
|
|
|
|
}
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2016-06-25 06:54:25 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
email_address.channel_id = channel.id
|
2017-07-20 12:01:14 +00:00
|
|
|
email_address.save!
|
2016-06-25 06:54:25 +00:00
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
ticket1 = Ticket.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
title: 'some delivery test',
|
|
|
|
group: group,
|
|
|
|
customer_id: 2,
|
|
|
|
state: Ticket::State.lookup(name: 'new'),
|
|
|
|
priority: Ticket::Priority.lookup(name: '2 normal'),
|
2016-06-25 06:54:25 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
assert(ticket1, 'ticket created')
|
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
article1 = Ticket::Article.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_id: ticket1.id,
|
|
|
|
to: 'some_recipient@example_not_existing_what_ever.com',
|
|
|
|
subject: 'some subject',
|
|
|
|
message_id: 'some@id',
|
|
|
|
body: 'some message delivery test',
|
|
|
|
internal: false,
|
|
|
|
sender: Ticket::Article::Sender.find_by(name: 'Agent'),
|
|
|
|
type: Ticket::Article::Type.find_by(name: 'email'),
|
2016-06-25 06:54:25 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
2017-03-24 18:42:28 +00:00
|
|
|
assert_nil(article1.preferences['delivery_retry'])
|
|
|
|
assert_nil(article1.preferences['delivery_status'])
|
|
|
|
assert_nil(article1.preferences['delivery_status_date'])
|
|
|
|
assert_nil(article1.preferences['delivery_status_message'])
|
2016-06-25 06:54:25 +00:00
|
|
|
|
2019-02-20 11:18:47 +00:00
|
|
|
TicketArticleCommunicateEmailJob.new.perform(article1.id)
|
2016-06-25 06:54:25 +00:00
|
|
|
|
|
|
|
article1_lookup = Ticket::Article.find(article1.id)
|
|
|
|
assert_equal(1, article1_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('success', article1_lookup.preferences['delivery_status'])
|
|
|
|
assert(article1_lookup.preferences['delivery_status_date'])
|
2017-03-24 18:42:28 +00:00
|
|
|
assert_nil(article1_lookup.preferences['delivery_status_message'])
|
2016-06-25 06:54:25 +00:00
|
|
|
|
|
|
|
# send with invalid smtp settings
|
2017-09-11 11:16:08 +00:00
|
|
|
channel.update!(
|
2016-06-25 06:54:25 +00:00
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
inbound: {
|
2016-06-25 06:54:25 +00:00
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: 'mx1.example.com',
|
|
|
|
user: 'example',
|
2016-06-25 06:54:25 +00:00
|
|
|
password: 'some_pw',
|
2018-12-19 17:31:51 +00:00
|
|
|
ssl: true,
|
2016-06-25 06:54:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: 'mx1.example.com',
|
|
|
|
port: 25,
|
2016-06-25 06:54:25 +00:00
|
|
|
start_tls: true,
|
2018-12-19 17:31:51 +00:00
|
|
|
user: 'not_existing',
|
|
|
|
password: 'not_existing',
|
2016-06-25 06:54:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
assert_raises(RuntimeError) do
|
2019-02-20 11:18:47 +00:00
|
|
|
TicketArticleCommunicateEmailJob.new.perform(article1.id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-25 06:54:25 +00:00
|
|
|
article1_lookup = Ticket::Article.find(article1.id)
|
|
|
|
assert_equal(2, article1_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('fail', article1_lookup.preferences['delivery_status'])
|
|
|
|
assert(article1_lookup.preferences['delivery_status_date'])
|
|
|
|
assert(article1_lookup.preferences['delivery_status_message'])
|
|
|
|
|
|
|
|
# send with correct smtp settings
|
2017-09-11 11:16:08 +00:00
|
|
|
channel.update!(
|
2016-06-25 06:54:25 +00:00
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
inbound: {
|
2016-06-25 06:54:25 +00:00
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: 'mx1.example.com',
|
|
|
|
user: 'example',
|
2016-06-25 06:54:25 +00:00
|
|
|
password: 'some_pw',
|
2018-12-19 17:31:51 +00:00
|
|
|
ssl: true,
|
2016-06-25 06:54:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: ENV['MAIL_SERVER'],
|
|
|
|
port: 25,
|
2016-06-25 06:54:25 +00:00
|
|
|
start_tls: true,
|
2018-12-19 17:31:51 +00:00
|
|
|
user: server_login,
|
|
|
|
password: server_password,
|
2016-06-25 06:54:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-02-20 11:18:47 +00:00
|
|
|
TicketArticleCommunicateEmailJob.new.perform(article1.id)
|
2016-06-25 06:54:25 +00:00
|
|
|
article1_lookup = Ticket::Article.find(article1.id)
|
|
|
|
assert_equal(3, article1_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('success', article1_lookup.preferences['delivery_status'])
|
|
|
|
assert(article1_lookup.preferences['delivery_status_date'])
|
|
|
|
assert_nil(article1_lookup.preferences['delivery_status_message'])
|
|
|
|
|
|
|
|
# check retry jobs
|
|
|
|
# remove background jobs
|
|
|
|
Delayed::Job.destroy_all
|
|
|
|
|
|
|
|
# send with invalid smtp settings
|
2017-09-11 11:16:08 +00:00
|
|
|
channel.update!(
|
2016-06-25 06:54:25 +00:00
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
inbound: {
|
2016-06-25 06:54:25 +00:00
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: 'mx1.example.com',
|
|
|
|
user: 'example',
|
2016-06-25 06:54:25 +00:00
|
|
|
password: 'some_pw',
|
2018-12-19 17:31:51 +00:00
|
|
|
ssl: true,
|
2016-06-25 06:54:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'smtp',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: 'mx1.example.com',
|
|
|
|
port: 25,
|
2016-06-25 06:54:25 +00:00
|
|
|
start_tls: true,
|
2018-12-19 17:31:51 +00:00
|
|
|
user: 'not_existing',
|
|
|
|
password: 'not_existing',
|
2016-06-25 06:54:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
# remove background jobs
|
|
|
|
Delayed::Job.destroy_all
|
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
article2 = Ticket::Article.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_id: ticket1.id,
|
|
|
|
to: 'some_recipient@example_not_existing_what_ever.com',
|
|
|
|
subject: 'some subject2',
|
|
|
|
message_id: 'some@id',
|
|
|
|
body: 'some message delivery test2',
|
|
|
|
internal: false,
|
|
|
|
sender: Ticket::Article::Sender.find_by(name: 'Agent'),
|
|
|
|
type: Ticket::Article::Type.find_by(name: 'email'),
|
2016-06-25 06:54:25 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
2017-07-26 18:46:31 +00:00
|
|
|
ticket1.state = Ticket::State.find_by(name: 'closed')
|
|
|
|
ticket1.save
|
|
|
|
|
2019-02-20 11:18:47 +00:00
|
|
|
assert(Delayed::Job.where(attempts: 1).none?)
|
|
|
|
Scheduler.worker(true)
|
2020-09-30 09:07:01 +00:00
|
|
|
assert(Delayed::Job.exists?(attempts: 1))
|
2017-07-26 18:46:31 +00:00
|
|
|
ticket1.reload
|
2016-06-25 06:54:25 +00:00
|
|
|
|
|
|
|
article2_lookup = Ticket::Article.find(article2.id)
|
|
|
|
assert_equal(2, ticket1.articles.count)
|
|
|
|
assert_equal(1, article2_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('fail', article2_lookup.preferences['delivery_status'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_date'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_message'])
|
|
|
|
|
|
|
|
Scheduler.worker(true)
|
2017-07-26 18:46:31 +00:00
|
|
|
ticket1.reload
|
2016-06-25 06:54:25 +00:00
|
|
|
|
|
|
|
article2_lookup = Ticket::Article.find(article2.id)
|
|
|
|
assert_equal(2, ticket1.articles.count)
|
|
|
|
assert_equal(1, article2_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('fail', article2_lookup.preferences['delivery_status'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_date'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_message'])
|
2017-07-26 18:46:31 +00:00
|
|
|
assert_equal('closed', ticket1.state.name)
|
2016-06-25 06:54:25 +00:00
|
|
|
|
2019-02-20 11:18:47 +00:00
|
|
|
travel 26.seconds
|
|
|
|
assert(Delayed::Job.where(attempts: 2).none?)
|
|
|
|
Scheduler.worker(true)
|
2020-09-30 09:07:01 +00:00
|
|
|
assert(Delayed::Job.exists?(attempts: 2))
|
2017-07-26 18:46:31 +00:00
|
|
|
ticket1.reload
|
2016-06-25 06:54:25 +00:00
|
|
|
|
|
|
|
article2_lookup = Ticket::Article.find(article2.id)
|
|
|
|
assert_equal(2, ticket1.articles.count)
|
|
|
|
assert_equal(2, article2_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('fail', article2_lookup.preferences['delivery_status'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_date'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_message'])
|
2017-07-26 18:46:31 +00:00
|
|
|
assert_equal('closed', ticket1.state.name)
|
2016-06-25 06:54:25 +00:00
|
|
|
|
|
|
|
Scheduler.worker(true)
|
2017-07-26 18:46:31 +00:00
|
|
|
ticket1.reload
|
|
|
|
|
2016-06-25 06:54:25 +00:00
|
|
|
article2_lookup = Ticket::Article.find(article2.id)
|
|
|
|
assert_equal(2, ticket1.articles.count)
|
|
|
|
assert_equal(2, article2_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('fail', article2_lookup.preferences['delivery_status'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_date'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_message'])
|
2017-07-26 18:46:31 +00:00
|
|
|
assert_equal('closed', ticket1.state.name)
|
2016-06-25 06:54:25 +00:00
|
|
|
|
2019-02-20 11:18:47 +00:00
|
|
|
travel 51.seconds
|
|
|
|
assert(Delayed::Job.where(attempts: 3).none?)
|
|
|
|
Scheduler.worker(true)
|
2020-09-30 09:07:01 +00:00
|
|
|
assert(Delayed::Job.exists?(attempts: 3))
|
2017-07-26 18:46:31 +00:00
|
|
|
ticket1.reload
|
|
|
|
|
2016-06-25 06:54:25 +00:00
|
|
|
article2_lookup = Ticket::Article.find(article2.id)
|
|
|
|
assert_equal(2, ticket1.articles.count)
|
|
|
|
assert_equal(3, article2_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('fail', article2_lookup.preferences['delivery_status'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_date'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_message'])
|
2017-07-26 18:46:31 +00:00
|
|
|
assert_equal('closed', ticket1.state.name)
|
2016-06-25 06:54:25 +00:00
|
|
|
|
2019-02-20 11:18:47 +00:00
|
|
|
travel 76.seconds
|
|
|
|
assert(Delayed::Job.where(attempts: 4).none?)
|
2017-10-01 12:25:52 +00:00
|
|
|
assert_raises(RuntimeError) do
|
2016-06-27 12:39:40 +00:00
|
|
|
Scheduler.worker(true)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-04-17 12:24:52 +00:00
|
|
|
assert(Delayed::Job.none?)
|
2017-07-26 18:46:31 +00:00
|
|
|
ticket1.reload
|
|
|
|
|
2016-06-25 06:54:25 +00:00
|
|
|
article2_lookup = Ticket::Article.find(article2.id)
|
2016-06-27 11:24:48 +00:00
|
|
|
article_delivery_system = ticket1.articles.last
|
2016-06-25 06:54:25 +00:00
|
|
|
assert_equal(3, ticket1.articles.count)
|
|
|
|
assert_equal(4, article2_lookup.preferences['delivery_retry'])
|
|
|
|
assert_equal('fail', article2_lookup.preferences['delivery_status'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_date'])
|
|
|
|
assert(article2_lookup.preferences['delivery_status_message'])
|
2016-06-27 11:24:48 +00:00
|
|
|
assert_equal('System', article_delivery_system.sender.name)
|
|
|
|
assert_equal(true, article_delivery_system.preferences['delivery_message'])
|
|
|
|
assert_equal(article2.id, article_delivery_system.preferences['delivery_article_id_related'])
|
2017-07-26 18:46:31 +00:00
|
|
|
assert_equal(true, article_delivery_system.preferences['notification'])
|
|
|
|
assert_equal(Ticket::State.find_by(default_follow_up: true).name, ticket1.state.name)
|
2016-06-25 06:54:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|