tickets_controller_test.rb 98 KB

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