notification_factory_test.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class NotificationFactoryTest < ActiveSupport::TestCase
  4. test 'notifications send' do
  5. result = NotificationFactory.send(
  6. recipient: User.find(2),
  7. subject: 'sime subject',
  8. body: 'some body',
  9. content_type: '',
  10. )
  11. assert_match('some body', result.to_s)
  12. assert_match('text/plain', result.to_s)
  13. assert_no_match('text/html', result.to_s)
  14. result = NotificationFactory.send(
  15. recipient: User.find(2),
  16. subject: 'sime subject',
  17. body: 'some body',
  18. content_type: 'text/plain',
  19. )
  20. assert_match('some body', result.to_s)
  21. assert_match('text/plain', result.to_s)
  22. assert_no_match('text/html', result.to_s)
  23. result = NotificationFactory.send(
  24. recipient: User.find(2),
  25. subject: 'sime subject',
  26. body: 'some <span>body</span>',
  27. content_type: 'text/html',
  28. )
  29. assert_match('some body', result.to_s)
  30. assert_match('text/plain', result.to_s)
  31. assert_match('<span>body</span>', result.to_s)
  32. assert_match('text/html', result.to_s)
  33. end
  34. test 'notifications base' do
  35. ticket = Ticket.create(
  36. title: 'some title äöüß',
  37. group: Group.lookup( name: 'Users'),
  38. customer_id: 2,
  39. state: Ticket::State.lookup( name: 'new' ),
  40. priority: Ticket::Priority.lookup( name: '2 normal' ),
  41. updated_by_id: 2,
  42. created_by_id: 2,
  43. )
  44. article_plain = Ticket::Article.create(
  45. ticket_id: ticket.id,
  46. type_id: Ticket::Article::Type.where(name: 'phone' ).first.id,
  47. sender_id: Ticket::Article::Sender.where(name: 'Customer' ).first.id,
  48. from: 'Zammad Feedback <feedback@example.org>',
  49. body: 'some text',
  50. internal: false,
  51. updated_by_id: 1,
  52. created_by_id: 1,
  53. )
  54. tests = [
  55. {
  56. locale: 'en',
  57. string: 'Hi #{recipient.firstname},',
  58. result: 'Hi Nicole,',
  59. },
  60. {
  61. locale: 'de-de',
  62. string: 'Hi #{recipient.firstname},',
  63. result: 'Hi Nicole,',
  64. },
  65. {
  66. locale: 'de-de',
  67. string: 'Hi #{recipient.firstname}, Group: #{ticket.group.name}',
  68. result: 'Hi Nicole, Group: Users',
  69. },
  70. {
  71. locale: 'de-de',
  72. string: '#{config.http_type} some text',
  73. result: 'http some text',
  74. },
  75. {
  76. locale: 'de-de',
  77. string: 'i18n(New) some text',
  78. result: 'Neu some text',
  79. },
  80. {
  81. locale: 'de-de',
  82. string: '\'i18n(#{ticket.state.name})\' ticket state',
  83. result: '\'neu\' ticket state',
  84. },
  85. {
  86. locale: 'de-de',
  87. string: 'a #{not_existing_object.test}',
  88. result: 'a #{not_existing_object / no such object}',
  89. },
  90. {
  91. locale: 'de-de',
  92. string: 'a #{ticket.level1}',
  93. result: 'a #{ticket.level1 / no such method}',
  94. },
  95. {
  96. locale: 'de-de',
  97. string: 'a #{ticket.level1.level2}',
  98. result: 'a #{ticket.level1 / no such method}',
  99. },
  100. {
  101. locale: 'de-de',
  102. string: 'a #{ticket.title.level2}',
  103. result: 'a #{ticket.title.level2 / no such method}',
  104. },
  105. {
  106. locale: 'de-de',
  107. string: 'by #{ticket.updated_by.fullname}',
  108. result: 'by Nicole Braun',
  109. },
  110. {
  111. locale: 'de-de',
  112. string: 'Subject #{article.from}, Group: #{ticket.group.name}',
  113. result: 'Subject Zammad Feedback <feedback@example.org>, Group: Users',
  114. },
  115. {
  116. locale: 'de-de',
  117. string: 'Body #{article.body}, Group: #{ticket.group.name}',
  118. result: 'Body some text, Group: Users',
  119. },
  120. {
  121. locale: 'de-de',
  122. string: '\#{puts `ls`}',
  123. result: '\#{puts `ls`} (not allowed)',
  124. },
  125. {
  126. locale: 'de-de',
  127. string: 'test i18n(new)',
  128. result: 'test neu',
  129. },
  130. {
  131. locale: 'de-de',
  132. string: 'test i18n()',
  133. result: 'test ',
  134. },
  135. {
  136. locale: 'de-de',
  137. string: 'test i18n(new) i18n(open)',
  138. result: 'test neu offen',
  139. },
  140. ]
  141. tests.each { |test|
  142. result = NotificationFactory.build(
  143. string: test[:string],
  144. objects: {
  145. ticket: ticket,
  146. article: article_plain,
  147. recipient: User.find(2),
  148. },
  149. locale: test[:locale]
  150. )
  151. assert_equal( test[:result], result, 'verify result' )
  152. }
  153. ticket.destroy
  154. end
  155. test 'notifications html' do
  156. ticket = Ticket.create(
  157. title: 'some title <b>äöüß</b> 2',
  158. group: Group.lookup( name: 'Users'),
  159. customer_id: 2,
  160. state: Ticket::State.lookup( name: 'new' ),
  161. priority: Ticket::Priority.lookup( name: '2 normal' ),
  162. updated_by_id: 1,
  163. created_by_id: 1,
  164. )
  165. article_html = Ticket::Article.create(
  166. ticket_id: ticket.id,
  167. type_id: Ticket::Article::Type.where(name: 'phone' ).first.id,
  168. sender_id: Ticket::Article::Sender.where(name: 'Customer' ).first.id,
  169. from: 'Zammad Feedback <feedback@example.org>',
  170. body: 'some <b>text</b><br>next line',
  171. content_type: 'text/html',
  172. internal: false,
  173. updated_by_id: 1,
  174. created_by_id: 1,
  175. )
  176. tests = [
  177. {
  178. locale: 'de-de',
  179. string: 'Subject #{ticket.title}',
  180. result: 'Subject some title <b>äöüß</b> 2',
  181. },
  182. {
  183. locale: 'de-de',
  184. string: 'Subject #{article.from}, Group: #{ticket.group.name}',
  185. result: 'Subject Zammad Feedback <feedback@example.org>, Group: Users',
  186. },
  187. {
  188. locale: 'de-de',
  189. string: 'Body #{article.body}, Group: #{ticket.group.name}',
  190. result: 'Body some text
  191. next line, Group: Users',
  192. },
  193. ]
  194. tests.each { |test|
  195. result = NotificationFactory.build(
  196. string: test[:string],
  197. objects: {
  198. ticket: ticket,
  199. article: article_html,
  200. recipient: User.find(2),
  201. },
  202. locale: test[:locale]
  203. )
  204. assert_equal( test[:result], result, 'verify result' )
  205. }
  206. ticket.destroy
  207. end
  208. test 'notifications attack' do
  209. ticket = Ticket.create(
  210. title: 'some title <b>äöüß</b> 3',
  211. group: Group.lookup( name: 'Users'),
  212. customer_id: 2,
  213. state: Ticket::State.lookup( name: 'new' ),
  214. priority: Ticket::Priority.lookup( name: '2 normal' ),
  215. updated_by_id: 1,
  216. created_by_id: 1,
  217. )
  218. article_html = Ticket::Article.create(
  219. ticket_id: ticket.id,
  220. type_id: Ticket::Article::Type.where(name: 'phone' ).first.id,
  221. sender_id: Ticket::Article::Sender.where(name: 'Customer' ).first.id,
  222. from: 'Zammad Feedback <feedback@example.org>',
  223. body: 'some <b>text</b><br>next line',
  224. content_type: 'text/html',
  225. internal: false,
  226. updated_by_id: 1,
  227. created_by_id: 1,
  228. )
  229. tests = [
  230. {
  231. locale: 'de-de',
  232. string: '\#{puts `ls`}',
  233. result: '\#{puts `ls`} (not allowed)',
  234. },
  235. {
  236. locale: 'de-de',
  237. string: 'attack#1 #{article.destroy}',
  238. result: 'attack#1 #{article.destroy} (not allowed)',
  239. },
  240. {
  241. locale: 'de-de',
  242. string: 'attack#2 #{Article.where}',
  243. result: 'attack#2 #{Article.where} (not allowed)',
  244. },
  245. {
  246. locale: 'de-de',
  247. string: 'attack#1 #{article.
  248. destroy}',
  249. result: 'attack#1 #{article.
  250. destroy} (not allowed)',
  251. },
  252. {
  253. locale: 'de-de',
  254. string: 'attack#1 #{article.find}',
  255. result: 'attack#1 #{article.find} (not allowed)',
  256. },
  257. {
  258. locale: 'de-de',
  259. string: 'attack#1 #{article.update(:name => "test")}',
  260. result: 'attack#1 #{article.update(:name => "test")} (not allowed)',
  261. },
  262. {
  263. locale: 'de-de',
  264. string: 'attack#1 #{article.all}',
  265. result: 'attack#1 #{article.all} (not allowed)',
  266. },
  267. {
  268. locale: 'de-de',
  269. string: 'attack#1 #{article.delete}',
  270. result: 'attack#1 #{article.delete} (not allowed)',
  271. },
  272. ]
  273. tests.each { |test|
  274. result = NotificationFactory.build(
  275. string: test[:string],
  276. objects: {
  277. ticket: ticket,
  278. article: article_html,
  279. recipient: User.find(2),
  280. },
  281. locale: test[:locale]
  282. )
  283. assert_equal( test[:result], result, 'verify result' )
  284. }
  285. ticket.destroy
  286. end
  287. end