ticket_spec.rb 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264
  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. params = {
  837. from: 'something which should not be changed on server side',
  838. ticket_id: ticket.id,
  839. subject: 'some subject',
  840. body: 'some body',
  841. type: 'email',
  842. internal: false,
  843. }
  844. post '/api/v1/ticket_articles', params: params, as: :json
  845. expect(response).to have_http_status(:created)
  846. expect(json_response['internal']).to eq(false)
  847. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  848. expect(response).to have_http_status(:unauthorized)
  849. expect(json_response).to be_a_kind_of(Hash)
  850. expect(json_response['error']).to eq('Not authorized (communication articles cannot be deleted)!')
  851. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  852. expect(response).to have_http_status(:unauthorized)
  853. expect(json_response).to be_a_kind_of(Hash)
  854. expect(json_response['error']).to eq('Not authorized (admin permission required)!')
  855. end
  856. it 'does ticket with correct ticket id (02.05)' do
  857. ticket = create(
  858. :ticket,
  859. title: 'ticket with corret ticket id',
  860. group: ticket_group,
  861. customer_id: customer_user.id,
  862. )
  863. authenticated_as(admin_user)
  864. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  865. expect(response).to have_http_status(:ok)
  866. expect(json_response).to be_a_kind_of(Hash)
  867. expect(json_response['id']).to eq(ticket.id)
  868. expect(json_response['title']).to eq('ticket with corret ticket id')
  869. expect(json_response['customer_id']).to eq(ticket.customer_id)
  870. expect(json_response['updated_by_id']).to eq(1)
  871. expect(json_response['created_by_id']).to eq(1)
  872. params = {
  873. title: 'ticket with corret ticket id - 2',
  874. customer_id: agent_user.id,
  875. }
  876. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  877. expect(response).to have_http_status(:ok)
  878. expect(json_response).to be_a_kind_of(Hash)
  879. expect(json_response['id']).to eq(ticket.id)
  880. expect(json_response['title']).to eq('ticket with corret ticket id - 2')
  881. expect(json_response['customer_id']).to eq(agent_user.id)
  882. expect(json_response['updated_by_id']).to eq(admin_user.id)
  883. expect(json_response['created_by_id']).to eq(1)
  884. params = {
  885. from: 'something which should not be changed on server side',
  886. ticket_id: ticket.id,
  887. subject: 'some subject',
  888. body: 'some body',
  889. }
  890. post '/api/v1/ticket_articles', params: params, as: :json
  891. expect(response).to have_http_status(:created)
  892. expect(json_response).to be_a_kind_of(Hash)
  893. expect(json_response['ticket_id']).to eq(ticket.id)
  894. expect(json_response['from']).to eq('Tickets Admin')
  895. expect(json_response['subject']).to eq('some subject')
  896. expect(json_response['body']).to eq('some body')
  897. expect(json_response['content_type']).to eq('text/plain')
  898. expect(json_response['internal']).to eq(false)
  899. expect(json_response['created_by_id']).to eq(admin_user.id)
  900. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  901. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  902. params = {
  903. subject: 'new subject',
  904. internal: true,
  905. }
  906. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  907. expect(response).to have_http_status(:ok)
  908. expect(json_response).to be_a_kind_of(Hash)
  909. expect(json_response['ticket_id']).to eq(ticket.id)
  910. expect(json_response['from']).to eq('Tickets Admin')
  911. expect(json_response['subject']).to eq('new subject')
  912. expect(json_response['body']).to eq('some body')
  913. expect(json_response['content_type']).to eq('text/plain')
  914. expect(json_response['internal']).to eq(true)
  915. expect(json_response['created_by_id']).to eq(admin_user.id)
  916. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  917. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  918. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  919. expect(response).to have_http_status(:ok)
  920. params = {
  921. ticket_id: ticket.id,
  922. subject: 'some subject',
  923. body: 'some body',
  924. type: 'email',
  925. }
  926. post '/api/v1/ticket_articles', params: params, as: :json
  927. expect(response).to have_http_status(:created)
  928. expect(json_response).to be_a_kind_of(Hash)
  929. expect(json_response['ticket_id']).to eq(ticket.id)
  930. expect(json_response['from']).to eq(%("Tickets Admin via #{ticket_group.email_address.realname}" <#{ticket_group.email_address.email}>))
  931. expect(json_response['subject']).to eq('some subject')
  932. expect(json_response['body']).to eq('some body')
  933. expect(json_response['content_type']).to eq('text/plain')
  934. expect(json_response['internal']).to eq(false)
  935. expect(json_response['created_by_id']).to eq(admin_user.id)
  936. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Agent').id)
  937. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'email').id)
  938. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  939. expect(response).to have_http_status(:unauthorized)
  940. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  941. expect(response).to have_http_status(:ok)
  942. end
  943. it 'does ticket pagination (02.05)' do
  944. title = "ticket pagination #{rand(999_999_999)}"
  945. tickets = []
  946. (1..20).each do |count|
  947. ticket = create(
  948. :ticket,
  949. title: "#{title} - #{count}",
  950. group: ticket_group,
  951. customer_id: customer_user.id,
  952. )
  953. create(
  954. :ticket_article,
  955. type: Ticket::Article::Type.lookup(name: 'note'),
  956. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  957. ticket_id: ticket.id,
  958. )
  959. tickets.push ticket
  960. travel 2.seconds
  961. end
  962. authenticated_as(admin_user)
  963. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: {}, as: :json
  964. expect(response).to have_http_status(:ok)
  965. expect(json_response).to be_a_kind_of(Hash)
  966. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  967. expect(json_response['tickets'][19]).to eq(tickets[0].id)
  968. expect(json_response['tickets_count']).to eq(20)
  969. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=10", params: {}, as: :json
  970. expect(response).to have_http_status(:ok)
  971. expect(json_response).to be_a_kind_of(Hash)
  972. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  973. expect(json_response['tickets'][9]).to eq(tickets[10].id)
  974. expect(json_response['tickets_count']).to eq(10)
  975. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=1&per_page=5", params: {}, as: :json
  976. expect(response).to have_http_status(:ok)
  977. expect(json_response).to be_a_kind_of(Hash)
  978. expect(json_response['tickets'][0]).to eq(tickets[19].id)
  979. expect(json_response['tickets'][4]).to eq(tickets[15].id)
  980. expect(json_response['tickets_count']).to eq(5)
  981. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40&page=2&per_page=5", params: {}, as: :json
  982. expect(response).to have_http_status(:ok)
  983. expect(json_response).to be_a_kind_of(Hash)
  984. expect(json_response['tickets'][0]).to eq(tickets[14].id)
  985. expect(json_response['tickets'][4]).to eq(tickets[10].id)
  986. expect(json_response['tickets_count']).to eq(5)
  987. get '/api/v1/tickets?limit=40&page=1&per_page=5', params: {}, as: :json
  988. expect(response).to have_http_status(:ok)
  989. expect(json_response).to be_a_kind_of(Array)
  990. tickets = Ticket.order(:id).limit(5)
  991. expect(json_response[0]['id']).to eq(tickets[0].id)
  992. expect(json_response[4]['id']).to eq(tickets[4].id)
  993. expect(json_response.count).to eq(5)
  994. get '/api/v1/tickets?limit=40&page=2&per_page=5', params: {}, as: :json
  995. expect(response).to have_http_status(:ok)
  996. expect(json_response).to be_a_kind_of(Array)
  997. tickets = Ticket.order(:id).limit(10)
  998. expect(json_response[0]['id']).to eq(tickets[5].id)
  999. expect(json_response[4]['id']).to eq(tickets[9].id)
  1000. expect(json_response.count).to eq(5)
  1001. end
  1002. it 'does ticket create with customer minimal (03.01)' do
  1003. params = {
  1004. title: 'a new ticket #c1',
  1005. state: 'new',
  1006. priority: '2 normal',
  1007. group: ticket_group.name,
  1008. article: {
  1009. body: 'some body',
  1010. },
  1011. }
  1012. authenticated_as(customer_user)
  1013. post '/api/v1/tickets', params: params, as: :json
  1014. expect(response).to have_http_status(:created)
  1015. expect(json_response).to be_a_kind_of(Hash)
  1016. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1017. expect(json_response['title']).to eq('a new ticket #c1')
  1018. expect(json_response['customer_id']).to eq(customer_user.id)
  1019. expect(json_response['updated_by_id']).to eq(customer_user.id)
  1020. expect(json_response['created_by_id']).to eq(customer_user.id)
  1021. end
  1022. it 'does ticket create with customer with wrong customer (03.02)' do
  1023. params = {
  1024. title: 'a new ticket #c2',
  1025. state: 'new',
  1026. priority: '2 normal',
  1027. group: ticket_group.name,
  1028. customer_id: agent_user.id,
  1029. article: {
  1030. content_type: 'text/plain', # or text/html
  1031. body: 'some body',
  1032. sender: 'System',
  1033. },
  1034. }
  1035. authenticated_as(customer_user)
  1036. post '/api/v1/tickets', params: params, as: :json
  1037. expect(response).to have_http_status(:created)
  1038. expect(json_response).to be_a_kind_of(Hash)
  1039. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1040. expect(json_response['title']).to eq('a new ticket #c2')
  1041. expect(json_response['customer_id']).to eq(customer_user.id)
  1042. expect(json_response['updated_by_id']).to eq(customer_user.id)
  1043. expect(json_response['created_by_id']).to eq(customer_user.id)
  1044. end
  1045. it 'does ticket create with customer with wrong customer hash (03.03)' do
  1046. params = {
  1047. title: 'a new ticket #c2',
  1048. state: 'new',
  1049. priority: '2 normal',
  1050. group: ticket_group.name,
  1051. customer: {
  1052. firstname: agent_user.firstname,
  1053. lastname: agent_user.lastname,
  1054. email: agent_user.email,
  1055. },
  1056. article: {
  1057. content_type: 'text/plain', # or text/html
  1058. body: 'some body',
  1059. sender: 'System',
  1060. },
  1061. }
  1062. authenticated_as(customer_user)
  1063. post '/api/v1/tickets', params: params, as: :json
  1064. expect(response).to have_http_status(:created)
  1065. expect(json_response).to be_a_kind_of(Hash)
  1066. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1067. expect(json_response['title']).to eq('a new ticket #c2')
  1068. expect(json_response['customer_id']).to eq(customer_user.id)
  1069. expect(json_response['updated_by_id']).to eq(customer_user.id)
  1070. expect(json_response['created_by_id']).to eq(customer_user.id)
  1071. end
  1072. it 'does ticket with wrong ticket id (03.04)' do
  1073. ticket = create(
  1074. :ticket,
  1075. title: 'ticket with wrong ticket id',
  1076. group: ticket_group,
  1077. customer_id: agent_user.id,
  1078. )
  1079. authenticated_as(customer_user)
  1080. get "/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. params = {
  1085. title: 'ticket with wrong ticket id - 2',
  1086. }
  1087. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1088. expect(response).to have_http_status(:unauthorized)
  1089. expect(json_response).to be_a_kind_of(Hash)
  1090. expect(json_response['error']).to eq('Not authorized')
  1091. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1092. expect(response).to have_http_status(:unauthorized)
  1093. expect(json_response).to be_a_kind_of(Hash)
  1094. expect(json_response['error']).to eq('Not authorized')
  1095. end
  1096. it 'does ticket with correct ticket id (03.05)' do
  1097. title = "ticket with corret ticket id testme#{rand(999_999_999)}"
  1098. ticket = create(
  1099. :ticket,
  1100. title: title,
  1101. group: ticket_group,
  1102. customer_id: customer_user.id,
  1103. )
  1104. authenticated_as(customer_user)
  1105. get "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1106. expect(response).to have_http_status(:ok)
  1107. expect(json_response).to be_a_kind_of(Hash)
  1108. expect(json_response['id']).to eq(ticket.id)
  1109. expect(json_response['title']).to eq(title)
  1110. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1111. expect(json_response['updated_by_id']).to eq(1)
  1112. expect(json_response['created_by_id']).to eq(1)
  1113. params = {
  1114. title: "#{title} - 2",
  1115. customer_id: agent_user.id,
  1116. }
  1117. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1118. expect(response).to have_http_status(:ok)
  1119. expect(json_response).to be_a_kind_of(Hash)
  1120. expect(json_response['id']).to eq(ticket.id)
  1121. expect(json_response['title']).to eq("#{title} - 2")
  1122. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1123. expect(json_response['updated_by_id']).to eq(customer_user.id)
  1124. expect(json_response['created_by_id']).to eq(1)
  1125. params = {
  1126. ticket_id: ticket.id,
  1127. subject: 'some subject',
  1128. body: 'some body',
  1129. }
  1130. post '/api/v1/ticket_articles', params: params, as: :json
  1131. expect(response).to have_http_status(:created)
  1132. article_json_response = json_response
  1133. expect(article_json_response).to be_a_kind_of(Hash)
  1134. expect(article_json_response['ticket_id']).to eq(ticket.id)
  1135. expect(article_json_response['from']).to eq('Tickets Customer1')
  1136. expect(article_json_response['subject']).to eq('some subject')
  1137. expect(article_json_response['body']).to eq('some body')
  1138. expect(article_json_response['content_type']).to eq('text/plain')
  1139. expect(article_json_response['created_by_id']).to eq(customer_user.id)
  1140. expect(article_json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1141. expect(article_json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  1142. Scheduler.worker(true)
  1143. get "/api/v1/tickets/search?query=#{CGI.escape(title)}", params: {}, as: :json
  1144. expect(response).to have_http_status(:ok)
  1145. expect(json_response).to be_a_kind_of(Hash)
  1146. expect(json_response['tickets'][0]).to eq(ticket.id)
  1147. expect(json_response['tickets_count']).to eq(1)
  1148. params = {
  1149. condition: {
  1150. 'ticket.title' => {
  1151. operator: 'contains',
  1152. value: title,
  1153. },
  1154. },
  1155. }
  1156. post '/api/v1/tickets/search', params: params, as: :json
  1157. expect(response).to have_http_status(:ok)
  1158. expect(json_response).to be_a_kind_of(Hash)
  1159. expect(json_response['tickets'][0]).to eq(ticket.id)
  1160. expect(json_response['tickets_count']).to eq(1)
  1161. delete "/api/v1/ticket_articles/#{article_json_response['id']}", params: {}, as: :json
  1162. expect(response).to have_http_status(:unauthorized)
  1163. expect(json_response).to be_a_kind_of(Hash)
  1164. expect(json_response['error']).to eq('Not authorized (agent permission required)!')
  1165. params = {
  1166. ticket_id: ticket.id,
  1167. subject: 'some subject',
  1168. body: 'some body',
  1169. type: 'email',
  1170. sender: 'Agent',
  1171. }
  1172. post '/api/v1/ticket_articles', params: params, as: :json
  1173. expect(response).to have_http_status(:created)
  1174. expect(json_response).to be_a_kind_of(Hash)
  1175. expect(json_response['ticket_id']).to eq(ticket.id)
  1176. expect(json_response['from']).to eq('Tickets Customer1')
  1177. expect(json_response['subject']).to eq('some subject')
  1178. expect(json_response['body']).to eq('some body')
  1179. expect(json_response['content_type']).to eq('text/plain')
  1180. expect(json_response['created_by_id']).to eq(customer_user.id)
  1181. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1182. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'note').id)
  1183. delete "/api/v1/ticket_articles/#{json_response['id']}", params: {}, as: :json
  1184. expect(response).to have_http_status(:unauthorized)
  1185. expect(json_response).to be_a_kind_of(Hash)
  1186. expect(json_response['error']).to eq('Not authorized (agent permission required)!')
  1187. params = {
  1188. from: 'something which should not be changed on server side',
  1189. ticket_id: ticket.id,
  1190. subject: 'some subject',
  1191. body: 'some body',
  1192. type: 'web',
  1193. sender: 'Agent',
  1194. internal: true,
  1195. }
  1196. post '/api/v1/ticket_articles', params: params, as: :json
  1197. expect(response).to have_http_status(:created)
  1198. expect(json_response).to be_a_kind_of(Hash)
  1199. expect(json_response['ticket_id']).to eq(ticket.id)
  1200. expect(json_response['from']).to eq('Tickets Customer1 <tickets-customer1@example.com>')
  1201. expect(json_response['subject']).to eq('some subject')
  1202. expect(json_response['body']).to eq('some body')
  1203. expect(json_response['content_type']).to eq('text/plain')
  1204. expect(json_response['internal']).to eq(false)
  1205. expect(json_response['created_by_id']).to eq(customer_user.id)
  1206. expect(json_response['sender_id']).to eq(Ticket::Article::Sender.lookup(name: 'Customer').id)
  1207. expect(json_response['type_id']).to eq(Ticket::Article::Type.lookup(name: 'web').id)
  1208. params = {
  1209. subject: 'new subject',
  1210. }
  1211. put "/api/v1/ticket_articles/#{json_response['id']}", params: params, as: :json
  1212. expect(response).to have_http_status(:unauthorized)
  1213. expect(json_response).to be_a_kind_of(Hash)
  1214. expect(json_response['error']).to eq('Not authorized (ticket.agent or admin permission required)!')
  1215. delete "/api/v1/tickets/#{ticket.id}", params: {}, as: :json
  1216. expect(response).to have_http_status(:unauthorized)
  1217. expect(json_response).to be_a_kind_of(Hash)
  1218. expect(json_response['error']).to eq('Not authorized (admin permission required)!')
  1219. end
  1220. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (03.6)' do
  1221. authenticated_as(customer_user)
  1222. params = {
  1223. title: 'a new ticket #3.6',
  1224. group: ticket_group.name,
  1225. customer: {
  1226. firstname: 'some firstname',
  1227. lastname: 'some lastname',
  1228. email: 'some_new_customer@example.com',
  1229. },
  1230. article: {
  1231. body: 'some test 123',
  1232. origin_by: agent_user.login,
  1233. },
  1234. }
  1235. post '/api/v1/tickets', params: params, as: :json
  1236. expect(response).to have_http_status(:created)
  1237. expect(json_response).to be_a_kind_of(Hash)
  1238. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1239. expect(json_response['title']).to eq('a new ticket #3.6')
  1240. expect(json_response['customer_id']).to eq(customer_user.id)
  1241. expect(json_response['updated_by_id']).to eq(customer_user.id)
  1242. expect(json_response['created_by_id']).to eq(customer_user.id)
  1243. ticket = Ticket.find(json_response['id'])
  1244. article = ticket.articles.first
  1245. expect(article.updated_by_id).to eq(customer_user.id)
  1246. expect(article.created_by_id).to eq(customer_user.id)
  1247. expect(article.origin_by_id).to eq(customer_user.id)
  1248. expect(article.sender.name).to eq('Customer')
  1249. expect(article.type.name).to eq('note')
  1250. expect(article.from).to eq('Tickets Customer1')
  1251. end
  1252. it 'does ticket create with agent - minimal article with customer hash with article.origin_by (03.6)' do
  1253. authenticated_as(customer_user)
  1254. params = {
  1255. title: 'a new ticket #3.6.1',
  1256. group: ticket_group.name,
  1257. customer: {
  1258. firstname: 'some firstname',
  1259. lastname: 'some lastname',
  1260. email: 'some_new_customer@example.com',
  1261. },
  1262. article: {
  1263. sender: 'Agent',
  1264. body: 'some test 123',
  1265. origin_by_id: agent_user.id,
  1266. },
  1267. }
  1268. post '/api/v1/tickets', params: params, as: :json
  1269. expect(response).to have_http_status(:created)
  1270. expect(json_response).to be_a_kind_of(Hash)
  1271. expect(json_response['state_id']).to eq(Ticket::State.lookup(name: 'new').id)
  1272. expect(json_response['title']).to eq('a new ticket #3.6.1')
  1273. expect(json_response['customer_id']).to eq(customer_user.id)
  1274. expect(json_response['updated_by_id']).to eq(customer_user.id)
  1275. expect(json_response['created_by_id']).to eq(customer_user.id)
  1276. ticket = Ticket.find(json_response['id'])
  1277. article = ticket.articles.first
  1278. expect(article.updated_by_id).to eq(customer_user.id)
  1279. expect(article.created_by_id).to eq(customer_user.id)
  1280. expect(article.origin_by_id).to eq(customer_user.id)
  1281. expect(article.sender.name).to eq('Customer')
  1282. expect(article.type.name).to eq('note')
  1283. expect(article.from).to eq('Tickets Customer1')
  1284. end
  1285. it 'does ticket show and response format (04.01)' do
  1286. title = "ticket testagent#{rand(999_999_999)}"
  1287. ticket = create(
  1288. :ticket,
  1289. title: title,
  1290. group: ticket_group,
  1291. customer_id: customer_user.id,
  1292. updated_by_id: agent_user.id,
  1293. created_by_id: agent_user.id,
  1294. )
  1295. authenticated_as(agent_user)
  1296. get "/api/v1/tickets/#{ticket.id}", 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['group']).to be_falsey
  1302. expect(json_response['priority']).to be_falsey
  1303. expect(json_response['owner']).to be_falsey
  1304. expect(json_response['customer_id']).to eq(ticket.customer_id)
  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=true", 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['customer_id']).to eq(ticket.customer_id)
  1313. expect(json_response['group']).to eq(ticket.group.name)
  1314. expect(json_response['priority']).to eq(ticket.priority.name)
  1315. expect(json_response['owner']).to eq(ticket.owner.login)
  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}?expand=false", 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['title']).to eq(ticket.title)
  1323. expect(json_response['group']).to be_falsey
  1324. expect(json_response['priority']).to be_falsey
  1325. expect(json_response['owner']).to be_falsey
  1326. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1327. expect(json_response['updated_by_id']).to eq(agent_user.id)
  1328. expect(json_response['created_by_id']).to eq(agent_user.id)
  1329. get "/api/v1/tickets/#{ticket.id}?full=true", params: {}, as: :json
  1330. expect(response).to have_http_status(:ok)
  1331. expect(json_response).to be_a_kind_of(Hash)
  1332. expect(json_response['id']).to eq(ticket.id)
  1333. expect(json_response['assets']).to be_truthy
  1334. expect(json_response['assets']['Ticket']).to be_truthy
  1335. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1336. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1337. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(ticket.title)
  1338. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1339. expect(json_response['assets']['User']).to be_truthy
  1340. expect(json_response['assets']['User'][agent_user.id.to_s]).to be_truthy
  1341. expect(json_response['assets']['User'][agent_user.id.to_s]['id']).to eq(agent_user.id)
  1342. expect(json_response['assets']['User'][agent_user.id.to_s]['firstname']).to eq(agent_user.firstname)
  1343. expect(json_response['assets']['User'][agent_user.id.to_s]['lastname']).to eq(agent_user.lastname)
  1344. expect(json_response['assets']['User']).to be_truthy
  1345. expect(json_response['assets']['User'][customer_user.id.to_s]).to be_truthy
  1346. expect(json_response['assets']['User'][customer_user.id.to_s]['id']).to eq(customer_user.id)
  1347. expect(json_response['assets']['User'][customer_user.id.to_s]['firstname']).to eq(customer_user.firstname)
  1348. expect(json_response['assets']['User'][customer_user.id.to_s]['lastname']).to eq(customer_user.lastname)
  1349. get "/api/v1/tickets/#{ticket.id}?full=false", params: {}, as: :json
  1350. expect(response).to have_http_status(:ok)
  1351. expect(json_response).to be_a_kind_of(Hash)
  1352. expect(json_response['id']).to eq(ticket.id)
  1353. expect(json_response['title']).to eq(ticket.title)
  1354. expect(json_response['group']).to be_falsey
  1355. expect(json_response['priority']).to be_falsey
  1356. expect(json_response['owner']).to be_falsey
  1357. expect(json_response['customer_id']).to eq(ticket.customer_id)
  1358. expect(json_response['updated_by_id']).to eq(agent_user.id)
  1359. expect(json_response['created_by_id']).to eq(agent_user.id)
  1360. end
  1361. it 'does ticket index and response format (04.02)' do
  1362. title = "ticket testagent#{rand(999_999_999)}"
  1363. ticket = create(
  1364. :ticket,
  1365. title: title,
  1366. group: ticket_group,
  1367. customer_id: customer_user.id,
  1368. updated_by_id: agent_user.id,
  1369. created_by_id: agent_user.id,
  1370. )
  1371. authenticated_as(agent_user)
  1372. get '/api/v1/tickets', params: {}, as: :json
  1373. expect(response).to have_http_status(:ok)
  1374. expect(json_response).to be_a_kind_of(Array)
  1375. expect(json_response[0]).to be_a_kind_of(Hash)
  1376. expect(json_response[0]['id']).to eq(1)
  1377. expect(json_response[1]['id']).to eq(ticket.id)
  1378. expect(json_response[1]['title']).to eq(ticket.title)
  1379. expect(json_response[1]['group']).to be_falsey
  1380. expect(json_response[1]['priority']).to be_falsey
  1381. expect(json_response[1]['owner']).to be_falsey
  1382. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1383. expect(json_response[1]['updated_by_id']).to eq(agent_user.id)
  1384. expect(json_response[1]['created_by_id']).to eq(agent_user.id)
  1385. get '/api/v1/tickets?expand=true', params: {}, as: :json
  1386. expect(response).to have_http_status(:ok)
  1387. expect(json_response).to be_a_kind_of(Array)
  1388. expect(json_response[0]).to be_a_kind_of(Hash)
  1389. expect(json_response[0]['id']).to eq(1)
  1390. expect(json_response[1]['id']).to eq(ticket.id)
  1391. expect(json_response[1]['title']).to eq(ticket.title)
  1392. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1393. expect(json_response[1]['group']).to eq(ticket.group.name)
  1394. expect(json_response[1]['priority']).to eq(ticket.priority.name)
  1395. expect(json_response[1]['owner']).to eq(ticket.owner.login)
  1396. expect(json_response[1]['updated_by_id']).to eq(agent_user.id)
  1397. expect(json_response[1]['created_by_id']).to eq(agent_user.id)
  1398. get '/api/v1/tickets?expand=false', params: {}, as: :json
  1399. expect(response).to have_http_status(:ok)
  1400. expect(json_response).to be_a_kind_of(Array)
  1401. expect(json_response[0]).to be_a_kind_of(Hash)
  1402. expect(json_response[0]['id']).to eq(1)
  1403. expect(json_response[1]['id']).to eq(ticket.id)
  1404. expect(json_response[1]['title']).to eq(ticket.title)
  1405. expect(json_response[1]['group']).to be_falsey
  1406. expect(json_response[1]['priority']).to be_falsey
  1407. expect(json_response[1]['owner']).to be_falsey
  1408. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1409. expect(json_response[1]['updated_by_id']).to eq(agent_user.id)
  1410. expect(json_response[1]['created_by_id']).to eq(agent_user.id)
  1411. get '/api/v1/tickets?full=true', params: {}, as: :json
  1412. expect(response).to have_http_status(:ok)
  1413. expect(json_response).to be_a_kind_of(Hash)
  1414. expect(json_response['record_ids'].class).to eq(Array)
  1415. expect(json_response['record_ids'][0]).to eq(1)
  1416. expect(json_response['record_ids'][1]).to eq(ticket.id)
  1417. expect(json_response['assets']).to be_truthy
  1418. expect(json_response['assets']['Ticket']).to be_truthy
  1419. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1420. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1421. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(ticket.title)
  1422. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1423. expect(json_response['assets']['User']).to be_truthy
  1424. expect(json_response['assets']['User'][agent_user.id.to_s]).to be_truthy
  1425. expect(json_response['assets']['User'][agent_user.id.to_s]['id']).to eq(agent_user.id)
  1426. expect(json_response['assets']['User'][agent_user.id.to_s]['firstname']).to eq(agent_user.firstname)
  1427. expect(json_response['assets']['User'][agent_user.id.to_s]['lastname']).to eq(agent_user.lastname)
  1428. expect(json_response['assets']['User']).to be_truthy
  1429. expect(json_response['assets']['User'][customer_user.id.to_s]).to be_truthy
  1430. expect(json_response['assets']['User'][customer_user.id.to_s]['id']).to eq(customer_user.id)
  1431. expect(json_response['assets']['User'][customer_user.id.to_s]['firstname']).to eq(customer_user.firstname)
  1432. expect(json_response['assets']['User'][customer_user.id.to_s]['lastname']).to eq(customer_user.lastname)
  1433. get '/api/v1/tickets?full=false', params: {}, as: :json
  1434. expect(response).to have_http_status(:ok)
  1435. expect(json_response).to be_a_kind_of(Array)
  1436. expect(json_response[0]).to be_a_kind_of(Hash)
  1437. expect(json_response[0]['id']).to eq(1)
  1438. expect(json_response[1]['id']).to eq(ticket.id)
  1439. expect(json_response[1]['title']).to eq(ticket.title)
  1440. expect(json_response[1]['group']).to be_falsey
  1441. expect(json_response[1]['priority']).to be_falsey
  1442. expect(json_response[1]['owner']).to be_falsey
  1443. expect(json_response[1]['customer_id']).to eq(ticket.customer_id)
  1444. expect(json_response[1]['updated_by_id']).to eq(agent_user.id)
  1445. expect(json_response[1]['created_by_id']).to eq(agent_user.id)
  1446. end
  1447. it 'does ticket create and response format (04.03)' do
  1448. title = "ticket testagent#{rand(999_999_999)}"
  1449. params = {
  1450. title: title,
  1451. group: ticket_group.name,
  1452. customer_id: customer_user.id,
  1453. state: 'new',
  1454. priority: '2 normal',
  1455. article: {
  1456. body: 'some test 123',
  1457. },
  1458. }
  1459. authenticated_as(agent_user)
  1460. post '/api/v1/tickets', params: params, as: :json
  1461. expect(response).to have_http_status(:created)
  1462. expect(json_response).to be_a_kind_of(Hash)
  1463. ticket = Ticket.find(json_response['id'])
  1464. expect(json_response['state_id']).to eq(ticket.state_id)
  1465. expect(json_response['state']).to be_falsey
  1466. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1467. expect(json_response['priority']).to be_falsey
  1468. expect(json_response['group_id']).to eq(ticket.group_id)
  1469. expect(json_response['group']).to be_falsey
  1470. expect(json_response['title']).to eq(title)
  1471. expect(json_response['customer_id']).to eq(customer_user.id)
  1472. expect(json_response['updated_by_id']).to eq(agent_user.id)
  1473. expect(json_response['created_by_id']).to eq(agent_user.id)
  1474. post '/api/v1/tickets?expand=true', params: params, as: :json
  1475. expect(response).to have_http_status(:created)
  1476. expect(json_response).to be_a_kind_of(Hash)
  1477. ticket = Ticket.find(json_response['id'])
  1478. expect(json_response['state_id']).to eq(ticket.state_id)
  1479. expect(json_response['state']).to eq(ticket.state.name)
  1480. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1481. expect(json_response['priority']).to eq(ticket.priority.name)
  1482. expect(json_response['group_id']).to eq(ticket.group_id)
  1483. expect(json_response['group']).to eq(ticket.group.name)
  1484. expect(json_response['title']).to eq(title)
  1485. expect(json_response['customer_id']).to eq(customer_user.id)
  1486. expect(json_response['updated_by_id']).to eq(agent_user.id)
  1487. expect(json_response['created_by_id']).to eq(agent_user.id)
  1488. post '/api/v1/tickets?full=true', params: params, as: :json
  1489. expect(response).to have_http_status(:created)
  1490. expect(json_response).to be_a_kind_of(Hash)
  1491. ticket = Ticket.find(json_response['id'])
  1492. expect(json_response['assets']).to be_truthy
  1493. expect(json_response['assets']['Ticket']).to be_truthy
  1494. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1495. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1496. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq(title)
  1497. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1498. expect(json_response['assets']['User']).to be_truthy
  1499. expect(json_response['assets']['User'][agent_user.id.to_s]).to be_truthy
  1500. expect(json_response['assets']['User'][agent_user.id.to_s]['id']).to eq(agent_user.id)
  1501. expect(json_response['assets']['User'][agent_user.id.to_s]['firstname']).to eq(agent_user.firstname)
  1502. expect(json_response['assets']['User'][agent_user.id.to_s]['lastname']).to eq(agent_user.lastname)
  1503. expect(json_response['assets']['User']).to be_truthy
  1504. expect(json_response['assets']['User'][customer_user.id.to_s]).to be_truthy
  1505. expect(json_response['assets']['User'][customer_user.id.to_s]['id']).to eq(customer_user.id)
  1506. expect(json_response['assets']['User'][customer_user.id.to_s]['firstname']).to eq(customer_user.firstname)
  1507. expect(json_response['assets']['User'][customer_user.id.to_s]['lastname']).to eq(customer_user.lastname)
  1508. end
  1509. it 'does ticket update and response formats (04.04)' do
  1510. title = "ticket testagent#{rand(999_999_999)}"
  1511. ticket = create(
  1512. :ticket,
  1513. title: title,
  1514. group: ticket_group,
  1515. customer_id: customer_user.id,
  1516. updated_by_id: agent_user.id,
  1517. created_by_id: agent_user.id,
  1518. )
  1519. params = {
  1520. title: 'a update ticket #1',
  1521. }
  1522. authenticated_as(agent_user)
  1523. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  1524. expect(response).to have_http_status(:ok)
  1525. expect(json_response).to be_a_kind_of(Hash)
  1526. ticket = Ticket.find(json_response['id'])
  1527. expect(json_response['state_id']).to eq(ticket.state_id)
  1528. expect(json_response['state']).to be_falsey
  1529. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1530. expect(json_response['priority']).to be_falsey
  1531. expect(json_response['group_id']).to eq(ticket.group_id)
  1532. expect(json_response['group']).to be_falsey
  1533. expect(json_response['title']).to eq('a update ticket #1')
  1534. expect(json_response['customer_id']).to eq(customer_user.id)
  1535. expect(json_response['updated_by_id']).to eq(agent_user.id)
  1536. expect(json_response['created_by_id']).to eq(agent_user.id)
  1537. params = {
  1538. title: 'a update ticket #2',
  1539. }
  1540. put "/api/v1/tickets/#{ticket.id}?expand=true", params: params, as: :json
  1541. expect(response).to have_http_status(:ok)
  1542. expect(json_response).to be_a_kind_of(Hash)
  1543. ticket = Ticket.find(json_response['id'])
  1544. expect(json_response['state_id']).to eq(ticket.state_id)
  1545. expect(json_response['state']).to eq(ticket.state.name)
  1546. expect(json_response['priority_id']).to eq(ticket.priority_id)
  1547. expect(json_response['priority']).to eq(ticket.priority.name)
  1548. expect(json_response['group_id']).to eq(ticket.group_id)
  1549. expect(json_response['group']).to eq(ticket.group.name)
  1550. expect(json_response['title']).to eq('a update ticket #2')
  1551. expect(json_response['customer_id']).to eq(customer_user.id)
  1552. expect(json_response['updated_by_id']).to eq(agent_user.id)
  1553. expect(json_response['created_by_id']).to eq(agent_user.id)
  1554. params = {
  1555. title: 'a update ticket #3',
  1556. }
  1557. put "/api/v1/tickets/#{ticket.id}?full=true", params: params, as: :json
  1558. expect(response).to have_http_status(:ok)
  1559. expect(json_response).to be_a_kind_of(Hash)
  1560. ticket = Ticket.find(json_response['id'])
  1561. expect(json_response['assets']).to be_truthy
  1562. expect(json_response['assets']['Ticket']).to be_truthy
  1563. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1564. expect(json_response['assets']['Ticket'][ticket.id.to_s]['id']).to eq(ticket.id)
  1565. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq('a update ticket #3')
  1566. expect(json_response['assets']['Ticket'][ticket.id.to_s]['customer_id']).to eq(ticket.customer_id)
  1567. expect(json_response['assets']['User']).to be_truthy
  1568. expect(json_response['assets']['User'][agent_user.id.to_s]).to be_truthy
  1569. expect(json_response['assets']['User'][agent_user.id.to_s]['id']).to eq(agent_user.id)
  1570. expect(json_response['assets']['User'][agent_user.id.to_s]['firstname']).to eq(agent_user.firstname)
  1571. expect(json_response['assets']['User'][agent_user.id.to_s]['lastname']).to eq(agent_user.lastname)
  1572. expect(json_response['assets']['User']).to be_truthy
  1573. expect(json_response['assets']['User'][customer_user.id.to_s]).to be_truthy
  1574. expect(json_response['assets']['User'][customer_user.id.to_s]['id']).to eq(customer_user.id)
  1575. expect(json_response['assets']['User'][customer_user.id.to_s]['firstname']).to eq(customer_user.firstname)
  1576. expect(json_response['assets']['User'][customer_user.id.to_s]['lastname']).to eq(customer_user.lastname)
  1577. # it should be not possible to modify the ticket number
  1578. expected_ticket_number = ticket.number
  1579. params = {
  1580. title: 'a update ticket #4',
  1581. number: '77777',
  1582. }
  1583. put "/api/v1/tickets/#{ticket.id}?full=true", params: params, as: :json
  1584. expect(response).to have_http_status(:ok)
  1585. expect(json_response).to be_a_kind_of(Hash)
  1586. ticket = Ticket.find(json_response['id'])
  1587. expect(json_response['assets']['Ticket'][ticket.id.to_s]['title']).to eq('a update ticket #4')
  1588. expect(json_response['assets']['Ticket'][ticket.id.to_s]['number']).to eq(expected_ticket_number)
  1589. end
  1590. it 'does ticket split with html - check attachments (05.01)' do
  1591. ticket = create(
  1592. :ticket,
  1593. title: 'some title',
  1594. group: ticket_group,
  1595. customer_id: customer_user.id,
  1596. updated_by_id: agent_user.id,
  1597. created_by_id: agent_user.id,
  1598. )
  1599. article = create(
  1600. :ticket_article,
  1601. type: Ticket::Article::Type.lookup(name: 'note'),
  1602. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1603. body: '<b>test</b> <img src="cid:15.274327094.140938@ZAMMAD.example.com"/> test <img src="cid:15.274327094.140938.3@ZAMMAD.example.com"/>',
  1604. content_type: 'text/html',
  1605. ticket_id: ticket.id,
  1606. )
  1607. Store.add(
  1608. object: 'Ticket::Article',
  1609. o_id: article.id,
  1610. data: 'content_file1_normally_should_be_an_image',
  1611. filename: 'some_file1.jpg',
  1612. preferences: {
  1613. 'Content-Type' => 'image/jpeg',
  1614. 'Mime-Type' => 'image/jpeg',
  1615. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  1616. 'Content-Disposition' => 'inline',
  1617. },
  1618. created_by_id: 1,
  1619. )
  1620. Store.add(
  1621. object: 'Ticket::Article',
  1622. o_id: article.id,
  1623. data: 'content_file2_normally_should_be_an_image',
  1624. filename: 'some_file2.jpg',
  1625. preferences: {
  1626. 'Content-Type' => 'image/jpeg',
  1627. 'Mime-Type' => 'image/jpeg',
  1628. 'Content-ID' => '15.274327094.140938.2@zammad.example.com',
  1629. 'Content-Disposition' => 'inline',
  1630. },
  1631. created_by_id: 1,
  1632. )
  1633. Store.add(
  1634. object: 'Ticket::Article',
  1635. o_id: article.id,
  1636. data: 'content_file3_normally_should_be_an_image',
  1637. filename: 'some_file3.jpg',
  1638. preferences: {
  1639. 'Content-Type' => 'image/jpeg',
  1640. 'Mime-Type' => 'image/jpeg',
  1641. 'Content-ID' => '15.274327094.140938.3@zammad.example.com',
  1642. },
  1643. created_by_id: 1,
  1644. )
  1645. Store.add(
  1646. object: 'Ticket::Article',
  1647. o_id: article.id,
  1648. data: 'content_file4_normally_should_be_an_image',
  1649. filename: 'some_file4.jpg',
  1650. preferences: {
  1651. 'Content-Type' => 'image/jpeg',
  1652. 'Mime-Type' => 'image/jpeg',
  1653. 'Content-ID' => '15.274327094.140938.4@zammad.example.com',
  1654. },
  1655. created_by_id: 1,
  1656. )
  1657. Store.add(
  1658. object: 'Ticket::Article',
  1659. o_id: article.id,
  1660. data: 'content_file1_normally_should_be_an_pdf',
  1661. filename: 'Rechnung_RE-2018-200.pdf',
  1662. preferences: {
  1663. 'Content-Type' => 'application/octet-stream; name="Rechnung_RE-2018-200.pdf"',
  1664. 'Mime-Type' => 'application/octet-stream',
  1665. 'Content-ID' => '8AB0BEC88984EE4EBEF643C79C8E0346@zammad.example.com',
  1666. 'Content-Description' => 'Rechnung_RE-2018-200.pdf',
  1667. 'Content-Disposition' => 'attachment',
  1668. },
  1669. created_by_id: 1,
  1670. )
  1671. authenticated_as(customer_user)
  1672. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=new_form_id123", params: {}, as: :json
  1673. expect(response).to have_http_status(:unauthorized)
  1674. authenticated_as(agent_user)
  1675. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=new_form_id123", params: {}, as: :json
  1676. expect(response).to have_http_status(:ok)
  1677. expect(json_response).to be_a_kind_of(Hash)
  1678. expect(json_response['assets']).to be_truthy
  1679. expect(json_response['assets']['Ticket']).to be_truthy
  1680. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1681. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1682. expect(json_response['attachments']).to be_truthy
  1683. expect(json_response['attachments'].count).to eq(3)
  1684. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=new_form_id123", params: {}, as: :json
  1685. expect(response).to have_http_status(:ok)
  1686. expect(json_response).to be_a_kind_of(Hash)
  1687. expect(json_response['assets']).to be_truthy
  1688. expect(json_response['assets']['Ticket']).to be_truthy
  1689. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1690. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1691. expect(json_response['attachments']).to be_truthy
  1692. expect(json_response['attachments'].count).to eq(0)
  1693. end
  1694. it 'does ticket split with plain - check attachments (05.02)' do
  1695. ticket = create(
  1696. :ticket,
  1697. title: 'some title',
  1698. group: ticket_group,
  1699. customer_id: customer_user.id,
  1700. updated_by_id: agent_user.id,
  1701. created_by_id: agent_user.id,
  1702. )
  1703. article = create(
  1704. :ticket_article,
  1705. type: Ticket::Article::Type.lookup(name: 'note'),
  1706. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1707. body: '<b>test</b> <img src="cid:15.274327094.140938@zammad.example.com"/>',
  1708. content_type: 'text/plain',
  1709. ticket_id: ticket.id,
  1710. )
  1711. Store.add(
  1712. object: 'Ticket::Article',
  1713. o_id: article.id,
  1714. data: 'content_file1_normally_should_be_an_image',
  1715. filename: 'some_file1.jpg',
  1716. preferences: {
  1717. 'Content-Type' => 'image/jpeg',
  1718. 'Mime-Type' => 'image/jpeg',
  1719. 'Content-ID' => '15.274327094.140938@zammad.example.com',
  1720. 'Content-Disposition' => 'inline',
  1721. },
  1722. created_by_id: 1,
  1723. )
  1724. Store.add(
  1725. object: 'Ticket::Article',
  1726. o_id: article.id,
  1727. data: 'content_file1_normally_should_be_an_image',
  1728. filename: 'some_file2.jpg',
  1729. preferences: {
  1730. 'Content-Type' => 'image/jpeg',
  1731. 'Mime-Type' => 'image/jpeg',
  1732. 'Content-ID' => '15.274327094.140938.2@zammad.example.com',
  1733. 'Content-Disposition' => 'inline',
  1734. },
  1735. created_by_id: 1,
  1736. )
  1737. Store.add(
  1738. object: 'Ticket::Article',
  1739. o_id: article.id,
  1740. data: 'content_file1_normally_should_be_an_pdf',
  1741. filename: 'Rechnung_RE-2018-200.pdf',
  1742. preferences: {
  1743. 'Content-Type' => 'application/octet-stream; name="Rechnung_RE-2018-200.pdf"',
  1744. 'Mime-Type' => 'application/octet-stream',
  1745. 'Content-ID' => '8AB0BEC88984EE4EBEF643C79C8E0346@zammad.example.com',
  1746. 'Content-Description' => 'Rechnung_RE-2018-200.pdf',
  1747. 'Content-Disposition' => 'attachment',
  1748. },
  1749. created_by_id: 1,
  1750. )
  1751. authenticated_as(agent_user)
  1752. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=new_form_id123", params: {}, as: :json
  1753. expect(response).to have_http_status(:ok)
  1754. expect(json_response).to be_a_kind_of(Hash)
  1755. expect(json_response['assets']).to be_truthy
  1756. expect(json_response['assets']['Ticket']).to be_truthy
  1757. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1758. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1759. expect(json_response['attachments']).to be_truthy
  1760. expect(json_response['attachments'].count).to eq(3)
  1761. get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=new_form_id123", params: {}, as: :json
  1762. expect(response).to have_http_status(:ok)
  1763. expect(json_response).to be_a_kind_of(Hash)
  1764. expect(json_response['assets']).to be_truthy
  1765. expect(json_response['assets']['Ticket']).to be_truthy
  1766. expect(json_response['assets']['Ticket'][ticket.id.to_s]).to be_truthy
  1767. expect(json_response['assets']['TicketArticle'][article.id.to_s]).to be_truthy
  1768. expect(json_response['attachments']).to be_truthy
  1769. expect(json_response['attachments'].count).to eq(0)
  1770. end
  1771. it 'does ticket merge (07.01)' do
  1772. group_no_permission = create(:group)
  1773. ticket1 = create(
  1774. :ticket,
  1775. title: 'ticket merge1',
  1776. group: ticket_group,
  1777. customer_id: customer_user.id,
  1778. )
  1779. ticket2 = create(
  1780. :ticket,
  1781. title: 'ticket merge2',
  1782. group: ticket_group,
  1783. customer_id: customer_user.id,
  1784. )
  1785. ticket3 = create(
  1786. :ticket,
  1787. title: 'ticket merge2',
  1788. group: group_no_permission,
  1789. customer_id: customer_user.id,
  1790. )
  1791. authenticated_as(customer_user)
  1792. put "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
  1793. expect(response).to have_http_status(:unauthorized)
  1794. authenticated_as(agent_user)
  1795. put "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
  1796. expect(response).to have_http_status(:ok)
  1797. expect(json_response).to be_a_kind_of(Hash)
  1798. expect(json_response['result']).to eq('failed')
  1799. expect(json_response['message']).to eq('No such master ticket number!')
  1800. put "/api/v1/ticket_merge/#{ticket3.id}/#{ticket1.number}", params: {}, as: :json
  1801. expect(response).to have_http_status(:unauthorized)
  1802. expect(json_response).to be_a_kind_of(Hash)
  1803. expect(json_response['error']).to eq('Not authorized')
  1804. expect(json_response['error_human']).to eq('Not authorized')
  1805. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket3.number}", params: {}, as: :json
  1806. expect(response).to have_http_status(:unauthorized)
  1807. expect(json_response).to be_a_kind_of(Hash)
  1808. expect(json_response['error']).to eq('Not authorized')
  1809. expect(json_response['error_human']).to eq('Not authorized')
  1810. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket2.number}", params: {}, as: :json
  1811. expect(response).to have_http_status(:ok)
  1812. expect(json_response).to be_a_kind_of(Hash)
  1813. expect(json_response['result']).to eq('success')
  1814. expect(json_response['master_ticket']['id']).to eq(ticket2.id)
  1815. end
  1816. it 'does ticket merge - change permission (07.02)' do
  1817. group_change_permission = Group.create!(
  1818. name: 'GroupWithChangePermission',
  1819. active: true,
  1820. updated_by_id: 1,
  1821. created_by_id: 1,
  1822. )
  1823. ticket1 = create(
  1824. :ticket,
  1825. title: 'ticket merge1',
  1826. group: group_change_permission,
  1827. customer_id: customer_user.id,
  1828. )
  1829. ticket2 = create(
  1830. :ticket,
  1831. title: 'ticket merge2',
  1832. group: group_change_permission,
  1833. customer_id: customer_user.id,
  1834. )
  1835. agent_user.group_names_access_map = { group_change_permission.name => %w[read change] }
  1836. authenticated_as(agent_user)
  1837. put "/api/v1/ticket_merge/#{ticket1.id}/#{ticket2.number}", params: {}, as: :json
  1838. expect(response).to have_http_status(:ok)
  1839. expect(json_response).to be_a_kind_of(Hash)
  1840. expect(json_response['result']).to eq('success')
  1841. expect(json_response['master_ticket']['id']).to eq(ticket2.id)
  1842. end
  1843. it 'does ticket search sorted (08.01)' do
  1844. title = "ticket pagination #{rand(999_999_999)}"
  1845. ticket1 = create(
  1846. :ticket,
  1847. title: "#{title} A",
  1848. group: ticket_group,
  1849. customer_id: customer_user.id,
  1850. created_at: '2018-02-05 17:42:00',
  1851. updated_at: '2018-02-05 20:42:00',
  1852. )
  1853. create(
  1854. :ticket_article,
  1855. type: Ticket::Article::Type.lookup(name: 'note'),
  1856. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1857. ticket_id: ticket1.id,
  1858. )
  1859. ticket2 = create(
  1860. :ticket,
  1861. title: "#{title} B",
  1862. group: ticket_group,
  1863. customer_id: customer_user.id,
  1864. state: Ticket::State.lookup(name: 'new'),
  1865. priority: Ticket::Priority.lookup(name: '3 hoch'),
  1866. created_at: '2018-02-05 19:42:00',
  1867. updated_at: '2018-02-05 19:42:00',
  1868. )
  1869. create(
  1870. :ticket_article,
  1871. type: Ticket::Article::Type.lookup(name: 'note'),
  1872. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1873. ticket_id: ticket2.id,
  1874. )
  1875. authenticated_as(admin_user)
  1876. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: {}, as: :json
  1877. expect(response).to have_http_status(:ok)
  1878. expect(json_response).to be_a_kind_of(Hash)
  1879. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1880. authenticated_as(admin_user)
  1881. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'created_at', order_by: 'asc' }, as: :json
  1882. expect(response).to have_http_status(:ok)
  1883. expect(json_response).to be_a_kind_of(Hash)
  1884. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1885. authenticated_as(admin_user)
  1886. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'title', order_by: 'asc' }, as: :json
  1887. expect(response).to have_http_status(:ok)
  1888. expect(json_response).to be_a_kind_of(Hash)
  1889. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1890. authenticated_as(admin_user)
  1891. get "/api/v1/tickets/search?query=#{CGI.escape(title)}&limit=40", params: { sort_by: 'title', order_by: 'desc' }, as: :json
  1892. expect(response).to have_http_status(:ok)
  1893. expect(json_response).to be_a_kind_of(Hash)
  1894. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1895. authenticated_as(admin_user)
  1896. 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
  1897. expect(response).to have_http_status(:ok)
  1898. expect(json_response).to be_a_kind_of(Hash)
  1899. expect(json_response['tickets']).to eq([ticket1.id, ticket2.id])
  1900. authenticated_as(admin_user)
  1901. 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
  1902. expect(response).to have_http_status(:ok)
  1903. expect(json_response).to be_a_kind_of(Hash)
  1904. expect(json_response['tickets']).to eq([ticket2.id, ticket1.id])
  1905. end
  1906. it 'does ticket history ' do
  1907. ticket1 = create(
  1908. :ticket,
  1909. title: 'some title',
  1910. group: ticket_group,
  1911. customer_id: customer_user.id,
  1912. )
  1913. create(
  1914. :ticket_article,
  1915. type: Ticket::Article::Type.lookup(name: 'note'),
  1916. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  1917. ticket_id: ticket1.id,
  1918. )
  1919. authenticated_as(agent_user)
  1920. get "/api/v1/ticket_history/#{ticket1.id}", params: {}, as: :json
  1921. expect(response).to have_http_status(:ok)
  1922. expect(json_response).to be_a_kind_of(Hash)
  1923. expect(json_response['history'].class).to eq(Array)
  1924. expect(json_response['assets'].class).to eq(Hash)
  1925. expect(json_response['assets']['User'][customer_user.id.to_s]).not_to be_nil
  1926. expect(json_response['assets']['Ticket'][ticket1.id.to_s]).not_to be_nil
  1927. authenticated_as(customer_user)
  1928. get "/api/v1/ticket_history/#{ticket1.id}", params: {}, as: :json
  1929. expect(response).to have_http_status(:unauthorized)
  1930. end
  1931. it 'does ticket related' do
  1932. ticket1 = create(
  1933. :ticket,
  1934. title: 'some title',
  1935. group: ticket_group,
  1936. customer_id: customer_user.id,
  1937. )
  1938. authenticated_as(agent_user)
  1939. get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
  1940. expect(response).to have_http_status(:ok)
  1941. authenticated_as(customer_user)
  1942. get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
  1943. expect(response).to have_http_status(:unauthorized)
  1944. end
  1945. it 'does ticket recent' do
  1946. authenticated_as(agent_user)
  1947. get '/api/v1/ticket_recent', params: {}, as: :json
  1948. expect(response).to have_http_status(:ok)
  1949. authenticated_as(customer_user)
  1950. get '/api/v1/ticket_recent', params: {}, as: :json
  1951. expect(response).to have_http_status(:unauthorized)
  1952. end
  1953. end
  1954. describe 'stats' do
  1955. let(:ticket1) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  1956. let(:ticket2) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  1957. let(:ticket3) { create(:ticket, customer: customer, organization: organization, group: ticket_group) }
  1958. let(:customer) { create(:customer_user, organization: organization) }
  1959. let(:organization) { create(:organization, shared: false) }
  1960. before do
  1961. authenticated_as(admin_user)
  1962. ticket1
  1963. travel 2.minutes
  1964. ticket2
  1965. travel 2.minutes
  1966. ticket3
  1967. travel 2.minutes
  1968. ticket2.touch
  1969. end
  1970. # https://github.com/zammad/zammad/issues/2296
  1971. it 'orders tickets by created_at desc (#2296)' do
  1972. get '/api/v1/ticket_stats', params: { organization_id: organization.id, user_id: customer.id }, as: :json
  1973. expect(response).to have_http_status(:ok)
  1974. expect(json_response)
  1975. .to be_a_kind_of(Hash)
  1976. .and include('user' => hash_including('open_ids' => [ticket3.id, ticket2.id, ticket1.id]))
  1977. .and include('organization' => hash_including('open_ids' => [ticket3.id, ticket2.id, ticket1.id]))
  1978. end
  1979. end
  1980. describe '/api/v1/tickets' do
  1981. subject(:ticket) { create(:ticket, state_name: 'closed') }
  1982. let(:admin) { create(:admin_user, groups: [ticket.group]) }
  1983. let(:agent) { create(:agent_user, groups: [ticket.group]) }
  1984. let(:customer) { ticket.customer }
  1985. describe 'reopening a ticket' do
  1986. shared_examples 'successfully reopen a ticket' do
  1987. it 'succeeds' do
  1988. put "/api/v1/tickets/#{ticket.id}",
  1989. params: { state_id: Ticket::State.find_by(name: 'open').id },
  1990. as: :json
  1991. expect(response).to have_http_status(:ok)
  1992. expect(json_response).to include('state_id' => Ticket::State.find_by(name: 'open').id)
  1993. end
  1994. end
  1995. shared_examples 'fail to reopen a ticket' do
  1996. it 'fails' do
  1997. put "/api/v1/tickets/#{ticket.id}",
  1998. params: { state_id: Ticket::State.find_by(name: 'open').id },
  1999. as: :json
  2000. expect(response).to have_http_status(:unprocessable_entity)
  2001. expect(json_response).to include('error' => 'Cannot follow-up on a closed ticket. Please create a new ticket.')
  2002. end
  2003. end
  2004. context 'when ticket.group.follow_up_possible = "yes"' do
  2005. before { ticket.group.update(follow_up_possible: 'yes') }
  2006. context 'as admin', authenticated_as: -> { admin } do
  2007. include_examples 'successfully reopen a ticket'
  2008. end
  2009. context 'as agent', authenticated_as: -> { agent } do
  2010. include_examples 'successfully reopen a ticket'
  2011. end
  2012. context 'as customer', authenticated_as: -> { customer } do
  2013. include_examples 'successfully reopen a ticket'
  2014. end
  2015. end
  2016. context 'when ticket.group.follow_up_possible = "new_ticket"' do
  2017. before { ticket.group.update(follow_up_possible: 'new_ticket') }
  2018. context 'as admin', authenticated_as: -> { admin } do
  2019. include_examples 'successfully reopen a ticket'
  2020. end
  2021. context 'as agent', authenticated_as: -> { agent } do
  2022. include_examples 'successfully reopen a ticket'
  2023. end
  2024. context 'as customer', authenticated_as: -> { customer } do
  2025. include_examples 'fail to reopen a ticket'
  2026. end
  2027. end
  2028. end
  2029. end
  2030. describe 'GET /api/v1/ticket_customer' do
  2031. subject(:ticket) { create(:ticket, customer: customer_authorized) }
  2032. let(:organization_authorized) { create(:organization) }
  2033. let(:customer_authorized) { create(:customer_user, organization: organization_authorized) }
  2034. let(:organization_unauthorized) { create(:organization) }
  2035. let(:customer_unauthorized) { create(:customer_user, organization: organization_unauthorized) }
  2036. let(:agent) { create(:agent_user, groups: [ticket.group]) }
  2037. describe 'listing information' do
  2038. before do
  2039. ticket
  2040. end
  2041. shared_examples 'has access' do
  2042. it 'succeeds' do
  2043. get '/api/v1/ticket_customer',
  2044. params: { customer_id: customer_authorized.id },
  2045. as: :json
  2046. expect(json_response['ticket_ids_open']).to include(ticket.id)
  2047. expect(json_response['ticket_ids_closed']).to be_blank
  2048. end
  2049. end
  2050. shared_examples 'has no access' do
  2051. it 'fails' do
  2052. get '/api/v1/ticket_customer',
  2053. params: { customer_id: customer_authorized.id },
  2054. as: :json
  2055. expect(json_response['ticket_ids_open']).to be_blank
  2056. expect(json_response['ticket_ids_closed']).to be_blank
  2057. expect(json_response['assets']).to be_blank
  2058. end
  2059. end
  2060. context 'as agent', authenticated_as: -> { agent } do
  2061. include_examples 'has access'
  2062. end
  2063. context 'as authorized customer', authenticated_as: -> { customer_authorized } do
  2064. include_examples 'has no access'
  2065. end
  2066. context 'as unauthorized customer', authenticated_as: -> { customer_unauthorized } do
  2067. include_examples 'has no access'
  2068. end
  2069. end
  2070. end
  2071. end