activity_stream_test.rb 11 KB

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