history_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. require 'test_helper'
  2. class HistoryTest < ActiveSupport::TestCase
  3. test 'ticket' do
  4. current_user = User.lookup(email: 'nicole.braun@zammad.org')
  5. tests = [
  6. # test 1
  7. {
  8. ticket_create: {
  9. ticket: {
  10. group_id: Group.lookup(name: 'Users').id,
  11. customer_id: current_user.id,
  12. owner_id: User.lookup(login: '-').id,
  13. title: 'Unit Test 1 (äöüß)!',
  14. state_id: Ticket::State.lookup(name: 'new').id,
  15. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  16. updated_by_id: current_user.id,
  17. created_by_id: current_user.id,
  18. },
  19. article: {
  20. updated_by_id: current_user.id,
  21. created_by_id: current_user.id,
  22. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  23. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  24. from: 'Unit Test <unittest@example.com>',
  25. body: 'Unit Test 123',
  26. internal: false,
  27. },
  28. },
  29. ticket_update: {
  30. ticket: {
  31. title: 'Unit Test 1 (äöüß) - update!',
  32. state_id: Ticket::State.lookup(name: 'open').id,
  33. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  34. },
  35. },
  36. history_check: [
  37. {
  38. result: true,
  39. history_object: 'Ticket',
  40. history_type: 'created',
  41. },
  42. {
  43. result: true,
  44. history_object: 'Ticket',
  45. history_type: 'updated',
  46. history_attribute: 'title',
  47. value_from: 'Unit Test 1 (äöüß)!',
  48. value_to: 'Unit Test 1 (äöüß) - update!',
  49. },
  50. {
  51. result: true,
  52. history_object: 'Ticket',
  53. history_type: 'updated',
  54. history_attribute: 'state',
  55. value_from: 'new',
  56. value_to: 'open',
  57. id_from: Ticket::State.lookup(name: 'new').id,
  58. id_to: Ticket::State.lookup(name: 'open').id,
  59. },
  60. {
  61. result: true,
  62. history_object: 'Ticket::Article',
  63. history_type: 'created',
  64. },
  65. {
  66. result: false,
  67. history_object: 'User',
  68. history_type: 'updated',
  69. },
  70. ]
  71. },
  72. # test 2
  73. {
  74. ticket_create: {
  75. ticket: {
  76. group_id: Group.lookup(name: 'Users').id,
  77. customer_id: current_user.id,
  78. owner_id: User.lookup(login: '-').id,
  79. title: 'Unit Test 2 (äöüß)!',
  80. state_id: Ticket::State.lookup(name: 'new').id,
  81. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  82. updated_by_id: current_user.id,
  83. created_by_id: current_user.id,
  84. },
  85. article: {
  86. created_by_id: current_user.id,
  87. updated_by_id: current_user.id,
  88. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  89. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  90. from: 'Unit Test <unittest@example.com>',
  91. body: 'Unit Test 123',
  92. internal: false,
  93. },
  94. },
  95. ticket_update: {
  96. ticket: {
  97. title: 'Unit Test 2 (äöüß) - update!',
  98. state_id: Ticket::State.lookup(name: 'open').id,
  99. owner_id: current_user.id,
  100. },
  101. article: {
  102. body: 'Unit Test 123 - 2',
  103. },
  104. },
  105. history_check: [
  106. {
  107. result: true,
  108. history_object: 'Ticket',
  109. history_type: 'created',
  110. },
  111. {
  112. result: true,
  113. history_object: 'Ticket',
  114. history_type: 'updated',
  115. history_attribute: 'title',
  116. value_from: 'Unit Test 2 (äöüß)!',
  117. value_to: 'Unit Test 2 (äöüß) - update!',
  118. },
  119. {
  120. result: false,
  121. history_object: 'Ticket',
  122. history_type: 'updated',
  123. history_attribute: 'owner',
  124. value_from: '-',
  125. value_to: 'Nicole Braun',
  126. id_from: User.lookup(login: '-').id,
  127. id_to: current_user.id,
  128. },
  129. {
  130. result: true,
  131. history_object: 'Ticket::Article',
  132. history_type: 'created',
  133. },
  134. {
  135. result: true,
  136. history_object: 'Ticket::Article',
  137. history_type: 'updated',
  138. history_attribute: 'body',
  139. value_from: 'Unit Test 123',
  140. value_to: 'Unit Test 123 - 2',
  141. },
  142. ]
  143. },
  144. ]
  145. tickets = []
  146. tests.each do |test|
  147. ticket = nil
  148. article = nil
  149. # use transaction
  150. ActiveRecord::Base.transaction do
  151. ticket = Ticket.create!(test[:ticket_create][:ticket])
  152. test[:ticket_create][:article][:ticket_id] = ticket.id
  153. article = Ticket::Article.create!(test[:ticket_create][:article])
  154. assert_equal(ticket.class, Ticket)
  155. assert_equal(article.class, Ticket::Article)
  156. # update ticket
  157. if test[:ticket_update][:ticket]
  158. ticket.update!(test[:ticket_update][:ticket])
  159. end
  160. if test[:ticket_update][:article]
  161. article.update!(test[:ticket_update][:article])
  162. end
  163. end
  164. # execute object transaction
  165. Observer::Transaction.commit
  166. # execute background jobs
  167. Scheduler.worker(true)
  168. # remember ticket
  169. tickets.push ticket
  170. # check history
  171. history_check(ticket.history_get, test[:history_check])
  172. end
  173. # delete tickets
  174. tickets.each(&:destroy!)
  175. end
  176. test 'user' do
  177. current_user = User.lookup(email: 'nicole.braun@zammad.org')
  178. name = rand(999_999)
  179. tests = [
  180. # test 1
  181. {
  182. user_create: {
  183. user: {
  184. login: "some_login_test-#{name}",
  185. firstname: 'Bob',
  186. lastname: 'Smith',
  187. email: "somebody-#{name}@example.com",
  188. active: true,
  189. updated_by_id: current_user.id,
  190. created_by_id: current_user.id,
  191. },
  192. },
  193. user_update: {
  194. user: {
  195. firstname: 'Bob',
  196. lastname: 'Master',
  197. email: "master-#{name}@example.com",
  198. active: false,
  199. },
  200. },
  201. history_check: [
  202. {
  203. result: true,
  204. history_object: 'User',
  205. history_type: 'created',
  206. },
  207. {
  208. result: true,
  209. history_object: 'User',
  210. history_type: 'updated',
  211. history_attribute: 'lastname',
  212. value_from: 'Smith',
  213. value_to: 'Master',
  214. },
  215. {
  216. result: true,
  217. history_object: 'User',
  218. history_type: 'updated',
  219. history_attribute: 'email',
  220. value_from: "somebody-#{name}@example.com",
  221. value_to: "master-#{name}@example.com",
  222. },
  223. {
  224. result: true,
  225. history_object: 'User',
  226. history_type: 'updated',
  227. history_attribute: 'active',
  228. value_from: 'true',
  229. value_to: 'false',
  230. },
  231. ],
  232. },
  233. ]
  234. users = []
  235. tests.each do |test|
  236. user = nil
  237. # user transaction
  238. ActiveRecord::Base.transaction do
  239. user = User.create!(test[:user_create][:user])
  240. assert_equal(user.class, User)
  241. # update user
  242. if test[:user_update][:user]
  243. test[:user_update][:user][:active] = false
  244. user.update!(test[:user_update][:user])
  245. end
  246. end
  247. # remember user
  248. users.push user
  249. # check history
  250. history_check(user.history_get, test[:history_check])
  251. end
  252. # delete user
  253. users.each(&:destroy!)
  254. end
  255. test 'organization' do
  256. current_user = User.lookup(email: 'nicole.braun@zammad.org')
  257. tests = [
  258. # test 1
  259. {
  260. organization_create: {
  261. organization: {
  262. name: 'Org äöüß',
  263. note: 'some note',
  264. updated_by_id: current_user.id,
  265. created_by_id: current_user.id,
  266. },
  267. },
  268. organization_update: {
  269. organization: {
  270. name: 'Org 123',
  271. note: 'some note',
  272. },
  273. },
  274. history_check: [
  275. {
  276. result: true,
  277. history_object: 'Organization',
  278. history_type: 'created',
  279. },
  280. {
  281. result: true,
  282. history_object: 'Organization',
  283. history_type: 'updated',
  284. history_attribute: 'name',
  285. value_from: 'Org äöüß',
  286. value_to: 'Org 123',
  287. },
  288. ],
  289. },
  290. ]
  291. organizations = []
  292. tests.each do |test|
  293. organization = nil
  294. # user transaction
  295. ActiveRecord::Base.transaction do
  296. organization = Organization.create!(test[:organization_create][:organization])
  297. assert_equal(organization.class, Organization)
  298. # update organization
  299. if test[:organization_update][:organization]
  300. organization.update!(test[:organization_update][:organization])
  301. end
  302. end
  303. # remember user
  304. organizations.push organization
  305. # check history
  306. history_check(organization.history_get, test[:history_check])
  307. end
  308. # delete user
  309. organizations.each(&:destroy!)
  310. end
  311. test 'ticket assets' do
  312. UserInfo.current_user_id = 1
  313. agent1 = User.create!(
  314. login: 'agent1@example.com',
  315. firstname: 'agent',
  316. lastname: '1',
  317. email: 'agent1@example.com',
  318. password: 'agentpw',
  319. active: true,
  320. roles: Role.where(name: %w[Agent Admin]),
  321. groups: Group.all,
  322. )
  323. current_user = User.lookup(email: 'nicole.braun@zammad.org')
  324. UserInfo.current_user_id = current_user.id
  325. ticket = Ticket.create!(
  326. title: 'test 1',
  327. group: Group.first,
  328. customer_id: current_user.id,
  329. state: Ticket::State.lookup(name: 'new'),
  330. priority: Ticket::Priority.lookup(name: '2 normal'),
  331. )
  332. article = Ticket::Article.create!(
  333. ticket_id: ticket.id,
  334. from: 'some_customer_com-1@example.com',
  335. to: 'some_zammad_com-1@example.com',
  336. subject: 'com test 1',
  337. message_id: 'some@id_com_1',
  338. body: 'some message 123',
  339. internal: false,
  340. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  341. type: Ticket::Article::Type.find_by(name: 'email'),
  342. )
  343. # verify if user of history record is in assets
  344. UserInfo.current_user_id = agent1.id
  345. ticket.state = Ticket::State.find_by(name: 'closed')
  346. ticket.save!
  347. # update updated_by (to not include agent1 in assets by ticket)
  348. UserInfo.current_user_id = current_user.id
  349. ticket.priority = Ticket::Priority.find_by(name: '3 high')
  350. ticket.save!
  351. history = ticket.history_get(true)
  352. assert(history[:assets][:User][current_user.id])
  353. assert(history[:assets][:User][agent1.id])
  354. assert(history[:assets][:Ticket][ticket.id])
  355. assert(history[:assets][:TicketArticle][article.id])
  356. end
  357. def history_check(history_list, history_check)
  358. history_check.each do |check_item|
  359. match = false
  360. history_list.each do |history_item|
  361. next if match
  362. next if history_item['object'] != check_item[:history_object]
  363. next if history_item['type'] != check_item[:history_type]
  364. if check_item[:history_attribute]
  365. next if check_item[:history_attribute] != history_item['attribute']
  366. end
  367. match = true
  368. if history_item['type'] == check_item[:history_type]
  369. assert(true, "History type #{history_item['type']} found!")
  370. end
  371. if check_item[:history_attribute]
  372. assert_equal(check_item[:history_attribute], history_item['attribute'], "check history attribute #{check_item[:history_attribute]}")
  373. end
  374. if check_item[:value_from]
  375. assert_equal(check_item[:value_from], history_item['value_from'], "check history :value_from #{history_item['value_from']} ok")
  376. end
  377. if check_item[:value_to]
  378. assert_equal(check_item[:value_to], history_item['value_to'], "check history :value_to #{history_item['value_to']} ok")
  379. end
  380. if check_item[:id_from]
  381. assert_equal(check_item[:id_from], history_item['id_from'], "check history :id_from #{history_item['id_from']} ok")
  382. end
  383. if check_item[:id_to]
  384. assert_equal(check_item[:id_to], history_item['id_to'], "check history :id_to #{history_item['id_to']} ok")
  385. end
  386. end
  387. if check_item[:result]
  388. assert(match, "history check not matched! #{check_item.inspect}")
  389. else
  390. assert_not(match, "history check matched but should not! #{check_item.inspect}")
  391. end
  392. end
  393. end
  394. end