recent_view_test.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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'], ticket1.id)
  38. assert(list[1]['object'], 'Ticket')
  39. assert(list[2]['o_id'], ticket2.id)
  40. assert(list[2]['object'], 'Ticket')
  41. assert(list[3]['o_id'], ticket1.id)
  42. assert(list[3]['object'], 'Ticket')
  43. ticket1.destroy
  44. ticket2.destroy
  45. list = RecentView.list(user1)
  46. assert(!list[0], 'check if recent view list is empty')
  47. travel_back
  48. end
  49. test 'existing tests' do
  50. user = User.find(2)
  51. # log entry of not existing object
  52. RecentView.user_log_destroy(user)
  53. RecentView.log('ObjectNotExisting', 1, user)
  54. # check if list is empty
  55. list = RecentView.list(user)
  56. assert(!list[0], 'check if recent view list is empty')
  57. # log entry of not existing record
  58. RecentView.user_log_destroy(user)
  59. RecentView.log('User', 99_999_999, user)
  60. # check if list is empty
  61. list = RecentView.list(user)
  62. assert(!list[0], 'check if recent view list is empty')
  63. # log entry of not existing model with permission check
  64. RecentView.user_log_destroy(user)
  65. RecentView.log('Overview', 99_999_999, user)
  66. # check if list is empty
  67. list = RecentView.list(user)
  68. assert(!list[0], 'check if recent view list is empty')
  69. end
  70. test 'permission tests' do
  71. customer = User.find(2)
  72. groups = Group.where(name: 'Users')
  73. roles = Role.where(name: 'Agent')
  74. agent = User.create_or_update(
  75. login: 'recent-viewed-agent@example.com',
  76. firstname: 'RecentViewed',
  77. lastname: 'Agent',
  78. email: 'recent-viewed-agent@example.com',
  79. password: 'agentpw',
  80. active: true,
  81. roles: roles,
  82. groups: groups,
  83. updated_by_id: 1,
  84. created_by_id: 1,
  85. )
  86. Group.create_if_not_exists(
  87. name: 'WithoutAccess',
  88. note: 'Test for not access check.',
  89. updated_by_id: 1,
  90. created_by_id: 1
  91. )
  92. # no access for customer
  93. ticket1 = Ticket.create(
  94. title: 'RecentViewTest 1 some title äöüß',
  95. group: Group.lookup(name: 'WithoutAccess'),
  96. customer_id: 1,
  97. state: Ticket::State.lookup(name: 'new'),
  98. priority: Ticket::Priority.lookup(name: '2 normal'),
  99. updated_by_id: 1,
  100. created_by_id: 1,
  101. )
  102. assert(ticket1, 'ticket created')
  103. # log entry of not existing object
  104. RecentView.user_log_destroy(customer)
  105. RecentView.log(ticket1.class.to_s, ticket1.id, customer)
  106. # check if list is empty
  107. list = RecentView.list(customer)
  108. assert(!list[0], 'check if recent view list is empty')
  109. # log entry of not existing object
  110. RecentView.user_log_destroy(agent)
  111. RecentView.log(ticket1.class.to_s, ticket1.id, agent)
  112. # check if list is empty
  113. list = RecentView.list(agent)
  114. assert(!list[0], 'check if recent view list is empty')
  115. # access for customer via customer id
  116. ticket1 = Ticket.create(
  117. title: 'RecentViewTest 1 some title äöüß',
  118. group: Group.lookup(name: 'WithoutAccess'),
  119. customer_id: 2,
  120. state: Ticket::State.lookup(name: 'new'),
  121. priority: Ticket::Priority.lookup(name: '2 normal'),
  122. updated_by_id: 1,
  123. created_by_id: 1,
  124. )
  125. assert(ticket1, 'ticket created')
  126. # log entry
  127. RecentView.user_log_destroy(customer)
  128. RecentView.log(ticket1.class.to_s, ticket1.id, customer)
  129. # check if list is empty
  130. list = RecentView.list(customer)
  131. assert(list[0]['o_id'], ticket1.id)
  132. assert(list[0]['object'], 'Ticket')
  133. assert(!list[1], 'check if recent view list is empty')
  134. # log entry
  135. organization = Organization.find(1)
  136. RecentView.user_log_destroy(customer)
  137. RecentView.log(organization.class.to_s, organization.id, customer)
  138. # check if list is empty
  139. list = RecentView.list(customer)
  140. assert(!list[0], 'check if recent view list is empty')
  141. # log entry
  142. organization = Organization.find(1)
  143. RecentView.user_log_destroy(agent)
  144. RecentView.log(organization.class.to_s, organization.id, agent)
  145. # check if list is empty
  146. list = RecentView.list(agent)
  147. assert(list[0]['o_id'], organization.id)
  148. assert(list[0]['object'], 'Organization')
  149. assert(!list[1], 'check if recent view list is empty')
  150. end
  151. end