activity_stream_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 = nil
  85. article = nil
  86. ticket = Ticket.create( test[:create][:ticket] )
  87. test[:check][0][:o_id] = ticket.id
  88. test[:check][2][:o_id] = ticket.id
  89. test[:check][2][:created_at] = ticket.created_at
  90. test[:check][2][:created_by_id] = current_user.id
  91. sleep 2
  92. test[:create][:article][:ticket_id] = ticket.id
  93. article = Ticket::Article.create( test[:create][:article] )
  94. test[:check][1][:o_id] = article.id
  95. test[:check][1][:created_at] = article.created_at
  96. test[:check][1][:created_by_id] = current_user.id
  97. assert_equal( ticket.class.to_s, 'Ticket' )
  98. assert_equal( article.class.to_s, 'Ticket::Article' )
  99. # update ticket
  100. if test[:update][:ticket]
  101. ticket.update_attributes( test[:update][:ticket] )
  102. # check updated user
  103. test[:check][3][:o_id] = current_user.id
  104. test[:check][3][:created_at] = ticket.created_at
  105. test[:check][3][:created_by_id] = current_user.id
  106. end
  107. if test[:update2][:ticket]
  108. ticket = Ticket.find( ticket.id )
  109. ticket.update_attributes( test[:update2][:ticket] )
  110. end
  111. if test[:update][:article]
  112. article.update_attributes( test[:update][:article] )
  113. end
  114. sleep 15
  115. if test[:update][:ticket]
  116. ticket.update_attributes( test[:update][:ticket] )
  117. end
  118. if test[:update2][:ticket]
  119. ticket.update_attributes( test[:update2][:ticket] )
  120. end
  121. # remember ticket
  122. tickets.push ticket
  123. # check activity_stream
  124. activity_stream_check( admin_user.activity_stream(3), test[:check] )
  125. }
  126. # delete tickets
  127. tickets.each { |ticket|
  128. ticket_id = ticket.id
  129. ticket.destroy
  130. found = Ticket.where( :id => ticket_id ).first
  131. assert( !found, "Ticket destroyed")
  132. }
  133. end
  134. test 'organization' do
  135. tests = [
  136. # test 1
  137. {
  138. :create => {
  139. :organization => {
  140. :name => 'some name',
  141. :updated_by_id => current_user.id,
  142. :created_by_id => current_user.id,
  143. },
  144. },
  145. :update1 => {
  146. :organization => {
  147. :name => 'some name (äöüß)',
  148. },
  149. },
  150. :update2 => {
  151. :organization => {
  152. :name => 'some name 2 (äöüß)',
  153. },
  154. },
  155. :check => [
  156. {
  157. :result => true,
  158. :object => 'Organization',
  159. :type => 'updated',
  160. },
  161. {
  162. :result => true,
  163. :object => 'Organization',
  164. :type => 'created',
  165. },
  166. ]
  167. },
  168. ]
  169. organizations = []
  170. tests.each { |test|
  171. organization = Organization.create( test[:create][:organization] )
  172. test[:check][0][:o_id] = organization.id
  173. test[:check][0][:created_at] = organization.created_at
  174. test[:check][0][:created_by_id] = current_user.id
  175. sleep 2
  176. assert_equal( organization.class.to_s, 'Organization' )
  177. if test[:update1][:organization]
  178. organization.update_attributes( test[:update1][:organization] )
  179. test[:check][1][:o_id] = organization.id
  180. test[:check][1][:updated_at] = organization.updated_at
  181. test[:check][1][:created_by_id] = current_user.id
  182. sleep 13
  183. end
  184. if test[:update2][:organization]
  185. organization.update_attributes( test[:update2][:organization] )
  186. end
  187. # remember organization
  188. organizations.push organization
  189. # check activity_stream
  190. activity_stream_check( admin_user.activity_stream(2), test[:check] )
  191. }
  192. # delete tickets
  193. organizations.each { |organization|
  194. organization_id = organization.id
  195. organization.destroy
  196. found = Organization.where( :id => organization_id ).first
  197. assert( !found, "Organization destroyed")
  198. }
  199. end
  200. test 'user with update check false' do
  201. tests = [
  202. # test 1
  203. {
  204. :create => {
  205. :user => {
  206. :login => 'someemail@example.com',
  207. :email => 'Bob Smith II <someemail@example.com>',
  208. :updated_by_id => current_user.id,
  209. :created_by_id => current_user.id,
  210. },
  211. },
  212. :update1 => {
  213. :user => {
  214. :firstname => 'Bob U',
  215. :lastname => 'Smith U',
  216. },
  217. },
  218. :check => [
  219. {
  220. :result => true,
  221. :object => 'User',
  222. :type => 'created',
  223. },
  224. {
  225. :result => false,
  226. :object => 'User',
  227. :type => 'updated',
  228. },
  229. ]
  230. },
  231. ]
  232. users = []
  233. tests.each { |test|
  234. user = User.create( test[:create][:user] )
  235. test[:check][0][:o_id] = user.id
  236. test[:check][0][:created_at] = user.created_at
  237. test[:check][0][:created_by_id] = current_user.id
  238. assert_equal( user.class.to_s, 'User' )
  239. if test[:update1][:user]
  240. user.update_attributes( test[:update1][:user] )
  241. test[:check][1][:o_id] = user.id
  242. test[:check][1][:updated_at] = user.updated_at
  243. test[:check][1][:created_by_id] = current_user.id
  244. end
  245. # remember organization
  246. users.push user
  247. # check activity_stream
  248. activity_stream_check( admin_user.activity_stream(3), test[:check] )
  249. }
  250. # delete tickets
  251. users.each { |user|
  252. user_id = user.id
  253. user.destroy
  254. found = User.where( :id => user_id ).first
  255. assert( !found, "User destroyed")
  256. }
  257. end
  258. test 'user with update check true' do
  259. tests = [
  260. # test 1
  261. {
  262. :create => {
  263. :user => {
  264. :login => 'someemail@example.com',
  265. :email => 'Bob Smith II <someemail@example.com>',
  266. :updated_by_id => current_user.id,
  267. :created_by_id => current_user.id,
  268. },
  269. },
  270. :update1 => {
  271. :user => {
  272. :firstname => 'Bob U',
  273. :lastname => 'Smith U',
  274. },
  275. },
  276. :update2 => {
  277. :user => {
  278. :firstname => 'Bob',
  279. :lastname => 'Smith',
  280. },
  281. },
  282. :check => [
  283. {
  284. :result => true,
  285. :object => 'User',
  286. :type => 'updated',
  287. },
  288. {
  289. :result => true,
  290. :object => 'User',
  291. :type => 'created',
  292. },
  293. ]
  294. },
  295. ]
  296. users = []
  297. tests.each { |test|
  298. user = User.create( test[:create][:user] )
  299. test[:check][0][:o_id] = user.id
  300. test[:check][0][:created_at] = user.created_at
  301. test[:check][0][:created_by_id] = current_user.id
  302. assert_equal( user.class.to_s, 'User' )
  303. if test[:update1][:user]
  304. user.update_attributes( test[:update1][:user] )
  305. test[:check][1][:o_id] = user.id
  306. test[:check][1][:updated_at] = user.updated_at
  307. test[:check][1][:created_by_id] = current_user.id
  308. end
  309. # to verify update which need to be logged
  310. sleep 14
  311. if test[:update2][:user]
  312. user.update_attributes( test[:update2][:user] )
  313. end
  314. # remember organization
  315. users.push user
  316. # check activity_stream
  317. activity_stream_check( admin_user.activity_stream(2), test[:check] )
  318. }
  319. # delete tickets
  320. users.each { |user|
  321. user_id = user.id
  322. user.destroy
  323. found = User.where( :id => user_id ).first
  324. assert( !found, "User destroyed")
  325. }
  326. end
  327. def activity_stream_check( activity_stream_list, checks )
  328. #activity_stream_list = activity_stream_list.reverse
  329. #puts 'AS ' + activity_stream_list.inspect
  330. check_count = 0
  331. checks.each { |check_item|
  332. check_count += 1
  333. #puts '+++++++++++'
  334. #puts check_item.inspect
  335. check_list = 0
  336. activity_stream_list.each { |item|
  337. check_list += 1
  338. next if check_list != check_count
  339. # next if match
  340. #puts '--------'
  341. #puts item.inspect
  342. #puts check_item.inspect
  343. if check_item[:result]
  344. assert_equal( check_item[:object], item['object'] )
  345. assert_equal( check_item[:type], item['type'] )
  346. assert_equal( check_item[:o_id], item['o_id'] )
  347. else
  348. if check_item[:object] == item['object'] && check_item[:type] == item['type'] && check_item[:o_id] == item['o_id']
  349. assert( false, "entry should not exist #{ item['object'] }/#{ item['type'] }/#{ item['o_id'] }" )
  350. end
  351. end
  352. }
  353. }
  354. end
  355. end