object_manager_attributes_controller_test.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. # rubocop:disable Lint/BooleanSymbol
  2. require 'test_helper'
  3. require 'rake'
  4. class ObjectManagerAttributesControllerTest < ActionDispatch::IntegrationTest
  5. self.use_transactional_tests = false
  6. setup do
  7. # set accept header
  8. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  9. roles = Role.where(name: %w[Admin Agent])
  10. groups = Group.all
  11. UserInfo.current_user_id = 1
  12. @admin = User.create_or_update(
  13. login: 'tickets-admin',
  14. firstname: 'Tickets',
  15. lastname: 'Admin',
  16. email: 'tickets-admin@example.com',
  17. password: 'adminpw',
  18. active: true,
  19. roles: roles,
  20. groups: groups,
  21. )
  22. end
  23. test 'add new ticket text object' do
  24. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  25. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  26. token = @response.headers['CSRF-TOKEN']
  27. # token based on headers
  28. params = {
  29. 'name': 'test1',
  30. 'object': 'Ticket',
  31. 'display': 'Test 1',
  32. 'active': true,
  33. 'data_type': 'input',
  34. 'data_option': {
  35. 'default': 'test',
  36. 'type': 'text',
  37. 'maxlength': 120
  38. },
  39. 'screens': {
  40. 'create_middle': {
  41. 'ticket.customer': {
  42. 'shown': true,
  43. 'item_class': 'column'
  44. },
  45. 'ticket.agent': {
  46. 'shown': true,
  47. 'item_class': 'column'
  48. }
  49. },
  50. 'edit': {
  51. 'ticket.customer': {
  52. 'shown': true
  53. },
  54. 'ticket.agent': {
  55. 'shown': true
  56. }
  57. }
  58. },
  59. 'id': 'c-196'
  60. }
  61. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  62. assert_response(201)
  63. result = JSON.parse(@response.body)
  64. assert(result)
  65. assert(result['data_option']['null'])
  66. assert_equal(result['data_option']['null'], true)
  67. assert_equal(result['name'], 'test1')
  68. end
  69. test 'add new ticket text object - no default' do
  70. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  71. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  72. token = @response.headers['CSRF-TOKEN']
  73. # token based on headers
  74. params = {
  75. 'name': 'test2',
  76. 'object': 'Ticket',
  77. 'display': 'Test 2',
  78. 'active': true,
  79. 'data_type': 'input',
  80. 'data_option': {
  81. 'type': 'text',
  82. 'maxlength': 120
  83. },
  84. 'screens': {
  85. 'create_middle': {
  86. 'ticket.customer': {
  87. 'shown': true,
  88. 'item_class': 'column'
  89. },
  90. 'ticket.agent': {
  91. 'shown': true,
  92. 'item_class': 'column'
  93. }
  94. },
  95. 'edit': {
  96. 'ticket.customer': {
  97. 'shown': true
  98. },
  99. 'ticket.agent': {
  100. 'shown': true
  101. }
  102. }
  103. },
  104. 'id': 'c-196'
  105. }
  106. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  107. assert_response(201)
  108. result = JSON.parse(@response.body)
  109. assert(result)
  110. assert(result['data_option']['null'])
  111. assert_equal(result['data_option']['null'], true)
  112. assert_equal(result['name'], 'test2')
  113. end
  114. test 'update ticket text object' do
  115. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  116. # add a new object
  117. object = ObjectManager::Attribute.add(
  118. name: 'test3',
  119. object: 'Ticket',
  120. display: 'Test 3',
  121. active: true,
  122. data_type: 'input',
  123. data_option: {
  124. default: 'test',
  125. type: 'text',
  126. maxlength: 120,
  127. null: true
  128. },
  129. screens: {
  130. create_middle: {
  131. 'ticket.customer' => {
  132. shown: true,
  133. item_class: 'column'
  134. },
  135. 'ticket.agent' => {
  136. shown: true,
  137. item_class: 'column'
  138. }
  139. },
  140. edit: {
  141. 'ticket.customer' => {
  142. shown: true
  143. },
  144. 'ticket.agent' => {
  145. shown: true
  146. }
  147. }
  148. },
  149. position: 1550,
  150. editable: true
  151. )
  152. migration = ObjectManager::Attribute.migration_execute
  153. assert_equal(migration, true)
  154. post "/api/v1/object_manager_attributes/#{object.id}", params: {}, headers: @headers
  155. token = @response.headers['CSRF-TOKEN']
  156. # parameters for updating
  157. params = {
  158. 'name': 'test4',
  159. 'object': 'Ticket',
  160. 'display': 'Test 4',
  161. 'active': true,
  162. 'data_type': 'input',
  163. 'data_option': {
  164. 'default': 'test',
  165. 'type': 'text',
  166. 'maxlength': 120
  167. },
  168. 'screens': {
  169. 'create_middle': {
  170. 'ticket.customer': {
  171. 'shown': true,
  172. 'item_class': 'column'
  173. },
  174. 'ticket.agent': {
  175. 'shown': true,
  176. 'item_class': 'column'
  177. }
  178. },
  179. 'edit': {
  180. 'ticket.customer': {
  181. 'shown': true
  182. },
  183. 'ticket.agent': {
  184. 'shown': true
  185. }
  186. }
  187. },
  188. 'id': 'c-196'
  189. }
  190. # update the object
  191. put "/api/v1/object_manager_attributes/#{object.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  192. assert_response(200)
  193. result = JSON.parse(@response.body)
  194. assert(result)
  195. assert(result['data_option']['null'])
  196. assert_equal(result['name'], 'test4')
  197. assert_equal(result['display'], 'Test 4')
  198. end
  199. test 'add new ticket boolean object' do
  200. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  201. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  202. token = @response.headers['CSRF-TOKEN']
  203. # token based on headers
  204. params = {
  205. 'active': true,
  206. 'data_option': {
  207. 'options': {
  208. 'false': 'no',
  209. 'true': 'yes'
  210. }
  211. },
  212. 'data_type': 'boolean',
  213. 'display': 'Boolean 2',
  214. 'id': 'c-200',
  215. 'name': 'bool2',
  216. 'object': 'Ticket',
  217. 'screens': {
  218. 'create_middle': {
  219. 'ticket.agent' => {
  220. 'item_class': 'column',
  221. 'shown': true
  222. },
  223. 'ticket.customer' => {
  224. 'item_class': 'column',
  225. 'shown': true
  226. }
  227. },
  228. 'edit': {
  229. 'ticket.agent' => {
  230. 'shown': true
  231. },
  232. 'ticket.customer' => {
  233. 'shown': true
  234. }
  235. }
  236. }
  237. }
  238. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  239. assert_response(201)
  240. result = JSON.parse(@response.body)
  241. assert(result)
  242. assert(result['data_option']['null'])
  243. assert_equal(result['data_option']['null'], true)
  244. assert_equal(result['name'], 'bool2')
  245. end
  246. test 'add new user select object' do
  247. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  248. post '/api/v1/object_manager_attributes', params: {}, headers: @headers
  249. token = @response.headers['CSRF-TOKEN']
  250. # token based on headers
  251. params = {
  252. 'active': true,
  253. 'data_option': {
  254. 'options': {
  255. 'key1': 'foo'
  256. }
  257. },
  258. 'data_type': 'select',
  259. 'display': 'Test 5',
  260. 'id': 'c-204',
  261. 'name': 'test5',
  262. 'object': 'User',
  263. 'screens': {
  264. 'create': {
  265. 'admin.user' => {
  266. 'shown': true
  267. },
  268. 'ticket.agent' => {
  269. 'shown': true
  270. },
  271. 'ticket.customer' => {
  272. 'shown': true
  273. }
  274. },
  275. 'edit': {
  276. 'admin.user' => {
  277. 'shown': true
  278. },
  279. 'ticket.agent' => {
  280. 'shown': true
  281. }
  282. },
  283. 'view': {
  284. 'admin.user' => {
  285. 'shown': true
  286. },
  287. 'ticket.agent' => {
  288. 'shown': true
  289. },
  290. 'ticket.customer' => {
  291. 'shown': true
  292. }
  293. }
  294. }
  295. }
  296. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  297. assert_response(201)
  298. result = JSON.parse(@response.body)
  299. assert(result)
  300. assert(result['data_option']['null'])
  301. assert_equal(result['data_option']['null'], true)
  302. assert_equal(result['name'], 'test5')
  303. end
  304. test 'update user select object' do
  305. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin', 'adminpw')
  306. # add a new object
  307. object = ObjectManager::Attribute.add(
  308. active: true,
  309. data_option: {
  310. options: {
  311. key1: 'foo'
  312. },
  313. null: true,
  314. default: '',
  315. },
  316. data_type: 'select',
  317. display: 'Test 6',
  318. id: 'c-204',
  319. name: 'test6',
  320. object: 'User',
  321. screens: {
  322. create: {
  323. 'admin.user' => {
  324. shown: true
  325. },
  326. 'ticket.agent' => {
  327. shown: true
  328. },
  329. 'ticket.customer' => {
  330. shown: true
  331. }
  332. },
  333. edit: {
  334. 'admin.user' => {
  335. shown: true
  336. },
  337. 'ticket.agent' => {
  338. shown: true
  339. }
  340. },
  341. view: {
  342. 'admin.user' => {
  343. shown: true
  344. },
  345. 'ticket.agent' => {
  346. shown: true
  347. },
  348. 'ticket.customer' => {
  349. shown: true
  350. }
  351. }
  352. },
  353. position: 1550,
  354. editable: true
  355. )
  356. migration = ObjectManager::Attribute.migration_execute
  357. assert_equal(migration, true)
  358. post "/api/v1/object_manager_attributes/#{object.id}", params: {}, headers: @headers
  359. token = @response.headers['CSRF-TOKEN']
  360. # parameters for updating
  361. params = {
  362. active: true,
  363. data_option: {
  364. options: {
  365. key1: 'foo',
  366. key2: 'bar'
  367. }
  368. },
  369. data_type: 'select',
  370. display: 'Test 7',
  371. id: 'c-204',
  372. name: 'test7',
  373. object: 'User',
  374. screens: {
  375. create: {
  376. 'admin.user' => {
  377. shown: true
  378. },
  379. 'ticket.agent' => {
  380. shown: true
  381. },
  382. 'ticket.customer' => {
  383. shown: true
  384. }
  385. },
  386. edit: {
  387. 'admin.user' => {
  388. shown: true
  389. },
  390. 'ticket.agent' => {
  391. shown: true
  392. }
  393. },
  394. view: {
  395. 'admin.user' => {
  396. shown: true
  397. },
  398. 'ticket.agent' => {
  399. shown: true
  400. },
  401. 'ticket.customer' => {
  402. shown: true
  403. }
  404. }
  405. }
  406. }
  407. # update the object
  408. put "/api/v1/object_manager_attributes/#{object.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  409. assert_response(200)
  410. result = JSON.parse(@response.body)
  411. assert(result)
  412. assert(result['data_option']['options'])
  413. assert_equal(result['name'], 'test7')
  414. assert_equal(result['display'], 'Test 7')
  415. end
  416. test '01 converts string to boolean for default value for boolean data type with true' do
  417. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  418. params = {
  419. 'name': "customerdescription#{rand(999_999_999)}",
  420. 'object': 'Ticket',
  421. 'display': "custom description#{rand(999_999_999)}",
  422. 'active': true,
  423. 'data_type': 'boolean',
  424. 'data_option': {
  425. 'options': {
  426. 'true': '',
  427. 'false': '',
  428. },
  429. 'default': 'true',
  430. 'screens': {
  431. 'create_middle': {
  432. 'ticket.customer': {
  433. 'shown': true,
  434. 'item_class': 'column'
  435. },
  436. 'ticket.agent': {
  437. 'shown': true,
  438. 'item_class': 'column'
  439. }
  440. },
  441. 'edit': {
  442. 'ticket.customer': {
  443. 'shown': true
  444. },
  445. 'ticket.agent': {
  446. 'shown': true
  447. }
  448. }
  449. }
  450. },
  451. 'id': 'c-201'
  452. }
  453. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  454. migration = ObjectManager::Attribute.migration_execute
  455. assert_equal(migration, true)
  456. assert_response(201) # created
  457. result = JSON.parse(@response.body)
  458. assert(result)
  459. assert(result['data_option']['default'])
  460. assert_equal(result['data_option']['default'], true)
  461. assert_equal(result['data_type'], 'boolean')
  462. end
  463. test '02 converts string to boolean for default value for boolean data type with false' do
  464. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('tickets-admin@example.com', 'adminpw')
  465. params = {
  466. 'name': "customerdescription_#{rand(999_999_999)}",
  467. 'object': 'Ticket',
  468. 'display': "custom description #{rand(999_999_999)}",
  469. 'active': true,
  470. 'data_type': 'boolean',
  471. 'data_option': {
  472. 'options': {
  473. 'true': '',
  474. 'false': '',
  475. },
  476. 'default': 'false',
  477. 'screens': {
  478. 'create_middle': {
  479. 'ticket.customer': {
  480. 'shown': true,
  481. 'item_class': 'column'
  482. },
  483. 'ticket.agent': {
  484. 'shown': true,
  485. 'item_class': 'column'
  486. }
  487. },
  488. 'edit': {
  489. 'ticket.customer': {
  490. 'shown': true
  491. },
  492. 'ticket.agent': {
  493. 'shown': true
  494. }
  495. }
  496. }
  497. },
  498. 'id': 'c-202'
  499. }
  500. post '/api/v1/object_manager_attributes', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  501. migration = ObjectManager::Attribute.migration_execute
  502. assert_equal(migration, true)
  503. assert_response(201) # created
  504. result = JSON.parse(@response.body)
  505. assert(result)
  506. assert_not(result['data_option']['default'])
  507. assert_equal(result['data_option']['default'], false)
  508. assert_equal(result['data_type'], 'boolean')
  509. end
  510. end