ticket_spec.rb 105 KB

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