activity_stream_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ActivityStreamTest < ActiveSupport::TestCase
  4. role = Role.lookup(name: 'Admin')
  5. group = Group.lookup(name: 'Users')
  6. admin_user = User.create_or_update(
  7. login: 'admin',
  8. firstname: 'Bob',
  9. lastname: 'Smith',
  10. email: 'bob@example.com',
  11. password: 'some_pass',
  12. active: true,
  13. role_ids: [role.id],
  14. group_ids: [group.id],
  15. updated_by_id: 1,
  16. created_by_id: 1
  17. )
  18. current_user = User.lookup(email: 'nicole.braun@zammad.org')
  19. activity_record_delay = if ENV['ZAMMAD_ACTIVITY_RECORD_DELAY']
  20. ENV['ZAMMAD_ACTIVITY_RECORD_DELAY'].to_i.seconds
  21. else
  22. 90.seconds
  23. end
  24. test 'ticket+user' do
  25. tests = [
  26. # test 1
  27. {
  28. create: {
  29. ticket: {
  30. group_id: Group.lookup(name: 'Users').id,
  31. customer_id: current_user.id,
  32. owner_id: User.lookup(login: '-').id,
  33. title: 'Unit Test 1 (äöüß)!',
  34. state_id: Ticket::State.lookup(name: 'new').id,
  35. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  36. updated_by_id: current_user.id,
  37. created_by_id: current_user.id,
  38. },
  39. article: {
  40. updated_by_id: current_user.id,
  41. created_by_id: current_user.id,
  42. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  43. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  44. from: 'Unit Test <unittest@example.com>',
  45. body: 'Unit Test 123',
  46. internal: false,
  47. },
  48. },
  49. update: {
  50. ticket: {
  51. title: 'Unit Test 1 (äöüß) - update!',
  52. state_id: Ticket::State.lookup(name: 'open').id,
  53. priority_id: Ticket::Priority.lookup(name: '1 low').id,
  54. },
  55. },
  56. update2: {
  57. ticket: {
  58. title: 'Unit Test 2 (äöüß) - update!',
  59. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  60. },
  61. },
  62. check: [
  63. {
  64. result: true,
  65. object: 'Ticket',
  66. type: 'update',
  67. },
  68. {
  69. result: true,
  70. object: 'Ticket::Article',
  71. type: 'create',
  72. },
  73. {
  74. result: true,
  75. object: 'Ticket',
  76. type: 'create',
  77. },
  78. {
  79. result: false,
  80. object: 'User',
  81. type: 'update',
  82. o_id: current_user.id,
  83. },
  84. ]
  85. },
  86. ]
  87. tickets = []
  88. tests.each { |test|
  89. ticket = Ticket.create(test[:create][:ticket])
  90. test[:check][0][:o_id] = ticket.id
  91. test[:check][2][:o_id] = ticket.id
  92. test[:check][2][:created_at] = ticket.created_at
  93. test[:check][2][:created_by_id] = current_user.id
  94. sleep 2
  95. test[:create][:article][:ticket_id] = ticket.id
  96. article = Ticket::Article.create(test[:create][:article])
  97. test[:check][1][:o_id] = article.id
  98. test[:check][1][:created_at] = article.created_at
  99. test[:check][1][:created_by_id] = current_user.id
  100. assert_equal(ticket.class.to_s, 'Ticket')
  101. assert_equal(article.class.to_s, 'Ticket::Article')
  102. # update ticket
  103. if test[:update][:ticket]
  104. ticket.update_attributes(test[:update][:ticket])
  105. # check updated user
  106. test[:check][3][:o_id] = current_user.id
  107. test[:check][3][:created_at] = ticket.created_at
  108. test[:check][3][:created_by_id] = current_user.id
  109. end
  110. if test[:update2][:ticket]
  111. ticket = Ticket.find(ticket.id)
  112. ticket.update_attributes(test[:update2][:ticket])
  113. end
  114. if test[:update][:article]
  115. article.update_attributes(test[:update][:article])
  116. end
  117. sleep activity_record_delay + 1
  118. if test[:update][:ticket]
  119. ticket.update_attributes(test[:update][:ticket])
  120. end
  121. if test[:update2][:ticket]
  122. ticket.update_attributes(test[:update2][:ticket])
  123. end
  124. # remember ticket
  125. tickets.push ticket
  126. # check activity_stream
  127. activity_stream_check(admin_user.activity_stream(3), test[:check])
  128. }
  129. # delete tickets
  130. tickets.each { |ticket|
  131. ticket_id = ticket.id
  132. ticket.destroy
  133. found = Ticket.where(id: ticket_id).first
  134. assert_not(found, 'Ticket destroyed')
  135. }
  136. end
  137. test 'organization' do
  138. tests = [
  139. # test 1
  140. {
  141. create: {
  142. organization: {
  143. name: 'some name',
  144. updated_by_id: current_user.id,
  145. created_by_id: current_user.id,
  146. },
  147. },
  148. update1: {
  149. organization: {
  150. name: 'some name (äöüß)',
  151. },
  152. },
  153. update2: {
  154. organization: {
  155. name: 'some name 2 (äöüß)',
  156. },
  157. },
  158. check: [
  159. {
  160. result: true,
  161. object: 'Organization',
  162. type: 'update',
  163. },
  164. {
  165. result: true,
  166. object: 'Organization',
  167. type: 'create',
  168. },
  169. ]
  170. },
  171. ]
  172. organizations = []
  173. tests.each { |test|
  174. organization = Organization.create(test[:create][:organization])
  175. test[:check][0][:o_id] = organization.id
  176. test[:check][0][:created_at] = organization.created_at
  177. test[:check][0][:created_by_id] = current_user.id
  178. sleep 2
  179. assert_equal(organization.class.to_s, 'Organization')
  180. if test[:update1][:organization]
  181. organization.update_attributes(test[:update1][:organization])
  182. test[:check][1][:o_id] = organization.id
  183. test[:check][1][:updated_at] = organization.updated_at
  184. test[:check][1][:created_by_id] = current_user.id
  185. sleep activity_record_delay - 1
  186. end
  187. if test[:update2][:organization]
  188. organization.update_attributes(test[:update2][:organization])
  189. end
  190. # remember organization
  191. organizations.push organization
  192. # check activity_stream
  193. activity_stream_check(admin_user.activity_stream(2), test[:check])
  194. }
  195. # delete tickets
  196. organizations.each { |organization|
  197. organization_id = organization.id
  198. organization.destroy
  199. found = Organization.where(id: organization_id).first
  200. assert( !found, 'Organization destroyed')
  201. }
  202. end
  203. test 'user with update check false' do
  204. tests = [
  205. # test 1
  206. {
  207. create: {
  208. user: {
  209. login: 'someemail@example.com',
  210. email: 'Bob Smith II <someemail@example.com>',
  211. updated_by_id: current_user.id,
  212. created_by_id: current_user.id,
  213. },
  214. },
  215. update1: {
  216. user: {
  217. firstname: 'Bob U',
  218. lastname: 'Smith U',
  219. },
  220. },
  221. check: [
  222. {
  223. result: true,
  224. object: 'User',
  225. type: 'create',
  226. },
  227. {
  228. result: false,
  229. object: 'User',
  230. type: 'update',
  231. },
  232. ]
  233. },
  234. ]
  235. users = []
  236. tests.each { |test|
  237. user = User.create(test[:create][:user])
  238. test[:check][0][:o_id] = user.id
  239. test[:check][0][:created_at] = user.created_at
  240. test[:check][0][:created_by_id] = current_user.id
  241. assert_equal(user.class.to_s, 'User')
  242. if test[:update1][:user]
  243. user.update_attributes(test[:update1][:user])
  244. test[:check][1][:o_id] = user.id
  245. test[:check][1][:updated_at] = user.updated_at
  246. test[:check][1][:created_by_id] = current_user.id
  247. end
  248. # remember organization
  249. users.push user
  250. # check activity_stream
  251. activity_stream_check(admin_user.activity_stream(3), test[:check])
  252. }
  253. # delete tickets
  254. users.each { |user|
  255. user_id = user.id
  256. user.destroy
  257. found = User.where( id: user_id ).first
  258. assert_not(found, 'User destroyed')
  259. }
  260. end
  261. test 'user with update check true' do
  262. tests = [
  263. # test 1
  264. {
  265. create: {
  266. user: {
  267. login: 'someemail@example.com',
  268. email: 'Bob Smith II <someemail@example.com>',
  269. updated_by_id: current_user.id,
  270. created_by_id: current_user.id,
  271. },
  272. },
  273. update1: {
  274. user: {
  275. firstname: 'Bob U',
  276. lastname: 'Smith U',
  277. },
  278. },
  279. update2: {
  280. user: {
  281. firstname: 'Bob',
  282. lastname: 'Smith',
  283. },
  284. },
  285. check: [
  286. {
  287. result: true,
  288. object: 'User',
  289. type: 'update',
  290. },
  291. {
  292. result: true,
  293. object: 'User',
  294. type: 'create',
  295. },
  296. ]
  297. },
  298. ]
  299. users = []
  300. tests.each { |test|
  301. user = User.create(test[:create][:user])
  302. test[:check][0][:o_id] = user.id
  303. test[:check][0][:created_at] = user.created_at
  304. test[:check][0][:created_by_id] = current_user.id
  305. assert_equal(user.class.to_s, 'User')
  306. if test[:update1][:user]
  307. user.update_attributes(test[:update1][:user])
  308. test[:check][1][:o_id] = user.id
  309. test[:check][1][:updated_at] = user.updated_at
  310. test[:check][1][:created_by_id] = current_user.id
  311. end
  312. # to verify update which need to be logged
  313. sleep activity_record_delay + 1
  314. if test[:update2][:user]
  315. user.update_attributes(test[:update2][:user])
  316. end
  317. # remember organization
  318. users.push user
  319. # check activity_stream
  320. activity_stream_check(admin_user.activity_stream(2), test[:check])
  321. }
  322. # delete tickets
  323. users.each { |user|
  324. user_id = user.id
  325. user.destroy
  326. found = User.where(id: user_id).first
  327. assert(!found, 'User destroyed')
  328. }
  329. end
  330. def activity_stream_check(activity_stream_list, checks)
  331. #activity_stream_list = activity_stream_list.reverse
  332. #puts 'AS ' + activity_stream_list.inspect
  333. check_count = 0
  334. checks.each { |check_item|
  335. check_count += 1
  336. #puts '+++++++++++'
  337. #puts check_item.inspect
  338. check_list = 0
  339. activity_stream_list.each { |item|
  340. check_list += 1
  341. next if check_list != check_count
  342. #next if match
  343. #puts '--------'
  344. #puts item.inspect
  345. #puts check_item.inspect
  346. if check_item[:result]
  347. assert_equal(check_item[:object], item['object'])
  348. assert_equal(check_item[:type], item['type'])
  349. assert_equal(check_item[:o_id], item['o_id'])
  350. elsif check_item[:object] == item['object'] && check_item[:type] == item['type'] && check_item[:o_id] == item['o_id']
  351. assert(false, "entry should not exist #{item['object']}/#{item['type']}/#{item['o_id']}")
  352. end
  353. }
  354. }
  355. end
  356. end