tickets_controller_test.rb 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class TicketsControllerTest < 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 - missing group' do
  46. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  47. params = {
  48. title: 'a new ticket #1',
  49. article: {
  50. content_type: 'text/plain', # or text/html
  51. body: 'some body',
  52. sender: 'Customer',
  53. type: 'note',
  54. },
  55. }
  56. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  57. assert_response(422)
  58. result = JSON.parse(@response.body)
  59. assert_equal(Hash, result.class)
  60. assert_equal('Group can\'t be blank', result['error_human'])
  61. end
  62. test '01.02 ticket create with agent - wrong group' do
  63. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  64. params = {
  65. title: 'a new ticket #2',
  66. group: 'not_existing',
  67. article: {
  68. content_type: 'text/plain', # or text/html
  69. body: 'some body',
  70. sender: 'Customer',
  71. type: 'note',
  72. },
  73. }
  74. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  75. assert_response(422)
  76. result = JSON.parse(@response.body)
  77. assert_equal(Hash, result.class)
  78. assert_equal('No lookup value found for \'group\': "not_existing"', result['error'])
  79. end
  80. test '01.03 ticket create with agent - missing article.body' do
  81. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  82. params = {
  83. title: 'a new ticket #3',
  84. group: 'Users',
  85. priority: '2 normal',
  86. state: 'new',
  87. customer_id: @customer_without_org.id,
  88. article: {},
  89. }
  90. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  91. assert_response(422)
  92. result = JSON.parse(@response.body)
  93. assert_equal(Hash, result.class)
  94. assert_equal('Need at least article: { body: "some text" }', result['error'])
  95. end
  96. test '01.03 ticket create with agent - minimal article' do
  97. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  98. params = {
  99. title: 'a new ticket #3',
  100. group: 'Users',
  101. priority: '2 normal',
  102. state: 'new',
  103. customer_id: @customer_without_org.id,
  104. article: {
  105. body: 'some test 123',
  106. },
  107. }
  108. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  109. assert_response(201)
  110. result = JSON.parse(@response.body)
  111. assert_equal(Hash, result.class)
  112. assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id'])
  113. assert_equal('a new ticket #3', result['title'])
  114. assert_equal(@customer_without_org.id, result['customer_id'])
  115. assert_equal(@agent.id, result['updated_by_id'])
  116. assert_equal(@agent.id, result['created_by_id'])
  117. end
  118. test '01.04 ticket create with agent - minimal article with guess customer' do
  119. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  120. params = {
  121. title: 'a new ticket #4',
  122. group: 'Users',
  123. priority: '2 normal',
  124. state: 'new',
  125. customer_id: 'guess:some_new_customer@example.com',
  126. article: {
  127. body: 'some test 123',
  128. },
  129. }
  130. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  131. assert_response(201)
  132. result = JSON.parse(@response.body)
  133. assert_equal(Hash, result.class)
  134. assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id'])
  135. assert_equal('a new ticket #4', result['title'])
  136. assert_equal(User.lookup(email: 'some_new_customer@example.com').id, result['customer_id'])
  137. assert_equal(@agent.id, result['updated_by_id'])
  138. assert_equal(@agent.id, result['created_by_id'])
  139. end
  140. test '02.02 ticket create with agent' do
  141. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  142. params = {
  143. title: 'a new ticket #1',
  144. state: 'new',
  145. priority: '2 normal',
  146. group: 'Users',
  147. customer: 'tickets-customer1@example.com',
  148. article: {
  149. content_type: 'text/plain', # or text/html
  150. body: 'some body',
  151. },
  152. links: {
  153. Ticket: {
  154. parent: [1],
  155. }
  156. }
  157. }
  158. post '/api/v1/tickets', 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(Ticket::State.lookup(name: 'new').id, result['state_id'])
  163. assert_equal('a new ticket #1', result['title'])
  164. assert_equal(@agent.id, result['updated_by_id'])
  165. assert_equal(@agent.id, result['created_by_id'])
  166. links = Link.list(
  167. link_object: 'Ticket',
  168. link_object_value: result['id'],
  169. )
  170. assert_equal('child', links[0]['link_type'])
  171. assert_equal('Ticket', links[0]['link_object'])
  172. assert_equal(1, links[0]['link_object_value'])
  173. end
  174. test '02.03 ticket with wrong ticket id' do
  175. group = Group.create_or_update(
  176. name: "GroupWithoutPermission-#{rand(9_999_999_999)}",
  177. active: true,
  178. updated_by_id: 1,
  179. created_by_id: 1,
  180. )
  181. ticket = Ticket.create!(
  182. title: 'ticket with wrong ticket id',
  183. group_id: group.id,
  184. customer_id: @customer_without_org.id,
  185. state: Ticket::State.lookup(name: 'new'),
  186. priority: Ticket::Priority.lookup(name: '2 normal'),
  187. updated_by_id: 1,
  188. created_by_id: 1,
  189. )
  190. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  191. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  192. assert_response(401)
  193. result = JSON.parse(@response.body)
  194. assert_equal(Hash, result.class)
  195. assert_equal('Not authorized', result['error'])
  196. params = {
  197. title: 'ticket with wrong ticket id - 2',
  198. }
  199. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  200. assert_response(401)
  201. result = JSON.parse(@response.body)
  202. assert_equal(Hash, result.class)
  203. assert_equal('Not authorized', result['error'])
  204. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  205. assert_response(401)
  206. result = JSON.parse(@response.body)
  207. assert_equal(Hash, result.class)
  208. assert_equal('Not authorized', result['error'])
  209. end
  210. test '02.04 ticket with correct ticket id' do
  211. title = "ticket with corret ticket id testagent#{rand(999_999_999)}"
  212. ticket = Ticket.create!(
  213. title: title,
  214. group: Group.lookup(name: 'Users'),
  215. customer_id: @customer_without_org.id,
  216. state: Ticket::State.lookup(name: 'new'),
  217. priority: Ticket::Priority.lookup(name: '2 normal'),
  218. updated_by_id: 1,
  219. created_by_id: 1,
  220. )
  221. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  222. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  223. assert_response(200)
  224. result = JSON.parse(@response.body)
  225. assert_equal(Hash, result.class)
  226. assert_equal(ticket.id, result['id'])
  227. assert_equal(title, result['title'])
  228. assert_equal(ticket.customer_id, result['customer_id'])
  229. assert_equal(1, result['updated_by_id'])
  230. assert_equal(1, result['created_by_id'])
  231. params = {
  232. title: "#{title} - 2",
  233. customer_id: @agent.id,
  234. }
  235. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  236. assert_response(200)
  237. result = JSON.parse(@response.body)
  238. assert_equal(Hash, result.class)
  239. assert_equal(ticket.id, result['id'])
  240. assert_equal("#{title} - 2", result['title'])
  241. assert_equal(@agent.id, result['customer_id'])
  242. assert_equal(@agent.id, result['updated_by_id'])
  243. assert_equal(1, result['created_by_id'])
  244. params = {
  245. ticket_id: ticket.id,
  246. subject: 'some subject',
  247. body: 'some body',
  248. }
  249. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  250. assert_response(201)
  251. article_result = JSON.parse(@response.body)
  252. assert_equal(Hash, article_result.class)
  253. assert_equal(ticket.id, article_result['ticket_id'])
  254. assert_equal('Tickets Agent', article_result['from'])
  255. assert_equal('some subject', article_result['subject'])
  256. assert_equal('some body', article_result['body'])
  257. assert_equal('text/plain', article_result['content_type'])
  258. assert_equal(false, article_result['internal'])
  259. assert_equal(@agent.id, article_result['created_by_id'])
  260. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, article_result['sender_id'])
  261. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, article_result['type_id'])
  262. Scheduler.worker(true)
  263. get "/api/v1/tickets/search?query=#{CGI.escape(title)}", {}, @headers.merge('Authorization' => credentials)
  264. assert_response(200)
  265. result = JSON.parse(@response.body)
  266. assert_equal(Hash, result.class)
  267. assert_equal(ticket.id, result['tickets'][0])
  268. assert_equal(1, result['tickets_count'])
  269. params = {
  270. condition: {
  271. 'ticket.title' => {
  272. operator: 'contains',
  273. value: title,
  274. },
  275. },
  276. }
  277. post '/api/v1/tickets/search', params.to_json, @headers.merge('Authorization' => credentials)
  278. assert_response(200)
  279. result = JSON.parse(@response.body)
  280. assert_equal(Hash, result.class)
  281. assert_equal(ticket.id, result['tickets'][0])
  282. assert_equal(1, result['tickets_count'])
  283. delete "/api/v1/ticket_articles/#{article_result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  284. assert_response(200)
  285. params = {
  286. from: 'something which should not be changed on server side',
  287. ticket_id: ticket.id,
  288. subject: 'some subject',
  289. body: 'some body',
  290. type: 'email',
  291. internal: true,
  292. }
  293. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  294. assert_response(201)
  295. result = JSON.parse(@response.body)
  296. assert_equal(Hash, result.class)
  297. assert_equal(ticket.id, result['ticket_id'])
  298. assert_equal('Tickets Agent via Zammad <zammad@localhost>', result['from'])
  299. assert_equal('some subject', result['subject'])
  300. assert_equal('some body', result['body'])
  301. assert_equal('text/plain', result['content_type'])
  302. assert_equal(true, result['internal'])
  303. assert_equal(@agent.id, result['created_by_id'])
  304. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  305. assert_equal(Ticket::Article::Type.lookup(name: 'email').id, result['type_id'])
  306. params = {
  307. subject: 'new subject',
  308. }
  309. put "/api/v1/ticket_articles/#{result['id']}", params.to_json, @headers.merge('Authorization' => credentials)
  310. assert_response(200)
  311. result = JSON.parse(@response.body)
  312. assert_equal(Hash, result.class)
  313. assert_equal(ticket.id, result['ticket_id'])
  314. assert_equal('Tickets Agent via Zammad <zammad@localhost>', result['from'])
  315. assert_equal('new subject', result['subject'])
  316. assert_equal('some body', result['body'])
  317. assert_equal('text/plain', result['content_type'])
  318. assert_equal(true, result['internal'])
  319. assert_equal(@agent.id, result['created_by_id'])
  320. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  321. assert_equal(Ticket::Article::Type.lookup(name: 'email').id, result['type_id'])
  322. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  323. assert_response(401)
  324. result = JSON.parse(@response.body)
  325. assert_equal(Hash, result.class)
  326. assert_equal('Not authorized (admin permission required)!', result['error'])
  327. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  328. assert_response(401)
  329. result = JSON.parse(@response.body)
  330. assert_equal(Hash, result.class)
  331. assert_equal('Not authorized (admin permission required)!', result['error'])
  332. end
  333. test '02.05 ticket with correct ticket id' do
  334. ticket = Ticket.create!(
  335. title: 'ticket with corret ticket id',
  336. group: Group.lookup(name: 'Users'),
  337. customer_id: @customer_without_org.id,
  338. state: Ticket::State.lookup(name: 'new'),
  339. priority: Ticket::Priority.lookup(name: '2 normal'),
  340. updated_by_id: 1,
  341. created_by_id: 1,
  342. )
  343. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  344. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  345. assert_response(200)
  346. result = JSON.parse(@response.body)
  347. assert_equal(Hash, result.class)
  348. assert_equal(ticket.id, result['id'])
  349. assert_equal('ticket with corret ticket id', result['title'])
  350. assert_equal(ticket.customer_id, result['customer_id'])
  351. assert_equal(1, result['updated_by_id'])
  352. assert_equal(1, result['created_by_id'])
  353. params = {
  354. title: 'ticket with corret ticket id - 2',
  355. customer_id: @agent.id,
  356. }
  357. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  358. assert_response(200)
  359. result = JSON.parse(@response.body)
  360. assert_equal(Hash, result.class)
  361. assert_equal(ticket.id, result['id'])
  362. assert_equal('ticket with corret ticket id - 2', result['title'])
  363. assert_equal(@agent.id, result['customer_id'])
  364. assert_equal(@admin.id, result['updated_by_id'])
  365. assert_equal(1, result['created_by_id'])
  366. params = {
  367. from: 'something which should not be changed on server side',
  368. ticket_id: ticket.id,
  369. subject: 'some subject',
  370. body: 'some body',
  371. }
  372. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  373. assert_response(201)
  374. result = JSON.parse(@response.body)
  375. assert_equal(Hash, result.class)
  376. assert_equal(ticket.id, result['ticket_id'])
  377. assert_equal('Tickets Admin', result['from'])
  378. assert_equal('some subject', result['subject'])
  379. assert_equal('some body', result['body'])
  380. assert_equal('text/plain', result['content_type'])
  381. assert_equal(false, result['internal'])
  382. assert_equal(@admin.id, result['created_by_id'])
  383. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  384. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  385. params = {
  386. subject: 'new subject',
  387. internal: true,
  388. }
  389. put "/api/v1/ticket_articles/#{result['id']}", params.to_json, @headers.merge('Authorization' => credentials)
  390. assert_response(200)
  391. result = JSON.parse(@response.body)
  392. assert_equal(Hash, result.class)
  393. assert_equal(ticket.id, result['ticket_id'])
  394. assert_equal('Tickets Admin', result['from'])
  395. assert_equal('new subject', result['subject'])
  396. assert_equal('some body', result['body'])
  397. assert_equal('text/plain', result['content_type'])
  398. assert_equal(true, result['internal'])
  399. assert_equal(@admin.id, result['created_by_id'])
  400. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  401. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  402. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  403. assert_response(200)
  404. params = {
  405. ticket_id: ticket.id,
  406. subject: 'some subject',
  407. body: 'some body',
  408. type: 'email',
  409. }
  410. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  411. assert_response(201)
  412. result = JSON.parse(@response.body)
  413. assert_equal(Hash, result.class)
  414. assert_equal(ticket.id, result['ticket_id'])
  415. assert_equal('Tickets Admin via Zammad <zammad@localhost>', result['from'])
  416. assert_equal('some subject', result['subject'])
  417. assert_equal('some body', result['body'])
  418. assert_equal('text/plain', result['content_type'])
  419. assert_equal(false, result['internal'])
  420. assert_equal(@admin.id, result['created_by_id'])
  421. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  422. assert_equal(Ticket::Article::Type.lookup(name: 'email').id, result['type_id'])
  423. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  424. assert_response(200)
  425. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  426. assert_response(200)
  427. end
  428. test '02.05 ticket pagination' do
  429. title = "ticket pagination #{rand(999_999_999)}"
  430. tickets = []
  431. (1..20).each { |count|
  432. ticket = Ticket.create!(
  433. title: "#{title} - #{count}",
  434. group: Group.lookup(name: 'Users'),
  435. customer_id: @customer_without_org.id,
  436. state: Ticket::State.lookup(name: 'new'),
  437. priority: Ticket::Priority.lookup(name: '2 normal'),
  438. updated_by_id: 1,
  439. created_by_id: 1,
  440. )
  441. Ticket::Article.create!(
  442. type: Ticket::Article::Type.lookup(name: 'note'),
  443. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  444. from: 'sender',
  445. subject: 'subject',
  446. body: 'some body',
  447. ticket_id: ticket.id,
  448. updated_by_id: 1,
  449. created_by_id: 1,
  450. )
  451. tickets.push ticket
  452. sleep 1
  453. }
  454. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  455. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", {}, @headers.merge('Authorization' => credentials)
  456. assert_response(200)
  457. result = JSON.parse(@response.body)
  458. assert_equal(Hash, result.class)
  459. assert_equal(tickets[19].id, result['tickets'][0])
  460. assert_equal(tickets[0].id, result['tickets'][19])
  461. assert_equal(20, result['tickets_count'])
  462. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=10", {}, @headers.merge('Authorization' => credentials)
  463. assert_response(200)
  464. result = JSON.parse(@response.body)
  465. assert_equal(Hash, result.class)
  466. assert_equal(tickets[19].id, result['tickets'][0])
  467. assert_equal(tickets[10].id, result['tickets'][9])
  468. assert_equal(10, result['tickets_count'])
  469. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=1&per_page=5", {}, @headers.merge('Authorization' => credentials)
  470. assert_response(200)
  471. result = JSON.parse(@response.body)
  472. assert_equal(Hash, result.class)
  473. assert_equal(tickets[19].id, result['tickets'][0])
  474. assert_equal(tickets[15].id, result['tickets'][4])
  475. assert_equal(5, result['tickets_count'])
  476. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=2&per_page=5", {}, @headers.merge('Authorization' => credentials)
  477. assert_response(200)
  478. result = JSON.parse(@response.body)
  479. assert_equal(Hash, result.class)
  480. assert_equal(tickets[14].id, result['tickets'][0])
  481. assert_equal(tickets[10].id, result['tickets'][4])
  482. assert_equal(5, result['tickets_count'])
  483. get '/api/v1/tickets?limit=40&page=1&per_page=5', {}, @headers.merge('Authorization' => credentials)
  484. assert_response(200)
  485. result = JSON.parse(@response.body)
  486. assert_equal(Array, result.class)
  487. tickets = Ticket.order(:id).limit(5)
  488. assert_equal(tickets[0].id, result[0]['id'])
  489. assert_equal(tickets[4].id, result[4]['id'])
  490. assert_equal(5, result.count)
  491. get '/api/v1/tickets?limit=40&page=2&per_page=5', {}, @headers.merge('Authorization' => credentials)
  492. assert_response(200)
  493. result = JSON.parse(@response.body)
  494. assert_equal(Array, result.class)
  495. tickets = Ticket.order(:id).limit(10)
  496. assert_equal(tickets[5].id, result[0]['id'])
  497. assert_equal(tickets[9].id, result[4]['id'])
  498. assert_equal(5, result.count)
  499. end
  500. test '03.01 ticket create with customer minimal' do
  501. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  502. params = {
  503. title: 'a new ticket #c1',
  504. state: 'new',
  505. priority: '2 normal',
  506. group: 'Users',
  507. article: {
  508. body: 'some body',
  509. },
  510. }
  511. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  512. assert_response(201)
  513. result = JSON.parse(@response.body)
  514. assert_equal(Hash, result.class)
  515. assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id'])
  516. assert_equal('a new ticket #c1', result['title'])
  517. assert_equal(@customer_without_org.id, result['customer_id'])
  518. assert_equal(@customer_without_org.id, result['updated_by_id'])
  519. assert_equal(@customer_without_org.id, result['created_by_id'])
  520. end
  521. test '03.02 ticket create with customer with wrong customer' do
  522. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  523. params = {
  524. title: 'a new ticket #c2',
  525. state: 'new',
  526. priority: '2 normal',
  527. group: 'Users',
  528. customer_id: @agent.id,
  529. article: {
  530. content_type: 'text/plain', # or text/html
  531. body: 'some body',
  532. sender: 'System',
  533. },
  534. }
  535. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  536. assert_response(201)
  537. result = JSON.parse(@response.body)
  538. assert_equal(Hash, result.class)
  539. assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id'])
  540. assert_equal('a new ticket #c2', result['title'])
  541. assert_equal(@customer_without_org.id, result['customer_id'])
  542. assert_equal(@customer_without_org.id, result['updated_by_id'])
  543. assert_equal(@customer_without_org.id, result['created_by_id'])
  544. end
  545. test '03.03 ticket with wrong ticket id' do
  546. ticket = Ticket.create!(
  547. title: 'ticket with wrong ticket id',
  548. group: Group.lookup(name: 'Users'),
  549. customer_id: @agent.id,
  550. state: Ticket::State.lookup(name: 'new'),
  551. priority: Ticket::Priority.lookup(name: '2 normal'),
  552. updated_by_id: 1,
  553. created_by_id: 1,
  554. )
  555. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  556. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  557. assert_response(401)
  558. result = JSON.parse(@response.body)
  559. assert_equal(Hash, result.class)
  560. assert_equal('Not authorized', result['error'])
  561. params = {
  562. title: 'ticket with wrong ticket id - 2',
  563. }
  564. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  565. assert_response(401)
  566. result = JSON.parse(@response.body)
  567. assert_equal(Hash, result.class)
  568. assert_equal('Not authorized', result['error'])
  569. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  570. assert_response(401)
  571. result = JSON.parse(@response.body)
  572. assert_equal(Hash, result.class)
  573. assert_equal('Not authorized', result['error'])
  574. end
  575. test '03.04 ticket with correct ticket id' do
  576. title = "ticket with corret ticket id testme#{rand(999_999_999)}"
  577. ticket = Ticket.create!(
  578. title: title,
  579. group: Group.lookup(name: 'Users'),
  580. customer_id: @customer_without_org.id,
  581. state: Ticket::State.lookup(name: 'new'),
  582. priority: Ticket::Priority.lookup(name: '2 normal'),
  583. updated_by_id: 1,
  584. created_by_id: 1,
  585. )
  586. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  587. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  588. assert_response(200)
  589. result = JSON.parse(@response.body)
  590. assert_equal(Hash, result.class)
  591. assert_equal(ticket.id, result['id'])
  592. assert_equal(title, result['title'])
  593. assert_equal(ticket.customer_id, result['customer_id'])
  594. assert_equal(1, result['updated_by_id'])
  595. assert_equal(1, result['created_by_id'])
  596. params = {
  597. title: "#{title} - 2",
  598. customer_id: @agent.id,
  599. }
  600. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  601. assert_response(200)
  602. result = JSON.parse(@response.body)
  603. assert_equal(Hash, result.class)
  604. assert_equal(ticket.id, result['id'])
  605. assert_equal("#{title} - 2", result['title'])
  606. assert_equal(ticket.customer_id, result['customer_id'])
  607. assert_equal(@customer_without_org.id, result['updated_by_id'])
  608. assert_equal(1, result['created_by_id'])
  609. params = {
  610. ticket_id: ticket.id,
  611. subject: 'some subject',
  612. body: 'some body',
  613. }
  614. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  615. assert_response(201)
  616. article_result = JSON.parse(@response.body)
  617. assert_equal(Hash, article_result.class)
  618. assert_equal(ticket.id, article_result['ticket_id'])
  619. assert_equal('Tickets Customer1', article_result['from'])
  620. assert_equal('some subject', article_result['subject'])
  621. assert_equal('some body', article_result['body'])
  622. assert_equal('text/plain', article_result['content_type'])
  623. assert_equal(@customer_without_org.id, article_result['created_by_id'])
  624. assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, article_result['sender_id'])
  625. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, article_result['type_id'])
  626. Scheduler.worker(true)
  627. get "/api/v1/tickets/search?query=#{CGI.escape(title)}", {}, @headers.merge('Authorization' => credentials)
  628. assert_response(200)
  629. result = JSON.parse(@response.body)
  630. assert_equal(Hash, result.class)
  631. assert_equal(ticket.id, result['tickets'][0])
  632. assert_equal(1, result['tickets_count'])
  633. params = {
  634. condition: {
  635. 'ticket.title' => {
  636. operator: 'contains',
  637. value: title,
  638. },
  639. },
  640. }
  641. post '/api/v1/tickets/search', params.to_json, @headers.merge('Authorization' => credentials)
  642. assert_response(200)
  643. result = JSON.parse(@response.body)
  644. assert_equal(Hash, result.class)
  645. assert_equal(ticket.id, result['tickets'][0])
  646. assert_equal(1, result['tickets_count'])
  647. delete "/api/v1/ticket_articles/#{article_result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  648. assert_response(401)
  649. result = JSON.parse(@response.body)
  650. assert_equal(Hash, result.class)
  651. assert_equal('Not authorized (admin permission required)!', result['error'])
  652. params = {
  653. ticket_id: ticket.id,
  654. subject: 'some subject',
  655. body: 'some body',
  656. type: 'email',
  657. sender: 'Agent',
  658. }
  659. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  660. assert_response(201)
  661. result = JSON.parse(@response.body)
  662. assert_equal(Hash, result.class)
  663. assert_equal(ticket.id, result['ticket_id'])
  664. assert_equal('Tickets Customer1', result['from'])
  665. assert_equal('some subject', result['subject'])
  666. assert_equal('some body', result['body'])
  667. assert_equal('text/plain', result['content_type'])
  668. assert_equal(@customer_without_org.id, result['created_by_id'])
  669. assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, result['sender_id'])
  670. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  671. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  672. assert_response(401)
  673. result = JSON.parse(@response.body)
  674. assert_equal(Hash, result.class)
  675. assert_equal('Not authorized (admin permission required)!', result['error'])
  676. params = {
  677. from: 'something which should not be changed on server side',
  678. ticket_id: ticket.id,
  679. subject: 'some subject',
  680. body: 'some body',
  681. type: 'web',
  682. sender: 'Agent',
  683. internal: true,
  684. }
  685. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  686. assert_response(201)
  687. result = JSON.parse(@response.body)
  688. assert_equal(Hash, result.class)
  689. assert_equal(ticket.id, result['ticket_id'])
  690. assert_equal('Tickets Customer1 <tickets-customer1@example.com>', result['from'])
  691. assert_equal('some subject', result['subject'])
  692. assert_equal('some body', result['body'])
  693. assert_equal('text/plain', result['content_type'])
  694. assert_equal(false, result['internal'])
  695. assert_equal(@customer_without_org.id, result['created_by_id'])
  696. assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, result['sender_id'])
  697. assert_equal(Ticket::Article::Type.lookup(name: 'web').id, result['type_id'])
  698. params = {
  699. subject: 'new subject',
  700. }
  701. put "/api/v1/ticket_articles/#{result['id']}", params.to_json, @headers.merge('Authorization' => credentials)
  702. assert_response(401)
  703. result = JSON.parse(@response.body)
  704. assert_equal(Hash, result.class)
  705. assert_equal('Not authorized (ticket.agent or admin permission required)!', result['error'])
  706. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  707. assert_response(401)
  708. result = JSON.parse(@response.body)
  709. assert_equal(Hash, result.class)
  710. assert_equal('Not authorized (admin permission required)!', result['error'])
  711. end
  712. end