tickets_controller_test.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. ticket = Ticket.create!(
  212. title: 'ticket with corret ticket id',
  213. group: Group.lookup(name: 'Users'),
  214. customer_id: @customer_without_org.id,
  215. state: Ticket::State.lookup(name: 'new'),
  216. priority: Ticket::Priority.lookup(name: '2 normal'),
  217. updated_by_id: 1,
  218. created_by_id: 1,
  219. )
  220. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-agent@example.com', 'agentpw')
  221. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  222. assert_response(200)
  223. result = JSON.parse(@response.body)
  224. assert_equal(Hash, result.class)
  225. assert_equal(ticket.id, result['id'])
  226. assert_equal('ticket with corret ticket id', result['title'])
  227. assert_equal(ticket.customer_id, result['customer_id'])
  228. assert_equal(1, result['updated_by_id'])
  229. assert_equal(1, result['created_by_id'])
  230. params = {
  231. title: 'ticket with corret ticket id - 2',
  232. customer_id: @agent.id,
  233. }
  234. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  235. assert_response(200)
  236. result = JSON.parse(@response.body)
  237. assert_equal(Hash, result.class)
  238. assert_equal(ticket.id, result['id'])
  239. assert_equal('ticket with corret ticket id - 2', result['title'])
  240. assert_equal(@agent.id, result['customer_id'])
  241. assert_equal(@agent.id, result['updated_by_id'])
  242. assert_equal(1, result['created_by_id'])
  243. params = {
  244. ticket_id: ticket.id,
  245. subject: 'some subject',
  246. body: 'some body',
  247. }
  248. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  249. assert_response(201)
  250. result = JSON.parse(@response.body)
  251. assert_equal(Hash, result.class)
  252. assert_equal(ticket.id, result['ticket_id'])
  253. assert_equal('Tickets Agent', result['from'])
  254. assert_equal('some subject', result['subject'])
  255. assert_equal('some body', result['body'])
  256. assert_equal('text/plain', result['content_type'])
  257. assert_equal(false, result['internal'])
  258. assert_equal(@agent.id, result['created_by_id'])
  259. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  260. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  261. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  262. assert_response(200)
  263. params = {
  264. from: 'something which should not be changed on server side',
  265. ticket_id: ticket.id,
  266. subject: 'some subject',
  267. body: 'some body',
  268. type: 'email',
  269. internal: true,
  270. }
  271. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  272. assert_response(201)
  273. result = JSON.parse(@response.body)
  274. assert_equal(Hash, result.class)
  275. assert_equal(ticket.id, result['ticket_id'])
  276. assert_equal('Tickets Agent via Zammad <zammad@localhost>', result['from'])
  277. assert_equal('some subject', result['subject'])
  278. assert_equal('some body', result['body'])
  279. assert_equal('text/plain', result['content_type'])
  280. assert_equal(true, result['internal'])
  281. assert_equal(@agent.id, result['created_by_id'])
  282. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  283. assert_equal(Ticket::Article::Type.lookup(name: 'email').id, result['type_id'])
  284. params = {
  285. subject: 'new subject',
  286. }
  287. put "/api/v1/ticket_articles/#{result['id']}", params.to_json, @headers.merge('Authorization' => credentials)
  288. assert_response(200)
  289. result = JSON.parse(@response.body)
  290. assert_equal(Hash, result.class)
  291. assert_equal(ticket.id, result['ticket_id'])
  292. assert_equal('Tickets Agent via Zammad <zammad@localhost>', result['from'])
  293. assert_equal('new subject', result['subject'])
  294. assert_equal('some body', result['body'])
  295. assert_equal('text/plain', result['content_type'])
  296. assert_equal(true, result['internal'])
  297. assert_equal(@agent.id, result['created_by_id'])
  298. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  299. assert_equal(Ticket::Article::Type.lookup(name: 'email').id, result['type_id'])
  300. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  301. assert_response(401)
  302. result = JSON.parse(@response.body)
  303. assert_equal(Hash, result.class)
  304. assert_equal('Not authorized (admin permission required)!', result['error'])
  305. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  306. assert_response(401)
  307. result = JSON.parse(@response.body)
  308. assert_equal(Hash, result.class)
  309. assert_equal('Not authorized (admin permission required)!', result['error'])
  310. end
  311. test '02.05 ticket with correct ticket id' do
  312. ticket = Ticket.create!(
  313. title: 'ticket with corret ticket id',
  314. group: Group.lookup(name: 'Users'),
  315. customer_id: @customer_without_org.id,
  316. state: Ticket::State.lookup(name: 'new'),
  317. priority: Ticket::Priority.lookup(name: '2 normal'),
  318. updated_by_id: 1,
  319. created_by_id: 1,
  320. )
  321. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  322. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  323. assert_response(200)
  324. result = JSON.parse(@response.body)
  325. assert_equal(Hash, result.class)
  326. assert_equal(ticket.id, result['id'])
  327. assert_equal('ticket with corret ticket id', result['title'])
  328. assert_equal(ticket.customer_id, result['customer_id'])
  329. assert_equal(1, result['updated_by_id'])
  330. assert_equal(1, result['created_by_id'])
  331. params = {
  332. title: 'ticket with corret ticket id - 2',
  333. customer_id: @agent.id,
  334. }
  335. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  336. assert_response(200)
  337. result = JSON.parse(@response.body)
  338. assert_equal(Hash, result.class)
  339. assert_equal(ticket.id, result['id'])
  340. assert_equal('ticket with corret ticket id - 2', result['title'])
  341. assert_equal(@agent.id, result['customer_id'])
  342. assert_equal(@admin.id, result['updated_by_id'])
  343. assert_equal(1, result['created_by_id'])
  344. params = {
  345. from: 'something which should not be changed on server side',
  346. ticket_id: ticket.id,
  347. subject: 'some subject',
  348. body: 'some body',
  349. }
  350. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  351. assert_response(201)
  352. result = JSON.parse(@response.body)
  353. assert_equal(Hash, result.class)
  354. assert_equal(ticket.id, result['ticket_id'])
  355. assert_equal('Tickets Admin', result['from'])
  356. assert_equal('some subject', result['subject'])
  357. assert_equal('some body', result['body'])
  358. assert_equal('text/plain', result['content_type'])
  359. assert_equal(false, result['internal'])
  360. assert_equal(@admin.id, result['created_by_id'])
  361. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  362. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  363. params = {
  364. subject: 'new subject',
  365. internal: true,
  366. }
  367. put "/api/v1/ticket_articles/#{result['id']}", params.to_json, @headers.merge('Authorization' => credentials)
  368. assert_response(200)
  369. result = JSON.parse(@response.body)
  370. assert_equal(Hash, result.class)
  371. assert_equal(ticket.id, result['ticket_id'])
  372. assert_equal('Tickets Admin', result['from'])
  373. assert_equal('new subject', result['subject'])
  374. assert_equal('some body', result['body'])
  375. assert_equal('text/plain', result['content_type'])
  376. assert_equal(true, result['internal'])
  377. assert_equal(@admin.id, result['created_by_id'])
  378. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  379. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  380. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  381. assert_response(200)
  382. params = {
  383. ticket_id: ticket.id,
  384. subject: 'some subject',
  385. body: 'some body',
  386. type: 'email',
  387. }
  388. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  389. assert_response(201)
  390. result = JSON.parse(@response.body)
  391. assert_equal(Hash, result.class)
  392. assert_equal(ticket.id, result['ticket_id'])
  393. assert_equal('Tickets Admin via Zammad <zammad@localhost>', result['from'])
  394. assert_equal('some subject', result['subject'])
  395. assert_equal('some body', result['body'])
  396. assert_equal('text/plain', result['content_type'])
  397. assert_equal(false, result['internal'])
  398. assert_equal(@admin.id, result['created_by_id'])
  399. assert_equal(Ticket::Article::Sender.lookup(name: 'Agent').id, result['sender_id'])
  400. assert_equal(Ticket::Article::Type.lookup(name: 'email').id, result['type_id'])
  401. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  402. assert_response(200)
  403. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  404. assert_response(200)
  405. end
  406. test '03.01 ticket create with customer minimal' do
  407. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  408. params = {
  409. title: 'a new ticket #c1',
  410. state: 'new',
  411. priority: '2 normal',
  412. group: 'Users',
  413. article: {
  414. body: 'some body',
  415. },
  416. }
  417. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  418. assert_response(201)
  419. result = JSON.parse(@response.body)
  420. assert_equal(Hash, result.class)
  421. assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id'])
  422. assert_equal('a new ticket #c1', result['title'])
  423. assert_equal(@customer_without_org.id, result['customer_id'])
  424. assert_equal(@customer_without_org.id, result['updated_by_id'])
  425. assert_equal(@customer_without_org.id, result['created_by_id'])
  426. end
  427. test '03.02 ticket create with customer with wrong customer' do
  428. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  429. params = {
  430. title: 'a new ticket #c2',
  431. state: 'new',
  432. priority: '2 normal',
  433. group: 'Users',
  434. customer_id: @agent.id,
  435. article: {
  436. content_type: 'text/plain', # or text/html
  437. body: 'some body',
  438. sender: 'System',
  439. },
  440. }
  441. post '/api/v1/tickets', params.to_json, @headers.merge('Authorization' => credentials)
  442. assert_response(201)
  443. result = JSON.parse(@response.body)
  444. assert_equal(Hash, result.class)
  445. assert_equal(Ticket::State.lookup(name: 'new').id, result['state_id'])
  446. assert_equal('a new ticket #c2', result['title'])
  447. assert_equal(@customer_without_org.id, result['customer_id'])
  448. assert_equal(@customer_without_org.id, result['updated_by_id'])
  449. assert_equal(@customer_without_org.id, result['created_by_id'])
  450. end
  451. test '03.03 ticket with wrong ticket id' do
  452. ticket = Ticket.create!(
  453. title: 'ticket with wrong ticket id',
  454. group: Group.lookup(name: 'Users'),
  455. customer_id: @agent.id,
  456. state: Ticket::State.lookup(name: 'new'),
  457. priority: Ticket::Priority.lookup(name: '2 normal'),
  458. updated_by_id: 1,
  459. created_by_id: 1,
  460. )
  461. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  462. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  463. assert_response(401)
  464. result = JSON.parse(@response.body)
  465. assert_equal(Hash, result.class)
  466. assert_equal('Not authorized', result['error'])
  467. params = {
  468. title: 'ticket with wrong ticket id - 2',
  469. }
  470. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  471. assert_response(401)
  472. result = JSON.parse(@response.body)
  473. assert_equal(Hash, result.class)
  474. assert_equal('Not authorized', result['error'])
  475. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  476. assert_response(401)
  477. result = JSON.parse(@response.body)
  478. assert_equal(Hash, result.class)
  479. assert_equal('Not authorized', result['error'])
  480. end
  481. test '03.04 ticket with correct ticket id' do
  482. ticket = Ticket.create!(
  483. title: 'ticket with corret ticket id',
  484. group: Group.lookup(name: 'Users'),
  485. customer_id: @customer_without_org.id,
  486. state: Ticket::State.lookup(name: 'new'),
  487. priority: Ticket::Priority.lookup(name: '2 normal'),
  488. updated_by_id: 1,
  489. created_by_id: 1,
  490. )
  491. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-customer1@example.com', 'customer1pw')
  492. get "/api/v1/tickets/#{ticket.id}", {}, @headers.merge('Authorization' => credentials)
  493. assert_response(200)
  494. result = JSON.parse(@response.body)
  495. assert_equal(Hash, result.class)
  496. assert_equal(ticket.id, result['id'])
  497. assert_equal('ticket with corret ticket id', result['title'])
  498. assert_equal(ticket.customer_id, result['customer_id'])
  499. assert_equal(1, result['updated_by_id'])
  500. assert_equal(1, result['created_by_id'])
  501. params = {
  502. title: 'ticket with corret ticket id - 2',
  503. customer_id: @agent.id,
  504. }
  505. put "/api/v1/tickets/#{ticket.id}", params.to_json, @headers.merge('Authorization' => credentials)
  506. assert_response(200)
  507. result = JSON.parse(@response.body)
  508. assert_equal(Hash, result.class)
  509. assert_equal(ticket.id, result['id'])
  510. assert_equal('ticket with corret ticket id - 2', result['title'])
  511. assert_equal(ticket.customer_id, result['customer_id'])
  512. assert_equal(@customer_without_org.id, result['updated_by_id'])
  513. assert_equal(1, result['created_by_id'])
  514. params = {
  515. ticket_id: ticket.id,
  516. subject: 'some subject',
  517. body: 'some body',
  518. }
  519. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  520. assert_response(201)
  521. result = JSON.parse(@response.body)
  522. assert_equal(Hash, result.class)
  523. assert_equal(ticket.id, result['ticket_id'])
  524. assert_equal('Tickets Customer1', result['from'])
  525. assert_equal('some subject', result['subject'])
  526. assert_equal('some body', result['body'])
  527. assert_equal('text/plain', result['content_type'])
  528. assert_equal(@customer_without_org.id, result['created_by_id'])
  529. assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, result['sender_id'])
  530. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  531. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  532. assert_response(401)
  533. result = JSON.parse(@response.body)
  534. assert_equal(Hash, result.class)
  535. assert_equal('Not authorized (admin permission required)!', result['error'])
  536. params = {
  537. ticket_id: ticket.id,
  538. subject: 'some subject',
  539. body: 'some body',
  540. type: 'email',
  541. sender: 'Agent',
  542. }
  543. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  544. assert_response(201)
  545. result = JSON.parse(@response.body)
  546. assert_equal(Hash, result.class)
  547. assert_equal(ticket.id, result['ticket_id'])
  548. assert_equal('Tickets Customer1', result['from'])
  549. assert_equal('some subject', result['subject'])
  550. assert_equal('some body', result['body'])
  551. assert_equal('text/plain', result['content_type'])
  552. assert_equal(@customer_without_org.id, result['created_by_id'])
  553. assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, result['sender_id'])
  554. assert_equal(Ticket::Article::Type.lookup(name: 'note').id, result['type_id'])
  555. delete "/api/v1/ticket_articles/#{result['id']}", {}.to_json, @headers.merge('Authorization' => credentials)
  556. assert_response(401)
  557. result = JSON.parse(@response.body)
  558. assert_equal(Hash, result.class)
  559. assert_equal('Not authorized (admin permission required)!', result['error'])
  560. params = {
  561. from: 'something which should not be changed on server side',
  562. ticket_id: ticket.id,
  563. subject: 'some subject',
  564. body: 'some body',
  565. type: 'web',
  566. sender: 'Agent',
  567. internal: true,
  568. }
  569. post '/api/v1/ticket_articles', params.to_json, @headers.merge('Authorization' => credentials)
  570. assert_response(201)
  571. result = JSON.parse(@response.body)
  572. assert_equal(Hash, result.class)
  573. assert_equal(ticket.id, result['ticket_id'])
  574. assert_equal('Tickets Customer1', result['from'])
  575. assert_equal('some subject', result['subject'])
  576. assert_equal('some body', result['body'])
  577. assert_equal('text/plain', result['content_type'])
  578. assert_equal(false, result['internal'])
  579. assert_equal(@customer_without_org.id, result['created_by_id'])
  580. assert_equal(Ticket::Article::Sender.lookup(name: 'Customer').id, result['sender_id'])
  581. assert_equal(Ticket::Article::Type.lookup(name: 'web').id, result['type_id'])
  582. params = {
  583. subject: 'new subject',
  584. }
  585. put "/api/v1/ticket_articles/#{result['id']}", params.to_json, @headers.merge('Authorization' => credentials)
  586. assert_response(401)
  587. result = JSON.parse(@response.body)
  588. assert_equal(Hash, result.class)
  589. assert_equal('Not authorized (ticket.agent or admin permission required)!', result['error'])
  590. delete "/api/v1/tickets/#{ticket.id}", {}.to_json, @headers.merge('Authorization' => credentials)
  591. assert_response(401)
  592. result = JSON.parse(@response.body)
  593. assert_equal(Hash, result.class)
  594. assert_equal('Not authorized (admin permission required)!', result['error'])
  595. end
  596. end