123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
- # encoding: utf-8
- require 'test_helper'
- class NotificationFactoryTest < ActiveSupport::TestCase
- Translation.load('de-de')
- test 'notifications send' do
- result = NotificationFactory.send(
- recipient: User.find(2),
- subject: 'sime subject',
- body: 'some body',
- content_type: '',
- )
- assert_match('some body', result.to_s)
- assert_match('text/plain', result.to_s)
- assert_no_match('text/html', result.to_s)
- result = NotificationFactory.send(
- recipient: User.find(2),
- subject: 'sime subject',
- body: 'some body',
- content_type: 'text/plain',
- )
- assert_match('some body', result.to_s)
- assert_match('text/plain', result.to_s)
- assert_no_match('text/html', result.to_s)
- result = NotificationFactory.send(
- recipient: User.find(2),
- subject: 'sime subject',
- body: 'some <span>body</span>',
- content_type: 'text/html',
- )
- assert_match('some body', result.to_s)
- assert_match('text/plain', result.to_s)
- assert_match('<span>body</span>', result.to_s)
- assert_match('text/html', result.to_s)
- end
- test 'notifications base' do
- ticket = Ticket.create(
- title: 'some title äöüß',
- group: Group.lookup(name: 'Users'),
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- updated_by_id: 2,
- created_by_id: 2,
- )
- article_plain = Ticket::Article.create(
- ticket_id: ticket.id,
- type_id: Ticket::Article::Type.where(name: 'phone').first.id,
- sender_id: Ticket::Article::Sender.where(name: 'Customer').first.id,
- from: 'Zammad Feedback <feedback@example.org>',
- body: 'some text',
- internal: false,
- updated_by_id: 1,
- created_by_id: 1,
- )
- tests = [
- {
- locale: 'en',
- string: 'Hi #{recipient.firstname},',
- result: 'Hi Nicole,',
- },
- {
- locale: 'de-de',
- string: 'Hi #{recipient.firstname},',
- result: 'Hi Nicole,',
- },
- {
- locale: 'de-de',
- string: 'Hi #{recipient.firstname}, Group: #{ticket.group.name}',
- result: 'Hi Nicole, Group: Users',
- },
- {
- locale: 'de-de',
- string: '#{config.http_type} some text',
- result: 'http some text',
- },
- {
- locale: 'de-de',
- string: 'i18n(New) some text',
- result: 'Neu some text',
- },
- {
- locale: 'de-de',
- string: '\'i18n(#{ticket.state.name})\' ticket state',
- result: '\'neu\' ticket state',
- },
- {
- locale: 'de-de',
- string: 'a #{not_existing_object.test}',
- result: 'a #{not_existing_object / no such object}',
- },
- {
- locale: 'de-de',
- string: 'a #{ticket.level1}',
- result: 'a #{ticket.level1 / no such method}',
- },
- {
- locale: 'de-de',
- string: 'a #{ticket.level1.level2}',
- result: 'a #{ticket.level1 / no such method}',
- },
- {
- locale: 'de-de',
- string: 'a #{ticket.title.level2}',
- result: 'a #{ticket.title.level2 / no such method}',
- },
- {
- locale: 'de-de',
- string: 'by #{ticket.updated_by.fullname}',
- result: 'by Nicole Braun',
- },
- {
- locale: 'de-de',
- string: 'Subject #{article.from}, Group: #{ticket.group.name}',
- result: 'Subject Zammad Feedback <feedback@example.org>, Group: Users',
- },
- {
- locale: 'de-de',
- string: 'Body #{article.body}, Group: #{ticket.group.name}',
- result: 'Body some text, Group: Users',
- },
- {
- locale: 'de-de',
- string: '\#{puts `ls`}',
- result: '\#{puts `ls`} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'test i18n(new)',
- result: 'test neu',
- },
- {
- locale: 'de-de',
- string: 'test i18n()',
- result: 'test ',
- },
- {
- locale: 'de-de',
- string: 'test i18n(new) i18n(open)',
- result: 'test neu offen',
- },
- ]
- tests.each { |test|
- result = NotificationFactory.build(
- string: test[:string],
- objects: {
- ticket: ticket,
- article: article_plain,
- recipient: User.find(2),
- },
- locale: test[:locale]
- )
- assert_equal(test[:result], result, 'verify result')
- }
- ticket.destroy
- end
- test 'notifications html' do
- ticket = Ticket.create(
- title: 'some title <b>äöüß</b> 2',
- group: Group.lookup(name: 'Users'),
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- article_html = Ticket::Article.create(
- ticket_id: ticket.id,
- type_id: Ticket::Article::Type.where(name: 'phone').first.id,
- sender_id: Ticket::Article::Sender.where(name: 'Customer').first.id,
- from: 'Zammad Feedback <feedback@example.org>',
- body: 'some <b>text</b><br>next line',
- content_type: 'text/html',
- internal: false,
- updated_by_id: 1,
- created_by_id: 1,
- )
- tests = [
- {
- locale: 'de-de',
- string: 'Subject #{ticket.title}',
- result: 'Subject some title <b>äöüß</b> 2',
- },
- {
- locale: 'de-de',
- string: 'Subject #{article.from}, Group: #{ticket.group.name}',
- result: 'Subject Zammad Feedback <feedback@example.org>, Group: Users',
- },
- {
- locale: 'de-de',
- string: 'Body #{article.body}, Group: #{ticket.group.name}',
- result: 'Body some text
- next line, Group: Users',
- },
- ]
- tests.each { |test|
- result = NotificationFactory.build(
- string: test[:string],
- objects: {
- ticket: ticket,
- article: article_html,
- recipient: User.find(2),
- },
- locale: test[:locale]
- )
- assert_equal(test[:result], result, 'verify result')
- }
- ticket.destroy
- end
- test 'notifications attack' do
- ticket = Ticket.create(
- title: 'some title <b>äöüß</b> 3',
- group: Group.lookup(name: 'Users'),
- customer_id: 2,
- state: Ticket::State.lookup(name: 'new'),
- priority: Ticket::Priority.lookup(name: '2 normal'),
- updated_by_id: 1,
- created_by_id: 1,
- )
- article_html = Ticket::Article.create(
- ticket_id: ticket.id,
- type_id: Ticket::Article::Type.where(name: 'phone').first.id,
- sender_id: Ticket::Article::Sender.where(name: 'Customer').first.id,
- from: 'Zammad Feedback <feedback@example.org>',
- body: 'some <b>text</b><br>next line',
- content_type: 'text/html',
- internal: false,
- updated_by_id: 1,
- created_by_id: 1,
- )
- tests = [
- {
- locale: 'de-de',
- string: '\#{puts `ls`}',
- result: '\#{puts `ls`} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'attack#1 #{article.destroy}',
- result: 'attack#1 #{article.destroy} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'attack#2 #{Article.where}',
- result: 'attack#2 #{Article.where} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'attack#1 #{article.
- destroy}',
- result: 'attack#1 #{article.
- destroy} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'attack#1 #{article.find}',
- result: 'attack#1 #{article.find} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'attack#1 #{article.update(:name => "test")}',
- result: 'attack#1 #{article.update(:name => "test")} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'attack#1 #{article.all}',
- result: 'attack#1 #{article.all} (not allowed)',
- },
- {
- locale: 'de-de',
- string: 'attack#1 #{article.delete}',
- result: 'attack#1 #{article.delete} (not allowed)',
- },
- ]
- tests.each { |test|
- result = NotificationFactory.build(
- string: test[:string],
- objects: {
- ticket: ticket,
- article: article_html,
- recipient: User.find(2),
- },
- locale: test[:locale]
- )
- assert_equal(test[:result], result, 'verify result')
- }
- ticket.destroy
- end
- test 'notifications template' do
- 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,
- )
- result = NotificationFactory.template(
- template: 'password_reset',
- locale: 'de-de',
- objects: {
- user: agent1,
- },
- )
- assert_match('Zurücksetzen Deines', result[:subject])
- assert_match('wir haben eine Anfrage zum Zurücksetzen', result[:body])
- assert_match('Dein', result[:body])
- assert_match('Dein', result[:body])
- assert_match('Notification<b>xxx</b>', result[:body])
- assert_no_match('Your', result[:body])
- result = NotificationFactory.template(
- template: 'password_reset',
- locale: 'de',
- objects: {
- user: agent1,
- },
- )
- assert_match('Zurücksetzen Deines', result[:subject])
- assert_match('wir haben eine Anfrage zum Zurücksetzen', result[:body])
- assert_match('Dein', result[:body])
- assert_match('Notification<b>xxx</b>', result[:body])
- assert_no_match('Your', result[:body])
- result = NotificationFactory.template(
- template: 'password_reset',
- locale: 'es-us',
- objects: {
- user: agent1,
- },
- )
- assert_match('Reset your', result[:subject])
- assert_match('We received a request to reset the password', result[:body])
- assert_match('Your', result[:body])
- assert_match('Notification<b>xxx</b>', result[:body])
- assert_no_match('Dein', result[:body])
- 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.template(
- template: 'ticket_create',
- locale: 'es-us',
- objects: {
- ticket: ticket,
- article: article,
- recipient: agent1,
- changes: changes,
- },
- )
- assert_match('New Ticket', result[:subject])
- assert_match('Notification<b>xxx</b>', result[:body])
- assert_match('has been created by', result[:body])
- assert_match('<b>test123</b>', result[:body])
- assert_match('Manage your notifications settings', result[:body])
- assert_no_match('Dein', result[:body])
- result = NotificationFactory.template(
- template: 'ticket_create',
- locale: 'de-de',
- objects: {
- ticket: ticket,
- article: article,
- recipient: agent1,
- changes: changes,
- },
- )
- assert_match('Neues Ticket', result[:subject])
- assert_match('Notification<b>xxx</b>', result[:body])
- assert_match('es wurde ein neues Ticket', result[:body])
- assert_match('<b>test123</b>', result[:body])
- assert_match('Benachrichtigungseinstellungen Verwalten', result[:body])
- assert_no_match('Your', 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),
- }
- result = NotificationFactory.template(
- template: 'ticket_update',
- locale: 'es-us',
- objects: {
- ticket: ticket,
- article: article,
- recipient: agent1,
- changes: changes,
- },
- )
- assert_match('Updated Ticket', result[:subject])
- assert_match('Notification<b>xxx</b>', result[:body])
- assert_match('has been updated by', result[:body])
- assert_match('<b>test123</b>', result[:body])
- assert_match('Manage your notifications settings', result[:body])
- assert_no_match('Dein', result[:body])
- result = NotificationFactory.template(
- template: 'ticket_update',
- locale: 'de-de',
- objects: {
- ticket: ticket,
- article: article,
- recipient: agent1,
- changes: changes,
- },
- )
- assert_match('Ticket aktualisiert', result[:subject])
- assert_match('Notification<b>xxx</b>', result[:body])
- assert_match('wurde von', result[:body])
- assert_match('<b>test123</b>', result[:body])
- assert_match('Benachrichtigungseinstellungen Verwalten', result[:body])
- assert_no_match('Your', result[:body])
- end
- test 'notifications settings' do
- groups = Group.all
- roles = Role.where(name: 'Agent')
- agent1 = User.create_or_update(
- login: 'notification-settings-agent1@example.com',
- firstname: 'Notification<b>xxx</b>',
- lastname: 'Agent1',
- email: 'notification-settings-agent1@example.com',
- password: 'agentpw',
- active: true,
- roles: roles,
- groups: groups,
- updated_by_id: 1,
- created_by_id: 1,
- )
- agent2 = User.create_or_update(
- login: 'notification-settings-agent2@example.com',
- firstname: 'Notification<b>xxx</b>',
- lastname: 'Agent2',
- email: 'notification-settings-agent2@example.com',
- password: 'agentpw',
- active: true,
- roles: roles,
- groups: groups,
- updated_by_id: 1,
- created_by_id: 1,
- )
- group_notification_setting = Group.create_or_update(
- name: 'NotificationSetting',
- updated_by_id: 1,
- created_by_id: 1,
- )
- ticket1 = 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: 'Notification Settings Test 1!',
- 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,
- )
- ticket2 = Ticket.create(
- group_id: Group.lookup(name: 'Users').id,
- customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
- owner_id: agent1.id,
- title: 'Notification Settings Test 2!',
- 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,
- )
- ticket3 = Ticket.create(
- group_id: group_notification_setting.id,
- customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
- owner_id: User.lookup(login: '-').id,
- title: 'Notification Settings Test 1!',
- 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,
- )
- ticket4 = Ticket.create(
- group_id: group_notification_setting.id,
- customer_id: User.lookup(email: 'nicole.braun@zammad.org').id,
- owner_id: agent1.id,
- title: 'Notification Settings Test 2!',
- 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,
- )
- agent1.preferences[:notification_config][:group_ids] = nil
- agent1.save
- result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = nil
- agent2.save
- result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
- assert_equal(nil, result)
- result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
- assert_equal(nil, result)
- # no group selection
- agent1.preferences[:notification_config][:group_ids] = []
- agent1.save
- result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = []
- agent2.save
- result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
- assert_equal(nil, result)
- result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
- assert_equal(nil, result)
- agent1.preferences[:notification_config][:group_ids] = ['-']
- agent1.save
- result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = ['-']
- agent2.save
- result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
- assert_equal(nil, result)
- result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
- assert_equal(nil, result)
- # dedecated group selection
- agent1.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
- agent1.save
- result = NotificationFactory.notification_settings(agent1, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket2, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent1, ticket3, 'create')
- assert_equal(nil, result)
- result = NotificationFactory.notification_settings(agent1, ticket4, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- agent2.preferences[:notification_config][:group_ids] = [Group.lookup(name: 'Users').id]
- agent2.save
- result = NotificationFactory.notification_settings(agent2, ticket1, 'create')
- assert_equal(true, result[:channels][:online])
- assert_equal(true, result[:channels][:email])
- result = NotificationFactory.notification_settings(agent2, ticket2, 'create')
- assert_equal(nil, result)
- result = NotificationFactory.notification_settings(agent2, ticket3, 'create')
- assert_equal(nil, result)
- assert_equal(nil, result)
- result = NotificationFactory.notification_settings(agent2, ticket4, 'create')
- assert_equal(nil, result)
- end
- end
|