activity_stream_test.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  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.name, 'Ticket')
  63. assert_equal(stream[0].type.name, '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.name, 'Ticket::Article')
  69. assert_equal(stream[1].type.name, '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.name, 'Ticket')
  75. assert_equal(stream[2].type.name, 'create')
  76. assert_not(stream[3])
  77. stream = @current_user.activity_stream(4)
  78. assert(stream.blank?)
  79. # delete article and check if entry has gone
  80. article.destroy!
  81. # check activity_stream
  82. stream = @admin_user.activity_stream(4)
  83. assert_equal(stream[0].group_id, ticket.group_id)
  84. assert_equal(stream[0].o_id, ticket.id)
  85. assert_equal(stream[0].created_by_id, @current_user.id)
  86. assert_equal(stream[0].created_at.to_s, updated_at.to_s)
  87. assert_equal(stream[0].object.name, 'Ticket')
  88. assert_equal(stream[0].type.name, 'update')
  89. assert_equal(stream[1].group_id, ticket.group_id)
  90. assert_equal(stream[1].o_id, ticket.id)
  91. assert_equal(stream[1].created_by_id, @current_user.id)
  92. assert_equal(stream[1].created_at.to_s, ticket.created_at.to_s)
  93. assert_equal(stream[1].object.name, 'Ticket')
  94. assert_equal(stream[1].type.name, 'create')
  95. assert_not(stream[2])
  96. stream = @current_user.activity_stream(4)
  97. assert(stream.blank?)
  98. # cleanup
  99. ticket.destroy!
  100. travel_back
  101. end
  102. test 'organization' do
  103. organization = Organization.create!(
  104. name: 'some name',
  105. updated_by_id: @current_user.id,
  106. created_by_id: @current_user.id,
  107. )
  108. travel 100.seconds
  109. assert_equal(organization.class, Organization)
  110. organization.update!(name: 'some name (äöüß)')
  111. updated_at = organization.updated_at
  112. travel 10.seconds
  113. organization.update!(name: 'some name 2 (äöüß)')
  114. # check activity_stream
  115. stream = @admin_user.activity_stream(3)
  116. assert_not(stream[0].group_id)
  117. assert_equal(stream[0].o_id, organization.id)
  118. assert_equal(stream[0].created_by_id, @current_user.id)
  119. assert_equal(stream[0].created_at.to_s, updated_at.to_s)
  120. assert_equal(stream[0].object.name, 'Organization')
  121. assert_equal(stream[0].type.name, 'update')
  122. assert_not(stream[1].group_id)
  123. assert_equal(stream[1].o_id, organization.id)
  124. assert_equal(stream[1].created_by_id, @current_user.id)
  125. assert_equal(stream[1].created_at.to_s, organization.created_at.to_s)
  126. assert_equal(stream[1].object.name, 'Organization')
  127. assert_equal(stream[1].type.name, 'create')
  128. assert_not(stream[2])
  129. stream = @current_user.activity_stream(4)
  130. assert(stream.blank?)
  131. # cleanup
  132. organization.destroy!
  133. travel_back
  134. end
  135. test 'user with update check false' do
  136. user = User.create!(
  137. login: 'someemail@example.com',
  138. email: 'someemail@example.com',
  139. firstname: 'Bob Smith II',
  140. updated_by_id: @current_user.id,
  141. created_by_id: @current_user.id,
  142. )
  143. assert_equal(user.class, User)
  144. user.update!(
  145. firstname: 'Bob U',
  146. lastname: 'Smith U',
  147. )
  148. # check activity_stream
  149. stream = @admin_user.activity_stream(3)
  150. assert_not(stream[0].group_id)
  151. assert_equal(stream[0].o_id, user.id)
  152. assert_equal(stream[0].created_by_id, @current_user.id)
  153. assert_equal(stream[0].created_at.to_s, user.created_at.to_s)
  154. assert_equal(stream[0].object.name, 'User')
  155. assert_equal(stream[0].type.name, 'create')
  156. assert_not(stream[1])
  157. stream = @current_user.activity_stream(4)
  158. assert(stream.blank?)
  159. # cleanup
  160. user.destroy!
  161. travel_back
  162. end
  163. test 'user with update check true' do
  164. user = User.create!(
  165. login: 'someemail@example.com',
  166. email: 'someemail@example.com',
  167. firstname: 'Bob Smith II',
  168. updated_by_id: @current_user.id,
  169. created_by_id: @current_user.id,
  170. )
  171. travel 100.seconds
  172. assert_equal(user.class, User)
  173. user.update!(
  174. firstname: 'Bob U',
  175. lastname: 'Smith U',
  176. )
  177. updated_at = user.updated_at
  178. travel 10.seconds
  179. user.update!(
  180. firstname: 'Bob',
  181. lastname: 'Smith',
  182. )
  183. # check activity_stream
  184. stream = @admin_user.activity_stream(3)
  185. assert_not(stream[0].group_id)
  186. assert_equal(stream[0].o_id, user.id)
  187. assert_equal(stream[0].created_by_id, @current_user.id)
  188. assert_equal(stream[0].created_at.to_s, updated_at.to_s)
  189. assert_equal(stream[0].object.name, 'User')
  190. assert_equal(stream[0].type.name, 'update')
  191. assert_not(stream[1].group_id)
  192. assert_equal(stream[1].o_id, user.id)
  193. assert_equal(stream[1].created_by_id, @current_user.id)
  194. assert_equal(stream[1].created_at.to_s, user.created_at.to_s)
  195. assert_equal(stream[1].object.name, 'User')
  196. assert_equal(stream[1].type.name, 'create')
  197. assert_not(stream[2])
  198. stream = @current_user.activity_stream(4)
  199. assert(stream.blank?)
  200. # cleanup
  201. user.destroy!
  202. travel_back
  203. end
  204. end