activity_stream_test.rb 7.3 KB

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