overviews_spec.rb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Ticket::Overviews, type: :graphql do
  4. context 'when fetching ticket overviews' do
  5. let(:agent) { create(:agent) }
  6. let(:query) do
  7. <<~QUERY
  8. query ticketOverviews(
  9. $ignoreUserConditions: Boolean!,
  10. $withTicketCount: Boolean!,
  11. $withCachedTicketCount: Boolean!
  12. $cacheTtl: Int!
  13. $filterOverviewIds: [ID!]
  14. ) {
  15. ticketOverviews(ignoreUserConditions: $ignoreUserConditions, filterOverviewIds: $filterOverviewIds) {
  16. id
  17. name
  18. link
  19. prio
  20. orderBy
  21. orderDirection
  22. organizationShared
  23. outOfOffice
  24. groupBy
  25. groupDirection
  26. viewColumnsRaw
  27. viewColumns {
  28. key
  29. value
  30. }
  31. orderColumns {
  32. key
  33. value
  34. }
  35. active
  36. ticketCount @include(if: $withTicketCount)
  37. cachedTicketCount(cacheTtl: $cacheTtl) @include(if: $withCachedTicketCount)
  38. }
  39. }
  40. QUERY
  41. end
  42. let(:ignore_user_conditions) { false }
  43. let(:with_ticket_count) { false }
  44. let(:with_cached_ticket_count) { false }
  45. let(:cache_ttl) { 4.seconds }
  46. let(:filter_overview_ids) { nil }
  47. let(:variables) do
  48. {
  49. withTicketCount: with_ticket_count,
  50. withCachedTicketCount: with_cached_ticket_count,
  51. ignoreUserConditions: ignore_user_conditions,
  52. cacheTtl: cache_ttl,
  53. filterOverviewIds: filter_overview_ids,
  54. }
  55. end
  56. context 'with an agent', authenticated_as: :agent do
  57. before do
  58. gql.execute(query, variables: variables)
  59. end
  60. it 'has agent overview' do
  61. expect(gql.result.data.first).to include('name' => 'My Assigned Tickets', 'link' => 'my_assigned', 'prio' => 1000, 'active' => true, 'groupBy' => nil, 'groupDirection' => nil)
  62. end
  63. it 'has view and order columns' do
  64. expect(gql.result.data.first).to include(
  65. 'viewColumnsRaw' => match_array(%w[title customer_id group_id created_at]),
  66. 'viewColumns' => include({ 'key' => 'title', 'value' => 'Title' }),
  67. 'orderColumns' => include({ 'key' => 'created_at', 'value' => 'Created at' }),
  68. )
  69. end
  70. it 'has shared organization and out of office fields' do
  71. expect(gql.result.data.first).to include(
  72. 'organizationShared' => false,
  73. 'outOfOffice' => false,
  74. )
  75. end
  76. context 'with object attributes and unknown attributes', db_strategy: :reset do
  77. let(:oa) do
  78. create(:object_manager_attribute_text, :required_screen).tap do
  79. ObjectManager::Attribute.migration_execute
  80. end
  81. end
  82. # Change the overview to include an object attribute column and a column that has an unknown field.
  83. let(:overview) do
  84. Overview.find_by('link' => 'my_assigned').tap do |overview|
  85. overview.view = { 's' => [oa.name, 'unknown_field'] }
  86. overview.save!
  87. end
  88. end
  89. let(:with_ticket_count) do
  90. overview
  91. false
  92. end
  93. it 'lists view columns correctly' do
  94. expect(gql.result.data.first).to include(
  95. 'viewColumns' => [ { 'key' => oa.name, 'value' => oa.display }, { 'key' => 'unknown_field', 'value' => nil }],
  96. )
  97. end
  98. end
  99. context 'when not ignoring user conditions' do
  100. it 'does not include replacement tickets overview' do
  101. expect(gql.result.data).not_to include(include('name' => 'My Replacement Tickets', 'outOfOffice' => true))
  102. end
  103. end
  104. context 'when ignoring user conditions' do
  105. let(:ignore_user_conditions) { true }
  106. it 'includes replacement tickets overview' do
  107. expect(gql.result.data).to include(include('name' => 'My Replacement Tickets', 'outOfOffice' => true))
  108. end
  109. end
  110. context 'without ticket count' do
  111. it 'does not include ticketCount field' do
  112. expect(gql.result.data.first).not_to have_key('ticketCount')
  113. end
  114. end
  115. context 'with ticket count' do
  116. let(:with_ticket_count) { true }
  117. it 'includes ticketCount field' do
  118. expect(gql.result.data.first['ticketCount']).to eq(0)
  119. end
  120. end
  121. end
  122. context 'with an agent, with filtering and caching', authenticated_as: :agent do
  123. def trace_queries(queries, &block)
  124. callback = lambda do |*, payload|
  125. queries[payload[:name]] ||= 0
  126. queries[payload[:name]] += 1
  127. end
  128. ActiveSupport::Notifications.subscribed(callback, 'sql.active_record', &block)
  129. queries
  130. end
  131. def ensure_no_ticket_queries(&block)
  132. queries = trace_queries({}, &block)
  133. expect(queries).not_to have_key('Ticket Count')
  134. end
  135. def ensure_ticket_queries(&block)
  136. queries = trace_queries({}, &block)
  137. expect(queries).to have_key('Ticket Count')
  138. end
  139. def ensure_cache_writes
  140. allow(Rails.cache).to receive(:write).and_call_original
  141. yield
  142. expect(Rails.cache).to have_received(:write).at_least(:once)
  143. end
  144. def ensure_no_cache_writes
  145. allow(Rails.cache).to receive(:write).and_call_original
  146. yield
  147. expect(Rails.cache).not_to have_received(:write)
  148. end
  149. let(:agent) { create(:agent, groups: [ticket.group]) }
  150. let!(:ticket) { create(:ticket) }
  151. let(:overview) { Overview.find_by(link: 'all_unassigned') }
  152. let(:filter_overview_ids) { [gql.id(overview)] }
  153. let(:with_cached_ticket_count) { true }
  154. it 'creates a cache on first call' do
  155. ensure_cache_writes do
  156. ensure_ticket_queries do
  157. gql.execute(query, variables:)
  158. end
  159. end
  160. expect(gql.result.data).to contain_exactly(include('cachedTicketCount' => 1))
  161. end
  162. it 'uses the cache on second call' do
  163. gql.execute(query, variables:)
  164. ensure_no_cache_writes do
  165. ensure_no_ticket_queries do
  166. gql.execute(query, variables:)
  167. end
  168. end
  169. expect(gql.result.data).to contain_exactly(include('cachedTicketCount' => 1))
  170. end
  171. it 'recreates the cache on second call if cache has expired' do
  172. freeze_time
  173. gql.execute(query, variables:)
  174. travel(cache_ttl)
  175. ensure_cache_writes do
  176. ensure_ticket_queries do
  177. gql.execute(query, variables:)
  178. end
  179. end
  180. expect(gql.result.data).to contain_exactly(include('cachedTicketCount' => 1))
  181. end
  182. it 'creates another cache on second call if different cacheTtl is provided' do
  183. gql.execute(query, variables:)
  184. ensure_cache_writes do
  185. ensure_ticket_queries do
  186. gql.execute(query, variables: variables.merge({ cacheTtl: cache_ttl + 1 }))
  187. end
  188. end
  189. expect(gql.result.data).to contain_exactly(include('cachedTicketCount' => 1))
  190. end
  191. context 'with a different user with different permissions' do
  192. let(:other_agent) { create(:agent) }
  193. it 'does not use the cache on second call' do
  194. gql.execute(query, variables:)
  195. gql.graphql_current_user = other_agent
  196. ensure_cache_writes do
  197. ensure_ticket_queries do
  198. gql.execute(query, variables:)
  199. end
  200. end
  201. expect(gql.result.data).to contain_exactly(include('cachedTicketCount' => 0))
  202. end
  203. end
  204. context 'with a different user with same permissions' do
  205. let(:other_agent) { create(:agent, groups: [ticket.group]) }
  206. context 'with non-personalized overview' do
  207. it 'uses the cache on second call' do
  208. gql.execute(query, variables:)
  209. gql.graphql_current_user = other_agent
  210. ensure_no_cache_writes do
  211. ensure_no_ticket_queries do
  212. gql.execute(query, variables:)
  213. end
  214. end
  215. expect(gql.result.data).to contain_exactly(include('cachedTicketCount' => 1))
  216. end
  217. end
  218. context 'with personalized overview' do
  219. let(:overview) { Overview.find_by(link: 'my_assigned') }
  220. it 'does not use the cache on second call' do
  221. gql.execute(query, variables:)
  222. gql.graphql_current_user = other_agent
  223. ensure_cache_writes do
  224. ensure_ticket_queries do
  225. gql.execute(query, variables:)
  226. end
  227. end
  228. expect(gql.result.data).to contain_exactly(include('cachedTicketCount' => 0))
  229. end
  230. end
  231. end
  232. end
  233. context 'with a customer', authenticated_as: :customer do
  234. before do
  235. gql.execute(query, variables: variables)
  236. end
  237. let(:customer) { create(:customer) }
  238. it 'has customer overview' do
  239. expect(gql.result.data.first).to include('name' => 'My Tickets', 'link' => 'my_tickets', 'prio' => 1100, 'active' => true,)
  240. end
  241. context 'when not ignoring user conditions' do
  242. it 'does not include shared organization overview' do
  243. expect(gql.result.data).not_to include(include('name' => 'My Organization Tickets', 'organizationShared' => true))
  244. end
  245. end
  246. context 'when ignoring user conditions' do
  247. let(:ignore_user_conditions) { true }
  248. it 'includes replacement tickets overview' do
  249. expect(gql.result.data).to include(include('name' => 'My Organization Tickets', 'organizationShared' => true))
  250. end
  251. end
  252. end
  253. context 'with unauthenticated users' do
  254. before do
  255. gql.execute(query, variables: variables)
  256. end
  257. it_behaves_like 'graphql responds with error if unauthenticated'
  258. end
  259. end
  260. end