ticket_spec.rb 95 KB

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