ticket_spec.rb 109 KB

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