notification_factory_test.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class NotificationFactoryTest < ActiveSupport::TestCase
  4. test 'notifications base' do
  5. ticket = Ticket.create(
  6. :title => 'some title äöüß',
  7. :group => Group.lookup( :name => 'Users'),
  8. :customer_id => 2,
  9. :state => Ticket::State.lookup( :name => 'new' ),
  10. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  11. :updated_by_id => 1,
  12. :created_by_id => 1,
  13. )
  14. article_plain = Ticket::Article.create(
  15. :ticket_id => ticket.id,
  16. :type_id => Ticket::Article::Type.where(:name => 'phone' ).first.id,
  17. :sender_id => Ticket::Article::Sender.where(:name => 'Customer' ).first.id,
  18. :from => 'Zammad Feedback <feedback@example.org>',
  19. :body => 'some text',
  20. :internal => false,
  21. :updated_by_id => 1,
  22. :created_by_id => 1,
  23. )
  24. tests = [
  25. {
  26. :locale => 'en',
  27. :string => 'Hi #{recipient.firstname},',
  28. :result => 'Hi Nicole,',
  29. },
  30. {
  31. :locale => 'de',
  32. :string => 'Hi #{recipient.firstname},',
  33. :result => 'Hi Nicole,',
  34. },
  35. {
  36. :locale => 'de',
  37. :string => 'Hi #{recipient.firstname}, Group: #{ticket.group.name}',
  38. :result => 'Hi Nicole, Group: Users',
  39. },
  40. {
  41. :locale => 'de',
  42. :string => '#{config.http_type} some text',
  43. :result => 'http some text',
  44. },
  45. {
  46. :locale => 'de',
  47. :string => 'i18n(New) some text',
  48. :result => 'Neu some text',
  49. },
  50. {
  51. :locale => 'de',
  52. :string => '\'i18n(#{ticket.state.name})\' ticket state',
  53. :result => '\'neu\' ticket state',
  54. },
  55. {
  56. :locale => 'de',
  57. :string => 'Subject #{article.from}, Group: #{ticket.group.name}',
  58. :result => 'Subject Zammad Feedback <feedback@example.org>, Group: Users',
  59. },
  60. {
  61. :locale => 'de',
  62. :string => 'Body #{article.body}, Group: #{ticket.group.name}',
  63. :result => 'Body > some text, Group: Users',
  64. },
  65. {
  66. :locale => 'de',
  67. :string => '\#{puts `ls`}',
  68. :result => '\#{puts `ls`}',
  69. },
  70. ]
  71. tests.each { |test|
  72. result = NotificationFactory.build(
  73. :string => test[:string],
  74. :objects => {
  75. :ticket => ticket,
  76. :article => article_plain,
  77. :recipient => User.find(2),
  78. },
  79. :locale => test[:locale]
  80. )
  81. assert_equal( test[:result], result, "verify result" )
  82. }
  83. ticket.destroy
  84. end
  85. test 'notifications html' do
  86. ticket = Ticket.create(
  87. :title => 'some title <b>äöüß</b> 2',
  88. :group => Group.lookup( :name => 'Users'),
  89. :customer_id => 2,
  90. :state => Ticket::State.lookup( :name => 'new' ),
  91. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  92. :updated_by_id => 1,
  93. :created_by_id => 1,
  94. )
  95. article_html = Ticket::Article.create(
  96. :ticket_id => ticket.id,
  97. :type_id => Ticket::Article::Type.where(:name => 'phone' ).first.id,
  98. :sender_id => Ticket::Article::Sender.where(:name => 'Customer' ).first.id,
  99. :from => 'Zammad Feedback <feedback@example.org>',
  100. :body => 'some <b>text</b><br>next line',
  101. :content_type => 'text/html',
  102. :internal => false,
  103. :updated_by_id => 1,
  104. :created_by_id => 1,
  105. )
  106. tests = [
  107. {
  108. :locale => 'de',
  109. :string => 'Subject #{ticket.title}',
  110. :result => 'Subject some title <b>äöüß</b> 2',
  111. },
  112. {
  113. :locale => 'de',
  114. :string => 'Subject #{article.from}, Group: #{ticket.group.name}',
  115. :result => 'Subject Zammad Feedback <feedback@example.org>, Group: Users',
  116. },
  117. {
  118. :locale => 'de',
  119. :string => 'Body #{article.body}, Group: #{ticket.group.name}',
  120. :result => 'Body > some text
  121. > next line, Group: Users',
  122. },
  123. ]
  124. tests.each { |test|
  125. result = NotificationFactory.build(
  126. :string => test[:string],
  127. :objects => {
  128. :ticket => ticket,
  129. :article => article_html,
  130. :recipient => User.find(2),
  131. },
  132. :locale => test[:locale]
  133. )
  134. assert_equal( test[:result], result, "verify result" )
  135. }
  136. ticket.destroy
  137. end
  138. test 'notifications attack' do
  139. ticket = Ticket.create(
  140. :title => 'some title <b>äöüß</b> 3',
  141. :group => Group.lookup( :name => 'Users'),
  142. :customer_id => 2,
  143. :state => Ticket::State.lookup( :name => 'new' ),
  144. :priority => Ticket::Priority.lookup( :name => '2 normal' ),
  145. :updated_by_id => 1,
  146. :created_by_id => 1,
  147. )
  148. article_html = Ticket::Article.create(
  149. :ticket_id => ticket.id,
  150. :type_id => Ticket::Article::Type.where(:name => 'phone' ).first.id,
  151. :sender_id => Ticket::Article::Sender.where(:name => 'Customer' ).first.id,
  152. :from => 'Zammad Feedback <feedback@example.org>',
  153. :body => 'some <b>text</b><br>next line',
  154. :content_type => 'text/html',
  155. :internal => false,
  156. :updated_by_id => 1,
  157. :created_by_id => 1,
  158. )
  159. tests = [
  160. {
  161. :locale => 'de',
  162. :string => '\#{puts `ls`}',
  163. :result => '\#{puts `ls`}',
  164. },
  165. {
  166. :locale => 'de',
  167. :string => 'attack#1 #{article.destroy}',
  168. :result => 'attack#1 #{article.destroy}',
  169. },
  170. {
  171. :locale => 'de',
  172. :string => 'attack#2 #{Article.where}',
  173. :result => 'attack#2 #{Article.where}',
  174. },
  175. {
  176. :locale => 'de',
  177. :string => 'attack#1 #{article.
  178. destroy}',
  179. :result => 'attack#1 #{article.
  180. destroy}',
  181. },
  182. {
  183. :locale => 'de',
  184. :string => 'attack#1 #{article.find}',
  185. :result => 'attack#1 #{article.find}',
  186. },
  187. {
  188. :locale => 'de',
  189. :string => 'attack#1 #{article.update}',
  190. :result => 'attack#1 #{article.update}',
  191. },
  192. {
  193. :locale => 'de',
  194. :string => 'attack#1 #{article.all}',
  195. :result => 'attack#1 #{article.all}',
  196. },
  197. {
  198. :locale => 'de',
  199. :string => 'attack#1 #{article.delete}',
  200. :result => 'attack#1 #{article.delete}',
  201. },
  202. ]
  203. tests.each { |test|
  204. result = NotificationFactory.build(
  205. :string => test[:string],
  206. :objects => {
  207. :ticket => ticket,
  208. :article => article_html,
  209. :recipient => User.find(2),
  210. },
  211. :locale => test[:locale]
  212. )
  213. assert_equal( test[:result], result, "verify result" )
  214. }
  215. ticket.destroy
  216. end
  217. end