object_manager_attributes_controller_test.rb 14 KB

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