recent_view_test.rb 5.4 KB

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