ticket_spec.rb 99 KB

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