ticket_spec.rb 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket', type: :request do
  4. let!(:ticket_group) do
  5. create(:group, email_address: create(:email_address))
  6. end
  7. let!(:ticket_group_without_create) do
  8. create(:group, email_address: create(:email_address))
  9. end
  10. let(:admin) do
  11. create(:admin, groups: Group.all, firstname: 'Tickets', lastname: 'Admin')
  12. end
  13. let!(:agent) do
  14. create(:agent, groups: Group.all, firstname: 'Tickets', lastname: 'Agent')
  15. end
  16. let!(:agent_change_only) do
  17. user = create(:agent, groups: Group.all, firstname: 'Tickets', lastname: 'Agent')
  18. user.group_names_access_map = {
  19. ticket_group_without_create.name => %w[read change],
  20. }
  21. user
  22. end
  23. let!(:customer) do
  24. create(
  25. :customer,
  26. login: 'tickets-customer1@example.com',
  27. firstname: 'Tickets',
  28. lastname: 'Customer1',
  29. email: 'tickets-customer1@example.com',
  30. )
  31. end
  32. describe 'request handling' do
  33. it 'does ticket create with agent - missing group (01.01)' do
  34. params = {
  35. title: 'a new ticket #1',
  36. article: {
  37. content_type: 'text/plain', # or text/html
  38. body: 'some body',
  39. sender: 'Customer',
  40. type: 'note',
  41. },
  42. }
  43. authenticated_as(agent)
  44. post '/api/v1/tickets', params: params, as: :json
  45. expect(response).to have_http_status(:unprocessable_entity)
  46. expect(json_response).to be_a(Hash)
  47. expect(json_response['error_human']).to eq("The required value 'group_id' is missing.")
  48. end
  49. it 'does ticket create with agent - wrong group (01.02)' do
  50. params = {
  51. title: 'a new ticket #2',
  52. group: 'not_existing',
  53. article: {
  54. content_type: 'text/plain', # or text/html
  55. body: 'some body',
  56. sender: 'Customer',
  57. type: 'note',
  58. },
  59. }
  60. authenticated_as(agent)
  61. post '/api/v1/tickets', params: params, as: :json
  62. expect(response).to have_http_status(:unprocessable_entity)
  63. expect(json_response).to be_a(Hash)
  64. expect(json_response['error']).to eq('No lookup value found for \'group\': "not_existing"')
  65. end
  66. it 'does ticket create with agent - valid group but no create permissions (01.02a)' do
  67. params = {
  68. title: 'a new ticket #1',
  69. group: ticket_group_without_create.name,
  70. priority: '2 normal',
  71. state: 'new',
  72. customer_id: customer.id,
  73. article: {
  74. content_type: 'text/plain', # or text/html
  75. body: 'some body',
  76. sender: 'Customer',
  77. type: 'note',
  78. },
  79. }
  80. authenticated_as(agent_change_only)
  81. post '/api/v1/tickets', params: params, as: :json
  82. expect(response).to have_http_status(:forbidden)
  83. expect(json_response).to be_a(Hash)
  84. expect(json_response['error']).to eq('Not authorized')
  85. end
  86. it 'does ticket create with agent - missing article.body (01.03)' do
  87. params = {
  88. title: 'a new ticket #3',
  89. group: ticket_group.name,
  90. priority: '2 normal',
  91. state: 'new',
  92. customer_id: customer.id,
  93. article: {},
  94. }
  95. authenticated_as(agent)
  96. expect { post '/api/v1/tickets', params: params, as: :json }.not_to change(Ticket, :count)
  97. expect(response).to have_http_status(:unprocessable_entity)
  98. expect(json_response).to be_a(Hash)
  99. expect(json_response['error']).to eq("Need at least an 'article body' field.")
  100. end
  101. it 'does ticket create with agent - article.body set to empty string (01.03)' do
  102. params = {
  103. title: 'a new ticket #3',
  104. group: ticket_group.name,
  105. priority: '2 normal',
  106. state: 'new',
  107. customer_id: customer.id,
  108. article: { body: " \n " },
  109. }
  110. authenticated_as(agent)
  111. expect { post '/api/v1/tickets', params: params, as: :json }.not_to change(Ticket, :count)
  112. expect(response).to have_http_status(:unprocessable_entity)
  113. expect(json_response).to be_a(Hash)
  114. expect(json_response['error']).to eq("Need at least an 'article body' field.")
  115. end
  116. it 'does ticket create with agent - missing article (01.03)' do
  117. params = {
  118. title: 'a new ticket #3',
  119. group: ticket_group.name,
  120. priority: '2 normal',
  121. state: 'new',
  122. customer_id: customer.id
  123. }
  124. authenticated_as(agent)
  125. expect { post '/api/v1/tickets', params: params, as: :json }.to change(Ticket, :count).by(1)
  126. expect(response).to have_http_status(:created)
  127. expect(json_response).to be_a(Hash)
  128. ticket = Ticket.find(json_response['id'])
  129. expect(ticket.articles).to be_empty
  130. end
  131. it 'does ticket create with agent - minimal article (01.03)' do
  132. params = {
  133. title: 'a new ticket #3',
  134. group: ticket_group.name,
  135. priority: '2 normal',
  136. state: 'new',
  137. customer_id: customer.id,
  138. article: {
  139. body: 'some test 123',
  140. },
  141. }
  142. authenticated_as(agent)
  143. post '/api/v1/tickets', params: params, as: :json
  144. expect(response).to have_http_status(:created)
  145. expect(json_response).to be_a(Hash)
  146. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  147. expect(json_response['title']).to eq('a new ticket #3')
  148. expect(json_response['customer_id']).to eq(customer.id)
  149. expect(json_response['updated_by_id']).to eq(agent.id)
  150. expect(json_response['created_by_id']).to eq(agent.id)
  151. end
  152. it 'does ticket create with agent - minimal article and customer.email (01.04)' do
  153. params = {
  154. title: 'a new ticket #3',
  155. group: ticket_group.name,
  156. priority: '2 normal',
  157. state: 'new',
  158. customer: customer.email,
  159. article: {
  160. body: 'some test 123',
  161. },
  162. }
  163. authenticated_as(agent)
  164. post '/api/v1/tickets', params: params, as: :json
  165. expect(response).to have_http_status(:created)
  166. expect(json_response).to be_a(Hash)
  167. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  168. expect(json_response['title']).to eq('a new ticket #3')
  169. expect(json_response['customer_id']).to eq(customer.id)
  170. expect(json_response['updated_by_id']).to eq(agent.id)
  171. expect(json_response['created_by_id']).to eq(agent.id)
  172. end
  173. it 'does ticket create with agent - wrong owner_id - 0 (01.05)' do
  174. params = {
  175. title: 'a new ticket #4',
  176. group: ticket_group.name,
  177. priority: '2 normal',
  178. owner_id: 0,
  179. state: 'new',
  180. customer_id: customer.id,
  181. article: {
  182. body: 'some test 123',
  183. },
  184. }
  185. authenticated_as(agent)
  186. post '/api/v1/tickets', params: params, as: :json
  187. expect(response).to have_http_status(:unprocessable_entity)
  188. expect(json_response).to be_a(Hash)
  189. expect(json_response['error']).to eq('Invalid value for param \'owner_id\': 0')
  190. end
  191. it 'does ticket create with agent - wrong owner_id - "" (01.06)' do
  192. params = {
  193. title: 'a new ticket #5',
  194. group: ticket_group.name,
  195. priority: '2 normal',
  196. owner_id: '',
  197. state: 'new',
  198. customer_id: customer.id,
  199. article: {
  200. body: 'some test 123',
  201. },
  202. }
  203. authenticated_as(agent)
  204. post '/api/v1/tickets', params: params, as: :json
  205. expect(response).to have_http_status(:created)
  206. expect(json_response).to be_a(Hash)
  207. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  208. expect(json_response['title']).to eq('a new ticket #5')
  209. expect(json_response['customer_id']).to eq(customer.id)
  210. expect(json_response['updated_by_id']).to eq(agent.id)
  211. expect(json_response['created_by_id']).to eq(agent.id)
  212. end
  213. it 'does ticket create with agent - wrong owner_id - 99999 (01.07)' do
  214. params = {
  215. title: 'a new ticket #6',
  216. group: ticket_group.name,
  217. priority: '2 normal',
  218. owner_id: 99_999,
  219. state: 'new',
  220. customer_id: customer.id,
  221. article: {
  222. body: 'some test 123',
  223. },
  224. }
  225. authenticated_as(agent)
  226. post '/api/v1/tickets', params: params, as: :json
  227. expect(response).to have_http_status(:unprocessable_entity)
  228. expect(json_response).to be_a(Hash)
  229. expect(json_response['error']).to eq('Invalid value for param \'owner_id\': 99999')
  230. end
  231. it 'does ticket create with agent - wrong owner_id - nil (01.08)' do
  232. params = {
  233. title: 'a new ticket #7',
  234. group: ticket_group.name,
  235. priority: '2 normal',
  236. owner_id: nil,
  237. state: 'new',
  238. customer_id: customer.id,
  239. article: {
  240. body: 'some test 123',
  241. },
  242. }
  243. authenticated_as(agent)
  244. post '/api/v1/tickets', params: params, as: :json
  245. expect(response).to have_http_status(:created)
  246. expect(json_response).to be_a(Hash)
  247. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  248. expect(json_response['title']).to eq('a new ticket #7')
  249. expect(json_response['customer_id']).to eq(customer.id)
  250. expect(json_response['updated_by_id']).to eq(agent.id)
  251. expect(json_response['created_by_id']).to eq(agent.id)
  252. end
  253. it 'does ticket create with agent - minimal article with guess customer (01.09)' do
  254. params = {
  255. title: 'a new ticket #9',
  256. group: ticket_group.name,
  257. priority: '2 normal',
  258. state: 'new',
  259. customer_id: 'guess:some_new_customer@example.com',
  260. article: {
  261. body: 'some test 123',
  262. },
  263. }
  264. authenticated_as(agent)
  265. post '/api/v1/tickets', params: params, as: :json
  266. expect(response).to have_http_status(:created)
  267. expect(json_response).to be_a(Hash)
  268. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  269. expect(json_response['title']).to eq('a new ticket #9')
  270. expect(json_response['customer_id']).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  271. expect(json_response['updated_by_id']).to eq(agent.id)
  272. expect(json_response['created_by_id']).to eq(agent.id)
  273. end
  274. it 'does ticket create with agent - minimal article with guess customer (01.10)' do
  275. params = {
  276. title: 'a new ticket #10',
  277. group: ticket_group.name,
  278. customer_id: 'guess:some_new_customer@example.com',
  279. article: {
  280. body: 'some test 123',
  281. },
  282. }
  283. authenticated_as(agent)
  284. post '/api/v1/tickets', params: params, as: :json
  285. expect(response).to have_http_status(:created)
  286. expect(json_response).to be_a(Hash)
  287. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  288. expect(json_response['title']).to eq('a new ticket #10')
  289. expect(json_response['customer_id']).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  290. expect(json_response['updated_by_id']).to eq(agent.id)
  291. expect(json_response['created_by_id']).to eq(agent.id)
  292. end
  293. it 'does ticket create with agent - minimal article with customer hash (01.11)' do
  294. params = {
  295. title: 'a new ticket #11',
  296. group: ticket_group.name,
  297. customer: {
  298. firstname: 'some firstname',
  299. lastname: 'some lastname',
  300. email: 'some_new_customer@example.com',
  301. },
  302. article: {
  303. body: 'some test 123',
  304. },
  305. }
  306. authenticated_as(agent)
  307. post '/api/v1/tickets', params: params, as: :json
  308. expect(response).to have_http_status(:created)
  309. expect(json_response).to be_a(Hash)
  310. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  311. expect(json_response['title']).to eq('a new ticket #11')
  312. expect(json_response['customer_id']).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  313. expect(json_response['updated_by_id']).to eq(agent.id)
  314. expect(json_response['created_by_id']).to eq(agent.id)
  315. end
  316. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (01.11)' do
  317. params = {
  318. title: 'a new ticket #11.1',
  319. group: ticket_group.name,
  320. customer: {
  321. firstname: 'some firstname',
  322. lastname: 'some lastname',
  323. email: 'some_new_customer@example.com',
  324. },
  325. article: {
  326. body: 'some test 123',
  327. origin_by: 'some_new_customer@example.com',
  328. },
  329. }
  330. authenticated_as(agent)
  331. post '/api/v1/tickets', params: params, as: :json
  332. expect(response).to have_http_status(:created)
  333. expect(json_response).to be_a(Hash)
  334. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  335. expect(json_response['title']).to eq('a new ticket #11.1')
  336. expect(json_response['customer_id']).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  337. expect(json_response['updated_by_id']).to eq(agent.id)
  338. expect(json_response['created_by_id']).to eq(agent.id)
  339. ticket = Ticket.find(json_response['id'])
  340. article = ticket.articles.first
  341. expect(article.updated_by_id).to eq(agent.id)
  342. expect(article.created_by_id).to eq(agent.id)
  343. expect(article.origin_by_id).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  344. expect(article.sender.name).to eq('Customer')
  345. expect(article.type.name).to eq('note')
  346. expect(article.from).to eq('some firstname some lastname')
  347. end
  348. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (01.11) - second test' do
  349. params = {
  350. title: 'a new ticket #11.2',
  351. group: ticket_group.name,
  352. customer: {
  353. firstname: 'some firstname',
  354. lastname: 'some lastname',
  355. email: 'some_new_customer@example.com',
  356. },
  357. article: {
  358. sender: 'Customer',
  359. body: 'some test 123',
  360. origin_by: 'some_new_customer@example.com',
  361. },
  362. }
  363. authenticated_as(agent)
  364. post '/api/v1/tickets', params: params, as: :json
  365. expect(response).to have_http_status(:created)
  366. expect(json_response).to be_a(Hash)
  367. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  368. expect(json_response['title']).to eq('a new ticket #11.2')
  369. expect(json_response['customer_id']).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  370. expect(json_response['updated_by_id']).to eq(agent.id)
  371. expect(json_response['created_by_id']).to eq(agent.id)
  372. ticket = Ticket.find(json_response['id'])
  373. article = ticket.articles.first
  374. expect(article.updated_by_id).to eq(agent.id)
  375. expect(article.created_by_id).to eq(agent.id)
  376. expect(article.origin_by_id).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  377. expect(article.sender.name).to eq('Customer')
  378. expect(article.type.name).to eq('note')
  379. expect(article.from).to eq('some firstname some lastname')
  380. end
  381. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (01.11) - third test' do
  382. params = {
  383. title: 'a new ticket #11.3',
  384. group: ticket_group.name,
  385. customer: {
  386. firstname: 'some firstname',
  387. lastname: 'some lastname',
  388. email: 'some_new_customer@example.com',
  389. },
  390. article: {
  391. sender: 'Agent',
  392. from: 'somebody',
  393. body: 'some test 123',
  394. origin_by: 'some_new_customer@example.com',
  395. },
  396. }
  397. authenticated_as(agent)
  398. post '/api/v1/tickets', params: params, as: :json
  399. expect(response).to have_http_status(:created)
  400. expect(json_response).to be_a(Hash)
  401. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  402. expect(json_response['title']).to eq('a new ticket #11.3')
  403. expect(json_response['customer_id']).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  404. expect(json_response['updated_by_id']).to eq(agent.id)
  405. expect(json_response['created_by_id']).to eq(agent.id)
  406. ticket = Ticket.find(json_response['id'])
  407. article = ticket.articles.first
  408. expect(article.updated_by_id).to eq(agent.id)
  409. expect(article.created_by_id).to eq(agent.id)
  410. expect(article.origin_by_id).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  411. expect(article.sender.name).to eq('Customer')
  412. expect(article.type.name).to eq('note')
  413. expect(article.from).to eq('some firstname some lastname')
  414. end
  415. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (01.11) - fourth test' do
  416. params = {
  417. title: 'a new ticket #11.4',
  418. group: ticket_group.name,
  419. customer: {
  420. firstname: 'some firstname',
  421. lastname: 'some lastname',
  422. email: 'some_new_customer@example.com',
  423. },
  424. article: {
  425. sender: 'Customer',
  426. body: 'some test 123',
  427. origin_by: customer.login,
  428. },
  429. }
  430. authenticated_as(agent)
  431. post '/api/v1/tickets', params: params, as: :json
  432. expect(response).to have_http_status(:created)
  433. expect(json_response).to be_a(Hash)
  434. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  435. expect(json_response['title']).to eq('a new ticket #11.4')
  436. expect(json_response['customer_id']).to eq(User.lookup(email: 'some_new_customer@example.com').id)
  437. expect(json_response['updated_by_id']).to eq(agent.id)
  438. expect(json_response['created_by_id']).to eq(agent.id)
  439. ticket = Ticket.find(json_response['id'])
  440. article = ticket.articles.first
  441. expect(article.updated_by_id).to eq(agent.id)
  442. expect(article.created_by_id).to eq(agent.id)
  443. expect(article.origin_by_id).to eq(customer.id)
  444. expect(article.sender.name).to eq('Customer')
  445. expect(article.type.name).to eq('note')
  446. expect(article.from).to eq('Tickets Customer1')
  447. end
  448. it 'does ticket create with agent - minimal article with missing body - with customer.id (01.12)' do
  449. params = {
  450. title: 'a new ticket #12',
  451. group: ticket_group.name,
  452. customer_id: customer.id,
  453. article: {
  454. subject: 'some test 123',
  455. },
  456. }
  457. authenticated_as(agent)
  458. post '/api/v1/tickets', params: params, as: :json
  459. expect(response).to have_http_status(:unprocessable_entity)
  460. expect(json_response).to be_a(Hash)
  461. expect(json_response['error']).to eq("Need at least an 'article body' field.")
  462. end
  463. it 'does ticket create with agent - minimal article and attachment with customer (01.13)' do
  464. params = {
  465. title: 'a new ticket #13',
  466. group: ticket_group.name,
  467. customer_id: customer.id,
  468. article: {
  469. subject: 'some test 123',
  470. body: 'some test 123',
  471. attachments: [
  472. { 'filename' => 'some_file.txt',
  473. 'data' => 'dGVzdCAxMjM=',
  474. 'mime-type' => 'text/plain' },
  475. ],
  476. },
  477. }
  478. authenticated_as(agent)
  479. post '/api/v1/tickets', params: params, as: :json
  480. expect(response).to have_http_status(:created)
  481. expect(json_response).to be_a(Hash)
  482. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  483. expect(json_response['title']).to eq('a new ticket #13')
  484. expect(json_response['customer_id']).to eq(customer.id)
  485. expect(json_response['updated_by_id']).to eq(agent.id)
  486. expect(json_response['created_by_id']).to eq(agent.id)
  487. ticket = Ticket.find(json_response['id'])
  488. expect(ticket.articles.count).to eq(1)
  489. expect(ticket.articles.first.attachments.count).to eq(1)
  490. file = ticket.articles.first.attachments.first
  491. expect(file.content).to eq('test 123')
  492. expect(file.filename).to eq('some_file.txt')
  493. expect(file.preferences['Mime-Type']).to eq('text/plain')
  494. expect(file.preferences['Content-ID']).to be_falsey
  495. end
  496. it 'does ticket create with agent - minimal article and attachment with customer (01.14)' do
  497. params = {
  498. title: 'a new ticket #14',
  499. group: ticket_group.name,
  500. customer_id: customer.id,
  501. article: {
  502. subject: 'some test 123',
  503. body: 'some test 123',
  504. attachments: [
  505. {
  506. 'filename' => 'some_file1.txt',
  507. 'data' => 'dGVzdCAxMjM=',
  508. 'mime-type' => 'text/plain',
  509. },
  510. {
  511. 'filename' => 'some_file2.txt',
  512. 'data' => 'w6TDtsO8w58=',
  513. 'mime-type' => 'text/plain',
  514. },
  515. ],
  516. },
  517. }
  518. authenticated_as(agent)
  519. post '/api/v1/tickets', params: params, as: :json
  520. expect(response).to have_http_status(:created)
  521. expect(json_response).to be_a(Hash)
  522. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  523. expect(json_response['title']).to eq('a new ticket #14')
  524. expect(json_response['customer_id']).to eq(customer.id)
  525. expect(json_response['updated_by_id']).to eq(agent.id)
  526. expect(json_response['created_by_id']).to eq(agent.id)
  527. ticket = Ticket.find(json_response['id'])
  528. expect(ticket.articles.count).to eq(1)
  529. expect(ticket.articles.first.attachments.count).to eq(2)
  530. file = ticket.articles.first.attachments.first
  531. expect(file.content).to eq('test 123')
  532. expect(file.filename).to eq('some_file1.txt')
  533. expect(file.preferences['Mime-Type']).to eq('text/plain')
  534. expect(file.preferences['Content-ID']).to be_falsey
  535. end
  536. it 'does ticket create with agent - minimal article and simple invalid base64 attachment with customer (01.15)' do
  537. params = {
  538. title: 'a new ticket #15',
  539. group: ticket_group.name,
  540. customer_id: customer.id,
  541. article: {
  542. subject: 'some test 123',
  543. body: 'some test 123',
  544. attachments: [
  545. { 'filename' => 'some_file.txt',
  546. 'data' => 'ABC_INVALID_BASE64',
  547. 'mime-type' => 'text/plain' },
  548. ],
  549. },
  550. }
  551. authenticated_as(agent)
  552. post '/api/v1/tickets', params: params, as: :json
  553. expect(response).to have_http_status(:unprocessable_entity)
  554. expect(json_response).to be_a(Hash)
  555. expect(json_response['error']).to eq('Invalid base64 for attachment with index \'0\'')
  556. end
  557. it 'does ticket create with agent - minimal article and large invalid base64 attachment with customer (01.15a)' do
  558. params = {
  559. title: 'a new ticket #15a',
  560. group: ticket_group.name,
  561. customer_id: customer.id,
  562. article: {
  563. subject: 'some test 123',
  564. body: 'some test 123',
  565. attachments: [
  566. { 'filename' => 'some_file.txt',
  567. 'data' => "LARGE_INVALID_BASE64_#{'#' * 20_000_000}",
  568. 'mime-type' => 'text/plain' },
  569. ],
  570. },
  571. }
  572. authenticated_as(agent)
  573. post '/api/v1/tickets', params: params, as: :json
  574. expect(response).to have_http_status(:unprocessable_entity)
  575. expect(json_response).to be_a(Hash)
  576. expect(json_response['error']).to eq('Invalid base64 for attachment with index \'0\'')
  577. end
  578. it 'does ticket create with agent - minimal article and valid multiline base64 with linebreaks attachment with customer (01.15b)' do
  579. params = {
  580. title: 'a new ticket #15b',
  581. group: ticket_group.name,
  582. customer_id: customer.id,
  583. article: {
  584. subject: 'some test 123',
  585. body: 'some test 123',
  586. attachments: [
  587. { 'filename' => 'some_file.txt',
  588. 'data' => Base64.encode64('a' * 1_000),
  589. 'mime-type' => 'text/plain' },
  590. ],
  591. },
  592. }
  593. authenticated_as(agent)
  594. post '/api/v1/tickets', params: params, as: :json
  595. expect(response).to have_http_status(:created)
  596. expect(json_response['title']).to eq('a new ticket #15b')
  597. ticket = Ticket.find(json_response['id'])
  598. expect(ticket.articles.count).to eq(1)
  599. expect(ticket.articles.first.attachments.count).to eq(1)
  600. file = ticket.articles.first.attachments.first
  601. expect(file.content).to eq('a' * 1_000)
  602. end
  603. it 'does ticket create with agent - minimal article and valid multiline base64 without linebreaks attachment with customer (01.15c)' do
  604. params = {
  605. title: 'a new ticket #15c',
  606. group: ticket_group.name,
  607. customer_id: customer.id,
  608. article: {
  609. subject: 'some test 123',
  610. body: 'some test 123',
  611. attachments: [
  612. { 'filename' => 'some_file.txt',
  613. 'data' => Base64.strict_encode64('a' * 1_000),
  614. 'mime-type' => 'text/plain' },
  615. ],
  616. },
  617. }
  618. authenticated_as(agent)
  619. post '/api/v1/tickets', params: params, as: :json
  620. expect(response).to have_http_status(:created)
  621. expect(json_response['title']).to eq('a new ticket #15c')
  622. ticket = Ticket.find(json_response['id'])
  623. expect(ticket.articles.count).to eq(1)
  624. expect(ticket.articles.first.attachments.count).to eq(1)
  625. file = ticket.articles.first.attachments.first
  626. expect(file.content).to eq('a' * 1_000)
  627. end
  628. it 'does ticket create with agent - minimal article and attachment invalid base64 with customer (01.16)' do
  629. params = {
  630. title: 'a new ticket #16',
  631. group: ticket_group.name,
  632. customer_id: customer.id,
  633. article: {
  634. subject: 'some test 123',
  635. body: 'some test 123',
  636. attachments: [
  637. { 'filename' => 'some_file.txt',
  638. 'data' => 'dGVzdCAxMjM=' },
  639. ],
  640. },
  641. }
  642. authenticated_as(agent)
  643. post '/api/v1/tickets', params: params, as: :json
  644. expect(response).to have_http_status(:unprocessable_entity)
  645. expect(json_response).to be_a(Hash)
  646. expect(json_response['error']).to eq('Attachment needs \'mime-type\' param for attachment with index \'0\'')
  647. end
  648. it 'does ticket create with agent - minimal article and inline attachments with customer (01.17)' do
  649. params = {
  650. title: 'a new ticket #17',
  651. group: ticket_group.name,
  652. customer_id: customer.id,
  653. article: {
  654. content_type: 'text/html',
  655. subject: 'some test 123',
  656. body: 'some test 123 <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
  657. AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
  658. 9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" /> <img src="data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAJAAD/4QMtaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjMtYzAxMSA2Ni4xNDU2NjEsIDIwMTIvMDIvMDYtMTQ6NTY6MjcgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBDUzYgKE1hY2ludG9zaCkiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6QzJCOTE2NzlGQUEwMTFFNjg0M0NGQjU0OUU4MTFEOEIiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6QzJCOTE2N0FGQUEwMTFFNjg0M0NGQjU0OUU4MTFEOEIiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDpDMkI5MTY3N0ZBQTAxMUU2ODQzQ0ZCNTQ5RTgxMUQ4QiIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDpDMkI5MTY3OEZBQTAxMUU2ODQzQ0ZCNTQ5RTgxMUQ4QiIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/Pv/uAA5BZG9iZQBkwAAAAAH/2wCEABQRERoTGioZGSo1KCEoNTEpKCgpMUE4ODg4OEFEREREREREREREREREREREREREREREREREREREREREREREREQBFhoaIh0iKRoaKTkpIik5RDktLTlEREREOERERERERERERERERERERERERERERERERERERERERERERERERERERP/AABEIABAADAMBIgACEQEDEQH/xABbAAEBAAAAAAAAAAAAAAAAAAAEBQEBAQAAAAAAAAAAAAAAAAAABAUQAAEEAgMAAAAAAAAAAAAAAAABAhIDESIxBAURAAICAwAAAAAAAAAAAAAAAAESABNRoQP/2gAMAwEAAhEDEQA/AJDq1rfF3Imeg/1+lFy2oR564DKWWWbweV+Buf/Z">',
  659. },
  660. }
  661. authenticated_as(agent)
  662. post '/api/v1/tickets', params: params, as: :json
  663. expect(response).to have_http_status(:created)
  664. expect(json_response).to be_a(Hash)
  665. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  666. expect(json_response['title']).to eq('a new ticket #17')
  667. expect(json_response['customer_id']).to eq(customer.id)
  668. expect(json_response['updated_by_id']).to eq(agent.id)
  669. expect(json_response['created_by_id']).to eq(agent.id)
  670. ticket = Ticket.find(json_response['id'])
  671. expect(ticket.articles.count).to eq(1)
  672. expect(ticket.articles.first.attachments.count).to eq(2)
  673. file = ticket.articles.first.attachments[0]
  674. expect(Digest::MD5.hexdigest(file.content)).to eq('d3c1e09bdefb92b6a06b791a24ca9599')
  675. expect(file.filename).to eq('image1.png')
  676. expect(file.preferences['Mime-Type']).to eq('image/png')
  677. expect(file.preferences['Content-ID']).to match(%r{#{ticket.id}\..+?@zammad.example.com})
  678. expect(file.preferences['Content-ID']).to be_truthy
  679. file = ticket.articles.first.attachments[1]
  680. expect(Digest::MD5.hexdigest(file.content)).to eq('006a2ca3793b550c8fe444acdeb39252')
  681. expect(file.filename).to eq('image2.jpeg')
  682. expect(file.preferences['Mime-Type']).to eq('image/jpeg')
  683. expect(file.preferences['Content-ID']).to match(%r{#{ticket.id}\..+?@zammad.example.com})
  684. expect(file.preferences['Content-ID']).to be_truthy
  685. end
  686. it 'does ticket create with agent - minimal article and inline attachments with customer (01.18)' do
  687. params = {
  688. title: 'a new ticket #18',
  689. group: ticket_group.name,
  690. customer_id: customer.id,
  691. article: {
  692. content_type: 'text/html',
  693. subject: 'some test 123',
  694. body: 'some test 123 <img src="data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAJAAD/4QMtaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjMtYzAxMSA2Ni4xNDU2NjEsIDIwMTIvMDIvMDYtMTQ6NTY6MjcgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBDUzYgKE1hY2ludG9zaCkiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6QzJCOTE2NzlGQUEwMTFFNjg0M0NGQjU0OUU4MTFEOEIiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6QzJCOTE2N0FGQUEwMTFFNjg0M0NGQjU0OUU4MTFEOEIiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDpDMkI5MTY3N0ZBQTAxMUU2ODQzQ0ZCNTQ5RTgxMUQ4QiIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDpDMkI5MTY3OEZBQTAxMUU2ODQzQ0ZCNTQ5RTgxMUQ4QiIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/Pv/uAA5BZG9iZQBkwAAAAAH/2wCEABQRERoTGioZGSo1KCEoNTEpKCgpMUE4ODg4OEFEREREREREREREREREREREREREREREREREREREREREREREREQBFhoaIh0iKRoaKTkpIik5RDktLTlEREREOERERERERERERERERERERERERERERERERERERERERERERERERERERP/AABEIABAADAMBIgACEQEDEQH/xABbAAEBAAAAAAAAAAAAAAAAAAAEBQEBAQAAAAAAAAAAAAAAAAAABAUQAAEEAgMAAAAAAAAAAAAAAAABAhIDESIxBAURAAICAwAAAAAAAAAAAAAAAAESABNRoQP/2gAMAwEAAhEDEQA/AJDq1rfF3Imeg/1+lFy2oR564DKWWWbweV+Buf/Z"
  695. >',
  696. attachments: [
  697. { 'filename' => 'some_file.txt',
  698. 'data' => 'dGVzdCAxMjM=',
  699. 'mime-type' => 'text/plain' },
  700. ],
  701. },
  702. }
  703. authenticated_as(agent)
  704. post '/api/v1/tickets', params: params, as: :json
  705. expect(response).to have_http_status(:created)
  706. expect(json_response).to be_a(Hash)
  707. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  708. expect(json_response['title']).to eq('a new ticket #18')
  709. expect(json_response['customer_id']).to eq(customer.id)
  710. expect(json_response['updated_by_id']).to eq(agent.id)
  711. expect(json_response['created_by_id']).to eq(agent.id)
  712. ticket = Ticket.find(json_response['id'])
  713. expect(ticket.articles.count).to eq(1)
  714. expect(ticket.articles.first.attachments.count).to eq(2)
  715. file = ticket.articles.first.attachments[0]
  716. expect(Digest::MD5.hexdigest(file.content)).to eq('006a2ca3793b550c8fe444acdeb39252')
  717. expect(file.filename).to eq('image1.jpeg')
  718. expect(file.preferences['Mime-Type']).to eq('image/jpeg')
  719. expect(file.preferences['Content-ID']).to be_truthy
  720. expect(file.preferences['Content-ID']).to match(%r{#{ticket.id}\..+?@zammad.example.com})
  721. file = ticket.articles.first.attachments[1]
  722. expect(Digest::MD5.hexdigest(file.content)).to eq('39d0d586a701e199389d954f2d592720')
  723. expect(file.filename).to eq('some_file.txt')
  724. expect(file.preferences['Mime-Type']).to eq('text/plain')
  725. expect(file.preferences['Content-ID']).to be_falsey
  726. end
  727. it 'does ticket create with agent (02.02)' do
  728. params = {
  729. title: 'a new ticket #1',
  730. state: 'new',
  731. priority: '2 normal',
  732. group: ticket_group.name,
  733. customer: 'tickets-customer1@example.com',
  734. article: {
  735. content_type: 'text/plain', # or text/html
  736. body: 'some body',
  737. },
  738. links: {
  739. Ticket: {
  740. parent: [1],
  741. }
  742. }
  743. }
  744. authenticated_as(agent)
  745. post '/api/v1/tickets', params: params, as: :json
  746. expect(response).to have_http_status(:created)
  747. expect(json_response).to be_a(Hash)
  748. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  749. expect(json_response['title']).to eq('a new ticket #1')
  750. expect(json_response['updated_by_id']).to eq(agent.id)
  751. expect(json_response['created_by_id']).to eq(agent.id)
  752. links = Link.list(
  753. link_object: 'Ticket',
  754. link_object_value: json_response['id'],
  755. )
  756. expect(links[0]['link_type']).to eq('child')
  757. expect(links[0]['link_object']).to eq('Ticket')
  758. expect(links[0]['link_object_value']).to eq(1)
  759. end
  760. it 'does ticket with wrong ticket id (02.03)' do
  761. group = create(:group)
  762. ticket = create(
  763. :ticket,
  764. title: 'ticket with wrong ticket id',
  765. group_id: group.id,
  766. customer_id: customer.id,
  767. )
  768. authenticated_as(agent)
  769. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  770. expect(response).to have_http_status(:forbidden)
  771. expect(json_response).to be_a(Hash)
  772. expect(json_response['error']).to eq('Not authorized')
  773. params = {
  774. title: 'ticket with wrong ticket id - 2',
  775. }
  776. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  777. expect(response).to have_http_status(:forbidden)
  778. expect(json_response).to be_a(Hash)
  779. expect(json_response['error']).to eq('Not authorized')
  780. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  781. expect(response).to have_http_status(:forbidden)
  782. expect(json_response).to be_a(Hash)
  783. expect(json_response['error']).to eq('Not authorized')
  784. end
  785. it 'does ticket with correct ticket id (02.04)', performs_jobs: true do
  786. title = "ticket with corret ticket id testagent#{SecureRandom.uuid}"
  787. ticket = create(
  788. :ticket,
  789. title: title,
  790. group: ticket_group,
  791. customer_id: customer.id,
  792. preferences: {
  793. some_key1: 123,
  794. },
  795. )
  796. authenticated_as(agent)
  797. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  798. expect(response).to have_http_status(:ok)
  799. expect(json_response).to be_a(Hash)
  800. expect(json_response['id']).to eq(ticket.id)
  801. expect(json_response['title']).to eq(title)
  802. expect(json_response['customer_id']).to eq(ticket.customer_id)
  803. expect(json_response['updated_by_id']).to eq(1)
  804. expect(json_response['created_by_id']).to eq(1)
  805. expect(json_response['preferences']['some_key1']).to eq(123)
  806. params = {
  807. title: "#{title} - 2",
  808. customer_id: agent.id,
  809. preferences: {
  810. some_key2: 'abc',
  811. },
  812. }
  813. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  814. expect(response).to have_http_status(:ok)
  815. expect(json_response).to be_a(Hash)
  816. expect(json_response['id']).to eq(ticket.id)
  817. expect(json_response['title']).to eq("#{title} - 2")
  818. expect(json_response['customer_id']).to eq(agent.id)
  819. expect(json_response['updated_by_id']).to eq(agent.id)
  820. expect(json_response['created_by_id']).to eq(1)
  821. expect(json_response['preferences']['some_key1']).to eq(123)
  822. expect(json_response['preferences']['some_key2']).to eq('abc')
  823. params = {
  824. ticket_id: ticket.id,
  825. subject: 'some subject',
  826. body: 'some body',
  827. }
  828. post '/api/v1/ticket_articles', params: params, as: :json
  829. expect(response).to have_http_status(:created)
  830. article_json_response = json_response
  831. expect(article_json_response).to be_a(Hash)
  832. expect(article_json_response['ticket_id']).to eq(ticket.id)
  833. expect(article_json_response['from']).to eq('Tickets Agent')
  834. expect(article_json_response['subject']).to eq('some subject')
  835. expect(article_json_response['body']).to eq('some body')
  836. expect(article_json_response['content_type']).to eq('text/plain')
  837. expect(article_json_response['internal']).to be(false)
  838. expect(article_json_response['created_by_id']).to eq(agent.id)
  839. expect(article_json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  840. expect(article_json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  841. perform_enqueued_jobs
  842. get "/api/v1/tickets/search?query=#{CGI.escape(title)}", params: {}, as: :json
  843. expect(response).to have_http_status(:ok)
  844. expect(json_response).to be_a(Hash)
  845. expect(json_response['tickets'][0]).to eq(ticket.id)
  846. expect(json_response['tickets_count']).to eq(1)
  847. params = {
  848. condition: {
  849. 'ticket.title' => {
  850. operator: 'contains',
  851. value: title,
  852. },
  853. },
  854. }
  855. post '/api/v1/tickets/search', params: params, as: :json
  856. expect(response).to have_http_status(:ok)
  857. expect(json_response).to be_a(Hash)
  858. expect(json_response['tickets'][0]).to eq(ticket.id)
  859. expect(json_response['tickets_count']).to eq(1)
  860. delete "/api/v1/ticket_articles/#{article_json_response['id']}", params: {}, as: :json
  861. expect(response).to have_http_status(:ok)
  862. params = {
  863. from: 'something which should not be changed on server side',
  864. ticket_id: ticket.id,
  865. subject: 'some subject',
  866. body: 'some body',
  867. type: 'email',
  868. internal: true,
  869. }
  870. post '/api/v1/ticket_articles', params: params, as: :json
  871. expect(response).to have_http_status(:created)
  872. expect(json_response).to be_a(Hash)
  873. expect(json_response['ticket_id']).to eq(ticket.id)
  874. expect(json_response['from']).to eq(%(Tickets Agent via #{ticket_group.email_address.name} <#{ticket_group.email_address.email}>))
  875. expect(json_response['subject']).to eq('some subject')
  876. expect(json_response['body']).to eq('some body')
  877. expect(json_response['content_type']).to eq('text/plain')
  878. expect(json_response['internal']).to be(true)
  879. expect(json_response['created_by_id']).to eq(agent.id)
  880. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  881. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)
  882. params = {
  883. subject: 'new subject',
  884. }
  885. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  886. expect(response).to have_http_status(:ok)
  887. expect(json_response).to be_a(Hash)
  888. expect(json_response['ticket_id']).to eq(ticket.id)
  889. expect(json_response['from']).to eq(%(Tickets Agent via #{ticket_group.email_address.name} <#{ticket_group.email_address.email}>))
  890. expect(json_response['subject']).not_to eq('new subject')
  891. expect(json_response['body']).to eq('some body')
  892. expect(json_response['content_type']).to eq('text/plain')
  893. expect(json_response['internal']).to be(true)
  894. expect(json_response['created_by_id']).to eq(agent.id)
  895. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  896. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)
  897. params = {
  898. from: 'something which should not be changed on server side',
  899. ticket_id: ticket.id,
  900. subject: 'some subject',
  901. body: 'some body',
  902. type: 'email',
  903. internal: false,
  904. }
  905. post '/api/v1/ticket_articles', params: params, as: :json
  906. expect(response).to have_http_status(:created)
  907. expect(json_response['internal']).to be(false)
  908. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  909. expect(response).to have_http_status(:forbidden)
  910. expect(json_response).to be_a(Hash)
  911. expect(json_response['error']).to eq('Not authorized (communication articles cannot be deleted)!')
  912. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  913. expect(response).to have_http_status(:forbidden)
  914. expect(json_response).to be_a(Hash)
  915. expect(json_response['error']).to eq('Not authorized (admin permission required)!')
  916. end
  917. it 'does ticket with correct ticket id (02.05)' do
  918. ticket = create(
  919. :ticket,
  920. title: 'ticket with corret ticket id',
  921. group: ticket_group,
  922. customer_id: customer.id,
  923. )
  924. authenticated_as(admin)
  925. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  926. expect(response).to have_http_status(:ok)
  927. expect(json_response).to be_a(Hash)
  928. expect(json_response['id']).to eq(ticket.id)
  929. expect(json_response['title']).to eq('ticket with corret ticket id')
  930. expect(json_response['customer_id']).to eq(ticket.customer_id)
  931. expect(json_response['updated_by_id']).to eq(1)
  932. expect(json_response['created_by_id']).to eq(1)
  933. params = {
  934. title: 'ticket with corret ticket id - 2',
  935. customer_id: agent.id,
  936. }
  937. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  938. expect(response).to have_http_status(:ok)
  939. expect(json_response).to be_a(Hash)
  940. expect(json_response['id']).to eq(ticket.id)
  941. expect(json_response['title']).to eq('ticket with corret ticket id - 2')
  942. expect(json_response['customer_id']).to eq(agent.id)
  943. expect(json_response['updated_by_id']).to eq(admin.id)
  944. expect(json_response['created_by_id']).to eq(1)
  945. params = {
  946. from: 'something which should not be changed on server side',
  947. ticket_id: ticket.id,
  948. subject: 'some subject',
  949. body: 'some body',
  950. }
  951. post '/api/v1/ticket_articles', params: params, as: :json
  952. expect(response).to have_http_status(:created)
  953. expect(json_response).to be_a(Hash)
  954. expect(json_response['ticket_id']).to eq(ticket.id)
  955. expect(json_response['from']).to eq('Tickets Admin')
  956. expect(json_response['subject']).to eq('some subject')
  957. expect(json_response['body']).to eq('some body')
  958. expect(json_response['content_type']).to eq('text/plain')
  959. expect(json_response['internal']).to be(false)
  960. expect(json_response['created_by_id']).to eq(admin.id)
  961. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  962. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  963. params = {
  964. subject: 'new subject',
  965. internal: true,
  966. }
  967. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  968. expect(response).to have_http_status(:ok)
  969. expect(json_response).to be_a(Hash)
  970. expect(json_response['ticket_id']).to eq(ticket.id)
  971. expect(json_response['from']).to eq('Tickets Admin')
  972. expect(json_response['subject']).not_to eq('new subject')
  973. expect(json_response['body']).to eq('some body')
  974. expect(json_response['content_type']).to eq('text/plain')
  975. expect(json_response['internal']).to be(true)
  976. expect(json_response['created_by_id']).to eq(admin.id)
  977. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  978. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  979. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  980. expect(response).to have_http_status(:ok)
  981. params = {
  982. ticket_id: ticket.id,
  983. subject: 'some subject',
  984. body: 'some body',
  985. type: 'email',
  986. }
  987. post '/api/v1/ticket_articles', params: params, as: :json
  988. expect(response).to have_http_status(:created)
  989. expect(json_response).to be_a(Hash)
  990. expect(json_response['ticket_id']).to eq(ticket.id)
  991. expect(json_response['from']).to eq(%(Tickets Admin via #{ticket_group.email_address.name} <#{ticket_group.email_address.email}>))
  992. expect(json_response['subject']).to eq('some subject')
  993. expect(json_response['body']).to eq('some body')
  994. expect(json_response['content_type']).to eq('text/plain')
  995. expect(json_response['internal']).to be(false)
  996. expect(json_response['created_by_id']).to eq(admin.id)
  997. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  998. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)
  999. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  1000. expect(response).to have_http_status(:forbidden)
  1001. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1002. expect(response).to have_http_status(:ok)
  1003. end
  1004. it 'does ticket pagination (02.05)' do
  1005. title = "ticket pagination #{SecureRandom.uuid}"
  1006. tickets = []
  1007. (1..20).each do |count|
  1008. ticket = create(
  1009. :ticket,
  1010. title: "#{title} - #{count}",
  1011. group: ticket_group,
  1012. customer_id: customer.id,
  1013. )
  1014. create(
  1015. :ticket_article,
  1016. type: Ticket::Article::Type.lookup(name: 'note'),
  1017. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1018. ticket_id: ticket.id,
  1019. )
  1020. tickets.push ticket
  1021. travel 2.seconds
  1022. end
  1023. authenticated_as(admin)
  1024. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: {}, as: :json
  1025. expect(response).to have_http_status(:ok)
  1026. expect(json_response).to be_a(Hash)
  1027. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  1028. expect(json_response['tickets'][19]).to eq(tickets[0].id)
  1029. expect(json_response['tickets_count']).to eq(20)
  1030. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=10", params: {}, as: :json
  1031. expect(response).to have_http_status(:ok)
  1032. expect(json_response).to be_a(Hash)
  1033. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  1034. expect(json_response['tickets'][9]).to eq(tickets[10].id)
  1035. expect(json_response['tickets_count']).to eq(10)
  1036. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=1&per_page=5", params: {}, as: :json
  1037. expect(response).to have_http_status(:ok)
  1038. expect(json_response).to be_a(Hash)
  1039. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  1040. expect(json_response['tickets'][4]).to eq(tickets[15].id)
  1041. expect(json_response['tickets_count']).to eq(5)
  1042. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=2&per_page=5", params: {}, as: :json
  1043. expect(response).to have_http_status(:ok)
  1044. expect(json_response).to be_a(Hash)
  1045. expect(json_response['tickets'][0]).to eq(tickets[14].id)
  1046. expect(json_response['tickets'][4]).to eq(tickets[10].id)
  1047. expect(json_response['tickets_count']).to eq(5)
  1048. get '/api/v1/tickets?limit=40&page=1&per_page=5', params: {}, as: :json
  1049. expect(response).to have_http_status(:ok)
  1050. expect(json_response).to be_a(Array)
  1051. tickets = Ticket.reorder(:id).limit(5)
  1052. expect(json_response[0]['id']).to eq(tickets[0].id)
  1053. expect(json_response[4]['id']).to eq(tickets[4].id)
  1054. expect(json_response.count).to eq(5)
  1055. get '/api/v1/tickets?limit=40&page=2&per_page=5', params: {}, as: :json
  1056. expect(response).to have_http_status(:ok)
  1057. expect(json_response).to be_a(Array)
  1058. tickets = Ticket.reorder(:id).limit(10)
  1059. expect(json_response[0]['id']).to eq(tickets[5].id)
  1060. expect(json_response[4]['id']).to eq(tickets[9].id)
  1061. expect(json_response.count).to eq(5)
  1062. end
  1063. it 'does ticket create with customer minimal (03.01)' do
  1064. params = {
  1065. title: 'a new ticket #c1',
  1066. state: 'new',
  1067. priority: '2 normal',
  1068. group: ticket_group.name,
  1069. article: {
  1070. body: 'some body',
  1071. },
  1072. }
  1073. authenticated_as(customer)
  1074. post '/api/v1/tickets', params: params, as: :json
  1075. expect(response).to have_http_status(:created)
  1076. expect(json_response).to be_a(Hash)
  1077. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1078. expect(json_response['title']).to eq('a new ticket #c1')
  1079. expect(json_response['customer_id']).to eq(customer.id)
  1080. expect(json_response['updated_by_id']).to eq(customer.id)
  1081. expect(json_response['created_by_id']).to eq(customer.id)
  1082. end
  1083. it 'does ticket create with customer with wrong customer (03.02)' do
  1084. params = {
  1085. title: 'a new ticket #c2',
  1086. state: 'new',
  1087. priority: '2 normal',
  1088. group: ticket_group.name,
  1089. customer_id: agent.id,
  1090. article: {
  1091. content_type: 'text/plain', # or text/html
  1092. body: 'some body',
  1093. sender: 'System',
  1094. },
  1095. }
  1096. authenticated_as(customer)
  1097. post '/api/v1/tickets', params: params, as: :json
  1098. expect(response).to have_http_status(:created)
  1099. expect(json_response).to be_a(Hash)
  1100. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1101. expect(json_response['title']).to eq('a new ticket #c2')
  1102. expect(json_response['customer_id']).to eq(customer.id)
  1103. expect(json_response['updated_by_id']).to eq(customer.id)
  1104. expect(json_response['created_by_id']).to eq(customer.id)
  1105. end
  1106. it 'does ticket create with customer with wrong customer hash (03.03)' do
  1107. params = {
  1108. title: 'a new ticket #c2',
  1109. state: 'new',
  1110. priority: '2 normal',
  1111. group: ticket_group.name,
  1112. customer: {
  1113. firstname: agent.firstname,
  1114. lastname: agent.lastname,
  1115. email: agent.email,
  1116. },
  1117. article: {
  1118. content_type: 'text/plain', # or text/html
  1119. body: 'some body',
  1120. sender: 'System',
  1121. },
  1122. }
  1123. authenticated_as(customer)
  1124. post '/api/v1/tickets', params: params, as: :json
  1125. expect(response).to have_http_status(:created)
  1126. expect(json_response).to be_a(Hash)
  1127. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1128. expect(json_response['title']).to eq('a new ticket #c2')
  1129. expect(json_response['customer_id']).to eq(customer.id)
  1130. expect(json_response['updated_by_id']).to eq(customer.id)
  1131. expect(json_response['created_by_id']).to eq(customer.id)
  1132. end
  1133. it 'does ticket with wrong ticket id (03.04)' do
  1134. ticket = create(
  1135. :ticket,
  1136. title: 'ticket with wrong ticket id',
  1137. group: ticket_group,
  1138. customer_id: agent.id,
  1139. )
  1140. authenticated_as(customer)
  1141. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1142. expect(response).to have_http_status(:forbidden)
  1143. expect(json_response).to be_a(Hash)
  1144. expect(json_response['error']).to eq('Not authorized')
  1145. params = {
  1146. title: 'ticket with wrong ticket id - 2',
  1147. }
  1148. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1149. expect(response).to have_http_status(:forbidden)
  1150. expect(json_response).to be_a(Hash)
  1151. expect(json_response['error']).to eq('Not authorized')
  1152. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1153. expect(response).to have_http_status(:forbidden)
  1154. expect(json_response).to be_a(Hash)
  1155. expect(json_response['error']).to eq('Not authorized')
  1156. end
  1157. it 'does ticket with correct ticket id (03.05)', performs_jobs: true do
  1158. title = "ticket with corret ticket id testme#{SecureRandom.uuid}"
  1159. ticket = create(
  1160. :ticket,
  1161. title: title,
  1162. group: ticket_group,
  1163. customer_id: customer.id,
  1164. )
  1165. authenticated_as(customer)
  1166. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1167. expect(response).to have_http_status(:ok)
  1168. expect(json_response).to be_a(Hash)
  1169. expect(json_response['id']).to eq(ticket.id)
  1170. expect(json_response['title']).to eq(title)
  1171. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1172. expect(json_response['updated_by_id']).to eq(1)
  1173. expect(json_response['created_by_id']).to eq(1)
  1174. params = {
  1175. title: "#{title} - 2",
  1176. customer_id: agent.id,
  1177. }
  1178. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1179. expect(response).to have_http_status(:ok)
  1180. expect(json_response).to be_a(Hash)
  1181. expect(json_response['id']).to eq(ticket.id)
  1182. expect(json_response['title']).to eq("#{title} - 2")
  1183. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1184. expect(json_response['updated_by_id']).to eq(customer.id)
  1185. expect(json_response['created_by_id']).to eq(1)
  1186. params = {
  1187. ticket_id: ticket.id,
  1188. subject: 'some subject',
  1189. body: 'some body',
  1190. }
  1191. post '/api/v1/ticket_articles', params: params, as: :json
  1192. expect(response).to have_http_status(:created)
  1193. article_json_response = json_response
  1194. expect(article_json_response).to be_a(Hash)
  1195. expect(article_json_response['ticket_id']).to eq(ticket.id)
  1196. expect(article_json_response['from']).to eq('Tickets Customer1')
  1197. expect(article_json_response['subject']).to eq('some subject')
  1198. expect(article_json_response['body']).to eq('some body')
  1199. expect(article_json_response['content_type']).to eq('text/plain')
  1200. expect(article_json_response['created_by_id']).to eq(customer.id)
  1201. expect(article_json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1202. expect(article_json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  1203. perform_enqueued_jobs
  1204. get "/api/v1/tickets/search?query=#{CGI.escape(title)}", params: {}, as: :json
  1205. expect(response).to have_http_status(:ok)
  1206. expect(json_response).to be_a(Hash)
  1207. expect(json_response['tickets'][0]).to eq(ticket.id)
  1208. expect(json_response['tickets_count']).to eq(1)
  1209. params = {
  1210. condition: {
  1211. 'ticket.title' => {
  1212. operator: 'contains',
  1213. value: title,
  1214. },
  1215. },
  1216. }
  1217. post '/api/v1/tickets/search', params: params, as: :json
  1218. expect(response).to have_http_status(:ok)
  1219. expect(json_response).to be_a(Hash)
  1220. expect(json_response['tickets'][0]).to eq(ticket.id)
  1221. expect(json_response['tickets_count']).to eq(1)
  1222. delete "/api/v1/ticket_articles/#{article_json_response['id']}", params: {}, as: :json
  1223. expect(response).to have_http_status(:forbidden)
  1224. expect(json_response).to be_a(Hash)
  1225. expect(json_response['error']).to eq('Not authorized (agent permission required)!')
  1226. params = {
  1227. ticket_id: ticket.id,
  1228. subject: 'some subject',
  1229. body: 'some body',
  1230. type: 'email',
  1231. sender: 'Agent',
  1232. }
  1233. post '/api/v1/ticket_articles', params: params, as: :json
  1234. expect(response).to have_http_status(:created)
  1235. expect(json_response).to be_a(Hash)
  1236. expect(json_response['ticket_id']).to eq(ticket.id)
  1237. expect(json_response['from']).to eq('Tickets Customer1')
  1238. expect(json_response['subject']).to eq('some subject')
  1239. expect(json_response['body']).to eq('some body')
  1240. expect(json_response['content_type']).to eq('text/plain')
  1241. expect(json_response['created_by_id']).to eq(customer.id)
  1242. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1243. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  1244. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  1245. expect(response).to have_http_status(:forbidden)
  1246. expect(json_response).to be_a(Hash)
  1247. expect(json_response['error']).to eq('Not authorized (agent permission required)!')
  1248. params = {
  1249. from: 'something which should not be changed on server side',
  1250. ticket_id: ticket.id,
  1251. subject: 'some subject',
  1252. body: 'some body',
  1253. type: 'web',
  1254. sender: 'Agent',
  1255. internal: true,
  1256. }
  1257. post '/api/v1/ticket_articles', params: params, as: :json
  1258. expect(response).to have_http_status(:created)
  1259. expect(json_response).to be_a(Hash)
  1260. expect(json_response['ticket_id']).to eq(ticket.id)
  1261. expect(json_response['from']).to eq('Tickets Customer1 <tickets-customer1@example.com>')
  1262. expect(json_response['subject']).to eq('some subject')
  1263. expect(json_response['body']).to eq('some body')
  1264. expect(json_response['content_type']).to eq('text/plain')
  1265. expect(json_response['internal']).to be(false)
  1266. expect(json_response['created_by_id']).to eq(customer.id)
  1267. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1268. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'web').id)
  1269. params = {
  1270. subject: 'new subject',
  1271. }
  1272. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  1273. expect(response).to have_http_status(:forbidden)
  1274. expect(json_response).to be_a(Hash)
  1275. expect(json_response['error']).to eq('Not authorized')
  1276. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1277. expect(response).to have_http_status(:forbidden)
  1278. expect(json_response).to be_a(Hash)
  1279. expect(json_response['error']).to eq('Not authorized (admin permission required)!')
  1280. end
  1281. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (03.6)' do
  1282. authenticated_as(customer)
  1283. params = {
  1284. title: 'a new ticket #3.6',
  1285. group: ticket_group.name,
  1286. customer: {
  1287. firstname: 'some firstname',
  1288. lastname: 'some lastname',
  1289. email: 'some_new_customer@example.com',
  1290. },
  1291. article: {
  1292. body: 'some test 123',
  1293. origin_by: agent.login,
  1294. },
  1295. }
  1296. post '/api/v1/tickets', params: params, as: :json
  1297. expect(response).to have_http_status(:created)
  1298. expect(json_response).to be_a(Hash)
  1299. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1300. expect(json_response['title']).to eq('a new ticket #3.6')
  1301. expect(json_response['customer_id']).to eq(customer.id)
  1302. expect(json_response['updated_by_id']).to eq(customer.id)
  1303. expect(json_response['created_by_id']).to eq(customer.id)
  1304. ticket = Ticket.find(json_response['id'])
  1305. article = ticket.articles.first
  1306. expect(article.updated_by_id).to eq(customer.id)
  1307. expect(article.created_by_id).to eq(customer.id)
  1308. expect(article.origin_by_id).to eq(customer.id)
  1309. expect(article.sender.name).to eq('Customer')
  1310. expect(article.type.name).to eq('note')
  1311. expect(article.from).to eq('Tickets Customer1')
  1312. end
  1313. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (03.6) - second test' do
  1314. authenticated_as(customer)
  1315. params = {
  1316. title: 'a new ticket #3.6.1',
  1317. group: ticket_group.name,
  1318. customer: {
  1319. firstname: 'some firstname',
  1320. lastname: 'some lastname',
  1321. email: 'some_new_customer@example.com',
  1322. },
  1323. article: {
  1324. sender: 'Agent',
  1325. body: 'some test 123',
  1326. origin_by_id: agent.id,
  1327. },
  1328. }
  1329. post '/api/v1/tickets', params: params, as: :json
  1330. expect(response).to have_http_status(:created)
  1331. expect(json_response).to be_a(Hash)
  1332. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1333. expect(json_response['title']).to eq('a new ticket #3.6.1')
  1334. expect(json_response['customer_id']).to eq(customer.id)
  1335. expect(json_response['updated_by_id']).to eq(customer.id)
  1336. expect(json_response['created_by_id']).to eq(customer.id)
  1337. ticket = Ticket.find(json_response['id'])
  1338. article = ticket.articles.first
  1339. expect(article.updated_by_id).to eq(customer.id)
  1340. expect(article.created_by_id).to eq(customer.id)
  1341. expect(article.origin_by_id).to eq(customer.id)
  1342. expect(article.sender.name).to eq('Customer')
  1343. expect(article.type.name).to eq('note')
  1344. expect(article.from).to eq('Tickets Customer1')
  1345. end
  1346. it 'does ticket show and response format (04.01)' do
  1347. title = "ticket testagent#{SecureRandom.uuid}"
  1348. ticket = create(
  1349. :ticket,
  1350. title: title,
  1351. group: ticket_group,
  1352. customer_id: customer.id,
  1353. updated_by_id: agent.id,
  1354. created_by_id: agent.id,
  1355. )
  1356. authenticated_as(agent)
  1357. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1358. expect(response).to have_http_status(:ok)
  1359. expect(json_response).to be_a(Hash)
  1360. expect(json_response['id']).to eq(ticket.id)
  1361. expect(json_response['title']).to eq(ticket.title)
  1362. expect(json_response['group']).to be_falsey
  1363. expect(json_response['priority']).to be_falsey
  1364. expect(json_response['owner']).to be_falsey
  1365. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1366. expect(json_response['updated_by_id']).to eq(agent.id)
  1367. expect(json_response['created_by_id']).to eq(agent.id)
  1368. get "/api/v1/tickets/#{ticket.id}?expand=true", params: {}, as: :json
  1369. expect(response).to have_http_status(:ok)
  1370. expect(json_response).to be_a(Hash)
  1371. expect(json_response['id']).to eq(ticket.id)
  1372. expect(json_response['title']).to eq(ticket.title)
  1373. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1374. expect(json_response['group']).to eq(ticket.group.name)
  1375. expect(json_response['priority']).to eq(ticket.priority.name)
  1376. expect(json_response['owner']).to eq(ticket.owner.login)
  1377. expect(json_response['updated_by_id']).to eq(agent.id)
  1378. expect(json_response['created_by_id']).to eq(agent.id)
  1379. get "/api/v1/tickets/#{ticket.id}?expand=false", params: {}, as: :json
  1380. expect(response).to have_http_status(:ok)
  1381. expect(json_response).to be_a(Hash)
  1382. expect(json_response['id']).to eq(ticket.id)
  1383. expect(json_response['title']).to eq(ticket.title)
  1384. expect(json_response['group']).to be_falsey
  1385. expect(json_response['priority']).to be_falsey
  1386. expect(json_response['owner']).to be_falsey
  1387. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1388. expect(json_response['updated_by_id']).to eq(agent.id)
  1389. expect(json_response['created_by_id']).to eq(agent.id)
  1390. get "/api/v1/tickets/#{ticket.id}?full=true", params: {}, as: :json
  1391. expect(response).to have_http_status(:ok)
  1392. expect(json_response).to be_a(Hash)
  1393. expect(json_response['id']).to eq(ticket.id)
  1394. expect(json_response['assets']).to be_truthy
  1395. expect(json_response['assets']['Ticket']).to be_truthy
  1396. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1397. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1398. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(ticket.title)
  1399. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1400. expect(json_response['assets']['User']).to be_truthy
  1401. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1402. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1403. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1404. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1405. expect(json_response['assets']['User']).to be_truthy
  1406. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1407. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1408. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1409. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1410. get "/api/v1/tickets/#{ticket.id}?full=false", params: {}, as: :json
  1411. expect(response).to have_http_status(:ok)
  1412. expect(json_response).to be_a(Hash)
  1413. expect(json_response['id']).to eq(ticket.id)
  1414. expect(json_response['title']).to eq(ticket.title)
  1415. expect(json_response['group']).to be_falsey
  1416. expect(json_response['priority']).to be_falsey
  1417. expect(json_response['owner']).to be_falsey
  1418. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1419. expect(json_response['updated_by_id']).to eq(agent.id)
  1420. expect(json_response['created_by_id']).to eq(agent.id)
  1421. end
  1422. it 'does ticket index and response format (04.02)' do
  1423. title = "ticket testagent#{SecureRandom.uuid}"
  1424. ticket = create(
  1425. :ticket,
  1426. title: title,
  1427. group: ticket_group,
  1428. customer_id: customer.id,
  1429. updated_by_id: agent.id,
  1430. created_by_id: agent.id,
  1431. )
  1432. authenticated_as(agent)
  1433. get '/api/v1/tickets', params: {}, as: :json
  1434. expect(response).to have_http_status(:ok)
  1435. expect(json_response).to be_a(Array)
  1436. expect(json_response[0]).to be_a(Hash)
  1437. expect(json_response[0]['id']).to eq(1)
  1438. expect(json_response[1]['id']).to eq(ticket.id)
  1439. expect(json_response[1]['title']).to eq(ticket.title)
  1440. expect(json_response[1]['group']).to be_falsey
  1441. expect(json_response[1]['priority']).to be_falsey
  1442. expect(json_response[1]['owner']).to be_falsey
  1443. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1444. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1445. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1446. get '/api/v1/tickets?expand=true', params: {}, as: :json
  1447. expect(response).to have_http_status(:ok)
  1448. expect(json_response).to be_a(Array)
  1449. expect(json_response[0]).to be_a(Hash)
  1450. expect(json_response[0]['id']).to eq(1)
  1451. expect(json_response[1]['id']).to eq(ticket.id)
  1452. expect(json_response[1]['title']).to eq(ticket.title)
  1453. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1454. expect(json_response[1]['group']).to eq(ticket.group.name)
  1455. expect(json_response[1]['priority']).to eq(ticket.priority.name)
  1456. expect(json_response[1]['owner']).to eq(ticket.owner.login)
  1457. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1458. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1459. get '/api/v1/tickets?expand=false', params: {}, as: :json
  1460. expect(response).to have_http_status(:ok)
  1461. expect(json_response).to be_a(Array)
  1462. expect(json_response[0]).to be_a(Hash)
  1463. expect(json_response[0]['id']).to eq(1)
  1464. expect(json_response[1]['id']).to eq(ticket.id)
  1465. expect(json_response[1]['title']).to eq(ticket.title)
  1466. expect(json_response[1]['group']).to be_falsey
  1467. expect(json_response[1]['priority']).to be_falsey
  1468. expect(json_response[1]['owner']).to be_falsey
  1469. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1470. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1471. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1472. get '/api/v1/tickets?full=true', params: {}, as: :json
  1473. expect(response).to have_http_status(:ok)
  1474. expect(json_response).to be_a(Hash)
  1475. expect(json_response['record_ids'].class).to eq(Array)
  1476. expect(json_response['record_ids'][0]).to eq(1)
  1477. expect(json_response['record_ids'][1]).to eq(ticket.id)
  1478. expect(json_response['assets']).to be_truthy
  1479. expect(json_response['assets']['Ticket']).to be_truthy
  1480. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1481. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1482. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(ticket.title)
  1483. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1484. expect(json_response['assets']['User']).to be_truthy
  1485. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1486. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1487. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1488. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1489. expect(json_response['assets']['User']).to be_truthy
  1490. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1491. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1492. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1493. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1494. get '/api/v1/tickets?full=false', params: {}, as: :json
  1495. expect(response).to have_http_status(:ok)
  1496. expect(json_response).to be_a(Array)
  1497. expect(json_response[0]).to be_a(Hash)
  1498. expect(json_response[0]['id']).to eq(1)
  1499. expect(json_response[1]['id']).to eq(ticket.id)
  1500. expect(json_response[1]['title']).to eq(ticket.title)
  1501. expect(json_response[1]['group']).to be_falsey
  1502. expect(json_response[1]['priority']).to be_falsey
  1503. expect(json_response[1]['owner']).to be_falsey
  1504. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1505. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1506. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1507. end
  1508. it 'does ticket create and response format (04.03)' do
  1509. title = "ticket testagent#{SecureRandom.uuid}"
  1510. params = {
  1511. title: title,
  1512. group: ticket_group.name,
  1513. customer_id: customer.id,
  1514. state: 'new',
  1515. priority: '2 normal',
  1516. article: {
  1517. body: 'some test 123',
  1518. },
  1519. }
  1520. authenticated_as(agent)
  1521. post '/api/v1/tickets', params: params, as: :json
  1522. expect(response).to have_http_status(:created)
  1523. expect(json_response).to be_a(Hash)
  1524. ticket = Ticket.find(json_response['id'])
  1525. expect(json_response['state_id']).to eq(ticket.state_id)
  1526. expect(json_response['state']).to be_falsey
  1527. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1528. expect(json_response['priority']).to be_falsey
  1529. expect(json_response['group_id']).to eq(ticket.group_id)
  1530. expect(json_response['group']).to be_falsey
  1531. expect(json_response['title']).to eq(title)
  1532. expect(json_response['customer_id']).to eq(customer.id)
  1533. expect(json_response['updated_by_id']).to eq(agent.id)
  1534. expect(json_response['created_by_id']).to eq(agent.id)
  1535. post '/api/v1/tickets?expand=true', params: params, as: :json
  1536. expect(response).to have_http_status(:created)
  1537. expect(json_response).to be_a(Hash)
  1538. ticket = Ticket.find(json_response['id'])
  1539. expect(json_response['state_id']).to eq(ticket.state_id)
  1540. expect(json_response['state']).to eq(ticket.state.name)
  1541. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1542. expect(json_response['priority']).to eq(ticket.priority.name)
  1543. expect(json_response['group_id']).to eq(ticket.group_id)
  1544. expect(json_response['group']).to eq(ticket.group.name)
  1545. expect(json_response['title']).to eq(title)
  1546. expect(json_response['customer_id']).to eq(customer.id)
  1547. expect(json_response['updated_by_id']).to eq(agent.id)
  1548. expect(json_response['created_by_id']).to eq(agent.id)
  1549. post '/api/v1/tickets?full=true', params: params, as: :json
  1550. expect(response).to have_http_status(:created)
  1551. expect(json_response).to be_a(Hash)
  1552. ticket = Ticket.find(json_response['id'])
  1553. expect(json_response['assets']).to be_truthy
  1554. expect(json_response['assets']['Ticket']).to be_truthy
  1555. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1556. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1557. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(title)
  1558. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1559. expect(json_response['assets']['User']).to be_truthy
  1560. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1561. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1562. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1563. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1564. expect(json_response['assets']['User']).to be_truthy
  1565. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1566. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1567. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1568. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1569. end
  1570. it 'does ticket update and response formats (04.04)' do
  1571. title = "ticket testagent#{SecureRandom.uuid}"
  1572. ticket = create(
  1573. :ticket,
  1574. title: title,
  1575. group: ticket_group,
  1576. customer_id: customer.id,
  1577. updated_by_id: agent.id,
  1578. created_by_id: agent.id,
  1579. )
  1580. params = {
  1581. title: 'a update ticket #1',
  1582. }
  1583. authenticated_as(agent)
  1584. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1585. expect(response).to have_http_status(:ok)
  1586. expect(json_response).to be_a(Hash)
  1587. ticket = Ticket.find(json_response['id'])
  1588. expect(json_response['state_id']).to eq(ticket.state_id)
  1589. expect(json_response['state']).to be_falsey
  1590. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1591. expect(json_response['priority']).to be_falsey
  1592. expect(json_response['group_id']).to eq(ticket.group_id)
  1593. expect(json_response['group']).to be_falsey
  1594. expect(json_response['title']).to eq('a update ticket #1')
  1595. expect(json_response['customer_id']).to eq(customer.id)
  1596. expect(json_response['updated_by_id']).to eq(agent.id)
  1597. expect(json_response['created_by_id']).to eq(agent.id)
  1598. params = {
  1599. title: 'a update ticket #2',
  1600. }
  1601. put "/api/v1/tickets/#{ticket.id}?expand=true", params: params, as: :json
  1602. expect(response).to have_http_status(:ok)
  1603. expect(json_response).to be_a(Hash)
  1604. ticket = Ticket.find(json_response['id'])
  1605. expect(json_response['state_id']).to eq(ticket.state_id)
  1606. expect(json_response['state']).to eq(ticket.state.name)
  1607. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1608. expect(json_response['priority']).to eq(ticket.priority.name)
  1609. expect(json_response['group_id']).to eq(ticket.group_id)
  1610. expect(json_response['group']).to eq(ticket.group.name)
  1611. expect(json_response['title']).to eq('a update ticket #2')
  1612. expect(json_response['customer_id']).to eq(customer.id)
  1613. expect(json_response['updated_by_id']).to eq(agent.id)
  1614. expect(json_response['created_by_id']).to eq(agent.id)
  1615. params = {
  1616. title: 'a update ticket #3',
  1617. }
  1618. put "/api/v1/tickets/#{ticket.id}?full=true", params: params, as: :json
  1619. expect(response).to have_http_status(:ok)
  1620. expect(json_response).to be_a(Hash)
  1621. ticket = Ticket.find(json_response['id'])
  1622. expect(json_response['assets']).to be_truthy
  1623. expect(json_response['assets']['Ticket']).to be_truthy
  1624. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1625. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1626. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq('a update ticket #3')
  1627. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1628. expect(json_response['assets']['User']).to be_truthy
  1629. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1630. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1631. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1632. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1633. expect(json_response['assets']['User']).to be_truthy
  1634. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1635. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1636. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1637. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1638. # it should be not possible to modify the ticket number
  1639. expected_ticket_number = ticket.number
  1640. params = {
  1641. title: 'a update ticket #4',
  1642. number: '77777',
  1643. }
  1644. put "/api/v1/tickets/#{ticket.id}?full=true", params: params, as: :json
  1645. expect(response).to have_http_status(:ok)
  1646. expect(json_response).to be_a(Hash)
  1647. ticket = Ticket.find(json_response['id'])
  1648. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq('a update ticket #4')
  1649. expect(json_response['assets']['Ticket'][ticket.id.to_s]['number']).to eq(expected_ticket_number)
  1650. end
  1651. it 'does ticket update with empty article param' do
  1652. title = 'a new ticket'
  1653. ticket = create(
  1654. :ticket,
  1655. title: title,
  1656. group: ticket_group,
  1657. customer_id: customer.id,
  1658. updated_by_id: agent.id,
  1659. created_by_id: agent.id,
  1660. )
  1661. params = {
  1662. state: Ticket::State.lookup(name: 'close'),
  1663. article: {}
  1664. }
  1665. authenticated_as(agent)
  1666. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1667. expect(response).to have_http_status(:ok)
  1668. expect(json_response).to be_a(Hash)
  1669. expect(json_response['state_id']).to eq(ticket.state_id)
  1670. expect(json_response['state']).to be_falsey
  1671. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1672. expect(json_response['priority']).to be_falsey
  1673. expect(json_response['group_id']).to eq(ticket.group_id)
  1674. expect(json_response['group']).to be_falsey
  1675. expect(json_response['customer_id']).to eq(customer.id)
  1676. expect(json_response['updated_by_id']).to eq(agent.id)
  1677. expect(json_response['created_by_id']).to eq(agent.id)
  1678. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1679. expect(json_response['title']).to eq(ticket.title)
  1680. expect(ticket.articles.count).to eq(0)
  1681. end
  1682. it 'does ticket split with html - check attachments (05.01)' do
  1683. ticket = create(
  1684. :ticket,
  1685. title: 'some title',
  1686. group: ticket_group,
  1687. customer_id: customer.id,
  1688. updated_by_id: agent.id,
  1689. created_by_id: agent.id,
  1690. )
  1691. article = create(
  1692. :ticket_article,
  1693. type: Ticket::Article::Type.lookup(name: 'note'),
  1694. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1695. body: '<b>test</b> <img src="cid:15.274327094.140938@ZAMMAD.example.com"/> test <img src="cid:15.274327094.140938.3@ZAMMAD.example.com"/>',
  1696. content_type: 'text/html',
  1697. ticket_id: ticket.id,
  1698. )
  1699. create(:store,
  1700. object: 'Ticket::Article',
  1701. o_id: article.id,
  1702. data: 'content_file1_normally_should_be_an_image',
  1703. filename: 'some_file1.jpg',
  1704. preferences: {
  1705. 'Content-Type' => 'image/jpeg',
  1706. 'Mime-Type' => 'image/jpeg',
  1707. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  1708. 'Content-Disposition' => 'inline',
  1709. })
  1710. create(:store,
  1711. object: 'Ticket::Article',
  1712. o_id: article.id,
  1713. data: 'content_file2_normally_should_be_an_image',
  1714. filename: 'some_file2.jpg',
  1715. preferences: {
  1716. 'Content-Type' => 'image/jpeg',
  1717. 'Mime-Type' => 'image/jpeg',
  1718. 'Content-ID' => '15.274327094.140938.2@zammad.example.com',
  1719. 'Content-Disposition' => 'inline',
  1720. })
  1721. create(:store,
  1722. object: 'Ticket::Article',
  1723. o_id: article.id,
  1724. data: 'content_file3_normally_should_be_an_image',
  1725. filename: 'some_file3.jpg',
  1726. preferences: {
  1727. 'Content-Type' => 'image/jpeg',
  1728. 'Mime-Type' => 'image/jpeg',
  1729. 'Content-ID' => '15.274327094.140938.3@zammad.example.com',
  1730. })
  1731. create(:store,
  1732. object: 'Ticket::Article',
  1733. o_id: article.id,
  1734. data: 'content_file4_normally_should_be_an_image',
  1735. filename: 'some_file4.jpg',
  1736. preferences: {
  1737. 'Content-Type' => 'image/jpeg',
  1738. 'Mime-Type' => 'image/jpeg',
  1739. 'Content-ID' => '15.274327094.140938.4@zammad.example.com',
  1740. })
  1741. create(:store,
  1742. object: 'Ticket::Article',
  1743. o_id: article.id,
  1744. data: 'content_file1_normally_should_be_an_pdf',
  1745. filename: 'Rechnung_RE-2018-200.pdf',
  1746. preferences: {
  1747. 'Content-Type' => 'application/octet-stream; name="Rechnung_RE-2018-200.pdf"',
  1748. 'Mime-Type' => 'application/octet-stream',
  1749. 'Content-ID' => '8AB0BEC88984EE4EBEF643C79C8E0346@zammad.example.com',
  1750. 'Content-Description' => 'Rechnung_RE-2018-200.pdf',
  1751. 'Content-Disposition' => 'attachment',
  1752. })
  1753. authenticated_as(customer)
  1754. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1755. expect(response).to have_http_status(:forbidden)
  1756. authenticated_as(agent)
  1757. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1758. expect(response).to have_http_status(:ok)
  1759. expect(json_response).to be_a(Hash)
  1760. expect(json_response['assets']).to be_truthy
  1761. expect(json_response['assets']['Ticket']).to be_truthy
  1762. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1763. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1764. expect(json_response['attachments']).to be_truthy
  1765. expect(json_response['attachments'].count).to eq(3)
  1766. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1767. expect(response).to have_http_status(:ok)
  1768. expect(json_response).to be_a(Hash)
  1769. expect(json_response['assets']).to be_truthy
  1770. expect(json_response['assets']['Ticket']).to be_truthy
  1771. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1772. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1773. expect(json_response['attachments']).to be_truthy
  1774. expect(json_response['attachments'].count).to eq(0)
  1775. end
  1776. it 'does ticket split with plain - check attachments (05.02)' do
  1777. ticket = create(
  1778. :ticket,
  1779. title: 'some title',
  1780. group: ticket_group,
  1781. customer_id: customer.id,
  1782. updated_by_id: agent.id,
  1783. created_by_id: agent.id,
  1784. )
  1785. article = create(
  1786. :ticket_article,
  1787. type: Ticket::Article::Type.lookup(name: 'note'),
  1788. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1789. body: '<b>test</b> <img src="cid:15.274327094.140938@zammad.example.com"/>',
  1790. content_type: 'text/plain',
  1791. ticket_id: ticket.id,
  1792. )
  1793. create(:store,
  1794. object: 'Ticket::Article',
  1795. o_id: article.id,
  1796. data: 'content_file1_normally_should_be_an_image',
  1797. filename: 'some_file1.jpg',
  1798. preferences: {
  1799. 'Content-Type' => 'image/jpeg',
  1800. 'Mime-Type' => 'image/jpeg',
  1801. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  1802. 'Content-Disposition' => 'inline',
  1803. })
  1804. create(:store,
  1805. object: 'Ticket::Article',
  1806. o_id: article.id,
  1807. data: 'content_file1_normally_should_be_an_image',
  1808. filename: 'some_file2.jpg',
  1809. preferences: {
  1810. 'Content-Type' => 'image/jpeg',
  1811. 'Mime-Type' => 'image/jpeg',
  1812. 'Content-ID' => '15.274327094.140938.2@zammad.example.com',
  1813. 'Content-Disposition' => 'inline',
  1814. })
  1815. create(:store,
  1816. object: 'Ticket::Article',
  1817. o_id: article.id,
  1818. data: 'content_file1_normally_should_be_an_pdf',
  1819. filename: 'Rechnung_RE-2018-200.pdf',
  1820. preferences: {
  1821. 'Content-Type' => 'application/octet-stream; name="Rechnung_RE-2018-200.pdf"',
  1822. 'Mime-Type' => 'application/octet-stream',
  1823. 'Content-ID' => '8AB0BEC88984EE4EBEF643C79C8E0346@zammad.example.com',
  1824. 'Content-Description' => 'Rechnung_RE-2018-200.pdf',
  1825. 'Content-Disposition' => 'attachment',
  1826. })
  1827. authenticated_as(agent)
  1828. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1829. expect(response).to have_http_status(:ok)
  1830. expect(json_response).to be_a(Hash)
  1831. expect(json_response['assets']).to be_truthy
  1832. expect(json_response['assets']['Ticket']).to be_truthy
  1833. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1834. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1835. expect(json_response['attachments']).to be_truthy
  1836. expect(json_response['attachments'].count).to eq(3)
  1837. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1838. expect(response).to have_http_status(:ok)
  1839. expect(json_response).to be_a(Hash)
  1840. expect(json_response['assets']).to be_truthy
  1841. expect(json_response['assets']['Ticket']).to be_truthy
  1842. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1843. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1844. expect(json_response['attachments']).to be_truthy
  1845. expect(json_response['attachments'].count).to eq(0)
  1846. end
  1847. it 'does ticket merge (07.01)' do
  1848. group_no_permission = create(:group)
  1849. ticket1 = create(
  1850. :ticket,
  1851. title: 'ticket merge1',
  1852. group: ticket_group,
  1853. customer_id: customer.id,
  1854. )
  1855. ticket2 = create(
  1856. :ticket,
  1857. title: 'ticket merge2',
  1858. group: ticket_group,
  1859. customer_id: customer.id,
  1860. )
  1861. ticket3 = create(
  1862. :ticket,
  1863. title: 'ticket merge2',
  1864. group: group_no_permission,
  1865. customer_id: customer.id,
  1866. )
  1867. authenticated_as(customer)
  1868. put "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
  1869. expect(response).to have_http_status(:forbidden)
  1870. authenticated_as(agent)
  1871. put "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
  1872. expect(response).to have_http_status(:ok)
  1873. expect(json_response).to be_a(Hash)
  1874. expect(json_response['result']).to eq('failed')
  1875. expect(json_response['message']).to eq('The target ticket number could not be found.')
  1876. put "/api/v1/ticket_merge/#{ticket3.id}/#{ticket1.number}", params: {}, as: :json
  1877. expect(response).to have_http_status(:forbidden)
  1878. expect(json_response).to be_a(Hash)
  1879. expect(json_response['error']).to eq('Not authorized')
  1880. expect(json_response['error_human']).to eq('Not authorized')
  1881. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket3.number}", params: {}, as: :json
  1882. expect(response).to have_http_status(:forbidden)
  1883. expect(json_response).to be_a(Hash)
  1884. expect(json_response['error']).to eq('Not authorized')
  1885. expect(json_response['error_human']).to eq('Not authorized')
  1886. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket2.number}", params: {}, as: :json
  1887. expect(response).to have_http_status(:ok)
  1888. expect(json_response).to be_a(Hash)
  1889. expect(json_response['result']).to eq('success')
  1890. expect(json_response['target_ticket']['id']).to eq(ticket2.id)
  1891. end
  1892. it 'does ticket merge - change permission (07.02)' do
  1893. group_change_permission = Group.create!(
  1894. name: 'GroupWithChangePermission',
  1895. active: true,
  1896. updated_by_id: 1,
  1897. created_by_id: 1,
  1898. )
  1899. ticket1 = create(
  1900. :ticket,
  1901. title: 'ticket merge1',
  1902. group: group_change_permission,
  1903. customer_id: customer.id,
  1904. )
  1905. ticket2 = create(
  1906. :ticket,
  1907. title: 'ticket merge2',
  1908. group: group_change_permission,
  1909. customer_id: customer.id,
  1910. )
  1911. agent.group_names_access_map = { group_change_permission.name => %w[read change] }
  1912. authenticated_as(agent)
  1913. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket2.number}", params: {}, as: :json
  1914. expect(response).to have_http_status(:ok)
  1915. expect(json_response).to be_a(Hash)
  1916. expect(json_response['result']).to eq('success')
  1917. expect(json_response['target_ticket']['id']).to eq(ticket2.id)
  1918. end
  1919. it 'does ticket search sorted (08.01)' do
  1920. title = "ticket pagination #{SecureRandom.uuid}"
  1921. ticket1 = create(
  1922. :ticket,
  1923. title: "#{title} A",
  1924. group: ticket_group,
  1925. customer_id: customer.id,
  1926. created_at: '2018-02-05 17:42:00',
  1927. updated_at: '2018-02-05 20:42:00',
  1928. )
  1929. create(
  1930. :ticket_article,
  1931. type: Ticket::Article::Type.lookup(name: 'note'),
  1932. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1933. ticket_id: ticket1.id,
  1934. )
  1935. ticket2 = create(
  1936. :ticket,
  1937. title: "#{title} B",
  1938. group: ticket_group,
  1939. customer_id: customer.id,
  1940. state: Ticket::State.lookup(name: 'new'),
  1941. priority: Ticket::Priority.lookup(name: '3 hoch'),
  1942. created_at: '2018-02-05 19:42:00',
  1943. updated_at: '2018-02-05 19:42:00',
  1944. )
  1945. create(
  1946. :ticket_article,
  1947. type: Ticket::Article::Type.lookup(name: 'note'),
  1948. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1949. ticket_id: ticket2.id,
  1950. )
  1951. authenticated_as(admin)
  1952. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: {}, as: :json
  1953. expect(response).to have_http_status(:ok)
  1954. expect(json_response).to be_a(Hash)
  1955. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1956. authenticated_as(admin)
  1957. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'created_at', order_by: 'asc' }, as: :json
  1958. expect(response).to have_http_status(:ok)
  1959. expect(json_response).to be_a(Hash)
  1960. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1961. authenticated_as(admin)
  1962. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'title', order_by: 'asc' }, as: :json
  1963. expect(response).to have_http_status(:ok)
  1964. expect(json_response).to be_a(Hash)
  1965. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1966. authenticated_as(admin)
  1967. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'title', order_by: 'desc' }, as: :json
  1968. expect(response).to have_http_status(:ok)
  1969. expect(json_response).to be_a(Hash)
  1970. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1971. authenticated_as(admin)
  1972. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: %w[created_at updated_at], order_by: %w[asc asc] }, as: :json
  1973. expect(response).to have_http_status(:ok)
  1974. expect(json_response).to be_a(Hash)
  1975. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1976. authenticated_as(admin)
  1977. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: %w[created_at updated_at], order_by: %w[desc asc] }, as: :json
  1978. expect(response).to have_http_status(:ok)
  1979. expect(json_response).to be_a(Hash)
  1980. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1981. end
  1982. it 'does ticket history' do
  1983. ticket1 = create(
  1984. :ticket,
  1985. title: 'some title',
  1986. group: ticket_group,
  1987. customer_id: customer.id,
  1988. )
  1989. create(
  1990. :ticket_article,
  1991. type: Ticket::Article::Type.lookup(name: 'note'),
  1992. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1993. ticket_id: ticket1.id,
  1994. )
  1995. authenticated_as(agent)
  1996. get "/api/v1/ticket_history/#{ticket1.id}", params: {}, as: :json
  1997. expect(response).to have_http_status(:ok)
  1998. expect(json_response).to be_a(Hash)
  1999. expect(json_response['history'].class).to eq(Array)
  2000. expect(json_response['assets'].class).to eq(Hash)
  2001. expect(json_response['assets']['User'][customer.id.to_s]).not_to be_nil
  2002. expect(json_response['assets']['Ticket'][ticket1.id.to_s]).not_to be_nil
  2003. authenticated_as(customer)
  2004. get "/api/v1/ticket_history/#{ticket1.id}", params: {}, as: :json
  2005. expect(response).to have_http_status(:forbidden)
  2006. end
  2007. it 'does ticket related' do
  2008. ticket1 = create(
  2009. :ticket,
  2010. title: 'some title',
  2011. group: ticket_group,
  2012. customer_id: customer.id,
  2013. )
  2014. authenticated_as(agent)
  2015. get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
  2016. expect(response).to have_http_status(:ok)
  2017. authenticated_as(customer)
  2018. get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
  2019. expect(response).to have_http_status(:forbidden)
  2020. end
  2021. it 'does ticket recent' do
  2022. authenticated_as(agent)
  2023. get '/api/v1/ticket_recent', params: {}, as: :json
  2024. expect(response).to have_http_status(:ok)
  2025. authenticated_as(customer)
  2026. get '/api/v1/ticket_recent', params: {}, as: :json
  2027. expect(response).to have_http_status(:forbidden)
  2028. end
  2029. end
  2030. describe 'mentions' do
  2031. let(:user1) { create(:agent, groups: [ticket_group]) }
  2032. let(:user2) { create(:agent, groups: [ticket_group]) }
  2033. let(:user3) { create(:agent, groups: [ticket_group]) }
  2034. def new_ticket_with_mentions(*user_ids)
  2035. params = {
  2036. title: 'a new ticket #11',
  2037. group: ticket_group.name,
  2038. customer: {
  2039. firstname: 'some firstname',
  2040. lastname: 'some lastname',
  2041. email: 'some_new_customer@example.com',
  2042. },
  2043. article: {
  2044. body: 'some test 123',
  2045. },
  2046. mentions: user_ids
  2047. }
  2048. authenticated_as(agent)
  2049. post '/api/v1/tickets', params: params, as: :json
  2050. json_response
  2051. end
  2052. it 'create ticket with mentions' do
  2053. new_ticket_with_mentions(user1.id, user2.id, user3.id)
  2054. expect(response).to have_http_status(:created)
  2055. expect(Mention.count).to eq(3)
  2056. end
  2057. it 'create ticket with one of mentions being invalid' do
  2058. new_ticket_with_mentions(user1.id, user2.id, create(:customer).id)
  2059. expect(response).to have_http_status(:unprocessable_entity)
  2060. expect(Mention.count).to eq(0)
  2061. end
  2062. it 'check ticket get' do
  2063. ticket = new_ticket_with_mentions(user1.id, user2.id, user3.id)
  2064. get "/api/v1/tickets/#{ticket['id']}?all=true", params: {}, as: :json
  2065. expect(response).to have_http_status(:ok)
  2066. expect(json_response['mentions'].count).to eq(3)
  2067. expect(json_response['assets']['Mention'].count).to eq(3)
  2068. end
  2069. end
  2070. describe 'stats' do
  2071. let(:ticket1) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  2072. let(:ticket2) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  2073. let(:ticket3) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  2074. let(:customer) { create(:customer, organization: organization) }
  2075. let(:organization) { create(:organization, shared: false) }
  2076. before do
  2077. authenticated_as(admin)
  2078. ticket1
  2079. travel 2.minutes
  2080. ticket2
  2081. travel 2.minutes
  2082. ticket3
  2083. travel 2.minutes
  2084. ticket2.touch
  2085. end
  2086. # https://github.com/zammad/zammad/issues/2296
  2087. it 'orders tickets by created_at desc (#2296)' do
  2088. get '/api/v1/ticket_stats', params: { organization_id: organization.id, user_id: customer.id }, as: :json
  2089. expect(response).to have_http_status(:ok)
  2090. expect(json_response)
  2091. .to be_a(Hash)
  2092. .and include('user' => hash_including('open_ids' => [ticket3.id, ticket2.id, ticket1.id]))
  2093. .and include('organization' => hash_including('open_ids' => [ticket3.id, ticket2.id, ticket1.id]))
  2094. end
  2095. end
  2096. describe '/api/v1/tickets' do
  2097. subject(:ticket) { create(:ticket, state_name: 'closed') }
  2098. let(:admin) { create(:admin, groups: [ticket.group]) }
  2099. let(:agent) { create(:agent, groups: [ticket.group]) }
  2100. let(:customer) { ticket.customer }
  2101. describe 'reopening a ticket' do
  2102. shared_examples 'successfully reopen a ticket' do
  2103. it 'succeeds' do
  2104. put "/api/v1/tickets/#{ticket.id}",
  2105. params: { state_id: Ticket::State.find_by(name: 'open').id },
  2106. as: :json
  2107. expect(response).to have_http_status(:ok)
  2108. expect(json_response).to include('state_id' => Ticket::State.find_by(name: 'open').id)
  2109. end
  2110. end
  2111. shared_examples 'fail to reopen a ticket' do
  2112. it 'fails' do
  2113. put "/api/v1/tickets/#{ticket.id}",
  2114. params: { state_id: Ticket::State.find_by(name: 'open').id },
  2115. as: :json
  2116. expect(response).to have_http_status(:unprocessable_entity)
  2117. expect(json_response).to include('error' => 'Cannot follow-up on a closed ticket. Please create a new ticket.')
  2118. end
  2119. end
  2120. context 'when ticket.group.follow_up_possible = "yes"' do
  2121. before { ticket.group.update(follow_up_possible: 'yes') }
  2122. context 'as admin', authenticated_as: -> { admin } do
  2123. include_examples 'successfully reopen a ticket'
  2124. end
  2125. context 'as agent', authenticated_as: -> { agent } do
  2126. include_examples 'successfully reopen a ticket'
  2127. end
  2128. context 'as customer', authenticated_as: -> { customer } do
  2129. include_examples 'successfully reopen a ticket'
  2130. end
  2131. end
  2132. context 'when ticket.group.follow_up_possible = "new_ticket"' do
  2133. before { ticket.group.update(follow_up_possible: 'new_ticket') }
  2134. context 'as admin', authenticated_as: -> { admin } do
  2135. include_examples 'successfully reopen a ticket'
  2136. end
  2137. context 'as agent', authenticated_as: -> { agent } do
  2138. include_examples 'successfully reopen a ticket'
  2139. end
  2140. context 'as customer', authenticated_as: -> { customer } do
  2141. include_examples 'fail to reopen a ticket'
  2142. end
  2143. end
  2144. end
  2145. end
  2146. describe 'GET /api/v1/tickets/:id' do
  2147. subject!(:ticket) { create(:ticket) }
  2148. let(:agent) { create(:agent, groups: [ticket.group]) }
  2149. context 'links present', authenticated_as: -> { agent } do
  2150. before do
  2151. create(:link, from: ticket, to: linked)
  2152. get "/api/v1/tickets/#{ticket.id}", params: { all: 'true' }, as: :json
  2153. end
  2154. let(:linked) { create(:ticket, group: ticket.group) }
  2155. it 'is present in response' do
  2156. expect(response).to have_http_status(:ok)
  2157. expect(json_response['links']).to eq([
  2158. {
  2159. 'link_type' => 'normal',
  2160. 'link_object' => 'Ticket',
  2161. 'link_object_value' => linked.id
  2162. }
  2163. ])
  2164. end
  2165. context 'no permission to linked Ticket Group' do
  2166. let(:linked) { create(:ticket) }
  2167. it 'is not present in response' do
  2168. expect(response).to have_http_status(:ok)
  2169. expect(json_response['links']).to be_blank
  2170. end
  2171. end
  2172. end
  2173. end
  2174. describe 'GET /api/v1/ticket_customer' do
  2175. subject(:ticket) { create(:ticket, customer: customer_authorized) }
  2176. let(:organization_authorized) { create(:organization) }
  2177. let(:customer_authorized) { create(:customer, organization: organization_authorized) }
  2178. let(:organization_unauthorized) { create(:organization) }
  2179. let(:customer_unauthorized) { create(:customer, organization: organization_unauthorized) }
  2180. let(:agent) { create(:agent, groups: [ticket.group]) }
  2181. describe 'listing information' do
  2182. before do
  2183. ticket
  2184. end
  2185. shared_examples 'has access' do
  2186. it 'succeeds' do
  2187. get '/api/v1/ticket_customer',
  2188. params: { customer_id: customer_authorized.id },
  2189. as: :json
  2190. expect(json_response['ticket_ids_open']).to include(ticket.id)
  2191. expect(json_response['ticket_ids_closed']).to be_blank
  2192. end
  2193. end
  2194. shared_examples 'has no access' do
  2195. it 'fails' do
  2196. get '/api/v1/ticket_customer',
  2197. params: { customer_id: customer_authorized.id },
  2198. as: :json
  2199. expect(json_response['ticket_ids_open']).to be_blank
  2200. expect(json_response['ticket_ids_closed']).to be_blank
  2201. expect(json_response['assets']).to be_blank
  2202. end
  2203. end
  2204. context 'as agent', authenticated_as: -> { agent } do
  2205. include_examples 'has access'
  2206. end
  2207. context 'as authorized customer', authenticated_as: -> { customer_authorized } do
  2208. include_examples 'has no access'
  2209. end
  2210. context 'as unauthorized customer', authenticated_as: -> { customer_unauthorized } do
  2211. include_examples 'has no access'
  2212. end
  2213. end
  2214. end
  2215. describe 'Assign user to multiple organizations #1573' do
  2216. let(:organizations) { create_list(:organization, 3) }
  2217. let(:customer) { create(:customer, organization: organizations[0], organizations: organizations[1..]) }
  2218. let(:ticket1) { create(:ticket, customer: customer, organization: organizations[0], group: Group.first) }
  2219. let(:ticket2) { create(:ticket, customer: customer, organization: organizations[1], group: Group.first) }
  2220. before do
  2221. ticket1 && ticket2
  2222. end
  2223. it 'does return multi organization tickets' do
  2224. authenticated_as(agent)
  2225. post '/api/v1/ticket_stats', params: { organization_id: customer.all_organization_ids, user_id: customer.id }, as: :json
  2226. expect(response).to have_http_status(:ok)
  2227. expect(json_response)
  2228. .to be_a(Hash)
  2229. .and include('user' => hash_including('open_ids' => [ticket2.id, ticket1.id]))
  2230. .and include('organization' => hash_including('open_ids' => [ticket2.id, ticket1.id]))
  2231. end
  2232. end
  2233. describe 'Articles are not indexed properly #4596', performs_jobs: true, searchindex: true do
  2234. let(:ticket) { create(:ticket, group: Group.first) }
  2235. def new_article_body
  2236. params = {
  2237. article: {
  2238. body: SecureRandom.uuid,
  2239. content_type: 'text/plain',
  2240. internal: false,
  2241. },
  2242. }
  2243. authenticated_as(agent)
  2244. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  2245. expect(response).to have_http_status(:ok)
  2246. perform_enqueued_jobs
  2247. SearchIndexBackend.refresh
  2248. params[:article][:body]
  2249. end
  2250. def delete_article_body
  2251. article = ticket.articles.last
  2252. delete "/api/v1/ticket_articles/#{article.id}"
  2253. expect(response).to have_http_status(:ok)
  2254. perform_enqueued_jobs
  2255. SearchIndexBackend.refresh
  2256. article.body
  2257. end
  2258. before do
  2259. ticket
  2260. searchindex_model_reload([Ticket])
  2261. end
  2262. it 'does find articles after creations', :aggregate_failures do
  2263. expect(SearchIndexBackend.search(new_article_body, 'Ticket', limit: 1)).to be_present
  2264. expect(SearchIndexBackend.search(new_article_body, 'Ticket', limit: 1)).to be_present
  2265. expect(SearchIndexBackend.search(delete_article_body, 'Ticket', limit: 1)).to be_blank
  2266. end
  2267. end
  2268. describe 'Agent with being "ticket.agent" and "ticket.customer" is creating + updating a ticket', :aggregate_failures, authenticated_as: :user do
  2269. let(:group_only_create) { create(:group) }
  2270. let(:user) { create(:agent_and_customer) }
  2271. before do
  2272. skip 'This test requires some changes to the metadata concerns for the Ticket::Article model which are not done yet.'
  2273. user.group_names_access_map = {
  2274. group_only_create.name => %w[create],
  2275. }
  2276. end
  2277. it 'contains correct information for sender if agent sets himself as customer and responds' do
  2278. params = {
  2279. title: 'Test title for issue #4647',
  2280. group_id: group_only_create.id,
  2281. customer_id: user.id,
  2282. article: {
  2283. type: 'web',
  2284. internal: false,
  2285. sender: 'Customer',
  2286. subject: 'Test subject',
  2287. body: SecureRandom.uuid,
  2288. },
  2289. }
  2290. post '/api/v1/tickets', params: params, as: :json
  2291. expect(response).to have_http_status(:created)
  2292. ticket = Ticket.last
  2293. expect(ticket.customer_id).to eq(user.id)
  2294. expect(ticket.articles.reload.first).to have_attributes(
  2295. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  2296. from: "#{user.fullname} <#{user.email}>",
  2297. )
  2298. response_params = {
  2299. article: {
  2300. body: SecureRandom.uuid,
  2301. },
  2302. }
  2303. put "/api/v1/tickets/#{ticket.id}", params: response_params, as: :json
  2304. expect(response).to have_http_status(:ok)
  2305. expect(ticket.reload.articles.last.sender.name).to eq('Customer')
  2306. end
  2307. end
  2308. describe 'Article contains wrong "origin_by" + "from" information', :aggregate_failures, authenticated_as: :user do
  2309. let(:api_role) do
  2310. role = create(:role, name: 'API', permission_names: ['ticket.agent'])
  2311. role.group_names_access_map = {
  2312. Group.first.name => %w[create change],
  2313. }
  2314. role
  2315. end
  2316. let(:user) { create(:user, roles: [api_role]) }
  2317. it 'contains correct "origin_by" + "from" information' do
  2318. params = {
  2319. title: 'Test title for issue #4647',
  2320. group_id: Group.first.id,
  2321. customer_id: 'guess:dummy@example.com',
  2322. article: {
  2323. type: 'web',
  2324. internal: false,
  2325. sender: 'Customer',
  2326. subject: 'Test subject',
  2327. body: SecureRandom.uuid,
  2328. },
  2329. }
  2330. post '/api/v1/tickets', params: params, as: :json
  2331. expect(response).to have_http_status(:created)
  2332. expect(Ticket.last.articles.first).to have_attributes(
  2333. origin_by_id: User.find_by(email: 'dummy@example.com').id,
  2334. from: 'dummy@example.com',
  2335. )
  2336. end
  2337. end
  2338. describe 'Agents can create new tags even if prohibited by the settings #3501', authenticated_as: :agent do
  2339. let(:tag) { SecureRandom.hex(4) }
  2340. before do
  2341. Setting.set('tag_new', false)
  2342. end
  2343. it 'does create the ticket without tags' do
  2344. params = {
  2345. title: 'a new ticket #3',
  2346. group: Group.first.name,
  2347. priority: '2 normal',
  2348. state: 'new',
  2349. customer_id: customer.id,
  2350. tags: tag,
  2351. }
  2352. post '/api/v1/tickets', params: params, as: :json
  2353. expect(response).to have_http_status(:created)
  2354. expect(Ticket.last.tag_list).to eq([])
  2355. end
  2356. end
  2357. end