ticket_spec.rb 108 KB

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