organization_updates_spec.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::OrganizationUpdates, type: :graphql do
  4. let(:agent) { create(:agent) }
  5. let(:organization) { create(:organization) }
  6. let(:variables) { { organizationId: gql.id(organization) } }
  7. let(:mock_channel) { build_mock_channel }
  8. let(:subscription) do
  9. <<~QUERY
  10. subscription organizationUpdates($organizationId: ID!) {
  11. organizationUpdates(organizationId: $organizationId) {
  12. organization {
  13. name
  14. }
  15. }
  16. }
  17. QUERY
  18. end
  19. before do
  20. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  21. end
  22. shared_examples 'subscribes and receives updates' do
  23. it 'subscribes' do
  24. expect(gql.result.data).to eq({ 'organization' => nil })
  25. end
  26. it 'receives organization updates' do
  27. organization.save!
  28. expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'organizationUpdates', 'organization', 'name')).to eq(organization.name)
  29. end
  30. end
  31. context 'with an agent', authenticated_as: :agent do
  32. context 'with permission' do
  33. include_examples 'subscribes and receives updates'
  34. context 'when losing permissions' do
  35. it 'receives no data anymore' do
  36. agent.update!(roles: [])
  37. organization.save!
  38. expect(mock_channel.mock_broadcasted_messages.first[:result]).to include(
  39. {
  40. 'data' => nil,
  41. 'errors' => include(
  42. include(
  43. 'message' => 'not allowed to show? this Organization',
  44. ),
  45. ),
  46. }
  47. )
  48. end
  49. end
  50. end
  51. it_behaves_like 'graphql responds with error if unauthenticated'
  52. end
  53. context 'with a customer', authenticated_as: :customer do
  54. let(:customer) { create(:customer, organization: organization) }
  55. include_examples 'subscribes and receives updates'
  56. context 'when losing permissions' do
  57. let(:customer) { create(:customer) }
  58. it 'raises an error' do
  59. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  60. end
  61. end
  62. end
  63. end