2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
require 'test_helper'
|
|
|
|
require 'net/imap'
|
|
|
|
|
|
|
|
class EmailKeepOnServerTest < ActiveSupport::TestCase
|
2021-07-09 13:05:05 +00:00
|
|
|
setup do
|
2017-07-20 12:01:14 +00:00
|
|
|
|
|
|
|
if ENV['KEEP_ON_MAIL_SERVER'].blank?
|
|
|
|
raise "Need KEEP_ON_MAIL_SERVER as ENV variable like export KEEP_ON_MAIL_SERVER='mx.example.com'"
|
|
|
|
end
|
|
|
|
if ENV['KEEP_ON_MAIL_SERVER_ACCOUNT'].blank?
|
|
|
|
raise "Need KEEP_ON_MAIL_SERVER_ACCOUNT as ENV variable like export KEEP_ON_MAIL_SERVER_ACCOUNT='user:somepass'"
|
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-07-20 12:01:14 +00:00
|
|
|
@server_login = ENV['KEEP_ON_MAIL_SERVER_ACCOUNT'].split(':')[0]
|
|
|
|
@server_password = ENV['KEEP_ON_MAIL_SERVER_ACCOUNT'].split(':')[1]
|
|
|
|
|
|
|
|
@folder = "keep_on_mail_server_#{rand(999_999_999)}"
|
|
|
|
|
|
|
|
email_address = EmailAddress.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
realname: 'me Helpdesk',
|
|
|
|
email: "me#{rand(999_999_999)}@example.com",
|
2017-07-20 12:01:14 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
group = Group.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'KeepOnServerTest',
|
2017-07-20 12:01:14 +00:00
|
|
|
email_address_id: email_address.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
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: {
|
2017-07-20 12:01:14 +00:00
|
|
|
adapter: 'imap',
|
|
|
|
options: {
|
2018-12-19 17:31:51 +00:00
|
|
|
host: ENV['KEEP_ON_MAIL_SERVER'],
|
|
|
|
user: @server_login,
|
2017-07-20 12:01:14 +00:00
|
|
|
password: @server_password,
|
2018-12-19 17:31:51 +00:00
|
|
|
ssl: true,
|
|
|
|
folder: @folder,
|
2021-07-16 13:44:10 +00:00
|
|
|
# keep_on_server: true,
|
2017-07-20 12:01:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
outbound: {
|
|
|
|
adapter: 'sendmail'
|
|
|
|
}
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2017-07-20 12:01:14 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
email_address.channel_id = @channel.id
|
|
|
|
email_address.save!
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'keep on server' do
|
|
|
|
@channel.options[:inbound][:options][:keep_on_server] = true
|
|
|
|
@channel.save!
|
|
|
|
|
|
|
|
# clean mailbox
|
|
|
|
imap = Net::IMAP.new(ENV['KEEP_ON_MAIL_SERVER'], 993, true, nil, false)
|
|
|
|
imap.login(@server_login, @server_password)
|
|
|
|
imap.create(@folder)
|
|
|
|
imap.select(@folder)
|
|
|
|
|
|
|
|
# put unseen message in it
|
|
|
|
imap.append(@folder, "Subject: hello1
|
|
|
|
From: shugo@example.com
|
|
|
|
To: shugo@example.com
|
|
|
|
Message-ID: <some1@example_keep_on_server>
|
|
|
|
|
|
|
|
hello world
|
2021-05-12 11:37:44 +00:00
|
|
|
".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
|
2017-07-20 12:01:14 +00:00
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
message_meta = imap.fetch(1, ['FLAGS'])[0].attr
|
|
|
|
assert_not(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
|
|
|
|
# fetch messages
|
|
|
|
article_count = Ticket::Article.count
|
|
|
|
@channel.fetch(true)
|
|
|
|
assert_equal(article_count + 1, Ticket::Article.count)
|
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
message_meta = imap.fetch(1, ['RFC822.HEADER', 'FLAGS'])[0].attr
|
|
|
|
assert(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
|
|
|
|
# fetch messages
|
|
|
|
article_count = Ticket::Article.count
|
|
|
|
@channel.fetch(true)
|
|
|
|
assert_equal(article_count, Ticket::Article.count)
|
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
# put unseen message in it
|
|
|
|
imap.append(@folder, "Subject: hello2
|
|
|
|
From: shugo@example.com
|
|
|
|
To: shugo@example.com
|
|
|
|
Message-ID: <some2@example_keep_on_server>
|
|
|
|
|
|
|
|
hello world
|
2021-05-12 11:37:44 +00:00
|
|
|
".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
|
2017-07-20 12:01:14 +00:00
|
|
|
|
|
|
|
message_meta = imap.fetch(1, ['FLAGS'])[0].attr
|
|
|
|
assert(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
message_meta = imap.fetch(2, ['FLAGS'])[0].attr
|
|
|
|
assert_not(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
|
|
|
|
# fetch messages
|
|
|
|
article_count = Ticket::Article.count
|
|
|
|
@channel.fetch(true)
|
|
|
|
assert_equal(article_count + 1, Ticket::Article.count)
|
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(2, message_ids.count)
|
|
|
|
|
|
|
|
message_meta = imap.fetch(1, ['FLAGS'])[0].attr
|
|
|
|
assert(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
message_meta = imap.fetch(2, ['FLAGS'])[0].attr
|
|
|
|
assert(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
|
|
|
|
# set messages to not seen
|
|
|
|
imap.store(1, '-FLAGS', [:Seen])
|
|
|
|
imap.store(2, '-FLAGS', [:Seen])
|
|
|
|
|
|
|
|
# fetch messages
|
|
|
|
article_count = Ticket::Article.count
|
|
|
|
@channel.fetch(true)
|
|
|
|
assert_equal(article_count, Ticket::Article.count)
|
|
|
|
|
|
|
|
imap.delete(@folder)
|
|
|
|
@channel.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'keep not on server' do
|
|
|
|
@channel.options[:inbound][:options][:keep_on_server] = false
|
|
|
|
@channel.save!
|
|
|
|
|
|
|
|
# clean mailbox
|
|
|
|
imap = Net::IMAP.new(ENV['KEEP_ON_MAIL_SERVER'], 993, true, nil, false)
|
|
|
|
imap.login(@server_login, @server_password)
|
|
|
|
imap.create(@folder)
|
|
|
|
imap.select(@folder)
|
|
|
|
|
|
|
|
# put unseen message in it
|
|
|
|
imap.append(@folder, "Subject: hello1
|
|
|
|
From: shugo@example.com
|
|
|
|
To: shugo@example.com
|
|
|
|
Message-ID: <some1@example_remove_from_server>
|
|
|
|
|
|
|
|
hello world
|
2021-05-12 11:37:44 +00:00
|
|
|
".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
|
2017-07-20 12:01:14 +00:00
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
message_meta = imap.fetch(1, ['FLAGS'])[0].attr
|
|
|
|
assert_not(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
|
|
|
|
# fetch messages
|
|
|
|
article_count = Ticket::Article.count
|
|
|
|
@channel.fetch(true)
|
|
|
|
assert_equal(article_count + 1, Ticket::Article.count)
|
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
# put unseen message in it
|
|
|
|
imap.append(@folder, "Subject: hello2
|
|
|
|
From: shugo@example.com
|
|
|
|
To: shugo@example.com
|
|
|
|
Message-ID: <some2@example_remove_from_server>
|
|
|
|
|
|
|
|
hello world
|
2021-05-12 11:37:44 +00:00
|
|
|
".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
|
2017-07-20 12:01:14 +00:00
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
message_meta = imap.fetch(1, ['FLAGS'])[0].attr
|
|
|
|
assert_not(message_meta['FLAGS'].include?(:Seen))
|
|
|
|
|
|
|
|
# fetch messages
|
|
|
|
article_count = Ticket::Article.count
|
|
|
|
@channel.fetch(true)
|
|
|
|
assert_equal(article_count + 1, Ticket::Article.count)
|
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
# put unseen message in it
|
|
|
|
imap.append(@folder, "Subject: hello2
|
|
|
|
From: shugo@example.com
|
|
|
|
To: shugo@example.com
|
|
|
|
Message-ID: <some2@example_remove_from_server>
|
|
|
|
|
|
|
|
hello world
|
2021-05-12 11:37:44 +00:00
|
|
|
".gsub(%r{\n}, "\r\n"), [], Time.zone.now)
|
2017-07-20 12:01:14 +00:00
|
|
|
|
|
|
|
# verify if message is still on server
|
|
|
|
message_ids = imap.sort(['DATE'], ['ALL'], 'US-ASCII')
|
|
|
|
assert_equal(1, message_ids.count)
|
|
|
|
|
|
|
|
# fetch messages
|
|
|
|
article_count = Ticket::Article.count
|
|
|
|
@channel.fetch(true)
|
|
|
|
assert_equal(article_count + 1, Ticket::Article.count)
|
|
|
|
|
|
|
|
imap.delete(@folder)
|
|
|
|
@channel.destroy!
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|