ticket_spec.rb 104 KB

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