model_test.rb 11 KB

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