ticket_spec.rb 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  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. expect(file).to be_inline
  722. file = ticket.articles.first.attachments[1]
  723. expect(Digest::MD5.hexdigest(file.content)).to eq('39d0d586a701e199389d954f2d592720')
  724. expect(file.filename).to eq('some_file.txt')
  725. expect(file.preferences['Mime-Type']).to eq('text/plain')
  726. expect(file.preferences['Content-ID']).to be_falsey
  727. expect(file).not_to be_inline
  728. end
  729. it 'does ticket create with agent (02.02)' do
  730. params = {
  731. title: 'a new ticket #1',
  732. state: 'new',
  733. priority: '2 normal',
  734. group: ticket_group.name,
  735. customer: 'tickets-customer1@example.com',
  736. article: {
  737. content_type: 'text/plain', # or text/html
  738. body: 'some body',
  739. },
  740. links: {
  741. Ticket: {
  742. parent: [1],
  743. }
  744. }
  745. }
  746. authenticated_as(agent)
  747. post '/api/v1/tickets', params: params, as: :json
  748. expect(response).to have_http_status(:created)
  749. expect(json_response).to be_a(Hash)
  750. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  751. expect(json_response['title']).to eq('a new ticket #1')
  752. expect(json_response['updated_by_id']).to eq(agent.id)
  753. expect(json_response['created_by_id']).to eq(agent.id)
  754. links = Link.list(
  755. link_object: 'Ticket',
  756. link_object_value: json_response['id'],
  757. )
  758. expect(links[0]['link_type']).to eq('child')
  759. expect(links[0]['link_object']).to eq('Ticket')
  760. expect(links[0]['link_object_value']).to eq(1)
  761. end
  762. it 'does ticket with wrong ticket id (02.03)' do
  763. group = create(:group)
  764. ticket = create(
  765. :ticket,
  766. title: 'ticket with wrong ticket id',
  767. group_id: group.id,
  768. customer_id: customer.id,
  769. )
  770. authenticated_as(agent)
  771. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  772. expect(response).to have_http_status(:forbidden)
  773. expect(json_response).to be_a(Hash)
  774. expect(json_response['error']).to eq('Not authorized')
  775. params = {
  776. title: 'ticket with wrong ticket id - 2',
  777. }
  778. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  779. expect(response).to have_http_status(:forbidden)
  780. expect(json_response).to be_a(Hash)
  781. expect(json_response['error']).to eq('Not authorized')
  782. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  783. expect(response).to have_http_status(:forbidden)
  784. expect(json_response).to be_a(Hash)
  785. expect(json_response['error']).to eq('Not authorized')
  786. end
  787. it 'does ticket with correct ticket id (02.04)', performs_jobs: true do
  788. title = "ticket with corret ticket id testagent#{SecureRandom.uuid}"
  789. ticket = create(
  790. :ticket,
  791. title: title,
  792. group: ticket_group,
  793. customer_id: customer.id,
  794. preferences: {
  795. some_key1: 123,
  796. },
  797. )
  798. authenticated_as(agent)
  799. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  800. expect(response).to have_http_status(:ok)
  801. expect(json_response).to be_a(Hash)
  802. expect(json_response['id']).to eq(ticket.id)
  803. expect(json_response['title']).to eq(title)
  804. expect(json_response['customer_id']).to eq(ticket.customer_id)
  805. expect(json_response['updated_by_id']).to eq(1)
  806. expect(json_response['created_by_id']).to eq(1)
  807. expect(json_response['preferences']['some_key1']).to eq(123)
  808. params = {
  809. title: "#{title} - 2",
  810. customer_id: agent.id,
  811. preferences: {
  812. some_key2: 'abc',
  813. },
  814. }
  815. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  816. expect(response).to have_http_status(:ok)
  817. expect(json_response).to be_a(Hash)
  818. expect(json_response['id']).to eq(ticket.id)
  819. expect(json_response['title']).to eq("#{title} - 2")
  820. expect(json_response['customer_id']).to eq(agent.id)
  821. expect(json_response['updated_by_id']).to eq(agent.id)
  822. expect(json_response['created_by_id']).to eq(1)
  823. expect(json_response['preferences']['some_key1']).to eq(123)
  824. expect(json_response['preferences']['some_key2']).to eq('abc')
  825. params = {
  826. ticket_id: ticket.id,
  827. subject: 'some subject',
  828. body: 'some body',
  829. }
  830. post '/api/v1/ticket_articles', params: params, as: :json
  831. expect(response).to have_http_status(:created)
  832. article_json_response = json_response
  833. expect(article_json_response).to be_a(Hash)
  834. expect(article_json_response['ticket_id']).to eq(ticket.id)
  835. expect(article_json_response['from']).to eq('Tickets Agent')
  836. expect(article_json_response['subject']).to eq('some subject')
  837. expect(article_json_response['body']).to eq('some body')
  838. expect(article_json_response['content_type']).to eq('text/plain')
  839. expect(article_json_response['internal']).to be(false)
  840. expect(article_json_response['created_by_id']).to eq(agent.id)
  841. expect(article_json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  842. expect(article_json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  843. perform_enqueued_jobs
  844. get "/api/v1/tickets/search?query=#{CGI.escape(title)}", params: {}, as: :json
  845. expect(response).to have_http_status(:ok)
  846. expect(json_response).to be_a(Hash)
  847. expect(json_response['tickets'][0]).to eq(ticket.id)
  848. expect(json_response['tickets_count']).to eq(1)
  849. params = {
  850. condition: {
  851. 'ticket.title' => {
  852. operator: 'contains',
  853. value: title,
  854. },
  855. },
  856. }
  857. post '/api/v1/tickets/search', params: params, as: :json
  858. expect(response).to have_http_status(:ok)
  859. expect(json_response).to be_a(Hash)
  860. expect(json_response['tickets'][0]).to eq(ticket.id)
  861. expect(json_response['tickets_count']).to eq(1)
  862. delete "/api/v1/ticket_articles/#{article_json_response['id']}", params: {}, as: :json
  863. expect(response).to have_http_status(:ok)
  864. params = {
  865. from: 'something which should not be changed on server side',
  866. ticket_id: ticket.id,
  867. subject: 'some subject',
  868. body: 'some body',
  869. type: 'email',
  870. internal: true,
  871. }
  872. post '/api/v1/ticket_articles', params: params, as: :json
  873. expect(response).to have_http_status(:created)
  874. expect(json_response).to be_a(Hash)
  875. expect(json_response['ticket_id']).to eq(ticket.id)
  876. expect(json_response['from']).to eq(%(Tickets Agent via #{ticket_group.email_address.name} <#{ticket_group.email_address.email}>))
  877. expect(json_response['subject']).to eq('some subject')
  878. expect(json_response['body']).to eq('some body')
  879. expect(json_response['content_type']).to eq('text/plain')
  880. expect(json_response['internal']).to be(true)
  881. expect(json_response['created_by_id']).to eq(agent.id)
  882. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  883. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)
  884. params = {
  885. subject: 'new subject',
  886. }
  887. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  888. expect(response).to have_http_status(:ok)
  889. expect(json_response).to be_a(Hash)
  890. expect(json_response['ticket_id']).to eq(ticket.id)
  891. expect(json_response['from']).to eq(%(Tickets Agent via #{ticket_group.email_address.name} <#{ticket_group.email_address.email}>))
  892. expect(json_response['subject']).not_to eq('new subject')
  893. expect(json_response['body']).to eq('some body')
  894. expect(json_response['content_type']).to eq('text/plain')
  895. expect(json_response['internal']).to be(true)
  896. expect(json_response['created_by_id']).to eq(agent.id)
  897. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  898. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)
  899. params = {
  900. from: 'something which should not be changed on server side',
  901. ticket_id: ticket.id,
  902. subject: 'some subject',
  903. body: 'some body',
  904. type: 'email',
  905. internal: false,
  906. }
  907. post '/api/v1/ticket_articles', params: params, as: :json
  908. expect(response).to have_http_status(:created)
  909. expect(json_response['internal']).to be(false)
  910. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  911. expect(response).to have_http_status(:forbidden)
  912. expect(json_response).to be_a(Hash)
  913. expect(json_response['error']).to eq('Not authorized (communication articles cannot be deleted)!')
  914. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  915. expect(response).to have_http_status(:forbidden)
  916. expect(json_response).to be_a(Hash)
  917. expect(json_response['error']).to eq('Not authorized (admin permission required)!')
  918. end
  919. it 'does ticket with correct ticket id (02.05)' do
  920. ticket = create(
  921. :ticket,
  922. title: 'ticket with corret ticket id',
  923. group: ticket_group,
  924. customer_id: customer.id,
  925. )
  926. authenticated_as(admin)
  927. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  928. expect(response).to have_http_status(:ok)
  929. expect(json_response).to be_a(Hash)
  930. expect(json_response['id']).to eq(ticket.id)
  931. expect(json_response['title']).to eq('ticket with corret ticket id')
  932. expect(json_response['customer_id']).to eq(ticket.customer_id)
  933. expect(json_response['updated_by_id']).to eq(1)
  934. expect(json_response['created_by_id']).to eq(1)
  935. params = {
  936. title: 'ticket with corret ticket id - 2',
  937. customer_id: agent.id,
  938. }
  939. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  940. expect(response).to have_http_status(:ok)
  941. expect(json_response).to be_a(Hash)
  942. expect(json_response['id']).to eq(ticket.id)
  943. expect(json_response['title']).to eq('ticket with corret ticket id - 2')
  944. expect(json_response['customer_id']).to eq(agent.id)
  945. expect(json_response['updated_by_id']).to eq(admin.id)
  946. expect(json_response['created_by_id']).to eq(1)
  947. params = {
  948. from: 'something which should not be changed on server side',
  949. ticket_id: ticket.id,
  950. subject: 'some subject',
  951. body: 'some body',
  952. }
  953. post '/api/v1/ticket_articles', params: params, as: :json
  954. expect(response).to have_http_status(:created)
  955. expect(json_response).to be_a(Hash)
  956. expect(json_response['ticket_id']).to eq(ticket.id)
  957. expect(json_response['from']).to eq('Tickets Admin')
  958. expect(json_response['subject']).to eq('some subject')
  959. expect(json_response['body']).to eq('some body')
  960. expect(json_response['content_type']).to eq('text/plain')
  961. expect(json_response['internal']).to be(false)
  962. expect(json_response['created_by_id']).to eq(admin.id)
  963. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  964. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  965. params = {
  966. subject: 'new subject',
  967. internal: true,
  968. }
  969. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  970. expect(response).to have_http_status(:ok)
  971. expect(json_response).to be_a(Hash)
  972. expect(json_response['ticket_id']).to eq(ticket.id)
  973. expect(json_response['from']).to eq('Tickets Admin')
  974. expect(json_response['subject']).not_to eq('new subject')
  975. expect(json_response['body']).to eq('some body')
  976. expect(json_response['content_type']).to eq('text/plain')
  977. expect(json_response['internal']).to be(true)
  978. expect(json_response['created_by_id']).to eq(admin.id)
  979. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  980. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  981. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  982. expect(response).to have_http_status(:ok)
  983. params = {
  984. ticket_id: ticket.id,
  985. subject: 'some subject',
  986. body: 'some body',
  987. type: 'email',
  988. }
  989. post '/api/v1/ticket_articles', params: params, as: :json
  990. expect(response).to have_http_status(:created)
  991. expect(json_response).to be_a(Hash)
  992. expect(json_response['ticket_id']).to eq(ticket.id)
  993. expect(json_response['from']).to eq(%(Tickets Admin via #{ticket_group.email_address.name} <#{ticket_group.email_address.email}>))
  994. expect(json_response['subject']).to eq('some subject')
  995. expect(json_response['body']).to eq('some body')
  996. expect(json_response['content_type']).to eq('text/plain')
  997. expect(json_response['internal']).to be(false)
  998. expect(json_response['created_by_id']).to eq(admin.id)
  999. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  1000. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)
  1001. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  1002. expect(response).to have_http_status(:forbidden)
  1003. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1004. expect(response).to have_http_status(:ok)
  1005. end
  1006. it 'does ticket pagination (02.05)' do
  1007. title = "ticket pagination #{SecureRandom.uuid}"
  1008. tickets = []
  1009. (1..20).each do |count|
  1010. ticket = create(
  1011. :ticket,
  1012. title: "#{title} - #{count}",
  1013. group: ticket_group,
  1014. customer_id: customer.id,
  1015. )
  1016. create(
  1017. :ticket_article,
  1018. type: Ticket::Article::Type.lookup(name: 'note'),
  1019. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1020. ticket_id: ticket.id,
  1021. )
  1022. tickets.push ticket
  1023. travel 2.seconds
  1024. end
  1025. authenticated_as(admin)
  1026. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: {}, as: :json
  1027. expect(response).to have_http_status(:ok)
  1028. expect(json_response).to be_a(Hash)
  1029. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  1030. expect(json_response['tickets'][19]).to eq(tickets[0].id)
  1031. expect(json_response['tickets_count']).to eq(20)
  1032. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=10", params: {}, as: :json
  1033. expect(response).to have_http_status(:ok)
  1034. expect(json_response).to be_a(Hash)
  1035. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  1036. expect(json_response['tickets'][9]).to eq(tickets[10].id)
  1037. expect(json_response['tickets_count']).to eq(10)
  1038. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=1&per_page=5", params: {}, as: :json
  1039. expect(response).to have_http_status(:ok)
  1040. expect(json_response).to be_a(Hash)
  1041. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  1042. expect(json_response['tickets'][4]).to eq(tickets[15].id)
  1043. expect(json_response['tickets_count']).to eq(5)
  1044. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=2&per_page=5", params: {}, as: :json
  1045. expect(response).to have_http_status(:ok)
  1046. expect(json_response).to be_a(Hash)
  1047. expect(json_response['tickets'][0]).to eq(tickets[14].id)
  1048. expect(json_response['tickets'][4]).to eq(tickets[10].id)
  1049. expect(json_response['tickets_count']).to eq(5)
  1050. get '/api/v1/tickets?limit=40&page=1&per_page=5', params: {}, as: :json
  1051. expect(response).to have_http_status(:ok)
  1052. expect(json_response).to be_a(Array)
  1053. tickets = Ticket.reorder(:id).limit(5)
  1054. expect(json_response[0]['id']).to eq(tickets[0].id)
  1055. expect(json_response[4]['id']).to eq(tickets[4].id)
  1056. expect(json_response.count).to eq(5)
  1057. get '/api/v1/tickets?limit=40&page=2&per_page=5', params: {}, as: :json
  1058. expect(response).to have_http_status(:ok)
  1059. expect(json_response).to be_a(Array)
  1060. tickets = Ticket.reorder(:id).limit(10)
  1061. expect(json_response[0]['id']).to eq(tickets[5].id)
  1062. expect(json_response[4]['id']).to eq(tickets[9].id)
  1063. expect(json_response.count).to eq(5)
  1064. end
  1065. it 'does ticket create with customer minimal (03.01)' do
  1066. params = {
  1067. title: 'a new ticket #c1',
  1068. state: 'new',
  1069. priority: '2 normal',
  1070. group: ticket_group.name,
  1071. article: {
  1072. body: 'some body',
  1073. },
  1074. }
  1075. authenticated_as(customer)
  1076. post '/api/v1/tickets', params: params, as: :json
  1077. expect(response).to have_http_status(:created)
  1078. expect(json_response).to be_a(Hash)
  1079. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1080. expect(json_response['title']).to eq('a new ticket #c1')
  1081. expect(json_response['customer_id']).to eq(customer.id)
  1082. expect(json_response['updated_by_id']).to eq(customer.id)
  1083. expect(json_response['created_by_id']).to eq(customer.id)
  1084. end
  1085. it 'does ticket create with customer with wrong customer (03.02)' do
  1086. params = {
  1087. title: 'a new ticket #c2',
  1088. state: 'new',
  1089. priority: '2 normal',
  1090. group: ticket_group.name,
  1091. customer_id: agent.id,
  1092. article: {
  1093. content_type: 'text/plain', # or text/html
  1094. body: 'some body',
  1095. sender: 'System',
  1096. },
  1097. }
  1098. authenticated_as(customer)
  1099. post '/api/v1/tickets', params: params, as: :json
  1100. expect(response).to have_http_status(:created)
  1101. expect(json_response).to be_a(Hash)
  1102. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1103. expect(json_response['title']).to eq('a new ticket #c2')
  1104. expect(json_response['customer_id']).to eq(customer.id)
  1105. expect(json_response['updated_by_id']).to eq(customer.id)
  1106. expect(json_response['created_by_id']).to eq(customer.id)
  1107. end
  1108. it 'does ticket create with customer with wrong customer hash (03.03)' do
  1109. params = {
  1110. title: 'a new ticket #c2',
  1111. state: 'new',
  1112. priority: '2 normal',
  1113. group: ticket_group.name,
  1114. customer: {
  1115. firstname: agent.firstname,
  1116. lastname: agent.lastname,
  1117. email: agent.email,
  1118. },
  1119. article: {
  1120. content_type: 'text/plain', # or text/html
  1121. body: 'some body',
  1122. sender: 'System',
  1123. },
  1124. }
  1125. authenticated_as(customer)
  1126. post '/api/v1/tickets', params: params, as: :json
  1127. expect(response).to have_http_status(:created)
  1128. expect(json_response).to be_a(Hash)
  1129. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1130. expect(json_response['title']).to eq('a new ticket #c2')
  1131. expect(json_response['customer_id']).to eq(customer.id)
  1132. expect(json_response['updated_by_id']).to eq(customer.id)
  1133. expect(json_response['created_by_id']).to eq(customer.id)
  1134. end
  1135. it 'does ticket with wrong ticket id (03.04)' do
  1136. ticket = create(
  1137. :ticket,
  1138. title: 'ticket with wrong ticket id',
  1139. group: ticket_group,
  1140. customer_id: agent.id,
  1141. )
  1142. authenticated_as(customer)
  1143. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1144. expect(response).to have_http_status(:forbidden)
  1145. expect(json_response).to be_a(Hash)
  1146. expect(json_response['error']).to eq('Not authorized')
  1147. params = {
  1148. title: 'ticket with wrong ticket id - 2',
  1149. }
  1150. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1151. expect(response).to have_http_status(:forbidden)
  1152. expect(json_response).to be_a(Hash)
  1153. expect(json_response['error']).to eq('Not authorized')
  1154. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1155. expect(response).to have_http_status(:forbidden)
  1156. expect(json_response).to be_a(Hash)
  1157. expect(json_response['error']).to eq('Not authorized')
  1158. end
  1159. it 'does ticket with correct ticket id (03.05)', performs_jobs: true do
  1160. title = "ticket with corret ticket id testme#{SecureRandom.uuid}"
  1161. ticket = create(
  1162. :ticket,
  1163. title: title,
  1164. group: ticket_group,
  1165. customer_id: customer.id,
  1166. )
  1167. authenticated_as(customer)
  1168. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1169. expect(response).to have_http_status(:ok)
  1170. expect(json_response).to be_a(Hash)
  1171. expect(json_response['id']).to eq(ticket.id)
  1172. expect(json_response['title']).to eq(title)
  1173. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1174. expect(json_response['updated_by_id']).to eq(1)
  1175. expect(json_response['created_by_id']).to eq(1)
  1176. params = {
  1177. title: "#{title} - 2",
  1178. customer_id: agent.id,
  1179. }
  1180. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1181. expect(response).to have_http_status(:ok)
  1182. expect(json_response).to be_a(Hash)
  1183. expect(json_response['id']).to eq(ticket.id)
  1184. expect(json_response['title']).to eq("#{title} - 2")
  1185. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1186. expect(json_response['updated_by_id']).to eq(customer.id)
  1187. expect(json_response['created_by_id']).to eq(1)
  1188. params = {
  1189. ticket_id: ticket.id,
  1190. subject: 'some subject',
  1191. body: 'some body',
  1192. }
  1193. post '/api/v1/ticket_articles', params: params, as: :json
  1194. expect(response).to have_http_status(:created)
  1195. article_json_response = json_response
  1196. expect(article_json_response).to be_a(Hash)
  1197. expect(article_json_response['ticket_id']).to eq(ticket.id)
  1198. expect(article_json_response['from']).to eq('Tickets Customer1')
  1199. expect(article_json_response['subject']).to eq('some subject')
  1200. expect(article_json_response['body']).to eq('some body')
  1201. expect(article_json_response['content_type']).to eq('text/plain')
  1202. expect(article_json_response['created_by_id']).to eq(customer.id)
  1203. expect(article_json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1204. expect(article_json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  1205. perform_enqueued_jobs
  1206. get "/api/v1/tickets/search?query=#{CGI.escape(title)}", params: {}, as: :json
  1207. expect(response).to have_http_status(:ok)
  1208. expect(json_response).to be_a(Hash)
  1209. expect(json_response['tickets'][0]).to eq(ticket.id)
  1210. expect(json_response['tickets_count']).to eq(1)
  1211. params = {
  1212. condition: {
  1213. 'ticket.title' => {
  1214. operator: 'contains',
  1215. value: title,
  1216. },
  1217. },
  1218. }
  1219. post '/api/v1/tickets/search', params: params, as: :json
  1220. expect(response).to have_http_status(:ok)
  1221. expect(json_response).to be_a(Hash)
  1222. expect(json_response['tickets'][0]).to eq(ticket.id)
  1223. expect(json_response['tickets_count']).to eq(1)
  1224. delete "/api/v1/ticket_articles/#{article_json_response['id']}", params: {}, as: :json
  1225. expect(response).to have_http_status(:forbidden)
  1226. expect(json_response).to be_a(Hash)
  1227. expect(json_response['error']).to eq('Not authorized (agent permission required)!')
  1228. params = {
  1229. ticket_id: ticket.id,
  1230. subject: 'some subject',
  1231. body: 'some body',
  1232. type: 'email',
  1233. sender: 'Agent',
  1234. }
  1235. post '/api/v1/ticket_articles', params: params, as: :json
  1236. expect(response).to have_http_status(:created)
  1237. expect(json_response).to be_a(Hash)
  1238. expect(json_response['ticket_id']).to eq(ticket.id)
  1239. expect(json_response['from']).to eq('Tickets Customer1')
  1240. expect(json_response['subject']).to eq('some subject')
  1241. expect(json_response['body']).to eq('some body')
  1242. expect(json_response['content_type']).to eq('text/plain')
  1243. expect(json_response['created_by_id']).to eq(customer.id)
  1244. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1245. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  1246. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  1247. expect(response).to have_http_status(:forbidden)
  1248. expect(json_response).to be_a(Hash)
  1249. expect(json_response['error']).to eq('Not authorized (agent permission required)!')
  1250. params = {
  1251. from: 'something which should not be changed on server side',
  1252. ticket_id: ticket.id,
  1253. subject: 'some subject',
  1254. body: 'some body',
  1255. type: 'web',
  1256. sender: 'Agent',
  1257. internal: true,
  1258. }
  1259. post '/api/v1/ticket_articles', params: params, as: :json
  1260. expect(response).to have_http_status(:created)
  1261. expect(json_response).to be_a(Hash)
  1262. expect(json_response['ticket_id']).to eq(ticket.id)
  1263. expect(json_response['from']).to eq('Tickets Customer1 <tickets-customer1@example.com>')
  1264. expect(json_response['subject']).to eq('some subject')
  1265. expect(json_response['body']).to eq('some body')
  1266. expect(json_response['content_type']).to eq('text/plain')
  1267. expect(json_response['internal']).to be(false)
  1268. expect(json_response['created_by_id']).to eq(customer.id)
  1269. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1270. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'web').id)
  1271. params = {
  1272. subject: 'new subject',
  1273. }
  1274. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  1275. expect(response).to have_http_status(:forbidden)
  1276. expect(json_response).to be_a(Hash)
  1277. expect(json_response['error']).to eq('Not authorized')
  1278. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1279. expect(response).to have_http_status(:forbidden)
  1280. expect(json_response).to be_a(Hash)
  1281. expect(json_response['error']).to eq('Not authorized (admin permission required)!')
  1282. end
  1283. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (03.6)' do
  1284. authenticated_as(customer)
  1285. params = {
  1286. title: 'a new ticket #3.6',
  1287. group: ticket_group.name,
  1288. customer: {
  1289. firstname: 'some firstname',
  1290. lastname: 'some lastname',
  1291. email: 'some_new_customer@example.com',
  1292. },
  1293. article: {
  1294. body: 'some test 123',
  1295. origin_by: agent.login,
  1296. },
  1297. }
  1298. post '/api/v1/tickets', params: params, as: :json
  1299. expect(response).to have_http_status(:created)
  1300. expect(json_response).to be_a(Hash)
  1301. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1302. expect(json_response['title']).to eq('a new ticket #3.6')
  1303. expect(json_response['customer_id']).to eq(customer.id)
  1304. expect(json_response['updated_by_id']).to eq(customer.id)
  1305. expect(json_response['created_by_id']).to eq(customer.id)
  1306. ticket = Ticket.find(json_response['id'])
  1307. article = ticket.articles.first
  1308. expect(article.updated_by_id).to eq(customer.id)
  1309. expect(article.created_by_id).to eq(customer.id)
  1310. expect(article.origin_by_id).to eq(customer.id)
  1311. expect(article.sender.name).to eq('Customer')
  1312. expect(article.type.name).to eq('note')
  1313. expect(article.from).to eq('Tickets Customer1')
  1314. end
  1315. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (03.6) - second test' do
  1316. authenticated_as(customer)
  1317. params = {
  1318. title: 'a new ticket #3.6.1',
  1319. group: ticket_group.name,
  1320. customer: {
  1321. firstname: 'some firstname',
  1322. lastname: 'some lastname',
  1323. email: 'some_new_customer@example.com',
  1324. },
  1325. article: {
  1326. sender: 'Agent',
  1327. body: 'some test 123',
  1328. origin_by_id: agent.id,
  1329. },
  1330. }
  1331. post '/api/v1/tickets', params: params, as: :json
  1332. expect(response).to have_http_status(:created)
  1333. expect(json_response).to be_a(Hash)
  1334. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1335. expect(json_response['title']).to eq('a new ticket #3.6.1')
  1336. expect(json_response['customer_id']).to eq(customer.id)
  1337. expect(json_response['updated_by_id']).to eq(customer.id)
  1338. expect(json_response['created_by_id']).to eq(customer.id)
  1339. ticket = Ticket.find(json_response['id'])
  1340. article = ticket.articles.first
  1341. expect(article.updated_by_id).to eq(customer.id)
  1342. expect(article.created_by_id).to eq(customer.id)
  1343. expect(article.origin_by_id).to eq(customer.id)
  1344. expect(article.sender.name).to eq('Customer')
  1345. expect(article.type.name).to eq('note')
  1346. expect(article.from).to eq('Tickets Customer1')
  1347. end
  1348. it 'does ticket show and response format (04.01)' do
  1349. title = "ticket testagent#{SecureRandom.uuid}"
  1350. ticket = create(
  1351. :ticket,
  1352. title: title,
  1353. group: ticket_group,
  1354. customer_id: customer.id,
  1355. updated_by_id: agent.id,
  1356. created_by_id: agent.id,
  1357. )
  1358. authenticated_as(agent)
  1359. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1360. expect(response).to have_http_status(:ok)
  1361. expect(json_response).to be_a(Hash)
  1362. expect(json_response['id']).to eq(ticket.id)
  1363. expect(json_response['title']).to eq(ticket.title)
  1364. expect(json_response['group']).to be_falsey
  1365. expect(json_response['priority']).to be_falsey
  1366. expect(json_response['owner']).to be_falsey
  1367. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1368. expect(json_response['updated_by_id']).to eq(agent.id)
  1369. expect(json_response['created_by_id']).to eq(agent.id)
  1370. get "/api/v1/tickets/#{ticket.id}?expand=true", params: {}, as: :json
  1371. expect(response).to have_http_status(:ok)
  1372. expect(json_response).to be_a(Hash)
  1373. expect(json_response['id']).to eq(ticket.id)
  1374. expect(json_response['title']).to eq(ticket.title)
  1375. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1376. expect(json_response['group']).to eq(ticket.group.name)
  1377. expect(json_response['priority']).to eq(ticket.priority.name)
  1378. expect(json_response['owner']).to eq(ticket.owner.login)
  1379. expect(json_response['updated_by_id']).to eq(agent.id)
  1380. expect(json_response['created_by_id']).to eq(agent.id)
  1381. get "/api/v1/tickets/#{ticket.id}?expand=false", params: {}, as: :json
  1382. expect(response).to have_http_status(:ok)
  1383. expect(json_response).to be_a(Hash)
  1384. expect(json_response['id']).to eq(ticket.id)
  1385. expect(json_response['title']).to eq(ticket.title)
  1386. expect(json_response['group']).to be_falsey
  1387. expect(json_response['priority']).to be_falsey
  1388. expect(json_response['owner']).to be_falsey
  1389. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1390. expect(json_response['updated_by_id']).to eq(agent.id)
  1391. expect(json_response['created_by_id']).to eq(agent.id)
  1392. get "/api/v1/tickets/#{ticket.id}?full=true", params: {}, as: :json
  1393. expect(response).to have_http_status(:ok)
  1394. expect(json_response).to be_a(Hash)
  1395. expect(json_response['id']).to eq(ticket.id)
  1396. expect(json_response['assets']).to be_truthy
  1397. expect(json_response['assets']['Ticket']).to be_truthy
  1398. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1399. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1400. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(ticket.title)
  1401. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1402. expect(json_response['assets']['User']).to be_truthy
  1403. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1404. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1405. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1406. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1407. expect(json_response['assets']['User']).to be_truthy
  1408. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1409. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1410. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1411. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1412. get "/api/v1/tickets/#{ticket.id}?full=false", params: {}, as: :json
  1413. expect(response).to have_http_status(:ok)
  1414. expect(json_response).to be_a(Hash)
  1415. expect(json_response['id']).to eq(ticket.id)
  1416. expect(json_response['title']).to eq(ticket.title)
  1417. expect(json_response['group']).to be_falsey
  1418. expect(json_response['priority']).to be_falsey
  1419. expect(json_response['owner']).to be_falsey
  1420. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1421. expect(json_response['updated_by_id']).to eq(agent.id)
  1422. expect(json_response['created_by_id']).to eq(agent.id)
  1423. end
  1424. it 'does ticket index and response format (04.02)' do
  1425. title = "ticket testagent#{SecureRandom.uuid}"
  1426. ticket = create(
  1427. :ticket,
  1428. title: title,
  1429. group: ticket_group,
  1430. customer_id: customer.id,
  1431. updated_by_id: agent.id,
  1432. created_by_id: agent.id,
  1433. )
  1434. authenticated_as(agent)
  1435. get '/api/v1/tickets', params: {}, as: :json
  1436. expect(response).to have_http_status(:ok)
  1437. expect(json_response).to be_a(Array)
  1438. expect(json_response[0]).to be_a(Hash)
  1439. expect(json_response[0]['id']).to eq(1)
  1440. expect(json_response[1]['id']).to eq(ticket.id)
  1441. expect(json_response[1]['title']).to eq(ticket.title)
  1442. expect(json_response[1]['group']).to be_falsey
  1443. expect(json_response[1]['priority']).to be_falsey
  1444. expect(json_response[1]['owner']).to be_falsey
  1445. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1446. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1447. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1448. get '/api/v1/tickets?expand=true', params: {}, as: :json
  1449. expect(response).to have_http_status(:ok)
  1450. expect(json_response).to be_a(Array)
  1451. expect(json_response[0]).to be_a(Hash)
  1452. expect(json_response[0]['id']).to eq(1)
  1453. expect(json_response[1]['id']).to eq(ticket.id)
  1454. expect(json_response[1]['title']).to eq(ticket.title)
  1455. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1456. expect(json_response[1]['group']).to eq(ticket.group.name)
  1457. expect(json_response[1]['priority']).to eq(ticket.priority.name)
  1458. expect(json_response[1]['owner']).to eq(ticket.owner.login)
  1459. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1460. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1461. get '/api/v1/tickets?expand=false', params: {}, as: :json
  1462. expect(response).to have_http_status(:ok)
  1463. expect(json_response).to be_a(Array)
  1464. expect(json_response[0]).to be_a(Hash)
  1465. expect(json_response[0]['id']).to eq(1)
  1466. expect(json_response[1]['id']).to eq(ticket.id)
  1467. expect(json_response[1]['title']).to eq(ticket.title)
  1468. expect(json_response[1]['group']).to be_falsey
  1469. expect(json_response[1]['priority']).to be_falsey
  1470. expect(json_response[1]['owner']).to be_falsey
  1471. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1472. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1473. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1474. get '/api/v1/tickets?full=true', params: {}, as: :json
  1475. expect(response).to have_http_status(:ok)
  1476. expect(json_response).to be_a(Hash)
  1477. expect(json_response['record_ids'].class).to eq(Array)
  1478. expect(json_response['record_ids'][0]).to eq(1)
  1479. expect(json_response['record_ids'][1]).to eq(ticket.id)
  1480. expect(json_response['assets']).to be_truthy
  1481. expect(json_response['assets']['Ticket']).to be_truthy
  1482. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1483. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1484. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(ticket.title)
  1485. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1486. expect(json_response['assets']['User']).to be_truthy
  1487. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1488. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1489. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1490. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1491. expect(json_response['assets']['User']).to be_truthy
  1492. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1493. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1494. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1495. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1496. get '/api/v1/tickets?full=false', params: {}, as: :json
  1497. expect(response).to have_http_status(:ok)
  1498. expect(json_response).to be_a(Array)
  1499. expect(json_response[0]).to be_a(Hash)
  1500. expect(json_response[0]['id']).to eq(1)
  1501. expect(json_response[1]['id']).to eq(ticket.id)
  1502. expect(json_response[1]['title']).to eq(ticket.title)
  1503. expect(json_response[1]['group']).to be_falsey
  1504. expect(json_response[1]['priority']).to be_falsey
  1505. expect(json_response[1]['owner']).to be_falsey
  1506. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1507. expect(json_response[1]['updated_by_id']).to eq(agent.id)
  1508. expect(json_response[1]['created_by_id']).to eq(agent.id)
  1509. end
  1510. it 'does ticket create and response format (04.03)' do
  1511. title = "ticket testagent#{SecureRandom.uuid}"
  1512. params = {
  1513. title: title,
  1514. group: ticket_group.name,
  1515. customer_id: customer.id,
  1516. state: 'new',
  1517. priority: '2 normal',
  1518. article: {
  1519. body: 'some test 123',
  1520. },
  1521. }
  1522. authenticated_as(agent)
  1523. post '/api/v1/tickets', params: params, as: :json
  1524. expect(response).to have_http_status(:created)
  1525. expect(json_response).to be_a(Hash)
  1526. ticket = Ticket.find(json_response['id'])
  1527. expect(json_response['state_id']).to eq(ticket.state_id)
  1528. expect(json_response['state']).to be_falsey
  1529. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1530. expect(json_response['priority']).to be_falsey
  1531. expect(json_response['group_id']).to eq(ticket.group_id)
  1532. expect(json_response['group']).to be_falsey
  1533. expect(json_response['title']).to eq(title)
  1534. expect(json_response['customer_id']).to eq(customer.id)
  1535. expect(json_response['updated_by_id']).to eq(agent.id)
  1536. expect(json_response['created_by_id']).to eq(agent.id)
  1537. post '/api/v1/tickets?expand=true', params: params, as: :json
  1538. expect(response).to have_http_status(:created)
  1539. expect(json_response).to be_a(Hash)
  1540. ticket = Ticket.find(json_response['id'])
  1541. expect(json_response['state_id']).to eq(ticket.state_id)
  1542. expect(json_response['state']).to eq(ticket.state.name)
  1543. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1544. expect(json_response['priority']).to eq(ticket.priority.name)
  1545. expect(json_response['group_id']).to eq(ticket.group_id)
  1546. expect(json_response['group']).to eq(ticket.group.name)
  1547. expect(json_response['title']).to eq(title)
  1548. expect(json_response['customer_id']).to eq(customer.id)
  1549. expect(json_response['updated_by_id']).to eq(agent.id)
  1550. expect(json_response['created_by_id']).to eq(agent.id)
  1551. post '/api/v1/tickets?full=true', params: params, as: :json
  1552. expect(response).to have_http_status(:created)
  1553. expect(json_response).to be_a(Hash)
  1554. ticket = Ticket.find(json_response['id'])
  1555. expect(json_response['assets']).to be_truthy
  1556. expect(json_response['assets']['Ticket']).to be_truthy
  1557. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1558. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1559. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(title)
  1560. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1561. expect(json_response['assets']['User']).to be_truthy
  1562. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1563. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1564. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1565. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1566. expect(json_response['assets']['User']).to be_truthy
  1567. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1568. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1569. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1570. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1571. end
  1572. it 'does ticket update and response formats (04.04)' do
  1573. title = "ticket testagent#{SecureRandom.uuid}"
  1574. ticket = create(
  1575. :ticket,
  1576. title: title,
  1577. group: ticket_group,
  1578. customer_id: customer.id,
  1579. updated_by_id: agent.id,
  1580. created_by_id: agent.id,
  1581. )
  1582. params = {
  1583. title: 'a update ticket #1',
  1584. }
  1585. authenticated_as(agent)
  1586. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1587. expect(response).to have_http_status(:ok)
  1588. expect(json_response).to be_a(Hash)
  1589. ticket = Ticket.find(json_response['id'])
  1590. expect(json_response['state_id']).to eq(ticket.state_id)
  1591. expect(json_response['state']).to be_falsey
  1592. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1593. expect(json_response['priority']).to be_falsey
  1594. expect(json_response['group_id']).to eq(ticket.group_id)
  1595. expect(json_response['group']).to be_falsey
  1596. expect(json_response['title']).to eq('a update ticket #1')
  1597. expect(json_response['customer_id']).to eq(customer.id)
  1598. expect(json_response['updated_by_id']).to eq(agent.id)
  1599. expect(json_response['created_by_id']).to eq(agent.id)
  1600. params = {
  1601. title: 'a update ticket #2',
  1602. }
  1603. put "/api/v1/tickets/#{ticket.id}?expand=true", params: params, as: :json
  1604. expect(response).to have_http_status(:ok)
  1605. expect(json_response).to be_a(Hash)
  1606. ticket = Ticket.find(json_response['id'])
  1607. expect(json_response['state_id']).to eq(ticket.state_id)
  1608. expect(json_response['state']).to eq(ticket.state.name)
  1609. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1610. expect(json_response['priority']).to eq(ticket.priority.name)
  1611. expect(json_response['group_id']).to eq(ticket.group_id)
  1612. expect(json_response['group']).to eq(ticket.group.name)
  1613. expect(json_response['title']).to eq('a update ticket #2')
  1614. expect(json_response['customer_id']).to eq(customer.id)
  1615. expect(json_response['updated_by_id']).to eq(agent.id)
  1616. expect(json_response['created_by_id']).to eq(agent.id)
  1617. params = {
  1618. title: 'a update ticket #3',
  1619. }
  1620. put "/api/v1/tickets/#{ticket.id}?full=true", params: params, as: :json
  1621. expect(response).to have_http_status(:ok)
  1622. expect(json_response).to be_a(Hash)
  1623. ticket = Ticket.find(json_response['id'])
  1624. expect(json_response['assets']).to be_truthy
  1625. expect(json_response['assets']['Ticket']).to be_truthy
  1626. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1627. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1628. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq('a update ticket #3')
  1629. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1630. expect(json_response['assets']['User']).to be_truthy
  1631. expect(json_response['assets']['User'][agent.id.to_s]).to be_truthy
  1632. expect(json_response['assets']['User'][agent.id.to_s]['id']).to eq(agent.id)
  1633. expect(json_response['assets']['User'][agent.id.to_s]['firstname']).to eq(agent.firstname)
  1634. expect(json_response['assets']['User'][agent.id.to_s]['lastname']).to eq(agent.lastname)
  1635. expect(json_response['assets']['User']).to be_truthy
  1636. expect(json_response['assets']['User'][customer.id.to_s]).to be_truthy
  1637. expect(json_response['assets']['User'][customer.id.to_s]['id']).to eq(customer.id)
  1638. expect(json_response['assets']['User'][customer.id.to_s]['firstname']).to eq(customer.firstname)
  1639. expect(json_response['assets']['User'][customer.id.to_s]['lastname']).to eq(customer.lastname)
  1640. # it should be not possible to modify the ticket number
  1641. expected_ticket_number = ticket.number
  1642. params = {
  1643. title: 'a update ticket #4',
  1644. number: '77777',
  1645. }
  1646. put "/api/v1/tickets/#{ticket.id}?full=true", params: params, as: :json
  1647. expect(response).to have_http_status(:ok)
  1648. expect(json_response).to be_a(Hash)
  1649. ticket = Ticket.find(json_response['id'])
  1650. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq('a update ticket #4')
  1651. expect(json_response['assets']['Ticket'][ticket.id.to_s]['number']).to eq(expected_ticket_number)
  1652. end
  1653. it 'does ticket update with empty article param' do
  1654. title = 'a new ticket'
  1655. ticket = create(
  1656. :ticket,
  1657. title: title,
  1658. group: ticket_group,
  1659. customer_id: customer.id,
  1660. updated_by_id: agent.id,
  1661. created_by_id: agent.id,
  1662. )
  1663. params = {
  1664. state: Ticket::State.lookup(name: 'close'),
  1665. article: {}
  1666. }
  1667. authenticated_as(agent)
  1668. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1669. expect(response).to have_http_status(:ok)
  1670. expect(json_response).to be_a(Hash)
  1671. expect(json_response['state_id']).to eq(ticket.state_id)
  1672. expect(json_response['state']).to be_falsey
  1673. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1674. expect(json_response['priority']).to be_falsey
  1675. expect(json_response['group_id']).to eq(ticket.group_id)
  1676. expect(json_response['group']).to be_falsey
  1677. expect(json_response['customer_id']).to eq(customer.id)
  1678. expect(json_response['updated_by_id']).to eq(agent.id)
  1679. expect(json_response['created_by_id']).to eq(agent.id)
  1680. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1681. expect(json_response['title']).to eq(ticket.title)
  1682. expect(ticket.articles.count).to eq(0)
  1683. end
  1684. it 'does ticket split with html - check attachments (05.01)' do
  1685. ticket = create(
  1686. :ticket,
  1687. title: 'some title',
  1688. group: ticket_group,
  1689. customer_id: customer.id,
  1690. updated_by_id: agent.id,
  1691. created_by_id: agent.id,
  1692. )
  1693. article = create(
  1694. :ticket_article,
  1695. type: Ticket::Article::Type.lookup(name: 'note'),
  1696. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1697. body: '<b>test</b> <img src="cid:15.274327094.140938@ZAMMAD.example.com"/> test <img src="cid:15.274327094.140938.3@ZAMMAD.example.com"/>',
  1698. content_type: 'text/html',
  1699. ticket_id: ticket.id,
  1700. )
  1701. create(:store,
  1702. object: 'Ticket::Article',
  1703. o_id: article.id,
  1704. data: 'content_file1_normally_should_be_an_image',
  1705. filename: 'some_file1.jpg',
  1706. preferences: {
  1707. 'Content-Type' => 'image/jpeg',
  1708. 'Mime-Type' => 'image/jpeg',
  1709. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  1710. 'Content-Disposition' => 'inline',
  1711. })
  1712. create(:store,
  1713. object: 'Ticket::Article',
  1714. o_id: article.id,
  1715. data: 'content_file2_normally_should_be_an_image',
  1716. filename: 'some_file2.jpg',
  1717. preferences: {
  1718. 'Content-Type' => 'image/jpeg',
  1719. 'Mime-Type' => 'image/jpeg',
  1720. 'Content-ID' => '15.274327094.140938.2@zammad.example.com',
  1721. 'Content-Disposition' => 'inline',
  1722. })
  1723. create(:store,
  1724. object: 'Ticket::Article',
  1725. o_id: article.id,
  1726. data: 'content_file3_normally_should_be_an_image',
  1727. filename: 'some_file3.jpg',
  1728. preferences: {
  1729. 'Content-Type' => 'image/jpeg',
  1730. 'Mime-Type' => 'image/jpeg',
  1731. 'Content-ID' => '15.274327094.140938.3@zammad.example.com',
  1732. })
  1733. create(:store,
  1734. object: 'Ticket::Article',
  1735. o_id: article.id,
  1736. data: 'content_file4_normally_should_be_an_image',
  1737. filename: 'some_file4.jpg',
  1738. preferences: {
  1739. 'Content-Type' => 'image/jpeg',
  1740. 'Mime-Type' => 'image/jpeg',
  1741. 'Content-ID' => '15.274327094.140938.4@zammad.example.com',
  1742. })
  1743. create(:store,
  1744. object: 'Ticket::Article',
  1745. o_id: article.id,
  1746. data: 'content_file1_normally_should_be_an_pdf',
  1747. filename: 'Rechnung_RE-2018-200.pdf',
  1748. preferences: {
  1749. 'Content-Type' => 'application/octet-stream; name="Rechnung_RE-2018-200.pdf"',
  1750. 'Mime-Type' => 'application/octet-stream',
  1751. 'Content-ID' => '8AB0BEC88984EE4EBEF643C79C8E0346@zammad.example.com',
  1752. 'Content-Description' => 'Rechnung_RE-2018-200.pdf',
  1753. 'Content-Disposition' => 'attachment',
  1754. })
  1755. authenticated_as(customer)
  1756. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1757. expect(response).to have_http_status(:forbidden)
  1758. authenticated_as(agent)
  1759. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1760. expect(response).to have_http_status(:ok)
  1761. expect(json_response).to be_a(Hash)
  1762. expect(json_response['assets']).to be_truthy
  1763. expect(json_response['assets']['Ticket']).to be_truthy
  1764. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1765. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1766. expect(json_response['attachments']).to be_truthy
  1767. expect(json_response['attachments'].count).to eq(3)
  1768. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1769. expect(response).to have_http_status(:ok)
  1770. expect(json_response).to be_a(Hash)
  1771. expect(json_response['assets']).to be_truthy
  1772. expect(json_response['assets']['Ticket']).to be_truthy
  1773. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1774. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1775. expect(json_response['attachments']).to be_truthy
  1776. expect(json_response['attachments'].count).to eq(0)
  1777. end
  1778. it 'does ticket split with plain - check attachments (05.02)' do
  1779. ticket = create(
  1780. :ticket,
  1781. title: 'some title',
  1782. group: ticket_group,
  1783. customer_id: customer.id,
  1784. updated_by_id: agent.id,
  1785. created_by_id: agent.id,
  1786. )
  1787. article = create(
  1788. :ticket_article,
  1789. type: Ticket::Article::Type.lookup(name: 'note'),
  1790. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1791. body: '<b>test</b> <img src="cid:15.274327094.140938@zammad.example.com"/>',
  1792. content_type: 'text/plain',
  1793. ticket_id: ticket.id,
  1794. )
  1795. create(:store,
  1796. object: 'Ticket::Article',
  1797. o_id: article.id,
  1798. data: 'content_file1_normally_should_be_an_image',
  1799. filename: 'some_file1.jpg',
  1800. preferences: {
  1801. 'Content-Type' => 'image/jpeg',
  1802. 'Mime-Type' => 'image/jpeg',
  1803. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  1804. 'Content-Disposition' => 'inline',
  1805. })
  1806. create(:store,
  1807. object: 'Ticket::Article',
  1808. o_id: article.id,
  1809. data: 'content_file1_normally_should_be_an_image',
  1810. filename: 'some_file2.jpg',
  1811. preferences: {
  1812. 'Content-Type' => 'image/jpeg',
  1813. 'Mime-Type' => 'image/jpeg',
  1814. 'Content-ID' => '15.274327094.140938.2@zammad.example.com',
  1815. 'Content-Disposition' => 'inline',
  1816. })
  1817. create(:store,
  1818. object: 'Ticket::Article',
  1819. o_id: article.id,
  1820. data: 'content_file1_normally_should_be_an_pdf',
  1821. filename: 'Rechnung_RE-2018-200.pdf',
  1822. preferences: {
  1823. 'Content-Type' => 'application/octet-stream; name="Rechnung_RE-2018-200.pdf"',
  1824. 'Mime-Type' => 'application/octet-stream',
  1825. 'Content-ID' => '8AB0BEC88984EE4EBEF643C79C8E0346@zammad.example.com',
  1826. 'Content-Description' => 'Rechnung_RE-2018-200.pdf',
  1827. 'Content-Disposition' => 'attachment',
  1828. })
  1829. authenticated_as(agent)
  1830. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1831. expect(response).to have_http_status(:ok)
  1832. expect(json_response).to be_a(Hash)
  1833. expect(json_response['assets']).to be_truthy
  1834. expect(json_response['assets']['Ticket']).to be_truthy
  1835. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1836. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1837. expect(json_response['attachments']).to be_truthy
  1838. expect(json_response['attachments'].count).to eq(3)
  1839. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=a464a40c-e84e-42d7-ab6e-5daea2bc1502", params: {}, as: :json
  1840. expect(response).to have_http_status(:ok)
  1841. expect(json_response).to be_a(Hash)
  1842. expect(json_response['assets']).to be_truthy
  1843. expect(json_response['assets']['Ticket']).to be_truthy
  1844. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1845. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1846. expect(json_response['attachments']).to be_truthy
  1847. expect(json_response['attachments'].count).to eq(0)
  1848. end
  1849. it 'does ticket merge (07.01)' do
  1850. group_no_permission = create(:group)
  1851. ticket1 = create(
  1852. :ticket,
  1853. title: 'ticket merge1',
  1854. group: ticket_group,
  1855. customer_id: customer.id,
  1856. )
  1857. ticket2 = create(
  1858. :ticket,
  1859. title: 'ticket merge2',
  1860. group: ticket_group,
  1861. customer_id: customer.id,
  1862. )
  1863. ticket3 = create(
  1864. :ticket,
  1865. title: 'ticket merge2',
  1866. group: group_no_permission,
  1867. customer_id: customer.id,
  1868. )
  1869. authenticated_as(customer)
  1870. put "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
  1871. expect(response).to have_http_status(:forbidden)
  1872. authenticated_as(agent)
  1873. put "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
  1874. expect(response).to have_http_status(:ok)
  1875. expect(json_response).to be_a(Hash)
  1876. expect(json_response['result']).to eq('failed')
  1877. expect(json_response['message']).to eq('The target ticket number could not be found.')
  1878. put "/api/v1/ticket_merge/#{ticket3.id}/#{ticket1.number}", params: {}, as: :json
  1879. expect(response).to have_http_status(:forbidden)
  1880. expect(json_response).to be_a(Hash)
  1881. expect(json_response['error']).to eq('Not authorized')
  1882. expect(json_response['error_human']).to eq('Not authorized')
  1883. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket3.number}", params: {}, as: :json
  1884. expect(response).to have_http_status(:forbidden)
  1885. expect(json_response).to be_a(Hash)
  1886. expect(json_response['error']).to eq('Not authorized')
  1887. expect(json_response['error_human']).to eq('Not authorized')
  1888. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket2.number}", params: {}, as: :json
  1889. expect(response).to have_http_status(:ok)
  1890. expect(json_response).to be_a(Hash)
  1891. expect(json_response['result']).to eq('success')
  1892. expect(json_response['target_ticket']['id']).to eq(ticket2.id)
  1893. end
  1894. it 'does ticket merge - change permission (07.02)' do
  1895. group_change_permission = Group.create!(
  1896. name: 'GroupWithChangePermission',
  1897. active: true,
  1898. updated_by_id: 1,
  1899. created_by_id: 1,
  1900. )
  1901. ticket1 = create(
  1902. :ticket,
  1903. title: 'ticket merge1',
  1904. group: group_change_permission,
  1905. customer_id: customer.id,
  1906. )
  1907. ticket2 = create(
  1908. :ticket,
  1909. title: 'ticket merge2',
  1910. group: group_change_permission,
  1911. customer_id: customer.id,
  1912. )
  1913. agent.group_names_access_map = { group_change_permission.name => %w[read change] }
  1914. authenticated_as(agent)
  1915. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket2.number}", params: {}, as: :json
  1916. expect(response).to have_http_status(:ok)
  1917. expect(json_response).to be_a(Hash)
  1918. expect(json_response['result']).to eq('success')
  1919. expect(json_response['target_ticket']['id']).to eq(ticket2.id)
  1920. end
  1921. it 'does ticket search sorted (08.01)' do
  1922. title = "ticket pagination #{SecureRandom.uuid}"
  1923. ticket1 = create(
  1924. :ticket,
  1925. title: "#{title} A",
  1926. group: ticket_group,
  1927. customer_id: customer.id,
  1928. created_at: '2018-02-05 17:42:00',
  1929. updated_at: '2018-02-05 20:42:00',
  1930. )
  1931. create(
  1932. :ticket_article,
  1933. type: Ticket::Article::Type.lookup(name: 'note'),
  1934. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1935. ticket_id: ticket1.id,
  1936. )
  1937. ticket2 = create(
  1938. :ticket,
  1939. title: "#{title} B",
  1940. group: ticket_group,
  1941. customer_id: customer.id,
  1942. state: Ticket::State.lookup(name: 'new'),
  1943. priority: Ticket::Priority.lookup(name: '3 hoch'),
  1944. created_at: '2018-02-05 19:42:00',
  1945. updated_at: '2018-02-05 19:42:00',
  1946. )
  1947. create(
  1948. :ticket_article,
  1949. type: Ticket::Article::Type.lookup(name: 'note'),
  1950. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1951. ticket_id: ticket2.id,
  1952. )
  1953. authenticated_as(admin)
  1954. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: {}, as: :json
  1955. expect(response).to have_http_status(:ok)
  1956. expect(json_response).to be_a(Hash)
  1957. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1958. authenticated_as(admin)
  1959. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'created_at', order_by: 'asc' }, as: :json
  1960. expect(response).to have_http_status(:ok)
  1961. expect(json_response).to be_a(Hash)
  1962. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1963. authenticated_as(admin)
  1964. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'title', order_by: 'asc' }, as: :json
  1965. expect(response).to have_http_status(:ok)
  1966. expect(json_response).to be_a(Hash)
  1967. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1968. authenticated_as(admin)
  1969. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'title', order_by: 'desc' }, as: :json
  1970. expect(response).to have_http_status(:ok)
  1971. expect(json_response).to be_a(Hash)
  1972. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1973. authenticated_as(admin)
  1974. 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
  1975. expect(response).to have_http_status(:ok)
  1976. expect(json_response).to be_a(Hash)
  1977. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1978. authenticated_as(admin)
  1979. 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
  1980. expect(response).to have_http_status(:ok)
  1981. expect(json_response).to be_a(Hash)
  1982. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1983. end
  1984. it 'does ticket history' do
  1985. ticket1 = create(
  1986. :ticket,
  1987. title: 'some title',
  1988. group: ticket_group,
  1989. customer_id: customer.id,
  1990. )
  1991. create(
  1992. :ticket_article,
  1993. type: Ticket::Article::Type.lookup(name: 'note'),
  1994. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1995. ticket_id: ticket1.id,
  1996. )
  1997. authenticated_as(agent)
  1998. get "/api/v1/ticket_history/#{ticket1.id}", params: {}, as: :json
  1999. expect(response).to have_http_status(:ok)
  2000. expect(json_response).to be_a(Hash)
  2001. expect(json_response['history'].class).to eq(Array)
  2002. expect(json_response['assets'].class).to eq(Hash)
  2003. expect(json_response['assets']['User'][customer.id.to_s]).not_to be_nil
  2004. expect(json_response['assets']['Ticket'][ticket1.id.to_s]).not_to be_nil
  2005. authenticated_as(customer)
  2006. get "/api/v1/ticket_history/#{ticket1.id}", params: {}, as: :json
  2007. expect(response).to have_http_status(:forbidden)
  2008. end
  2009. it 'does ticket related' do
  2010. ticket1 = create(
  2011. :ticket,
  2012. title: 'some title',
  2013. group: ticket_group,
  2014. customer_id: customer.id,
  2015. )
  2016. authenticated_as(agent)
  2017. get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
  2018. expect(response).to have_http_status(:ok)
  2019. authenticated_as(customer)
  2020. get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
  2021. expect(response).to have_http_status(:forbidden)
  2022. end
  2023. it 'does ticket recent' do
  2024. authenticated_as(agent)
  2025. get '/api/v1/ticket_recent', params: {}, as: :json
  2026. expect(response).to have_http_status(:ok)
  2027. authenticated_as(customer)
  2028. get '/api/v1/ticket_recent', params: {}, as: :json
  2029. expect(response).to have_http_status(:forbidden)
  2030. end
  2031. end
  2032. describe 'mentions' do
  2033. let(:user1) { create(:agent, groups: [ticket_group]) }
  2034. let(:user2) { create(:agent, groups: [ticket_group]) }
  2035. let(:user3) { create(:agent, groups: [ticket_group]) }
  2036. def new_ticket_with_mentions(*user_ids)
  2037. params = {
  2038. title: 'a new ticket #11',
  2039. group: ticket_group.name,
  2040. customer: {
  2041. firstname: 'some firstname',
  2042. lastname: 'some lastname',
  2043. email: 'some_new_customer@example.com',
  2044. },
  2045. article: {
  2046. body: 'some test 123',
  2047. },
  2048. mentions: user_ids
  2049. }
  2050. authenticated_as(agent)
  2051. post '/api/v1/tickets', params: params, as: :json
  2052. json_response
  2053. end
  2054. it 'create ticket with mentions' do
  2055. new_ticket_with_mentions(user1.id, user2.id, user3.id)
  2056. expect(response).to have_http_status(:created)
  2057. expect(Mention.count).to eq(3)
  2058. end
  2059. it 'create ticket with one of mentions being invalid' do
  2060. new_ticket_with_mentions(user1.id, user2.id, create(:customer).id)
  2061. expect(response).to have_http_status(:unprocessable_entity)
  2062. expect(Mention.count).to eq(0)
  2063. end
  2064. it 'check ticket get' do
  2065. ticket = new_ticket_with_mentions(user1.id, user2.id, user3.id)
  2066. get "/api/v1/tickets/#{ticket['id']}?all=true", params: {}, as: :json
  2067. expect(response).to have_http_status(:ok)
  2068. expect(json_response['mentions'].count).to eq(3)
  2069. expect(json_response['assets']['Mention'].count).to eq(3)
  2070. end
  2071. end
  2072. describe 'stats' do
  2073. let(:ticket1) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  2074. let(:ticket2) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  2075. let(:ticket3) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  2076. let(:customer) { create(:customer, organization: organization) }
  2077. let(:organization) { create(:organization, shared: false) }
  2078. before do
  2079. authenticated_as(admin)
  2080. ticket1
  2081. travel 2.minutes
  2082. ticket2
  2083. travel 2.minutes
  2084. ticket3
  2085. travel 2.minutes
  2086. ticket2.touch
  2087. end
  2088. # https://github.com/zammad/zammad/issues/2296
  2089. it 'orders tickets by created_at desc (#2296)' do
  2090. get '/api/v1/ticket_stats', params: { organization_id: organization.id, user_id: customer.id }, as: :json
  2091. expect(response).to have_http_status(:ok)
  2092. expect(json_response)
  2093. .to be_a(Hash)
  2094. .and include('user' => hash_including('open_ids' => [ticket3.id, ticket2.id, ticket1.id]))
  2095. .and include('organization' => hash_including('open_ids' => [ticket3.id, ticket2.id, ticket1.id]))
  2096. end
  2097. end
  2098. describe '/api/v1/tickets' do
  2099. subject(:ticket) { create(:ticket, state_name: 'closed') }
  2100. let(:admin) { create(:admin, groups: [ticket.group]) }
  2101. let(:agent) { create(:agent, groups: [ticket.group]) }
  2102. let(:customer) { ticket.customer }
  2103. describe 'reopening a ticket' do
  2104. shared_examples 'successfully reopen a ticket' do
  2105. it 'succeeds' do
  2106. put "/api/v1/tickets/#{ticket.id}",
  2107. params: { state_id: Ticket::State.find_by(name: 'open').id },
  2108. as: :json
  2109. expect(response).to have_http_status(:ok)
  2110. expect(json_response).to include('state_id' => Ticket::State.find_by(name: 'open').id)
  2111. end
  2112. end
  2113. shared_examples 'fail to reopen a ticket' do
  2114. it 'fails' do
  2115. put "/api/v1/tickets/#{ticket.id}",
  2116. params: { state_id: Ticket::State.find_by(name: 'open').id },
  2117. as: :json
  2118. expect(response).to have_http_status(:unprocessable_entity)
  2119. expect(json_response).to include('error' => 'Cannot follow-up on a closed ticket. Please create a new ticket.')
  2120. end
  2121. end
  2122. context 'when ticket.group.follow_up_possible = "yes"' do
  2123. before { ticket.group.update(follow_up_possible: 'yes') }
  2124. context 'as admin', authenticated_as: -> { admin } do
  2125. include_examples 'successfully reopen a ticket'
  2126. end
  2127. context 'as agent', authenticated_as: -> { agent } do
  2128. include_examples 'successfully reopen a ticket'
  2129. end
  2130. context 'as customer', authenticated_as: -> { customer } do
  2131. include_examples 'successfully reopen a ticket'
  2132. end
  2133. end
  2134. context 'when ticket.group.follow_up_possible = "new_ticket"' do
  2135. before { ticket.group.update(follow_up_possible: 'new_ticket') }
  2136. context 'as admin', authenticated_as: -> { admin } do
  2137. include_examples 'successfully reopen a ticket'
  2138. end
  2139. context 'as agent', authenticated_as: -> { agent } do
  2140. include_examples 'successfully reopen a ticket'
  2141. end
  2142. context 'as customer', authenticated_as: -> { customer } do
  2143. include_examples 'fail to reopen a ticket'
  2144. end
  2145. end
  2146. end
  2147. end
  2148. describe 'GET /api/v1/tickets/:id' do
  2149. subject!(:ticket) { create(:ticket) }
  2150. let(:agent) { create(:agent, groups: [ticket.group]) }
  2151. context 'links present', authenticated_as: -> { agent } do
  2152. before do
  2153. create(:link, from: ticket, to: linked)
  2154. get "/api/v1/tickets/#{ticket.id}", params: { all: 'true' }, as: :json
  2155. end
  2156. let(:linked) { create(:ticket, group: ticket.group) }
  2157. it 'is present in response' do
  2158. expect(response).to have_http_status(:ok)
  2159. expect(json_response['links']).to eq([
  2160. {
  2161. 'link_type' => 'normal',
  2162. 'link_object' => 'Ticket',
  2163. 'link_object_value' => linked.id
  2164. }
  2165. ])
  2166. end
  2167. context 'no permission to linked Ticket Group' do
  2168. let(:linked) { create(:ticket) }
  2169. it 'is not present in response' do
  2170. expect(response).to have_http_status(:ok)
  2171. expect(json_response['links']).to be_blank
  2172. end
  2173. end
  2174. end
  2175. end
  2176. describe 'GET /api/v1/ticket_customer' do
  2177. subject(:ticket) { create(:ticket, customer: customer_authorized) }
  2178. let(:organization_authorized) { create(:organization) }
  2179. let(:customer_authorized) { create(:customer, organization: organization_authorized) }
  2180. let(:organization_unauthorized) { create(:organization) }
  2181. let(:customer_unauthorized) { create(:customer, organization: organization_unauthorized) }
  2182. let(:agent) { create(:agent, groups: [ticket.group]) }
  2183. describe 'listing information' do
  2184. before do
  2185. ticket
  2186. end
  2187. shared_examples 'has access' do
  2188. it 'succeeds' do
  2189. get '/api/v1/ticket_customer',
  2190. params: { customer_id: customer_authorized.id },
  2191. as: :json
  2192. expect(json_response['ticket_ids_open']).to include(ticket.id)
  2193. expect(json_response['ticket_ids_closed']).to be_blank
  2194. end
  2195. end
  2196. shared_examples 'has no access' do
  2197. it 'fails' do
  2198. get '/api/v1/ticket_customer',
  2199. params: { customer_id: customer_authorized.id },
  2200. as: :json
  2201. expect(json_response['ticket_ids_open']).to be_blank
  2202. expect(json_response['ticket_ids_closed']).to be_blank
  2203. expect(json_response['assets']).to be_blank
  2204. end
  2205. end
  2206. context 'as agent', authenticated_as: -> { agent } do
  2207. include_examples 'has access'
  2208. end
  2209. context 'as authorized customer', authenticated_as: -> { customer_authorized } do
  2210. include_examples 'has no access'
  2211. end
  2212. context 'as unauthorized customer', authenticated_as: -> { customer_unauthorized } do
  2213. include_examples 'has no access'
  2214. end
  2215. end
  2216. end
  2217. describe 'Assign user to multiple organizations #1573' do
  2218. let(:organizations) { create_list(:organization, 3) }
  2219. let(:customer) { create(:customer, organization: organizations[0], organizations: organizations[1..]) }
  2220. let(:ticket1) { create(:ticket, customer: customer, organization: organizations[0], group: Group.first) }
  2221. let(:ticket2) { create(:ticket, customer: customer, organization: organizations[1], group: Group.first) }
  2222. before do
  2223. ticket1 && ticket2
  2224. end
  2225. it 'does return multi organization tickets' do
  2226. authenticated_as(agent)
  2227. post '/api/v1/ticket_stats', params: { organization_id: customer.all_organization_ids, user_id: customer.id }, as: :json
  2228. expect(response).to have_http_status(:ok)
  2229. expect(json_response)
  2230. .to be_a(Hash)
  2231. .and include('user' => hash_including('open_ids' => [ticket2.id, ticket1.id]))
  2232. .and include('organization' => hash_including('open_ids' => [ticket2.id, ticket1.id]))
  2233. end
  2234. end
  2235. describe 'Articles are not indexed properly #4596', performs_jobs: true, searchindex: true do
  2236. let(:ticket) { create(:ticket, group: Group.first) }
  2237. def new_article_body
  2238. params = {
  2239. article: {
  2240. body: SecureRandom.uuid,
  2241. content_type: 'text/plain',
  2242. internal: false,
  2243. },
  2244. }
  2245. authenticated_as(agent)
  2246. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  2247. expect(response).to have_http_status(:ok)
  2248. perform_enqueued_jobs
  2249. SearchIndexBackend.refresh
  2250. params[:article][:body]
  2251. end
  2252. def delete_article_body
  2253. article = ticket.articles.last
  2254. delete "/api/v1/ticket_articles/#{article.id}"
  2255. expect(response).to have_http_status(:ok)
  2256. perform_enqueued_jobs
  2257. SearchIndexBackend.refresh
  2258. article.body
  2259. end
  2260. before do
  2261. ticket
  2262. searchindex_model_reload([Ticket])
  2263. end
  2264. it 'does find articles after creations', :aggregate_failures do
  2265. expect(SearchIndexBackend.search(new_article_body, 'Ticket', limit: 1)).to be_present
  2266. expect(SearchIndexBackend.search(new_article_body, 'Ticket', limit: 1)).to be_present
  2267. expect(SearchIndexBackend.search(delete_article_body, 'Ticket', limit: 1)).to be_blank
  2268. end
  2269. end
  2270. describe 'Agent with being "ticket.agent" and "ticket.customer" is creating + updating a ticket', :aggregate_failures, authenticated_as: :user do
  2271. let(:group_only_create) { create(:group) }
  2272. let(:user) { create(:agent_and_customer) }
  2273. before do
  2274. skip 'This test requires some changes to the metadata concerns for the Ticket::Article model which are not done yet.'
  2275. user.group_names_access_map = {
  2276. group_only_create.name => %w[create],
  2277. }
  2278. end
  2279. it 'contains correct information for sender if agent sets himself as customer and responds' do
  2280. params = {
  2281. title: 'Test title for issue #4647',
  2282. group_id: group_only_create.id,
  2283. customer_id: user.id,
  2284. article: {
  2285. type: 'web',
  2286. internal: false,
  2287. sender: 'Customer',
  2288. subject: 'Test subject',
  2289. body: SecureRandom.uuid,
  2290. },
  2291. }
  2292. post '/api/v1/tickets', params: params, as: :json
  2293. expect(response).to have_http_status(:created)
  2294. ticket = Ticket.last
  2295. expect(ticket.customer_id).to eq(user.id)
  2296. expect(ticket.articles.reload.first).to have_attributes(
  2297. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  2298. from: "#{user.fullname} <#{user.email}>",
  2299. )
  2300. response_params = {
  2301. article: {
  2302. body: SecureRandom.uuid,
  2303. },
  2304. }
  2305. put "/api/v1/tickets/#{ticket.id}", params: response_params, as: :json
  2306. expect(response).to have_http_status(:ok)
  2307. expect(ticket.reload.articles.last.sender.name).to eq('Customer')
  2308. end
  2309. end
  2310. describe 'Article contains wrong "origin_by" + "from" information', :aggregate_failures, authenticated_as: :user do
  2311. let(:api_role) do
  2312. role = create(:role, name: 'API', permission_names: ['ticket.agent'])
  2313. role.group_names_access_map = {
  2314. Group.first.name => %w[create change],
  2315. }
  2316. role
  2317. end
  2318. let(:user) { create(:user, roles: [api_role]) }
  2319. it 'contains correct "origin_by" + "from" information' do
  2320. params = {
  2321. title: 'Test title for issue #4647',
  2322. group_id: Group.first.id,
  2323. customer_id: 'guess:dummy@example.com',
  2324. article: {
  2325. type: 'web',
  2326. internal: false,
  2327. sender: 'Customer',
  2328. subject: 'Test subject',
  2329. body: SecureRandom.uuid,
  2330. },
  2331. }
  2332. post '/api/v1/tickets', params: params, as: :json
  2333. expect(response).to have_http_status(:created)
  2334. expect(Ticket.last.articles.first).to have_attributes(
  2335. origin_by_id: User.find_by(email: 'dummy@example.com').id,
  2336. from: 'dummy@example.com',
  2337. )
  2338. end
  2339. end
  2340. describe 'Agents can create new tags even if prohibited by the settings #3501', authenticated_as: :agent do
  2341. let(:tag) { SecureRandom.hex(4) }
  2342. before do
  2343. Setting.set('tag_new', false)
  2344. end
  2345. it 'does create the ticket without tags' do
  2346. params = {
  2347. title: 'a new ticket #3',
  2348. group: Group.first.name,
  2349. priority: '2 normal',
  2350. state: 'new',
  2351. customer_id: customer.id,
  2352. tags: tag,
  2353. }
  2354. post '/api/v1/tickets', params: params, as: :json
  2355. expect(response).to have_http_status(:created)
  2356. expect(Ticket.last.tag_list).to eq([])
  2357. end
  2358. end
  2359. end