recent_view_test.rb 5.6 KB

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