ticket_articles_controller_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class TicketArticlesControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # create agent
  8. roles = Role.where(name: %w(Admin Agent))
  9. groups = Group.all
  10. UserInfo.current_user_id = 1
  11. @admin = User.create_or_update(
  12. login: 'tickets-admin',
  13. firstname: 'Tickets',
  14. lastname: 'Admin',
  15. email: 'tickets-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. # create agent
  22. roles = Role.where(name: 'Agent')
  23. @agent = User.create_or_update(
  24. login: 'tickets-agent@example.com',
  25. firstname: 'Tickets',
  26. lastname: 'Agent',
  27. email: 'tickets-agent@example.com',
  28. password: 'agentpw',
  29. active: true,
  30. roles: roles,
  31. groups: groups,
  32. )
  33. # create customer without org
  34. roles = Role.where(name: 'Customer')
  35. @customer_without_org = User.create_or_update(
  36. login: 'tickets-customer1@example.com',
  37. firstname: 'Tickets',
  38. lastname: 'Customer1',
  39. email: 'tickets-customer1@example.com',
  40. password: 'customer1pw',
  41. active: true,
  42. roles: roles,
  43. )
  44. end
  45. test '01.01 ticket create with agent and articles' do
  46. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  47. params = {
  48. title: 'a new ticket #1',
  49. group: 'Users',
  50. customer_id: @customer_without_org.id,
  51. article: {
  52. body: 'some body',
  53. }
  54. }
  55. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  56. assert_response(201)
  57. result = JSON.parse(@response.body)
  58. params = {
  59. ticket_id: result['id'],
  60. content_type: 'text/plain', # or text/html
  61. body: 'some body',
  62. type: 'note',
  63. }
  64. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  65. assert_response(201)
  66. result = JSON.parse(@response.body)
  67. assert_equal(Hash, result.class)
  68. assert_equal(nil, result['subject'])
  69. assert_equal('some body', result['body'])
  70. assert_equal('text/plain', result['content_type'])
  71. assert_equal(@agent.id, result['updated_by_id'])
  72. assert_equal(@agent.id, result['created_by_id'])
  73. ticket = Ticket.find(result['ticket_id'])
  74. assert_equal(2, ticket.articles.count)
  75. assert_equal(0, ticket.articles[0].attachments.count)
  76. assert_equal(0, ticket.articles[1].attachments.count)
  77. params = {
  78. ticket_id: result['ticket_id'],
  79. content_type: 'text/html', # or text/html
  80. body: 'some body <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
  81. AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
  82. 9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />',
  83. type: 'note',
  84. }
  85. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  86. assert_response(201)
  87. result = JSON.parse(@response.body)
  88. assert_equal(Hash, result.class)
  89. assert_equal(nil, result['subject'])
  90. assert_no_match(/some body <img src="cid:.+?/, result['body'])
  91. assert_match(%r{some body <img src="/api/v1/ticket_attachment/.+?" alt="Red dot"}, result['body'])
  92. assert_equal('text/html', result['content_type'])
  93. assert_equal(@agent.id, result['updated_by_id'])
  94. assert_equal(@agent.id, result['created_by_id'])
  95. assert_equal(3, ticket.articles.count)
  96. assert_equal(0, ticket.articles[0].attachments.count)
  97. assert_equal(0, ticket.articles[1].attachments.count)
  98. assert_equal(1, ticket.articles[2].attachments.count)
  99. assert(ticket.articles[2].attachments[0]['id'])
  100. assert_match(/@zammad.example.com/, ticket.articles[2].attachments[0]['filename'])
  101. assert_equal('21', ticket.articles[2].attachments[0]['size'])
  102. assert_equal('image/png', ticket.articles[2].attachments[0]['preferences']['Mime-Type'])
  103. assert_equal('inline', ticket.articles[2].attachments[0]['preferences']['Content-Disposition'])
  104. assert_match(/@zammad.example.com/, ticket.articles[2].attachments[0]['preferences']['Content-ID'])
  105. params = {
  106. ticket_id: result['ticket_id'],
  107. content_type: 'text/html', # or text/html
  108. body: 'some body',
  109. type: 'note',
  110. attachments: [
  111. 'filename' => 'some_file.txt',
  112. 'data' => 'dGVzdCAxMjM=',
  113. 'mime-type' => 'text/plain',
  114. ],
  115. }
  116. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  117. assert_response(201)
  118. result = JSON.parse(@response.body)
  119. assert_equal(Hash, result.class)
  120. assert_equal(nil, result['subject'])
  121. assert_equal('some body', result['body'])
  122. assert_equal('text/html', result['content_type'])
  123. assert_equal(@agent.id, result['updated_by_id'])
  124. assert_equal(@agent.id, result['created_by_id'])
  125. assert_equal(4, ticket.articles.count)
  126. assert_equal(0, ticket.articles[0].attachments.count)
  127. assert_equal(0, ticket.articles[1].attachments.count)
  128. assert_equal(1, ticket.articles[2].attachments.count)
  129. assert_equal(1, ticket.articles[3].attachments.count)
  130. get "/api/v1/ticket_articles/#{result['id']}?expand=true", {}.to_json, @headers.merge('Authorization' => credentials)
  131. assert_response(200)
  132. result = JSON.parse(@response.body)
  133. assert_equal(Hash, result.class)
  134. assert_equal(1, result['attachments'].count)
  135. assert(result['attachments'][0]['id'])
  136. assert_equal('some_file.txt', result['attachments'][0]['filename'])
  137. assert_equal('8', result['attachments'][0]['size'])
  138. assert_equal('text/plain', result['attachments'][0]['preferences']['Mime-Type'])
  139. end
  140. test '02.01 ticket create with customer and articles' do
  141. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  142. params = {
  143. title: 'a new ticket #2',
  144. group: 'Users',
  145. article: {
  146. body: 'some body',
  147. }
  148. }
  149. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  150. assert_response(201)
  151. result = JSON.parse(@response.body)
  152. params = {
  153. ticket_id: result['id'],
  154. content_type: 'text/plain', # or text/html
  155. body: 'some body',
  156. type: 'note',
  157. }
  158. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  159. assert_response(201)
  160. result = JSON.parse(@response.body)
  161. assert_equal(Hash, result.class)
  162. assert_equal(nil, result['subject'])
  163. assert_equal('some body', result['body'])
  164. assert_equal('text/plain', result['content_type'])
  165. assert_equal(@customer_without_org.id, result['updated_by_id'])
  166. assert_equal(@customer_without_org.id, result['created_by_id'])
  167. ticket = Ticket.find(result['ticket_id'])
  168. assert_equal(2, ticket.articles.count)
  169. assert_equal('Customer', ticket.articles[1].sender.name)
  170. assert_equal(0, ticket.articles[0].attachments.count)
  171. assert_equal(0, ticket.articles[1].attachments.count)
  172. params = {
  173. ticket_id: result['ticket_id'],
  174. content_type: 'text/plain', # or text/html
  175. body: 'some body',
  176. sender: 'Agent',
  177. type: 'note',
  178. }
  179. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  180. assert_response(201)
  181. result = JSON.parse(@response.body)
  182. assert_equal(Hash, result.class)
  183. assert_equal(nil, result['subject'])
  184. assert_equal('some body', result['body'])
  185. assert_equal('text/plain', result['content_type'])
  186. assert_equal(@customer_without_org.id, result['updated_by_id'])
  187. assert_equal(@customer_without_org.id, result['created_by_id'])
  188. ticket = Ticket.find(result['ticket_id'])
  189. assert_equal(3, ticket.articles.count)
  190. assert_equal('Customer', ticket.articles[2].sender.name)
  191. assert_equal(false, ticket.articles[2].internal)
  192. assert_equal(0, ticket.articles[0].attachments.count)
  193. assert_equal(0, ticket.articles[1].attachments.count)
  194. assert_equal(0, ticket.articles[2].attachments.count)
  195. params = {
  196. ticket_id: result['ticket_id'],
  197. content_type: 'text/plain', # or text/html
  198. body: 'some body 2',
  199. sender: 'Agent',
  200. type: 'note',
  201. internal: true,
  202. }
  203. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  204. assert_response(201)
  205. result = JSON.parse(@response.body)
  206. assert_equal(Hash, result.class)
  207. assert_equal(nil, result['subject'])
  208. assert_equal('some body 2', result['body'])
  209. assert_equal('text/plain', result['content_type'])
  210. assert_equal(@customer_without_org.id, result['updated_by_id'])
  211. assert_equal(@customer_without_org.id, result['created_by_id'])
  212. ticket = Ticket.find(result['ticket_id'])
  213. assert_equal(4, ticket.articles.count)
  214. assert_equal('Customer', ticket.articles[3].sender.name)
  215. assert_equal(false, ticket.articles[3].internal)
  216. assert_equal(0, ticket.articles[0].attachments.count)
  217. assert_equal(0, ticket.articles[1].attachments.count)
  218. assert_equal(0, ticket.articles[2].attachments.count)
  219. assert_equal(0, ticket.articles[3].attachments.count)
  220. # add internal article
  221. article = Ticket::Article.create(
  222. ticket_id: ticket.id,
  223. from: 'some_sender@example.com',
  224. to: 'some_recipient@example.com',
  225. subject: 'some subject',
  226. message_id: 'some@id',
  227. body: 'some message 123',
  228. internal: true,
  229. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  230. type: Ticket::Article::Type.find_by(name: 'note'),
  231. updated_by_id: 1,
  232. created_by_id: 1,
  233. )
  234. assert_equal(5, ticket.articles.count)
  235. assert_equal('Agent', ticket.articles[4].sender.name)
  236. assert_equal(1, ticket.articles[4].updated_by_id)
  237. assert_equal(1, ticket.articles[4].created_by_id)
  238. assert_equal(0, ticket.articles[0].attachments.count)
  239. assert_equal(0, ticket.articles[1].attachments.count)
  240. assert_equal(0, ticket.articles[2].attachments.count)
  241. assert_equal(0, ticket.articles[3].attachments.count)
  242. assert_equal(0, ticket.articles[4].attachments.count)
  243. get "/api/v1/ticket_articles/#{article.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  244. assert_response(401)
  245. result = JSON.parse(@response.body)
  246. assert_equal(Hash, result.class)
  247. assert_equal('Not authorized', result['error'])
  248. put "/api/v1/ticket_articles/#{article.id}", { internal: false }.to_json, @headers.merge('Authorization' => credentials)
  249. assert_response(401)
  250. result = JSON.parse(@response.body)
  251. assert_equal(Hash, result.class)
  252. assert_equal('Not authorized', result['error'])
  253. end
  254. end