123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- require 'rails_helper'
- require 'models/concerns/can_perform_changes_examples'
- RSpec.describe 'Ticket::PerformChanges', :aggregate_failures do
- subject(:object) { create(:ticket, group: group, owner: create(:agent, groups: [group])) }
- let(:group) { create(:group) }
- let(:performable) do
- create(:trigger, perform: perform, activator: 'action', execution_condition_mode: 'always', condition: { 'ticket.state_id'=>{ 'operator' => 'is', 'value' => Ticket::State.pluck(:id) } })
- end
- include_examples 'CanPerformChanges', object_name: 'Ticket'
- context 'when invalid data is given' do
- context 'with not existing attribute' do
- let(:perform) do
- {
- 'ticket.foobar' => {
- 'value' => 'dummy',
- }
- }
- end
- it 'raises an error' do
- expect { object.perform_changes(performable, 'trigger', object, User.first) }
- .to raise_error(RuntimeError, 'The given trigger contains invalid attributes, stopping!')
- end
- end
- context 'with invalid action in "perform" hash' do
- let(:perform) do
- {
- 'dummy' => {
- 'value' => 'delete',
- }
- }
- end
- it 'raises an error' do
- expect { object.perform_changes(performable, 'trigger', object, User.first) }
- .to raise_error(RuntimeError, 'The given trigger contains no valid actions, stopping!')
- end
- end
- end
- # Regression test for https://github.com/zammad/zammad/issues/2001
- describe 'argument handling' do
- let(:perform) do
- {
- 'notification.email' => {
- body: "Hello \#{ticket.customer.firstname} \#{ticket.customer.lastname},",
- recipient: %w[article_last_sender ticket_owner ticket_customer ticket_agents],
- subject: "Autoclose (\#{ticket.title})"
- }
- }
- end
- it 'does not mutate contents of "perform" hash' do
- expect { object.perform_changes(performable, 'trigger', {}, 1) }
- .not_to change { perform }
- end
- end
- context 'with "ticket.state_id" key in "perform" hash' do
- let(:perform) do
- {
- 'ticket.state_id' => {
- 'value' => Ticket::State.lookup(name: 'closed').id
- }
- }
- end
- it 'changes #state to specified value' do
- expect { object.perform_changes(performable, 'trigger', object, User.first) }
- .to change { object.reload.state.name }.to('closed')
- end
- end
- # Test for backwards compatibility after PR https://github.com/zammad/zammad/pull/2862
- context 'with "pending_time" => { "value": DATE } in "perform" hash' do
- let(:perform) do
- {
- 'ticket.state_id' => {
- 'value' => Ticket::State.lookup(name: 'pending reminder').id.to_s
- },
- 'ticket.pending_time' => {
- 'value' => timestamp,
- },
- }
- end
- let(:timestamp) { Time.zone.now }
- it 'changes pending date to given date' do
- freeze_time do
- expect { object.perform_changes(performable, 'trigger', object, User.first) }
- .to change(object, :pending_time)
- .to timestamp.change(sec: 0)
- end
- end
- end
- # Test for PR https://github.com/zammad/zammad/pull/2862
- context 'with "pending_time" => { "operator": "relative" } in "perform" hash' do
- shared_examples 'verify' do
- it 'verify relative pending time rule' do
- freeze_time do
- target_time = relative_value
- .send(relative_range)
- .from_now
- .change(sec: 0)
- expect { object.perform_changes(performable, 'trigger', object, User.first) }
- .to change(object, :pending_time)
- .to target_time
- end
- end
- end
- let(:perform) do
- {
- 'ticket.state_id' => {
- 'value' => Ticket::State.lookup(name: 'pending reminder').id.to_s
- },
- 'ticket.pending_time' => {
- 'operator' => 'relative',
- 'value' => relative_value,
- 'range' => relative_range_config
- },
- }
- end
- let(:relative_range_config) { relative_range.to_s.singularize }
- context 'when value in days' do
- let(:relative_value) { 2 }
- let(:relative_range) { :days }
- include_examples 'verify'
- end
- context 'when value in minutes' do
- let(:relative_value) { 60 }
- let(:relative_range) { :minutes }
- include_examples 'verify'
- end
- context 'when value in weeks' do
- let(:relative_value) { 2 }
- let(:relative_range) { :weeks }
- include_examples 'verify'
- end
- end
- context 'with tags in "perform" hash' do
- let(:user) { create(:agent, groups: [group]) }
- let(:perform) do
- {
- 'ticket.tags' => { 'operator' => tag_operator, 'value' => 'tag1, tag2' }
- }
- end
- context 'with add' do
- let(:tag_operator) { 'add' }
- it 'adds the tags' do
- expect { object.perform_changes(performable, 'trigger', object, user.id) }
- .to change { object.reload.tag_list }.to(%w[tag1 tag2])
- end
- end
- context 'with remove' do
- let(:tag_operator) { 'remove' }
- before do
- %w[tag1 tag2].each { |tag| object.tag_add(tag, 1) }
- end
- it 'removes the tags' do
- expect { object.perform_changes(performable, 'trigger', object, user.id) }
- .to change { object.reload.tag_list }.to([])
- end
- end
- end
- context 'with "pre_condition" in "perform" hash' do
- let(:user) { create(:agent, groups: [group]) }
- let(:perform) do
- {
- 'ticket.owner_id' => {
- 'pre_condition' => pre_condition,
- 'value' => value,
- 'value_completion' => '',
- }
- }
- end
- context 'with current_user.id' do
- let(:pre_condition) { 'current_user.id' }
- let(:value) { '' }
- it 'changes to specified value' do
- expect { object.perform_changes(performable, 'trigger', object, user.id) }
- .to change { object.reload.owner.id }.to(user.id)
- end
- end
- context 'with specific user' do
- let(:another_user) { create(:agent, groups: [group]) }
- let(:pre_condition) { 'specific' }
- let(:value) { another_user.id }
- it 'changes to specified value' do
- expect { object.perform_changes(performable, 'trigger', object, user.id) }
- .to change { object.reload.owner.id }.to(another_user.id)
- end
- end
- context 'with current_user.id, but missing user' do
- let(:pre_condition) { 'current_user.id' }
- let(:value) { '' }
- it 'raises an error' do
- expect { object.perform_changes(performable, 'trigger', object, nil) }
- .to raise_error(RuntimeError, "The required parameter 'user_id' is missing.")
- end
- end
- context 'with not_set' do
- let(:pre_condition) { 'not_set' }
- let(:value) { '' }
- it 'changes to user with id 1' do
- expect { object.perform_changes(performable, 'trigger', object, user.id) }
- .to change { object.reload.owner.id }.to(1)
- end
- end
- end
- context 'with "ticket.action" => { "value" => "delete" } in "perform" hash' do
- let(:perform) do
- {
- 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s },
- 'ticket.action' => { 'value' => 'delete' },
- }
- end
- it 'performs a ticket deletion on a ticket' do
- expect { object.perform_changes(performable, 'trigger', object, User.first) }
- .to change(object, :destroyed?).to(true)
- end
- end
- context 'with a "notification.email" trigger' do
- # Regression test for https://github.com/zammad/zammad/issues/1543
- #
- # If a new article fires an email notification trigger,
- # and then another article is added to the same ticket
- # before that trigger is performed,
- # the email template's 'article' var should refer to the originating article,
- # not the newest one.
- #
- # (This occurs whenever one action fires multiple email notification triggers.)
- context 'when two articles are created before the trigger fires once (race condition)' do
- let!(:article) { create(:ticket_article, ticket: object) }
- let!(:new_article) { create(:ticket_article, ticket: object) }
- let(:trigger) do
- build(:trigger,
- perform: {
- 'notification.email' => {
- body: 'Sample notification',
- recipient: 'ticket_customer',
- subject: 'Sample subject'
- }
- })
- end
- let(:objects) do
- last_article = nil
- last_internal_article = nil
- last_external_article = nil
- all_articles = object.articles
- if article.nil?
- last_article = all_articles.last
- last_internal_article = all_articles.reverse.find(&:internal?)
- last_external_article = all_articles.reverse.find { |a| !a.internal? }
- else
- last_article = article
- last_internal_article = article.internal? ? article : all_articles.reverse.find(&:internal?)
- last_external_article = article.internal? ? all_articles.reverse.find { |a| !a.internal? } : article
- end
- {
- ticket: object,
- article: last_article,
- last_article: last_article,
- last_internal_article: last_internal_article,
- last_external_article: last_external_article,
- created_article: article,
- created_internal_article: article&.internal? ? article : nil,
- created_external_article: article&.internal? ? nil : article,
- }
- end
- # required by Ticket#perform_changes for email notifications
- before do
- allow(NotificationFactory::Mailer).to receive(:template).and_call_original
- article.ticket.group.update(email_address: create(:email_address))
- end
- it 'passes the first article to NotificationFactory::Mailer' do
- object.perform_changes(trigger, 'trigger', { article_id: article.id }, 1)
- expect(NotificationFactory::Mailer)
- .to have_received(:template)
- .with(hash_including(objects: objects))
- .at_least(:once)
- expect(NotificationFactory::Mailer)
- .not_to have_received(:template)
- .with(hash_including(objects: { ticket: object, article: new_article }))
- end
- end
- context 'when dispatching email through an inactive channel' do
- let!(:article) { create(:ticket_article, ticket: object) }
- let(:trigger) do
- build(:trigger,
- perform: {
- 'notification.email' => {
- body: 'Sample notification',
- recipient: 'ticket_customer',
- subject: 'Sample subject'
- }
- })
- end
- # required by Ticket#perform_changes for email notifications
- before do
- allow(NotificationFactory::Mailer).to receive(:template).and_call_original
- allow(Rails.logger).to receive(:info)
- article.ticket.group.update(email_address: create(:email_address, channel: create(:channel, active: false)))
- end
- it 'does not pass the article to NotificationFactory::Mailer' do
- object.perform_changes(trigger, 'trigger', { article_id: article.id }, 1)
- expect(Rails.logger).to have_received(:info).with(match(%r{because the channel .* is not active}))
- # no specific email content awaiting needed, since we do not expect to receive any mail (what ever it is) at the point
- expect(NotificationFactory::Mailer).not_to have_received(:template)
- end
- end
- end
- context 'with a notification trigger' do
- # https://github.com/zammad/zammad/issues/2782
- #
- # Notification triggers should log notification as private or public
- # according to given configuration
- let(:user) { create(:admin, mobile: '+37061010000') }
- let(:perform) do
- {
- notification_key => {
- body: 'Old programmers never die. They just branch to a new address.',
- recipient: 'ticket_agents',
- subject: 'Old programmers never die. They just branch to a new address.'
- }
- }.deep_merge(additional_options).deep_stringify_keys
- end
- let(:notification_key) { "notification.#{notification_type}" }
- let!(:ticket_article) { create(:ticket_article, ticket: object) }
- let(:item) do
- {
- object: 'Ticket',
- object_id: object.id,
- user_id: user.id,
- type: 'update',
- article_id: ticket_article.id
- }
- end
- before { object.group.users << user }
- shared_examples 'verify log visibility status' do
- shared_examples 'notification trigger' do
- it 'adds Ticket::Article' do
- expect { object.perform_changes(performable, 'trigger', object, user) }
- .to change { object.articles.count }.by(1)
- end
- it 'new Ticket::Article visibility reflects setting' do
- object.perform_changes(performable, 'trigger', object, User.first)
- new_article = object.articles.reload.last
- expect(new_article.internal).to be target_internal_value
- end
- end
- context 'when set to private' do
- let(:additional_options) do
- {
- notification_key => {
- internal: true
- }
- }
- end
- let(:target_internal_value) { true }
- it_behaves_like 'notification trigger'
- end
- context 'when set to internal' do
- let(:additional_options) do
- {
- notification_key => {
- internal: false
- }
- }
- end
- let(:target_internal_value) { false }
- it_behaves_like 'notification trigger'
- end
- context 'when no selection was made' do # ensure previously created triggers default to public
- let(:additional_options) do
- {}
- end
- let(:target_internal_value) { false }
- it_behaves_like 'notification trigger'
- end
- end
- context 'when dispatching email' do
- let(:notification_type) { :email }
- include_examples 'verify log visibility status'
- end
- shared_examples 'add a new article' do
- it 'adds a new article' do
- expect { object.perform_changes(performable, 'trigger', item, user) }
- .to change { object.articles.count }.by(1)
- end
- end
- shared_examples 'add attachment to new article' do
- include_examples 'add a new article'
- it 'adds attachment to the new article' do
- object.perform_changes(performable, 'trigger', item, user)
- article = object.articles.reload.last
- expect(article.type.name).to eq('email')
- expect(article.sender.name).to eq('System')
- expect(article.attachments.count).to eq(1)
- expect(article.attachments[0].filename).to eq('some_file.pdf')
- expect(article.attachments[0].preferences['Content-ID']).to eq('image/pdf@01CAB192.K8H512Y9')
- end
- end
- shared_examples 'does not add attachment to new article' do
- include_examples 'add a new article'
- it 'does not add attachment to the new article' do
- object.perform_changes(performable, 'trigger', item, user)
- article = object.articles.reload.last
- expect(article.type.name).to eq('email')
- expect(article.sender.name).to eq('System')
- expect(article.attachments.count).to eq(0)
- end
- end
- context 'when dispatching email with include attachment present' do
- let(:notification_type) { :email }
- let(:additional_options) do
- {
- notification_key => {
- include_attachments: 'true'
- }
- }
- end
- context 'when ticket has an attachment' do
- before do
- UserInfo.current_user_id = 1
- create(:store,
- object: 'Ticket::Article',
- o_id: ticket_article.id,
- data: 'dGVzdCAxMjM=',
- filename: 'some_file.pdf',
- preferences: {
- 'Content-Type': 'image/pdf',
- 'Content-ID': 'image/pdf@01CAB192.K8H512Y9',
- })
- end
- include_examples 'add attachment to new article'
- end
- context 'when ticket does not have an attachment' do
- include_examples 'does not add attachment to new article'
- end
- end
- context 'when dispatching email with include attachment not present' do
- let(:notification_type) { :email }
- let(:additional_options) do
- {
- notification_key => {
- include_attachments: 'false'
- }
- }
- end
- context 'when ticket has an attachment' do
- before do
- UserInfo.current_user_id = 1
- create(:store,
- object: 'Ticket::Article',
- o_id: ticket_article.id,
- data: 'dGVzdCAxMjM=',
- filename: 'some_file.pdf',
- preferences: {
- 'Content-Type': 'image/pdf',
- 'Content-ID': 'image/pdf@01CAB192.K8H512Y9',
- })
- end
- include_examples 'does not add attachment to new article'
- end
- context 'when ticket does not have an attachment' do
- include_examples 'does not add attachment to new article'
- end
- end
- context 'when dispatching SMS' do
- let(:notification_type) { :sms }
- before { create(:channel, area: 'Sms::Notification') }
- include_examples 'verify log visibility status'
- end
- end
- context 'with a "notification.webhook" trigger', performs_jobs: true do
- let(:webhook) { create(:webhook, endpoint: 'http://api.example.com/webhook', signature_token: '53CR3t') }
- let(:trigger) do
- create(:trigger,
- perform: {
- 'notification.webhook' => { 'webhook_id' => webhook.id }
- })
- end
- let(:context_data) do
- {
- type: 'info',
- execution: 'trigger',
- changes: { 'state_id' => %w[2 4] },
- user_id: 1,
- }
- end
- it 'schedules the webhooks notification job' do
- expect { object.perform_changes(trigger, 'trigger', context_data, 1) }.to have_enqueued_job(TriggerWebhookJob).with(
- trigger,
- object,
- nil,
- changes: { 'State' => %w[open closed] },
- user_id: 1,
- execution_type: 'trigger',
- event_type: 'info',
- )
- end
- end
- context 'with a "article.note" trigger' do
- let(:user) { create(:agent, groups: [group]) }
- let(:perform) do
- { 'article.note' => { 'subject' => 'Test subject note', 'internal' => 'true', 'body' => 'Test body note' } }
- end
- it 'adds the note' do
- object.perform_changes(performable, 'trigger', object, user.id)
- expect(object.articles.reload.last).to have_attributes(
- subject: 'Test subject note',
- body: 'Test body note',
- internal: true,
- )
- end
- end
- context 'with a "ticket.subscribe" trigger', current_user_id: 1 do
- let(:user) { create(:agent, groups: [group]) }
- let(:perform) do
- { 'ticket.subscribe' => { 'pre_condition' => 'current_user.id', 'value' => '', 'value_completion' => '' } }
- end
- it 'subscribes current user to ticket' do
- object.perform_changes(performable, 'trigger', object, user.id)
- expect(Mention.last).to have_attributes(
- mentionable: object,
- user: user,
- )
- end
- context 'with specific user' do
- let(:agent) { create(:agent, groups: [group]) }
- let(:perform) do
- { 'ticket.subscribe' => { 'pre_condition' => 'specific', 'value' => agent.id, 'value_completion' => '' } }
- end
- it 'subscribes specific user to ticket' do
- object.perform_changes(performable, 'trigger', object, user.id)
- expect(Mention.last).to have_attributes(
- mentionable: object,
- user: agent,
- )
- end
- end
- end
- context 'with a "ticket.unsubscribe" trigger', current_user_id: 1 do
- let(:user) { create(:agent, groups: [group]) }
- let(:other_user) { create(:agent, groups: [group]) }
- let!(:mention) do
- Mention.subscribe!(object, user)
- Mention.last
- end
- let!(:other_mention) do
- Mention.subscribe!(object, other_user)
- Mention.last
- end
- let(:perform) do
- { 'ticket.unsubscribe' => { 'pre_condition' => 'current_user.id', 'value' => '', 'value_completion' => '' } }
- end
- it 'unsubscribes current user from ticket' do
- object.perform_changes(performable, 'trigger', object, user.id)
- expect(Mention).not_to exist(mention.id)
- end
- context 'with specific user' do
- let(:perform) do
- { 'ticket.unsubscribe' => { 'pre_condition' => 'specific', 'value' => other_user.id, 'value_completion' => '' } }
- end
- it 'un subscribes specific user from ticket' do
- object.perform_changes(performable, 'trigger', object, other_user.id)
- expect(Mention).not_to exist(other_mention.id)
- end
- end
- context 'when unsubscribing all users' do
- let(:perform) do
- { 'ticket.unsubscribe' => { 'pre_condition' => 'not_set', 'value' => '', 'value_completion' => '' } }
- end
- it 'unsubscribes all users from ticket' do
- expect { object.perform_changes(performable, 'trigger', object, user.id) }
- .to change { object.mentions.exists? }
- .to false
- end
- end
- end
- describe 'Check if blocking notifications works' do
- context 'when mail delivery failed' do
- let(:ticket) { create(:ticket) }
- let(:customer) { create(:customer) }
- let(:perform) do
- {
- 'notification.email' => {
- body: "Hello \#{ticket.customer.firstname} \#{ticket.customer.lastname},",
- recipient: ["userid_#{customer.id}"],
- subject: "Autoclose (\#{ticket.title})",
- }
- }
- end
- context 'with a normal user' do
- it 'sends trigger base notification' do
- expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }.to change { ticket.reload.articles.count }.by(1)
- end
- end
- context 'with a permanent failed user' do
- let(:failed_date) { 1.second.ago }
- let(:customer) do
- user = create(:customer)
- user.preferences.merge!(mail_delivery_failed: true, mail_delivery_failed_data: failed_date)
- user.save!
- user
- end
- it 'sends no trigger base notification' do
- expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }.not_to change { ticket.reload.articles.count }
- expect(customer.reload.preferences).to include(
- mail_delivery_failed: true,
- mail_delivery_failed_data: failed_date,
- )
- end
- context 'with failed date 61 days ago' do
- let(:failed_date) { 61.days.ago }
- it 'sends trigger base notification' do
- expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }.to change { ticket.reload.articles.count }.by(1)
- expect(customer.reload.preferences).to include(
- mail_delivery_failed: false,
- mail_delivery_failed_data: nil,
- )
- end
- end
- context 'with failed date 70 days ago' do
- let(:failed_date) { 70.days.ago }
- it 'sends trigger base notification' do
- expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }.to change { ticket.reload.articles.count }.by(1)
- expect(customer.reload.preferences).to include(
- mail_delivery_failed: false,
- mail_delivery_failed_data: nil,
- )
- end
- end
- end
- end
- end
- end
|