object_manager_attributes_controller_test.rb 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. require 'integration_test_helper'
  2. require 'rake'
  3. class ObjectManagerAttributesControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. roles = Role.where(name: %w[Admin Agent])
  8. groups = Group.all
  9. UserInfo.current_user_id = 1
  10. @admin = User.create_or_update(
  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. end
  21. test 'add new ticket text object' do
  22. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  23. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  24. token = @response.headers['CSRF-TOKEN']
  25. # token based on headers
  26. params = {
  27. 'name': 'test1',
  28. 'object': 'Ticket',
  29. 'display': 'Test 1',
  30. 'active': true,
  31. 'data_type': 'input',
  32. 'data_option': {
  33. 'default': 'test',
  34. 'type': 'text',
  35. 'maxlength': 120
  36. },
  37. 'screens': {
  38. 'create_middle': {
  39. 'ticket.customer': {
  40. 'shown': true,
  41. 'item_class': 'column'
  42. },
  43. 'ticket.agent': {
  44. 'shown': true,
  45. 'item_class': 'column'
  46. }
  47. },
  48. 'edit': {
  49. 'ticket.customer': {
  50. 'shown': true
  51. },
  52. 'ticket.agent': {
  53. 'shown': true
  54. }
  55. }
  56. },
  57. 'id': 'c-196'
  58. }
  59. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  60. assert_response(201)
  61. result = JSON.parse(@response.body)
  62. assert(result)
  63. assert(result['data_option']['null'])
  64. assert_equal(result['data_option']['null'], true)
  65. assert_equal(result['name'], 'test1')
  66. end
  67. test 'add new ticket text object - no default' do
  68. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  69. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  70. token = @response.headers['CSRF-TOKEN']
  71. # token based on headers
  72. params = {
  73. 'name': 'test2',
  74. 'object': 'Ticket',
  75. 'display': 'Test 2',
  76. 'active': true,
  77. 'data_type': 'input',
  78. 'data_option': {
  79. 'type': 'text',
  80. 'maxlength': 120
  81. },
  82. 'screens': {
  83. 'create_middle': {
  84. 'ticket.customer': {
  85. 'shown': true,
  86. 'item_class': 'column'
  87. },
  88. 'ticket.agent': {
  89. 'shown': true,
  90. 'item_class': 'column'
  91. }
  92. },
  93. 'edit': {
  94. 'ticket.customer': {
  95. 'shown': true
  96. },
  97. 'ticket.agent': {
  98. 'shown': true
  99. }
  100. }
  101. },
  102. 'id': 'c-196'
  103. }
  104. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  105. assert_response(201)
  106. result = JSON.parse(@response.body)
  107. assert(result)
  108. assert(result['data_option']['null'])
  109. assert_equal(result['data_option']['null'], true)
  110. assert_equal(result['name'], 'test2')
  111. end
  112. test 'update ticket text object' do
  113. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  114. # add a new object
  115. object = ObjectManager::Attribute.add(
  116. name: 'test3',
  117. object: 'Ticket',
  118. display: 'Test 3',
  119. active: true,
  120. data_type: 'input',
  121. data_option: {
  122. default: 'test',
  123. type: 'text',
  124. maxlength: 120,
  125. null: true
  126. },
  127. screens: {
  128. create_middle: {
  129. 'ticket.customer' => {
  130. shown: true,
  131. item_class: 'column'
  132. },
  133. 'ticket.agent' => {
  134. shown: true,
  135. item_class: 'column'
  136. }
  137. },
  138. edit: {
  139. 'ticket.customer' => {
  140. shown: true
  141. },
  142. 'ticket.agent' => {
  143. shown: true
  144. }
  145. }
  146. },
  147. position: 1550,
  148. editable: true
  149. )
  150. migration = ObjectManager::Attribute.migration_execute
  151. assert_equal(migration, true)
  152. post "/api/v1/object_manager_attributes/#{object.id}", params: {}, headers: @headers
  153. token = @response.headers['CSRF-TOKEN']
  154. # parameters for updating
  155. params = {
  156. 'name': 'test4',
  157. 'object': 'Ticket',
  158. 'display': 'Test 4',
  159. 'active': true,
  160. 'data_type': 'input',
  161. 'data_option': {
  162. 'default': 'test',
  163. 'type': 'text',
  164. 'maxlength': 120
  165. },
  166. 'screens': {
  167. 'create_middle': {
  168. 'ticket.customer': {
  169. 'shown': true,
  170. 'item_class': 'column'
  171. },
  172. 'ticket.agent': {
  173. 'shown': true,
  174. 'item_class': 'column'
  175. }
  176. },
  177. 'edit': {
  178. 'ticket.customer': {
  179. 'shown': true
  180. },
  181. 'ticket.agent': {
  182. 'shown': true
  183. }
  184. }
  185. },
  186. 'id': 'c-196'
  187. }
  188. # update the object
  189. put "/api/v1/object_manager_attributes/#{object.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  190. assert_response(200)
  191. result = JSON.parse(@response.body)
  192. assert(result)
  193. assert(result['data_option']['null'])
  194. assert_equal(result['name'], 'test4')
  195. assert_equal(result['display'], 'Test 4')
  196. end
  197. test 'add new ticket boolean object' do
  198. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  199. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  200. token = @response.headers['CSRF-TOKEN']
  201. # token based on headers
  202. params = {
  203. 'active': true,
  204. 'data_option': {
  205. 'options': {
  206. 'false': 'no',
  207. 'true': 'yes'
  208. }
  209. },
  210. 'data_type': 'boolean',
  211. 'display': 'Boolean 2',
  212. 'id': 'c-200',
  213. 'name': 'bool2',
  214. 'object': 'Ticket',
  215. 'screens': {
  216. 'create_middle': {
  217. 'ticket.agent' => {
  218. 'item_class': 'column',
  219. 'shown': true
  220. },
  221. 'ticket.customer' => {
  222. 'item_class': 'column',
  223. 'shown': true
  224. }
  225. },
  226. 'edit': {
  227. 'ticket.agent' => {
  228. 'shown': true
  229. },
  230. 'ticket.customer' => {
  231. 'shown': true
  232. }
  233. }
  234. }
  235. }
  236. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  237. assert_response(201)
  238. result = JSON.parse(@response.body)
  239. assert(result)
  240. assert(result['data_option']['null'])
  241. assert_equal(result['data_option']['null'], true)
  242. assert_equal(result['name'], 'bool2')
  243. end
  244. test 'add new user select object' do
  245. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  246. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  247. token = @response.headers['CSRF-TOKEN']
  248. # token based on headers
  249. params = {
  250. 'active': true,
  251. 'data_option': {
  252. 'options': {
  253. 'key1': 'foo'
  254. }
  255. },
  256. 'data_type': 'select',
  257. 'display': 'Test 5',
  258. 'id': 'c-204',
  259. 'name': 'test5',
  260. 'object': 'User',
  261. 'screens': {
  262. 'create': {
  263. 'admin.user' => {
  264. 'shown': true
  265. },
  266. 'ticket.agent' => {
  267. 'shown': true
  268. },
  269. 'ticket.customer' => {
  270. 'shown': true
  271. }
  272. },
  273. 'edit': {
  274. 'admin.user' => {
  275. 'shown': true
  276. },
  277. 'ticket.agent' => {
  278. 'shown': true
  279. }
  280. },
  281. 'view': {
  282. 'admin.user' => {
  283. 'shown': true
  284. },
  285. 'ticket.agent' => {
  286. 'shown': true
  287. },
  288. 'ticket.customer' => {
  289. 'shown': true
  290. }
  291. }
  292. }
  293. }
  294. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  295. assert_response(201)
  296. result = JSON.parse(@response.body)
  297. assert(result)
  298. assert(result['data_option']['null'])
  299. assert_equal(result['data_option']['null'], true)
  300. assert_equal(result['name'], 'test5')
  301. end
  302. test 'update user select object' do
  303. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  304. # add a new object
  305. object = ObjectManager::Attribute.add(
  306. active: true,
  307. data_option: {
  308. options: {
  309. key1: 'foo'
  310. },
  311. null: true,
  312. default: '',
  313. },
  314. data_type: 'select',
  315. display: 'Test 6',
  316. id: 'c-204',
  317. name: 'test6',
  318. object: 'User',
  319. screens: {
  320. create: {
  321. 'admin.user' => {
  322. shown: true
  323. },
  324. 'ticket.agent' => {
  325. shown: true
  326. },
  327. 'ticket.customer' => {
  328. shown: true
  329. }
  330. },
  331. edit: {
  332. 'admin.user' => {
  333. shown: true
  334. },
  335. 'ticket.agent' => {
  336. shown: true
  337. }
  338. },
  339. view: {
  340. 'admin.user' => {
  341. shown: true
  342. },
  343. 'ticket.agent' => {
  344. shown: true
  345. },
  346. 'ticket.customer' => {
  347. shown: true
  348. }
  349. }
  350. },
  351. position: 1550,
  352. editable: true
  353. )
  354. migration = ObjectManager::Attribute.migration_execute
  355. assert_equal(migration, true)
  356. post "/api/v1/object_manager_attributes/#{object.id}", params: {}, headers: @headers
  357. token = @response.headers['CSRF-TOKEN']
  358. # parameters for updating
  359. params = {
  360. active: true,
  361. data_option: {
  362. options: {
  363. key1: 'foo',
  364. key2: 'bar'
  365. }
  366. },
  367. data_type: 'select',
  368. display: 'Test 7',
  369. id: 'c-204',
  370. name: 'test7',
  371. object: 'User',
  372. screens: {
  373. create: {
  374. 'admin.user' => {
  375. shown: true
  376. },
  377. 'ticket.agent' => {
  378. shown: true
  379. },
  380. 'ticket.customer' => {
  381. shown: true
  382. }
  383. },
  384. edit: {
  385. 'admin.user' => {
  386. shown: true
  387. },
  388. 'ticket.agent' => {
  389. shown: true
  390. }
  391. },
  392. view: {
  393. 'admin.user' => {
  394. shown: true
  395. },
  396. 'ticket.agent' => {
  397. shown: true
  398. },
  399. 'ticket.customer' => {
  400. shown: true
  401. }
  402. }
  403. }
  404. }
  405. # update the object
  406. put "/api/v1/object_manager_attributes/#{object.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  407. assert_response(200)
  408. result = JSON.parse(@response.body)
  409. assert(result)
  410. assert(result['data_option']['options'])
  411. assert_equal(result['name'], 'test7')
  412. assert_equal(result['display'], 'Test 7')
  413. end
  414. test '01 converts string to boolean for default value for boolean data type with true' do
  415. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  416. params = {
  417. 'name': "customerdescription#{rand(999_999_999)}",
  418. 'object': 'Ticket',
  419. 'display': "custom description#{rand(999_999_999)}",
  420. 'active': true,
  421. 'data_type': 'boolean',
  422. 'data_option': {
  423. 'options': {
  424. 'true': '',
  425. 'false': '',
  426. },
  427. 'default': 'true',
  428. 'screens': {
  429. 'create_middle': {
  430. 'ticket.customer': {
  431. 'shown': true,
  432. 'item_class': 'column'
  433. },
  434. 'ticket.agent': {
  435. 'shown': true,
  436. 'item_class': 'column'
  437. }
  438. },
  439. 'edit': {
  440. 'ticket.customer': {
  441. 'shown': true
  442. },
  443. 'ticket.agent': {
  444. 'shown': true
  445. }
  446. }
  447. }
  448. },
  449. 'id': 'c-201'
  450. }
  451. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  452. migration = ObjectManager::Attribute.migration_execute
  453. assert_equal(migration, true)
  454. assert_response(201) # created
  455. result = JSON.parse(@response.body)
  456. assert(result)
  457. assert(result['data_option']['default'])
  458. assert_equal(result['data_option']['default'], true)
  459. assert_equal(result['data_type'], 'boolean')
  460. end
  461. test '02 converts string to boolean for default value for boolean data type with false' do
  462. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  463. params = {
  464. 'name': "customerdescription_#{rand(999_999_999)}",
  465. 'object': 'Ticket',
  466. 'display': "custom description #{rand(999_999_999)}",
  467. 'active': true,
  468. 'data_type': 'boolean',
  469. 'data_option': {
  470. 'options': {
  471. 'true': '',
  472. 'false': '',
  473. },
  474. 'default': 'false',
  475. 'screens': {
  476. 'create_middle': {
  477. 'ticket.customer': {
  478. 'shown': true,
  479. 'item_class': 'column'
  480. },
  481. 'ticket.agent': {
  482. 'shown': true,
  483. 'item_class': 'column'
  484. }
  485. },
  486. 'edit': {
  487. 'ticket.customer': {
  488. 'shown': true
  489. },
  490. 'ticket.agent': {
  491. 'shown': true
  492. }
  493. }
  494. }
  495. },
  496. }
  497. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  498. migration = ObjectManager::Attribute.migration_execute
  499. assert_equal(migration, true)
  500. assert_response(201) # created
  501. result = JSON.parse(@response.body)
  502. assert(result)
  503. assert_not(result['data_option']['default'])
  504. assert_equal(result['data_option']['default'], false)
  505. assert_equal(result['data_type'], 'boolean')
  506. end
  507. test '03 ticket attributes cannot be removed when it is referenced by an overview' do
  508. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  509. # 1. create a new ticket attribute and execute migration
  510. migration = ObjectManager::Attribute.migration_execute
  511. params = {
  512. 'name': 'test_attribute_referenced_by_an_overview',
  513. 'object': 'Ticket',
  514. 'display': 'Test Attribute',
  515. 'active': true,
  516. 'data_type': 'input',
  517. 'data_option': {
  518. 'default': '',
  519. 'type': 'text',
  520. 'maxlength': 120,
  521. 'null': true,
  522. 'options': {},
  523. 'relation': ''
  524. },
  525. 'screens': {
  526. 'create_middle': {
  527. 'ticket.customer': {
  528. 'shown': true,
  529. 'item_class': 'column'
  530. },
  531. 'ticket.agent': {
  532. 'shown': true,
  533. 'item_class': 'column'
  534. }
  535. },
  536. 'edit': {
  537. 'ticket.customer': {
  538. 'shown': true
  539. },
  540. 'ticket.agent': {
  541. 'shown': true
  542. }
  543. }
  544. },
  545. }
  546. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  547. migration = ObjectManager::Attribute.migration_execute
  548. assert_equal(migration, true)
  549. # 2. create an overview that uses the attribute
  550. params = {
  551. name: 'test_overview',
  552. roles: Role.where(name: 'Agent').pluck(:name),
  553. condition: {
  554. 'ticket.state_id': {
  555. 'operator': 'is',
  556. 'value': Ticket::State.all.pluck(:id),
  557. },
  558. 'ticket.test_attribute_referenced_by_an_overview': {
  559. 'operator': 'contains',
  560. 'value': 'DUMMY'
  561. },
  562. },
  563. order: {
  564. by: 'created_at',
  565. direction: 'DESC',
  566. },
  567. view: {
  568. d: %w[title customer state created_at],
  569. s: %w[number title customer state created_at],
  570. m: %w[number title customer state created_at],
  571. view_mode_default: 's',
  572. },
  573. user_ids: [ '1' ],
  574. }
  575. if Overview.where('name like ?', '%test%').empty?
  576. post '/api/v1/overviews', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  577. assert_response(201)
  578. result = JSON.parse(@response.body)
  579. assert_equal(Hash, result.class)
  580. assert_equal('test_overview', result['name'])
  581. end
  582. # 3. attempt to delete the ticket attribute
  583. get '/api/v1/object_manager_attributes', headers: @headers.merge('Authorization' => credentials)
  584. assert_response(200)
  585. result = JSON.parse(@response.body)
  586. target_attribute = result.select { |x| x['name'] == 'test_attribute_referenced_by_an_overview' && x['object'] == 'Ticket' }
  587. assert_equal target_attribute.size, 1
  588. target_id = target_attribute[0]['id']
  589. delete "/api/v1/object_manager_attributes/#{target_id}", headers: @headers.merge('Authorization' => credentials)
  590. assert_response(422)
  591. assert @response.body.include?('Overview')
  592. assert @response.body.include?('test_overview')
  593. assert @response.body.include?('cannot be deleted!')
  594. end
  595. test '04 ticket attributes cannot be removed when it is referenced by a trigger' do
  596. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  597. # 1. create a new ticket attribute and execute migration
  598. migration = ObjectManager::Attribute.migration_execute
  599. params = {
  600. 'name': 'test_attribute_referenced_by_a_trigger',
  601. 'object': 'Ticket',
  602. 'display': 'Test Attribute',
  603. 'active': true,
  604. 'data_type': 'input',
  605. 'data_option': {
  606. 'default': '',
  607. 'type': 'text',
  608. 'maxlength': 120,
  609. 'null': true,
  610. 'options': {},
  611. 'relation': ''
  612. },
  613. 'screens': {
  614. 'create_middle': {
  615. 'ticket.customer': {
  616. 'shown': true,
  617. 'item_class': 'column'
  618. },
  619. 'ticket.agent': {
  620. 'shown': true,
  621. 'item_class': 'column'
  622. }
  623. },
  624. 'edit': {
  625. 'ticket.customer': {
  626. 'shown': true
  627. },
  628. 'ticket.agent': {
  629. 'shown': true
  630. }
  631. }
  632. },
  633. }
  634. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  635. migration = ObjectManager::Attribute.migration_execute
  636. assert_equal(migration, true)
  637. # 2. create an trigger that uses the attribute
  638. params = {
  639. name: 'test_trigger',
  640. condition: {
  641. 'ticket.test_attribute_referenced_by_a_trigger': {
  642. 'operator': 'contains',
  643. 'value': 'DUMMY'
  644. }
  645. },
  646. 'perform': {
  647. 'ticket.state_id': {
  648. 'value': '2'
  649. }
  650. },
  651. 'active': true,
  652. 'id': 'c-3'
  653. }
  654. if Trigger.where('name like ?', '%test%').empty?
  655. post '/api/v1/triggers', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  656. assert_response(201)
  657. result = JSON.parse(@response.body)
  658. assert_equal(Hash, result.class)
  659. assert_equal('test_trigger', result['name'])
  660. end
  661. # 3. attempt to delete the ticket attribute
  662. get '/api/v1/object_manager_attributes', headers: @headers.merge('Authorization' => credentials)
  663. assert_response(200)
  664. result = JSON.parse(@response.body)
  665. target_attribute = result.select { |x| x['name'] == 'test_attribute_referenced_by_a_trigger' && x['object'] == 'Ticket' }
  666. assert_equal target_attribute.size, 1
  667. target_id = target_attribute[0]['id']
  668. delete "/api/v1/object_manager_attributes/#{target_id}", headers: @headers.merge('Authorization' => credentials)
  669. assert_response(422)
  670. assert @response.body.include?('Trigger')
  671. assert @response.body.include?('test_trigger')
  672. assert @response.body.include?('cannot be deleted!')
  673. end
  674. test '05 ticket attributes cannot be removed when it is referenced by a scheduler' do
  675. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  676. # 1. create a new ticket attribute and execute migration
  677. migration = ObjectManager::Attribute.migration_execute
  678. params = {
  679. 'name': 'test_attribute_referenced_by_a_scheduler',
  680. 'object': 'Ticket',
  681. 'display': 'Test Attribute',
  682. 'active': true,
  683. 'data_type': 'input',
  684. 'data_option': {
  685. 'default': '',
  686. 'type': 'text',
  687. 'maxlength': 120,
  688. 'null': true,
  689. 'options': {},
  690. 'relation': ''
  691. },
  692. 'screens': {
  693. 'create_middle': {
  694. 'ticket.customer': {
  695. 'shown': true,
  696. 'item_class': 'column'
  697. },
  698. 'ticket.agent': {
  699. 'shown': true,
  700. 'item_class': 'column'
  701. }
  702. },
  703. 'edit': {
  704. 'ticket.customer': {
  705. 'shown': true
  706. },
  707. 'ticket.agent': {
  708. 'shown': true
  709. }
  710. }
  711. },
  712. }
  713. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  714. migration = ObjectManager::Attribute.migration_execute
  715. assert_equal(migration, true)
  716. # 2. create a scheduler that uses the attribute
  717. params = {
  718. name: 'test_scheduler',
  719. 'timeplan': {
  720. 'days': {
  721. 'Mon': true,
  722. 'Tue': false,
  723. 'Wed': false,
  724. 'Thu': false,
  725. 'Fri': false,
  726. 'Sat': false,
  727. 'Sun': false
  728. },
  729. 'hours': {
  730. '0': true,
  731. '1': false,
  732. '2': false,
  733. '3': false,
  734. '4': false,
  735. '5': false,
  736. '6': false,
  737. '7': false,
  738. '8': false,
  739. '9': false,
  740. '10': false,
  741. '11': false,
  742. '12': false,
  743. '13': false,
  744. '14': false,
  745. '15': false,
  746. '16': false,
  747. '17': false,
  748. '18': false,
  749. '19': false,
  750. '20': false,
  751. '21': false,
  752. '22': false,
  753. '23': false
  754. },
  755. 'minutes': {
  756. '0': true,
  757. '10': false,
  758. '20': false,
  759. '30': false,
  760. '40': false,
  761. '50': false
  762. }
  763. },
  764. 'condition': {
  765. 'ticket.test_attribute_referenced_by_a_scheduler': {
  766. 'operator': 'contains',
  767. 'value': 'DUMMY'
  768. }
  769. },
  770. 'perform': {
  771. 'ticket.state_id': {
  772. 'value': '2'
  773. }
  774. },
  775. 'disable_notification': true,
  776. 'note': '',
  777. 'active': true,
  778. 'id': 'c-0'
  779. }
  780. if Job.where('name like ?', '%test%').empty?
  781. post '/api/v1/jobs', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  782. assert_response(201)
  783. result = JSON.parse(@response.body)
  784. assert_equal(Hash, result.class)
  785. assert_equal('test_scheduler', result['name'])
  786. end
  787. # 3. attempt to delete the ticket attribute
  788. get '/api/v1/object_manager_attributes', headers: @headers.merge('Authorization' => credentials)
  789. assert_response(200)
  790. result = JSON.parse(@response.body)
  791. target_attribute = result.select { |x| x['name'] == 'test_attribute_referenced_by_a_scheduler' && x['object'] == 'Ticket' }
  792. assert_equal target_attribute.size, 1
  793. target_id = target_attribute[0]['id']
  794. delete "/api/v1/object_manager_attributes/#{target_id}", headers: @headers.merge('Authorization' => credentials)
  795. assert_response(422)
  796. assert @response.body.include?('Job')
  797. assert @response.body.include?('test_scheduler')
  798. assert @response.body.include?('cannot be deleted!')
  799. end
  800. test '06 ticket attributes can be removed when it is referenced by an overview but by user object' do
  801. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  802. # 1. create a new ticket attribute and execute migration
  803. migration = ObjectManager::Attribute.migration_execute
  804. params = {
  805. 'name': 'test_attribute_referenced_by_an_overview',
  806. 'object': 'Ticket',
  807. 'display': 'Test Attribute',
  808. 'active': true,
  809. 'data_type': 'input',
  810. 'data_option': {
  811. 'default': '',
  812. 'type': 'text',
  813. 'maxlength': 120,
  814. 'null': true,
  815. 'options': {},
  816. 'relation': ''
  817. },
  818. 'screens': {
  819. 'create_middle': {
  820. 'ticket.customer': {
  821. 'shown': true,
  822. 'item_class': 'column'
  823. },
  824. 'ticket.agent': {
  825. 'shown': true,
  826. 'item_class': 'column'
  827. }
  828. },
  829. 'edit': {
  830. 'ticket.customer': {
  831. 'shown': true
  832. },
  833. 'ticket.agent': {
  834. 'shown': true
  835. }
  836. }
  837. },
  838. }
  839. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  840. params = {
  841. 'name': 'test_attribute_referenced_by_an_overview',
  842. 'object': 'User',
  843. 'display': 'Test Attribute',
  844. 'active': true,
  845. 'data_type': 'input',
  846. 'data_option': {
  847. 'default': '',
  848. 'type': 'text',
  849. 'maxlength': 120,
  850. 'null': true,
  851. 'options': {},
  852. 'relation': ''
  853. },
  854. 'screens': {
  855. 'create_middle': {
  856. 'ticket.customer': {
  857. 'shown': true,
  858. 'item_class': 'column'
  859. },
  860. 'ticket.agent': {
  861. 'shown': true,
  862. 'item_class': 'column'
  863. }
  864. },
  865. 'edit': {
  866. 'ticket.customer': {
  867. 'shown': true
  868. },
  869. 'ticket.agent': {
  870. 'shown': true
  871. }
  872. }
  873. },
  874. }
  875. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  876. migration = ObjectManager::Attribute.migration_execute
  877. assert_equal(migration, true)
  878. # 2. create an overview that uses the attribute
  879. params = {
  880. name: 'test_overview',
  881. roles: Role.where(name: 'Agent').pluck(:name),
  882. condition: {
  883. 'ticket.state_id': {
  884. 'operator': 'is',
  885. 'value': Ticket::State.all.pluck(:id),
  886. },
  887. 'ticket.test_attribute_referenced_by_an_overview': {
  888. 'operator': 'contains',
  889. 'value': 'DUMMY'
  890. },
  891. },
  892. order: {
  893. by: 'created_at',
  894. direction: 'DESC',
  895. },
  896. view: {
  897. d: %w[title customer state created_at],
  898. s: %w[number title customer state created_at],
  899. m: %w[number title customer state created_at],
  900. view_mode_default: 's',
  901. },
  902. user_ids: [ '1' ],
  903. }
  904. if Overview.where('name like ?', '%test%').empty?
  905. post '/api/v1/overviews', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  906. assert_response(201)
  907. result = JSON.parse(@response.body)
  908. assert_equal(Hash, result.class)
  909. assert_equal('test_overview', result['name'])
  910. end
  911. # 3. attempt to delete the ticket attribute
  912. get '/api/v1/object_manager_attributes', headers: @headers.merge('Authorization' => credentials)
  913. assert_response(200)
  914. result = JSON.parse(@response.body)
  915. target_attribute = result.select { |x| x['name'] == 'test_attribute_referenced_by_an_overview' && x['object'] == 'User' }
  916. assert_equal target_attribute.size, 1
  917. target_id = target_attribute[0]['id']
  918. delete "/api/v1/object_manager_attributes/#{target_id}", headers: @headers.merge('Authorization' => credentials)
  919. assert_response(200)
  920. target_attribute = result.select { |x| x['name'] == 'test_attribute_referenced_by_an_overview' && x['object'] == 'Ticket' }
  921. assert_equal target_attribute.size, 1
  922. target_id = target_attribute[0]['id']
  923. delete "/api/v1/object_manager_attributes/#{target_id}", headers: @headers.merge('Authorization' => credentials)
  924. assert_response(422)
  925. assert @response.body.include?('Overview')
  926. assert @response.body.include?('test_overview')
  927. assert @response.body.include?('cannot be deleted!')
  928. end
  929. test '07 verify if attribute type can not be changed' do
  930. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  931. params = {
  932. 'name': "customerdescription_#{rand(999_999_999)}",
  933. 'object': 'Ticket',
  934. 'display': "custom description #{rand(999_999_999)}",
  935. 'active': true,
  936. 'data_type': 'boolean',
  937. 'data_option': {
  938. 'options': {
  939. 'true': '',
  940. 'false': '',
  941. },
  942. 'default': 'false',
  943. 'screens': {
  944. 'create_middle': {
  945. 'ticket.customer': {
  946. 'shown': true,
  947. 'item_class': 'column'
  948. },
  949. 'ticket.agent': {
  950. 'shown': true,
  951. 'item_class': 'column'
  952. }
  953. },
  954. 'edit': {
  955. 'ticket.customer': {
  956. 'shown': true
  957. },
  958. 'ticket.agent': {
  959. 'shown': true
  960. }
  961. }
  962. }
  963. },
  964. }
  965. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  966. assert_response(201) # created
  967. result = JSON.parse(@response.body)
  968. assert(result)
  969. assert_not(result['data_option']['default'])
  970. assert_equal(result['data_option']['default'], false)
  971. assert_equal(result['data_type'], 'boolean')
  972. migration = ObjectManager::Attribute.migration_execute
  973. assert_equal(migration, true)
  974. params['data_type'] = 'input'
  975. params['data_option'] = {
  976. 'default': 'test',
  977. 'type': 'text',
  978. 'maxlength': 120
  979. }
  980. put "/api/v1/object_manager_attributes/#{result['id']}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  981. assert_response(422)
  982. result = JSON.parse(@response.body)
  983. assert(result)
  984. assert(result['error'])
  985. end
  986. test '08 verify if attribute type can be changed' do
  987. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  988. params = {
  989. 'name': "customerdescription_#{rand(999_999_999)}",
  990. 'object': 'Ticket',
  991. 'display': "custom description #{rand(999_999_999)}",
  992. 'active': true,
  993. 'data_type': 'input',
  994. 'data_option': {
  995. 'default': 'test',
  996. 'type': 'text',
  997. 'maxlength': 120,
  998. },
  999. 'screens': {
  1000. 'create_middle': {
  1001. 'ticket.customer': {
  1002. 'shown': true,
  1003. 'item_class': 'column'
  1004. },
  1005. 'ticket.agent': {
  1006. 'shown': true,
  1007. 'item_class': 'column'
  1008. }
  1009. },
  1010. 'edit': {
  1011. 'ticket.customer': {
  1012. 'shown': true
  1013. },
  1014. 'ticket.agent': {
  1015. 'shown': true
  1016. }
  1017. },
  1018. },
  1019. }
  1020. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  1021. assert_response(201) # created
  1022. result = JSON.parse(@response.body)
  1023. assert(result)
  1024. assert_equal(result['data_option']['default'], 'test')
  1025. assert_equal(result['data_type'], 'input')
  1026. migration = ObjectManager::Attribute.migration_execute
  1027. assert_equal(migration, true)
  1028. params['data_type'] = 'select'
  1029. params['data_option'] = {
  1030. 'default': 'fuu',
  1031. 'options': {
  1032. 'key1': 'foo',
  1033. 'key2': 'fuu',
  1034. }
  1035. }
  1036. put "/api/v1/object_manager_attributes/#{result['id']}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  1037. assert_response(200)
  1038. result = JSON.parse(@response.body)
  1039. assert(result)
  1040. assert_equal(result['data_option']['default'], 'test')
  1041. assert_equal(result['data_option_new']['default'], 'fuu')
  1042. assert_equal(result['data_type'], 'select')
  1043. end
  1044. end