recently_viewed_spec.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Tickets::RecentlyViewed, type: :graphql do
  4. let(:query) do
  5. <<~QUERY
  6. query ticketsRecentlyViewed($exceptTicketInternalId: Int) {
  7. ticketsRecentlyViewed(exceptTicketInternalId: $exceptTicketInternalId) {
  8. id
  9. }
  10. }
  11. QUERY
  12. end
  13. let(:variables) { { exceptTicketInternalId: ticket.id } }
  14. let(:group) { create(:group) }
  15. let(:customer) { create(:customer) }
  16. let(:ticket) { create(:ticket, group:, customer:) }
  17. let(:customer_ticket) { create(:ticket, group:, customer:) }
  18. let(:inaccessible_customer_ticket) { create(:ticket, customer:) }
  19. let(:user) { create(:agent, groups: [group]) }
  20. before do
  21. RecentView.log('Ticket', ticket.id, user)
  22. RecentView.log('Ticket', customer_ticket.id, user)
  23. RecentView.log('Ticket', inaccessible_customer_ticket.id, user)
  24. gql.execute(query, variables: variables)
  25. end
  26. context 'with an agent', authenticated_as: :user do
  27. it 'returns data' do
  28. expect(gql.result.data).to eq(
  29. [{ 'id' => gql.id(customer_ticket) }]
  30. )
  31. end
  32. end
  33. context 'with a customer', authenticated_as: :user do
  34. let(:user) { create(:customer) }
  35. it 'raises an error' do
  36. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  37. end
  38. end
  39. it_behaves_like 'graphql responds with error if unauthenticated'
  40. end