Refactoring: Migrate email_process_sender_name_update_if_needed to RSpec

This commit is contained in:
Ryan Lue 2019-03-14 14:24:48 +08:00 committed by Thorsten Eckel
parent 5b06840b33
commit 17fd53f175
2 changed files with 30 additions and 41 deletions

View file

@ -55,6 +55,36 @@ RSpec.describe Channel::EmailParser, type: :model do
end
end
describe 'auto-updating existing users' do
context 'with a previous email with no real name in the From: header' do
let!(:customer) { Channel::EmailParser.new.process({}, previous_email).first.customer }
let(:previous_email) { <<~RAW.chomp }
From: customer@example.com
To: myzammad@example.com
Subject: test sender name update 1
Some Text
RAW
context 'and a new email with a real name in the From: header' do
let(:new_email) { <<~RAW.chomp }
From: Max Smith <customer@example.com>
To: myzammad@example.com
Subject: test sender name update 2
Some Text
RAW
it 'updates the customers #firstname and #lastname' do
expect { Channel::EmailParser.new.process({}, new_email) }
.to change { customer.reload.firstname }.from('').to('Max')
.and change { customer.reload.lastname }.from('').to('Smith')
end
end
end
end
describe 'associating emails to tickets' do
let(:mail_file) { Rails.root.join('test', 'data', 'mail', 'mail001.box') }
let(:ticket_ref) { Setting.get('ticket_hook') + Setting.get('ticket_hook_divider') + ticket.number }

View file

@ -1,41 +0,0 @@
require 'test_helper'
class EmailProcessSenderNameUpdateIfNeeded < ActiveSupport::TestCase
test 'basic' do
email_raw_string = "From: customer@example.com
To: myzammad@example.com
Subject: test sender name update 1
Some Text"
ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
ticket = Ticket.find(ticket_p.id)
article = Ticket::Article.find(article_p.id)
assert_equal('test sender name update 1', ticket.title)
assert_equal('new', ticket.state.name)
assert_equal('Customer', ticket.create_article_sender.name)
assert_equal('Customer', article.sender.name)
assert_equal('customer@example.com', ticket.customer.email)
assert_equal('', ticket.customer.firstname)
assert_equal('', ticket.customer.lastname)
email_raw_string = "From: Max Smith <customer@example.com>
To: myzammad@example.com
Subject: test sender name update 2
Some Text"
ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string)
ticket = Ticket.find(ticket_p.id)
article = Ticket::Article.find(article_p.id)
assert_equal('test sender name update 2', ticket.title)
assert_equal('new', ticket.state.name)
assert_equal('Customer', ticket.create_article_sender.name)
assert_equal('Customer', article.sender.name)
assert_equal('customer@example.com', ticket.customer.email)
assert_equal('Max', ticket.customer.firstname)
assert_equal('Smith', ticket.customer.lastname)
end
end