ticket_spec.rb 94 KB

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