ticket_spec.rb 104 KB

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