activity_stream_test.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ActivityStreamTest < ActiveSupport::TestCase
  4. setup do
  5. roles = Role.where(name: %w(Admin Agent))
  6. groups = Group.where(name: 'Users')
  7. @admin_user = User.create_or_update(
  8. login: 'admin',
  9. firstname: 'Bob',
  10. lastname: 'Smith',
  11. email: 'bob+active_stream@example.com',
  12. password: 'some_pass',
  13. active: true,
  14. roles: roles,
  15. groups: groups,
  16. updated_by_id: 1,
  17. created_by_id: 1
  18. )
  19. @current_user = User.lookup(email: 'nicole.braun@zammad.org')
  20. ActivityStream.delete_all
  21. end
  22. test 'ticket+user' do
  23. ticket = Ticket.create!(
  24. group_id: Group.lookup(name: 'Users').id,
  25. customer_id: @current_user.id,
  26. owner_id: User.lookup(login: '-').id,
  27. title: 'Unit Test 1 (äöüß)!',
  28. state_id: Ticket::State.lookup(name: 'new').id,
  29. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  30. updated_by_id: @current_user.id,
  31. created_by_id: @current_user.id,
  32. )
  33. travel 2.seconds
  34. article = Ticket::Article.create!(
  35. ticket_id: ticket.id,
  36. updated_by_id: @current_user.id,
  37. created_by_id: @current_user.id,
  38. type_id: Ticket::Article::Type.lookup(name: 'phone').id,
  39. sender_id: Ticket::Article::Sender.lookup(name: 'Customer').id,
  40. from: 'Unit Test <unittest@example.com>',
  41. body: 'Unit Test 123',
  42. internal: false,
  43. )
  44. travel 100.seconds
  45. ticket.update!(
  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. updated_at = ticket.updated_at
  51. travel 1.second
  52. ticket.update!(
  53. title: 'Unit Test 2 (äöüß) - update!',
  54. priority_id: Ticket::Priority.lookup(name: '2 normal').id,
  55. )
  56. # check activity_stream
  57. stream = @admin_user.activity_stream(4)
  58. assert_equal(stream[0]['group_id'], ticket.group_id)
  59. assert_equal(stream[0]['o_id'], ticket.id)
  60. assert_equal(stream[0]['created_by_id'], @current_user.id)
  61. assert_equal(stream[0]['created_at'].to_s, updated_at.to_s)
  62. assert_equal(stream[0]['object'], 'Ticket')
  63. assert_equal(stream[0]['type'], 'update')
  64. assert_equal(stream[1]['group_id'], ticket.group_id)
  65. assert_equal(stream[1]['o_id'], article.id)
  66. assert_equal(stream[1]['created_by_id'], @current_user.id)
  67. assert_equal(stream[1]['created_at'].to_s, article.created_at.to_s)
  68. assert_equal(stream[1]['object'], 'Ticket::Article')
  69. assert_equal(stream[1]['type'], 'create')
  70. assert_equal(stream[2]['group_id'], ticket.group_id)
  71. assert_equal(stream[2]['o_id'], ticket.id)
  72. assert_equal(stream[2]['created_by_id'], @current_user.id)
  73. assert_equal(stream[2]['created_at'].to_s, ticket.created_at.to_s)
  74. assert_equal(stream[2]['object'], 'Ticket')
  75. assert_equal(stream[2]['type'], 'create')
  76. assert_not(stream[3])
  77. stream = @current_user.activity_stream(4)
  78. assert(stream.empty?)
  79. # cleanup
  80. ticket.destroy!
  81. travel_back
  82. end
  83. test 'organization' do
  84. organization = Organization.create!(
  85. name: 'some name',
  86. updated_by_id: @current_user.id,
  87. created_by_id: @current_user.id,
  88. )
  89. travel 100.seconds
  90. assert_equal(organization.class, Organization)
  91. organization.update!(name: 'some name (äöüß)')
  92. updated_at = organization.updated_at
  93. travel 10.seconds
  94. organization.update!(name: 'some name 2 (äöüß)')
  95. # check activity_stream
  96. stream = @admin_user.activity_stream(3)
  97. assert_not(stream[0]['group_id'])
  98. assert_equal(stream[0]['o_id'], organization.id)
  99. assert_equal(stream[0]['created_by_id'], @current_user.id)
  100. assert_equal(stream[0]['created_at'].to_s, updated_at.to_s)
  101. assert_equal(stream[0]['object'], 'Organization')
  102. assert_equal(stream[0]['type'], 'update')
  103. assert_not(stream[1]['group_id'])
  104. assert_equal(stream[1]['o_id'], organization.id)
  105. assert_equal(stream[1]['created_by_id'], @current_user.id)
  106. assert_equal(stream[1]['created_at'].to_s, organization.created_at.to_s)
  107. assert_equal(stream[1]['object'], 'Organization')
  108. assert_equal(stream[1]['type'], 'create')
  109. assert_not(stream[2])
  110. stream = @current_user.activity_stream(4)
  111. assert(stream.empty?)
  112. # cleanup
  113. organization.destroy!
  114. travel_back
  115. end
  116. test 'user with update check false' do
  117. user = User.create!(
  118. login: 'someemail@example.com',
  119. email: 'someemail@example.com',
  120. firstname: 'Bob Smith II',
  121. updated_by_id: @current_user.id,
  122. created_by_id: @current_user.id,
  123. )
  124. assert_equal(user.class, User)
  125. user.update!(
  126. firstname: 'Bob U',
  127. lastname: 'Smith U',
  128. )
  129. # check activity_stream
  130. stream = @admin_user.activity_stream(3)
  131. assert_not(stream[0]['group_id'])
  132. assert_equal(stream[0]['o_id'], user.id)
  133. assert_equal(stream[0]['created_by_id'], @current_user.id)
  134. assert_equal(stream[0]['created_at'].to_s, user.created_at.to_s)
  135. assert_equal(stream[0]['object'], 'User')
  136. assert_equal(stream[0]['type'], 'create')
  137. assert_not(stream[1])
  138. stream = @current_user.activity_stream(4)
  139. assert(stream.empty?)
  140. # cleanup
  141. user.destroy!
  142. travel_back
  143. end
  144. test 'user with update check true' do
  145. user = User.create!(
  146. login: 'someemail@example.com',
  147. email: 'someemail@example.com',
  148. firstname: 'Bob Smith II',
  149. updated_by_id: @current_user.id,
  150. created_by_id: @current_user.id,
  151. )
  152. travel 100.seconds
  153. assert_equal(user.class, User)
  154. user.update!(
  155. firstname: 'Bob U',
  156. lastname: 'Smith U',
  157. )
  158. updated_at = user.updated_at
  159. travel 10.seconds
  160. user.update!(
  161. firstname: 'Bob',
  162. lastname: 'Smith',
  163. )
  164. # check activity_stream
  165. stream = @admin_user.activity_stream(3)
  166. assert_not(stream[0]['group_id'])
  167. assert_equal(stream[0]['o_id'], user.id)
  168. assert_equal(stream[0]['created_by_id'], @current_user.id)
  169. assert_equal(stream[0]['created_at'].to_s, updated_at.to_s)
  170. assert_equal(stream[0]['object'], 'User')
  171. assert_equal(stream[0]['type'], 'update')
  172. assert_not(stream[1]['group_id'])
  173. assert_equal(stream[1]['o_id'], user.id)
  174. assert_equal(stream[1]['created_by_id'], @current_user.id)
  175. assert_equal(stream[1]['created_at'].to_s, user.created_at.to_s)
  176. assert_equal(stream[1]['object'], 'User')
  177. assert_equal(stream[1]['type'], 'create')
  178. assert_not(stream[2])
  179. stream = @current_user.activity_stream(4)
  180. assert(stream.empty?)
  181. # cleanup
  182. user.destroy!
  183. travel_back
  184. end
  185. end