recent_view_test.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. require 'test_helper'
  2. class RecentViewTest < ActiveSupport::TestCase
  3. test 'simple tests' do
  4. ticket1 = Ticket.create(
  5. title: 'RecentViewTest 1 some title äöüß',
  6. group: Group.lookup( name: 'Users'),
  7. customer_id: 2,
  8. state: Ticket::State.lookup( name: 'new' ),
  9. priority: Ticket::Priority.lookup( name: '2 normal' ),
  10. updated_by_id: 1,
  11. created_by_id: 1,
  12. )
  13. assert(ticket1, 'ticket created')
  14. ticket2 = Ticket.create(
  15. title: 'RecentViewTest 2 some title äöüß',
  16. group: Group.lookup(name: 'Users'),
  17. customer_id: 2,
  18. state: Ticket::State.lookup(name: 'new'),
  19. priority: Ticket::Priority.lookup(name: '2 normal'),
  20. updated_by_id: 1,
  21. created_by_id: 1,
  22. )
  23. assert(ticket2, 'ticket created')
  24. user1 = User.find(2)
  25. RecentView.user_log_destroy(user1)
  26. RecentView.log(ticket1.class.to_s, ticket1.id, user1)
  27. travel 1.second
  28. RecentView.log(ticket2.class.to_s, ticket2.id, user1)
  29. travel 1.second
  30. RecentView.log(ticket1.class.to_s, ticket1.id, user1)
  31. travel 1.second
  32. RecentView.log(ticket1.class.to_s, ticket1.id, user1)
  33. list = RecentView.list(user1)
  34. assert(list[0].o_id, ticket1.id)
  35. assert(list[0].object.name, 'Ticket')
  36. assert(list[1].o_id, ticket2.id)
  37. assert(list[1].object.name, 'Ticket')
  38. assert_equal(2, list.count)
  39. ticket1.destroy
  40. ticket2.destroy
  41. list = RecentView.list(user1)
  42. assert_not(list[0], 'check if recent view list is empty')
  43. travel_back
  44. end
  45. test 'existing tests' do
  46. user = User.find(2)
  47. # log entry of not existing object
  48. RecentView.user_log_destroy(user)
  49. RecentView.log('ObjectNotExisting', 1, user)
  50. # check if list is empty
  51. list = RecentView.list(user)
  52. assert_not(list[0], 'check if recent view list is empty')
  53. # log entry of not existing record
  54. RecentView.user_log_destroy(user)
  55. RecentView.log('User', 99_999_999, user)
  56. # check if list is empty
  57. list = RecentView.list(user)
  58. assert_not(list[0], 'check if recent view list is empty')
  59. # log entry of not existing model with permission check
  60. RecentView.user_log_destroy(user)
  61. RecentView.log('Overview', 99_999_999, user)
  62. # check if list is empty
  63. list = RecentView.list(user)
  64. assert_not(list[0], 'check if recent view list is empty')
  65. end
  66. test 'permission tests' do
  67. customer = User.find(2)
  68. groups = Group.where(name: 'Users')
  69. roles = Role.where(name: 'Agent')
  70. agent = User.create_or_update(
  71. login: 'recent-viewed-agent@example.com',
  72. firstname: 'RecentViewed',
  73. lastname: 'Agent',
  74. email: 'recent-viewed-agent@example.com',
  75. password: 'agentpw',
  76. active: true,
  77. roles: roles,
  78. groups: groups,
  79. updated_by_id: 1,
  80. created_by_id: 1,
  81. )
  82. Group.create_if_not_exists(
  83. name: 'WithoutAccess',
  84. note: 'Test for not access check.',
  85. updated_by_id: 1,
  86. created_by_id: 1
  87. )
  88. organization2 = Organization.create_if_not_exists(
  89. name: 'Customer Organization Recent View 2',
  90. note: 'some note',
  91. updated_by_id: 1,
  92. created_by_id: 1,
  93. )
  94. # no access for customer
  95. ticket1 = Ticket.create(
  96. title: 'RecentViewTest 1 some title äöüß',
  97. group: Group.lookup(name: 'WithoutAccess'),
  98. customer_id: 1,
  99. state: Ticket::State.lookup(name: 'new'),
  100. priority: Ticket::Priority.lookup(name: '2 normal'),
  101. updated_by_id: 1,
  102. created_by_id: 1,
  103. )
  104. assert(ticket1, 'ticket created')
  105. # log entry of not existing object
  106. RecentView.user_log_destroy(customer)
  107. RecentView.log(ticket1.class.to_s, ticket1.id, customer)
  108. # check if list is empty
  109. list = RecentView.list(customer)
  110. assert_not(list[0], 'check if recent view list is empty')
  111. # log entry of not existing object
  112. RecentView.user_log_destroy(agent)
  113. RecentView.log(ticket1.class.to_s, ticket1.id, agent)
  114. # check if list is empty
  115. list = RecentView.list(agent)
  116. assert_not(list[0], 'check if recent view list is empty')
  117. # access for customer via customer id
  118. ticket1 = Ticket.create(
  119. title: 'RecentViewTest 1 some title äöüß',
  120. group: Group.lookup(name: 'WithoutAccess'),
  121. customer_id: 2,
  122. state: Ticket::State.lookup(name: 'new'),
  123. priority: Ticket::Priority.lookup(name: '2 normal'),
  124. updated_by_id: 1,
  125. created_by_id: 1,
  126. )
  127. assert(ticket1, 'ticket created')
  128. # log entry
  129. RecentView.user_log_destroy(customer)
  130. RecentView.log(ticket1.class.to_s, ticket1.id, customer)
  131. # check if list is empty
  132. list = RecentView.list(customer)
  133. assert(list[0].o_id, ticket1.id)
  134. assert(list[0].object.name, 'Ticket')
  135. assert_not(list[1], 'check if recent view list is empty')
  136. # log entry
  137. organization1 = Organization.find(1)
  138. RecentView.user_log_destroy(customer)
  139. RecentView.log(organization1.class.to_s, organization1.id, customer)
  140. RecentView.log(organization2.class.to_s, organization2.id, customer)
  141. # check if list is empty
  142. list = RecentView.list(customer)
  143. assert(list[0], 'check if recent view list is empty')
  144. assert_not(list[1], 'check if recent view list is empty')
  145. # log entry
  146. organization1 = Organization.find(1)
  147. RecentView.user_log_destroy(agent)
  148. RecentView.log(organization1.class.to_s, organization1.id, agent)
  149. # check if list is empty
  150. list = RecentView.list(agent)
  151. assert(list[0].o_id, organization1.id)
  152. assert(list[0].object.name, 'Organization')
  153. assert_not(list[1], 'check if recent view list is empty')
  154. organization2.destroy
  155. end
  156. end