Refactoring: Migrate notification_factory_slack_template_test to RSpec

This commit is contained in:
Ryan Lue 2019-02-19 15:01:54 +08:00 committed by Thorsten Eckel
parent d281c1b177
commit 755c8afb6e
2 changed files with 95 additions and 144 deletions

View file

@ -0,0 +1,95 @@
require 'rails_helper'
RSpec.describe NotificationFactory::Slack do
describe '.template' do
subject(:template) do
NotificationFactory::Slack.template(
template: action,
locale: 'en-us',
timezone: 'Europe/Berlin',
objects: {
ticket: ticket,
article: article,
recipient: agent,
current_user: current_user,
changes: changes,
}
)
end
let(:ticket) { article.ticket }
let(:article) { create(:ticket_article) }
let(:agent) { create(:agent_user) }
let(:current_user) { create(:agent_user) }
context 'for "ticket_create", with an empty "changes" hash' do
let(:action) { 'ticket_create' }
let(:changes) { {} }
it 'returns a hash with subject: <ticket title> (as Markdown heading)' do
expect(template).to include(subject: "# #{ticket.title}")
end
it 'returns a hash with body: <article author & body>' do
expect(template[:body])
.to match(/Created by #{current_user.fullname}/)
.and match(/#{article.body}\z/)
end
end
context 'for "ticket_update", with a populated "changes" hash' do
let(:action) { 'ticket_update' }
let(:changes) do
{
state: %w[aaa bbb],
group: %w[xxx yyy],
pending_time: [Time.zone.parse('2019-04-01T10:00:00Z0'), Time.zone.parse('2019-04-01T23:00:00Z0')],
}
end
it 'returns a hash with subject: <ticket title> (as Markdown heading)' do
expect(template).to include(subject: "# #{ticket.title}")
end
it 'returns a hash with body: <article editor, changes, & body>' do
expect(template[:body])
.to match(/Updated by #{current_user.fullname}/)
.and match(/state: aaa -> bbb/)
.and match(/group: xxx -> yyy/)
.and match(%r{pending_time: 04/01/2019 12:00 \(Europe/Berlin\) -> 04/02/2019 01:00 \(Europe/Berlin\)})
.and match(/#{article.body}\z/)
end
end
context 'for "ticket_escalate"' do
before { ticket.escalation_at = escalation_time }
let(:escalation_time) { Time.zone.parse('2019-04-01T10:00:00Z') }
subject(:template) do
NotificationFactory::Slack.template(
template: 'ticket_escalation',
locale: 'en-us',
timezone: 'Europe/Berlin',
objects: {
ticket: ticket,
article: article,
recipient: agent,
}
)
end
it 'returns a hash with subject: <ticket title> (as Markdown heading)' do
expect(template).to include(subject: "# #{ticket.title}")
end
it 'returns a hash with body: <ticket customer, escalation time, & body>' do
expect(template[:body])
.to match(/A ticket \(#{ticket.title}\) from "#{ticket.customer.fullname}"/)
.and match(%r{is escalated since "04/01/2019 12:00 \(Europe/Berlin\)"!})
.and match(/#{article.body}\z/)
end
end
end
end

View file

@ -1,144 +0,0 @@
require 'test_helper'
class NotificationFactorySlackTemplateTest < ActiveSupport::TestCase
test 'notifications template' do
Translation.load('de-de')
groups = Group.where(name: 'Users')
roles = Role.where(name: 'Agent')
agent1 = User.create_or_update(
login: 'notification-template-agent1@example.com',
firstname: 'Notification<b>xxx</b>',
lastname: 'Agent1<b>yyy</b>',
email: 'notification-template-agent1@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
preferences: {
locale: 'de-de',
},
updated_by_id: 1,
created_by_id: 1,
)
agent_current_user = User.create_or_update(
login: 'notification-template-current_user@example.com',
firstname: 'Notification Current',
lastname: 'User<b>xxx</b>',
email: 'notification-template-current_user@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
preferences: {
locale: 'de-de',
},
updated_by_id: 1,
created_by_id: 1,
)
ticket = Ticket.create(
group_id: Group.lookup(name: 'Users').id,
customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
owner_id: User.lookup(login: '-').id,
title: 'Welcome to Zammad!',
state_id: Ticket::State.lookup(name: 'new').id,
priority_id: Ticket::Priority.lookup(name: '2 normal').id,
updated_by_id: 1,
created_by_id: 1,
)
article = Ticket::Article.create(
ticket_id: ticket.id,
type_id: Ticket::Article::Type.lookup(name: 'phone').id,
sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
from: 'Zammad Feedback <feedback@zammad.org>',
content_type: 'text/plain',
body: 'Welcome!
<b>test123</b>',
internal: false,
updated_by_id: 1,
created_by_id: 1,
)
changes = {}
result = NotificationFactory::Slack.template(
template: 'ticket_create',
locale: 'en-us',
timezone: 'Europe/Berlin',
objects: {
ticket: ticket,
article: article,
recipient: agent1,
current_user: agent_current_user,
changes: changes,
},
)
assert_match('# Welcome to Zammad!', result[:subject])
assert_match('User<b>xxx</b>', result[:body])
assert_match('Created by', result[:body])
assert_match('<b>test123</b>', result[:body])
assert_no_match('Dein', result[:body])
assert_no_match('longname', result[:body])
assert_match('Current User', result[:body])
article = Ticket::Article.create(
ticket_id: ticket.id,
type_id: Ticket::Article::Type.lookup(name: 'phone').id,
sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
from: 'Zammad Feedback <feedback@zammad.org>',
content_type: 'text/html',
body: 'Welcome!
<b>test123</b>',
internal: false,
updated_by_id: 1,
created_by_id: 1,
)
changes = {
state: %w[aaa bbb],
group: %w[xxx yyy],
pending_time: [Time.zone.parse('2019-04-01T10:00:00Z0'), Time.zone.parse('2019-04-01T23:00:00Z0')],
}
result = NotificationFactory::Slack.template(
template: 'ticket_update',
locale: 'en-us',
timezone: 'Europe/Berlin',
objects: {
ticket: ticket,
article: article,
recipient: agent1,
current_user: agent_current_user,
changes: changes,
},
)
assert_match('# Welcome to Zammad!', result[:subject])
assert_match('User<b>xxx</b>', result[:body])
assert_match('state: aaa -> bbb', result[:body])
assert_match('group: xxx -> yyy', result[:body])
assert_match('pending_time: 04/01/2019 12:00 (Europe/Berlin) -> 04/02/2019 01:00 (Europe/Berlin)', result[:body])
assert_no_match('Dein', result[:body])
assert_no_match('longname', result[:body])
assert_match('Current User', result[:body])
# en notification
ticket.escalation_at = Time.zone.parse('2019-04-01T10:00:00Z')
result = NotificationFactory::Slack.template(
template: 'ticket_escalation',
locale: 'en-us',
timezone: 'Europe/Berlin',
objects: {
ticket: ticket,
article: article,
recipient: agent1,
}
)
assert_match('# Welcome to Zammad!', result[:subject])
assert_match('is escalated since "04/01/2019 12:00 (Europe/Berlin)"!', result[:body])
end
end