Refactoring: Migrate email_process_state_open_test to RSpec

This commit is contained in:
Ryan Lue 2019-05-06 14:06:30 +08:00 committed by Martin Edenhofer
parent d4b0d953d0
commit 3b1e51c789
3 changed files with 24 additions and 153 deletions

View file

@ -121,6 +121,12 @@ RSpec.describe Channel::EmailParser, type: :model do
expect(Ticket::Article.last.sender.name).to eq('Agent')
end
it 'sets ticket.state to "new"' do
Channel::EmailParser.new.process({}, raw_mail)
expect(Ticket.last.state.name).to eq('new')
end
end
context 'when from address matches an existing customer' do
@ -131,6 +137,12 @@ RSpec.describe Channel::EmailParser, type: :model do
expect(Ticket.last.articles.first.sender.name).to eq('Customer')
end
it 'sets ticket.state to "new"' do
Channel::EmailParser.new.process({}, raw_mail)
expect(Ticket.last.state.name).to eq('new')
end
end
context 'when from address is unrecognized' do

View file

@ -339,12 +339,22 @@ RSpec.describe Ticket, type: :model do
end
describe '#state' do
context 'when originally "new"' do
context 'when originally "new" (default)' do
context 'and a customer article is added' do
let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Customer') }
it 'stays "new"' do
expect { article }
.not_to change { ticket.state.name }.from('new')
end
end
context 'and a non-customer article is added' do
let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Agent') }
it 'switches to "open"' do
expect { article }.to change { ticket.state.name }.from('new').to('open')
expect { article }
.to change { ticket.state.name }.from('new').to('open')
end
end
end

View file

@ -1,151 +0,0 @@
require 'test_helper'
class EmailProcessStateOpenSet < ActiveSupport::TestCase
setup do
groups = Group.all
roles = Role.where(name: 'Agent')
@agent1 = User.create_or_update(
login: 'agent-ticket-state-open-set@example.com',
firstname: 'StateOpenSet',
lastname: 'Agent1',
email: 'agent-ticket-state-open-set@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
updated_by_id: 1,
created_by_id: 1,
)
roles = Role.where(name: 'Customer')
@customer1 = User.create_or_update(
login: 'customer-ticket-state-open-set@example.com',
firstname: 'StateOpenSet',
lastname: 'Customer',
email: 'customer-ticket-state-open-set@example.com',
password: 'customerpw',
active: true,
roles: roles,
updated_by_id: 1,
created_by_id: 1,
)
end
test 'new as agent' do
email_raw_string = "From: agent-ticket-state-open-set@example.com
To: customer-ticket-state-open-set@example.com
Subject: test sender is agent
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 is agent', ticket.title)
assert_equal('new', ticket.state.name)
assert_equal('Agent', ticket.create_article_sender.name)
assert_equal('Agent', article.sender.name)
assert_equal('customer-ticket-state-open-set@example.com', ticket.customer.email)
assert_equal('StateOpenSet', ticket.customer.firstname)
assert_equal('Customer', ticket.customer.lastname)
article = Ticket::Article.create!(
ticket_id: ticket.id,
type_id: Ticket::Article::Type.find_by(name: 'email').id,
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
body: 'test',
internal: false,
updated_by_id: @customer1.id,
created_by_id: @customer1.id,
)
ticket.reload
assert_equal('test sender is agent', ticket.title)
assert_equal('new', ticket.state.name)
assert_equal('Agent', ticket.create_article_sender.name)
assert_equal('Customer', article.sender.name)
assert_equal('customer-ticket-state-open-set@example.com', ticket.customer.email)
assert_equal('StateOpenSet', ticket.customer.firstname)
assert_equal('Customer', ticket.customer.lastname)
article = Ticket::Article.create!(
ticket_id: ticket.id,
type_id: Ticket::Article::Type.find_by(name: 'email').id,
sender_id: Ticket::Article::Sender.find_by(name: 'Agent').id,
body: 'test',
internal: false,
updated_by_id: @agent1.id,
created_by_id: @agent1.id,
)
ticket.reload
assert_equal('test sender is agent', ticket.title)
assert_equal('open', ticket.state.name)
assert_equal('Agent', ticket.create_article_sender.name)
assert_equal('Agent', article.sender.name)
assert_equal('customer-ticket-state-open-set@example.com', ticket.customer.email)
assert_equal('StateOpenSet', ticket.customer.firstname)
assert_equal('Customer', ticket.customer.lastname)
end
test 'new as customer' do
email_raw_string = "From: customer-ticket-state-open-set@example.com
To: agent-ticket-state-open-set@example.com
Subject: test sender is customer
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 is customer', 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-ticket-state-open-set@example.com', ticket.customer.email)
assert_equal('StateOpenSet', ticket.customer.firstname)
assert_equal('Customer', ticket.customer.lastname)
article = Ticket::Article.create!(
ticket_id: ticket.id,
type_id: Ticket::Article::Type.find_by(name: 'email').id,
sender_id: Ticket::Article::Sender.find_by(name: 'Customer').id,
body: 'test',
internal: false,
updated_by_id: @customer1.id,
created_by_id: @customer1.id,
)
ticket.reload
assert_equal('test sender is customer', 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-ticket-state-open-set@example.com', ticket.customer.email)
assert_equal('StateOpenSet', ticket.customer.firstname)
assert_equal('Customer', ticket.customer.lastname)
article = Ticket::Article.create!(
ticket_id: ticket.id,
type_id: Ticket::Article::Type.find_by(name: 'email').id,
sender_id: Ticket::Article::Sender.find_by(name: 'Agent').id,
body: 'test',
internal: false,
updated_by_id: @agent1.id,
created_by_id: @agent1.id,
)
ticket.reload
assert_equal('test sender is customer', ticket.title)
assert_equal('open', ticket.state.name)
assert_equal('Customer', ticket.create_article_sender.name)
assert_equal('Agent', article.sender.name)
assert_equal('customer-ticket-state-open-set@example.com', ticket.customer.email)
assert_equal('StateOpenSet', ticket.customer.firstname)
assert_equal('Customer', ticket.customer.lastname)
end
end