organization_spec.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::Organization, type: :graphql do
  4. context 'when fetching organization' do
  5. let(:user) { create(:agent) }
  6. let(:organization) { create(:organization) }
  7. let(:variables) { { organizationId: gql.id(organization) } }
  8. let(:query) do
  9. <<~QUERY
  10. query organization($organizationId: ID, $organizationInternalId: Int) {
  11. organization( organization: { organizationId: $organizationId, organizationInternalId: $organizationInternalId } ) {
  12. id
  13. name
  14. shared
  15. domain
  16. domainAssignment
  17. active
  18. note
  19. ticketsCount {
  20. open
  21. closed
  22. }
  23. }
  24. }
  25. QUERY
  26. end
  27. before do
  28. gql.execute(query, variables: variables)
  29. end
  30. context 'with authenticated session', authenticated_as: :user do
  31. context 'when querying by Id' do
  32. it 'has data' do
  33. expect(gql.result.data).to include('name' => organization.name, 'shared' => organization.shared)
  34. end
  35. end
  36. context 'when querying by internalId' do
  37. let(:variables) { { organizationInternalId: organization.id } }
  38. it 'has data' do
  39. expect(gql.result.data).to include('name' => organization.name, 'shared' => organization.shared)
  40. end
  41. end
  42. context 'without organization' do
  43. let(:organization) { create(:organization).tap(&:destroy) }
  44. it 'fetches no organization' do
  45. expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
  46. end
  47. end
  48. context 'without organization assignment - no permission' do
  49. let(:user) { create(:customer) }
  50. it 'returns an error' do
  51. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  52. end
  53. end
  54. context 'with organization assignment - permission' do
  55. let(:user) { create(:customer, organization: organization) }
  56. it 'returns a record, but only limited data', :aggregate_failures do
  57. expect(gql.result.data).to include('name' => organization.name, 'shared' => nil)
  58. end
  59. context 'with assignment to another organization' do
  60. let(:user) { create(:customer, organization: create(:organization)) }
  61. it 'returns an error' do
  62. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  63. end
  64. end
  65. end
  66. end
  67. it_behaves_like 'graphql responds with error if unauthenticated'
  68. end
  69. end