model_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class ModelTest < ActiveSupport::TestCase
  4. test 'create_if_not_exists test' do
  5. group1 = Group.create_if_not_exists(
  6. name: 'model1-create_if_not_exists',
  7. active: true,
  8. updated_at: '2015-02-05 16:37:00',
  9. updated_by_id: 1,
  10. created_by_id: 1,
  11. )
  12. assert_raises(ActiveRecord::RecordNotUnique) do
  13. Group.create_if_not_exists(
  14. name: 'model1-Create_If_Not_Exists',
  15. active: true,
  16. updated_at: '2015-02-05 16:37:00',
  17. updated_by_id: 1,
  18. created_by_id: 1,
  19. )
  20. end
  21. group2 = Group.create_if_not_exists(
  22. name: 'model1-create_if_not_exists',
  23. active: true,
  24. updated_at: '2015-02-05 16:39:00',
  25. updated_by_id: 1,
  26. created_by_id: 1,
  27. )
  28. assert_equal(group1.id, group2.id)
  29. assert_equal(group2.updated_at.to_s, '2015-02-05 16:37:00 UTC')
  30. end
  31. test 'create_or_update test' do
  32. group1 = Group.create_or_update(
  33. name: 'model1-create_or_update',
  34. active: true,
  35. updated_at: '2015-02-05 16:37:00',
  36. updated_by_id: 1,
  37. created_by_id: 1,
  38. )
  39. assert_raises(ActiveRecord::RecordNotUnique) do
  40. Group.create_or_update(
  41. name: 'model1-Create_Or_Update',
  42. active: true,
  43. updated_at: '2015-02-05 16:37:00',
  44. updated_by_id: 1,
  45. created_by_id: 1,
  46. )
  47. end
  48. group2 = Group.create_or_update(
  49. name: 'model1-create_or_update',
  50. active: true,
  51. updated_at: '2015-02-05 16:39:00',
  52. updated_by_id: 1,
  53. created_by_id: 1,
  54. )
  55. assert_equal(group1.id, group2.id)
  56. assert_equal(group2.updated_at.to_s, '2015-02-05 16:39:00 UTC')
  57. end
  58. test 'references test' do
  59. # create base
  60. groups = Group.where(name: 'Users')
  61. roles = Role.where(name: %w[Agent Admin])
  62. agent1 = User.create_or_update(
  63. login: 'model-agent1@example.com',
  64. firstname: 'Model',
  65. lastname: 'Agent1',
  66. email: 'model-agent1@example.com',
  67. password: 'agentpw',
  68. active: true,
  69. roles: roles,
  70. groups: groups,
  71. updated_at: '2015-02-05 16:37:00',
  72. updated_by_id: 1,
  73. created_by_id: 1,
  74. )
  75. agent2 = User.create_or_update(
  76. login: 'model-agent2@example.com',
  77. firstname: 'Model',
  78. lastname: 'Agent2',
  79. email: 'model-agent2@example.com',
  80. password: 'agentpw',
  81. active: true,
  82. roles: roles,
  83. groups: groups,
  84. updated_at: '2015-02-05 17:37:00',
  85. updated_by_id: agent1.id,
  86. created_by_id: 1,
  87. )
  88. organization1 = Organization.create_if_not_exists(
  89. name: 'Model Org 1',
  90. updated_at: '2015-02-05 16:37:00',
  91. updated_by_id: 1,
  92. created_by_id: 1,
  93. )
  94. organization2 = Organization.create_if_not_exists(
  95. name: 'Model Org 2',
  96. updated_at: '2015-02-05 16:37:00',
  97. updated_by_id: agent1.id,
  98. created_by_id: 1,
  99. )
  100. roles = Role.where(name: 'Customer')
  101. User.create_or_update(
  102. login: 'model-customer1@example.com',
  103. firstname: 'Model',
  104. lastname: 'Customer1',
  105. email: 'model-customer1@example.com',
  106. password: 'customerpw',
  107. active: true,
  108. organization_id: organization1.id,
  109. roles: roles,
  110. updated_at: '2015-02-05 16:37:00',
  111. updated_by_id: 1,
  112. created_by_id: 1,
  113. )
  114. User.create_or_update(
  115. login: 'model-customer2@example.com',
  116. firstname: 'Model',
  117. lastname: 'Customer2',
  118. email: 'model-customer2@example.com',
  119. password: 'customerpw',
  120. active: true,
  121. organization_id: nil,
  122. roles: roles,
  123. updated_at: '2015-02-05 16:37:00',
  124. updated_by_id: agent1.id,
  125. created_by_id: 1,
  126. )
  127. User.create_or_update(
  128. login: 'model-customer3@example.com',
  129. firstname: 'Model',
  130. lastname: 'Customer3',
  131. email: 'model-customer3@example.com',
  132. password: 'customerpw',
  133. active: true,
  134. organization_id: nil,
  135. roles: roles,
  136. updated_at: '2015-02-05 16:37:00',
  137. updated_by_id: agent1.id,
  138. created_by_id: agent1.id,
  139. )
  140. # user
  141. # verify agent1
  142. references1 = Models.references('User', agent1.id)
  143. assert_equal(references1['User']['updated_by_id'], 3)
  144. assert_equal(references1['User']['created_by_id'], 1)
  145. assert_equal(references1['Organization']['updated_by_id'], 1)
  146. assert_equal(references1['UserGroup']['user_id'], 1)
  147. assert_not(references1['Group'])
  148. references_total1 = Models.references_total('User', agent1.id)
  149. assert_equal(references_total1, 8)
  150. # verify agent2
  151. references2 = Models.references('User', agent2.id)
  152. assert_not(references2['User'])
  153. assert_not(references2['Organization'])
  154. assert_not(references2['Group'])
  155. assert_equal(references2['UserGroup']['user_id'], 1)
  156. references_total2 = Models.references_total('User', agent2.id)
  157. assert_equal(references_total2, 1)
  158. Models.merge('User', agent2.id, agent1.id)
  159. # verify agent1
  160. references1 = Models.references('User', agent1.id)
  161. assert_not(references1['User'])
  162. assert_not(references1['Organization'])
  163. assert_not(references1['Group'])
  164. assert_not(references1['UserGroup'])
  165. assert(references1.blank?)
  166. references_total1 = Models.references_total('User', agent1.id)
  167. assert_equal(references_total1, 0)
  168. # verify agent2
  169. references2 = Models.references('User', agent2.id)
  170. assert_equal(references2['User']['updated_by_id'], 3)
  171. assert_equal(references2['User']['created_by_id'], 1)
  172. assert_equal(references2['Organization']['updated_by_id'], 1)
  173. assert_equal(references2['UserGroup']['user_id'], 2)
  174. assert_not(references2['Group'])
  175. references_total2 = Models.references_total('User', agent2.id)
  176. assert_equal(references_total2, 9)
  177. # org
  178. # verify agent1
  179. references1 = Models.references('Organization', organization1.id)
  180. assert_equal(references1['User']['organization_id'], 1)
  181. assert_not(references1['Organization'])
  182. assert_not(references1['Group'])
  183. references_total1 = Models.references_total('Organization', organization1.id)
  184. assert_equal(references_total1, 1)
  185. # verify agent2
  186. references2 = Models.references('Organization', organization2.id)
  187. assert(references2.blank?)
  188. references_total2 = Models.references_total('Organization', organization2.id)
  189. assert_equal(references_total2, 0)
  190. Models.merge('Organization', organization2.id, organization1.id)
  191. # verify agent1
  192. references1 = Models.references('Organization', organization1.id)
  193. assert(references1.blank?)
  194. references_total1 = Models.references_total('Organization', organization1.id)
  195. assert_equal(references_total1, 0)
  196. # verify agent2
  197. references2 = Models.references('Organization', organization2.id)
  198. assert_equal(references2['User']['organization_id'], 1)
  199. assert_not(references2['Organization'])
  200. assert_not(references2['Group'])
  201. references_total2 = Models.references_total('Organization', organization2.id)
  202. assert_equal(references_total2, 1)
  203. end
  204. test 'searchable test' do
  205. searchable = Models.searchable
  206. assert(searchable.include?(Ticket))
  207. assert(searchable.include?(User))
  208. assert(searchable.include?(Organization))
  209. assert(searchable.include?(Chat::Session))
  210. assert(searchable.include?(KnowledgeBase::Answer::Translation))
  211. assert_equal(5, searchable.count)
  212. end
  213. test 'param_cleanup test' do
  214. params = {
  215. id: 123,
  216. abc: true,
  217. firstname: '123',
  218. created_by_id: 1,
  219. created_at: Time.zone.now,
  220. updated_by_id: 1,
  221. updated_at: Time.zone.now,
  222. action: 'some action',
  223. controller: 'some controller',
  224. }
  225. result = User.param_cleanup(params, true)
  226. assert_not(result.key?(:id))
  227. assert_not(result.key?(:abc))
  228. assert_equal('123', result[:firstname])
  229. assert_not(result.key?(:created_by_id))
  230. assert_not(result.key?(:created_at))
  231. assert_not(result.key?(:updated_by_id))
  232. assert_not(result.key?(:updated_at))
  233. assert_not(result.key?(:action))
  234. assert_not(result.key?(:controller))
  235. params = {
  236. id: 123,
  237. abc: true,
  238. firstname: '123',
  239. created_by_id: 1,
  240. created_at: Time.zone.now,
  241. updated_by_id: 1,
  242. updated_at: Time.zone.now,
  243. action: 'some action',
  244. controller: 'some controller',
  245. }
  246. result = User.param_cleanup(params)
  247. assert_equal(123, result[:id])
  248. assert_not(result.key?(:abc))
  249. assert_equal('123', result[:firstname])
  250. assert_not(result.key?(:created_by_id))
  251. assert_not(result.key?(:created_at))
  252. assert_not(result.key?(:updated_by_id))
  253. assert_not(result.key?(:updated_at))
  254. assert_not(result.key?(:action))
  255. assert_not(result.key?(:controller))
  256. Setting.set('import_mode', true)
  257. params = {
  258. id: 123,
  259. abc: true,
  260. firstname: '123',
  261. created_by_id: 1,
  262. created_at: Time.zone.now,
  263. updated_by_id: 1,
  264. updated_at: Time.zone.now,
  265. action: 'some action',
  266. controller: 'some controller',
  267. }
  268. result = User.param_cleanup(params, true)
  269. assert_not(result.key?(:abc))
  270. assert_equal('123', result[:firstname])
  271. assert_equal(1, result[:created_by_id])
  272. assert(result[:created_at])
  273. assert_equal(1, result[:updated_by_id])
  274. assert(result[:updated_at])
  275. assert_not(result.key?(:action))
  276. assert_not(result.key?(:controller))
  277. params = {
  278. id: 123,
  279. abc: true,
  280. firstname: '123',
  281. created_by_id: 1,
  282. created_at: Time.zone.now,
  283. updated_by_id: 1,
  284. updated_at: Time.zone.now,
  285. action: 'some action',
  286. controller: 'some controller',
  287. }
  288. result = User.param_cleanup(params)
  289. assert_equal(123, result[:id])
  290. assert_equal('123', result[:firstname])
  291. assert_equal(1, result[:created_by_id])
  292. assert(result[:created_at])
  293. assert_equal(1, result[:updated_by_id])
  294. assert(result[:updated_at])
  295. assert_not(result.key?(:action))
  296. assert_not(result.key?(:controller))
  297. end
  298. test 'param_preferences_merge test' do
  299. params = {
  300. id: 123,
  301. firstname: '123',
  302. created_by_id: 1,
  303. created_at: Time.zone.now,
  304. updated_by_id: 1,
  305. updated_at: Time.zone.now,
  306. preferences: {},
  307. }
  308. user = User.new(params)
  309. assert(user.preferences.blank?)
  310. user.preferences = { A: 1, B: 2 }
  311. assert(user.preferences.present?)
  312. params = {
  313. firstname: '123 ABC',
  314. preferences: { 'B' => 3, C: 4 },
  315. }
  316. clean_params = User.param_cleanup(params)
  317. clean_user_params = user.param_preferences_merge(clean_params)
  318. assert_equal(clean_user_params[:firstname], '123 ABC')
  319. assert(clean_user_params[:preferences].present?)
  320. assert_equal(clean_user_params[:preferences]['A'], 1)
  321. assert_equal(clean_user_params[:preferences]['B'], 3)
  322. assert_equal(clean_user_params[:preferences]['C'], 4)
  323. assert_equal(clean_user_params[:preferences][:A], 1)
  324. assert_equal(clean_user_params[:preferences][:B], 3)
  325. assert_equal(clean_user_params[:preferences][:C], 4)
  326. params = {
  327. firstname: '123 ABCD',
  328. preferences: {},
  329. }
  330. clean_params = User.param_cleanup(params)
  331. clean_user_params = user.param_preferences_merge(clean_params)
  332. assert_equal(clean_user_params[:firstname], '123 ABCD')
  333. assert(clean_user_params[:preferences].present?)
  334. assert_equal(clean_user_params[:preferences]['A'], 1)
  335. assert_equal(clean_user_params[:preferences]['B'], 2)
  336. assert_nil(clean_user_params[:preferences]['C'])
  337. assert_equal(clean_user_params[:preferences][:A], 1)
  338. assert_equal(clean_user_params[:preferences][:B], 2)
  339. assert_nil(clean_user_params[:preferences][:C])
  340. end
  341. end