model_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. 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. 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. 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(searchable.include?(KnowledgeBase::Answer::Translation))
  210. assert_equal(5, searchable.count)
  211. end
  212. test 'param_cleanup test' do
  213. params = {
  214. id: 123,
  215. abc: true,
  216. firstname: '123',
  217. created_by_id: 1,
  218. created_at: Time.zone.now,
  219. updated_by_id: 1,
  220. updated_at: Time.zone.now,
  221. action: 'some action',
  222. controller: 'some controller',
  223. }
  224. result = User.param_cleanup(params, true)
  225. assert_not(result.key?(:id))
  226. assert_not(result.key?(:abc))
  227. assert_equal('123', result[:firstname])
  228. assert_not(result.key?(:created_by_id))
  229. assert_not(result.key?(:created_at))
  230. assert_not(result.key?(:updated_by_id))
  231. assert_not(result.key?(:updated_at))
  232. assert_not(result.key?(:action))
  233. assert_not(result.key?(:controller))
  234. params = {
  235. id: 123,
  236. abc: true,
  237. firstname: '123',
  238. created_by_id: 1,
  239. created_at: Time.zone.now,
  240. updated_by_id: 1,
  241. updated_at: Time.zone.now,
  242. action: 'some action',
  243. controller: 'some controller',
  244. }
  245. result = User.param_cleanup(params)
  246. assert_equal(123, result[:id])
  247. assert_not(result.key?(:abc))
  248. assert_equal('123', result[:firstname])
  249. assert_not(result.key?(:created_by_id))
  250. assert_not(result.key?(:created_at))
  251. assert_not(result.key?(:updated_by_id))
  252. assert_not(result.key?(:updated_at))
  253. assert_not(result.key?(:action))
  254. assert_not(result.key?(:controller))
  255. Setting.set('import_mode', true)
  256. params = {
  257. id: 123,
  258. abc: true,
  259. firstname: '123',
  260. created_by_id: 1,
  261. created_at: Time.zone.now,
  262. updated_by_id: 1,
  263. updated_at: Time.zone.now,
  264. action: 'some action',
  265. controller: 'some controller',
  266. }
  267. result = User.param_cleanup(params, true)
  268. assert_not(result.key?(:abc))
  269. assert_equal('123', result[:firstname])
  270. assert_equal(1, result[:created_by_id])
  271. assert(result[:created_at])
  272. assert_equal(1, result[:updated_by_id])
  273. assert(result[:updated_at])
  274. assert_not(result.key?(:action))
  275. assert_not(result.key?(:controller))
  276. params = {
  277. id: 123,
  278. abc: true,
  279. firstname: '123',
  280. created_by_id: 1,
  281. created_at: Time.zone.now,
  282. updated_by_id: 1,
  283. updated_at: Time.zone.now,
  284. action: 'some action',
  285. controller: 'some controller',
  286. }
  287. result = User.param_cleanup(params)
  288. assert_equal(123, result[:id])
  289. assert_equal('123', result[:firstname])
  290. assert_equal(1, result[:created_by_id])
  291. assert(result[:created_at])
  292. assert_equal(1, result[:updated_by_id])
  293. assert(result[:updated_at])
  294. assert_not(result.key?(:action))
  295. assert_not(result.key?(:controller))
  296. end
  297. test 'param_preferences_merge test' do
  298. params = {
  299. id: 123,
  300. firstname: '123',
  301. created_by_id: 1,
  302. created_at: Time.zone.now,
  303. updated_by_id: 1,
  304. updated_at: Time.zone.now,
  305. preferences: {},
  306. }
  307. user = User.new(params)
  308. assert(user.preferences.blank?)
  309. user.preferences = { A: 1, B: 2 }
  310. assert(user.preferences.present?)
  311. params = {
  312. firstname: '123 ABC',
  313. preferences: { 'B' => 3, C: 4 },
  314. }
  315. clean_params = User.param_cleanup(params)
  316. clean_user_params = user.param_preferences_merge(clean_params)
  317. assert_equal(clean_user_params[:firstname], '123 ABC')
  318. assert(clean_user_params[:preferences].present?)
  319. assert_equal(clean_user_params[:preferences]['A'], 1)
  320. assert_equal(clean_user_params[:preferences]['B'], 3)
  321. assert_equal(clean_user_params[:preferences]['C'], 4)
  322. assert_equal(clean_user_params[:preferences][:A], 1)
  323. assert_equal(clean_user_params[:preferences][:B], 3)
  324. assert_equal(clean_user_params[:preferences][:C], 4)
  325. params = {
  326. firstname: '123 ABCD',
  327. preferences: {},
  328. }
  329. clean_params = User.param_cleanup(params)
  330. clean_user_params = user.param_preferences_merge(clean_params)
  331. assert_equal(clean_user_params[:firstname], '123 ABCD')
  332. assert(clean_user_params[:preferences].present?)
  333. assert_equal(clean_user_params[:preferences]['A'], 1)
  334. assert_equal(clean_user_params[:preferences]['B'], 2)
  335. assert_nil(clean_user_params[:preferences]['C'])
  336. assert_equal(clean_user_params[:preferences][:A], 1)
  337. assert_equal(clean_user_params[:preferences][:B], 2)
  338. assert_nil(clean_user_params[:preferences][:C])
  339. end
  340. end