user_spec.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Queries::User, type: :graphql do
  4. context 'when fetching user' do
  5. let(:authenticated) { create(:agent) }
  6. let(:user) do
  7. create(:user).tap do |user|
  8. create(:authorization, provider: 'testprovider', username: 'testuser', user_id: user.id)
  9. end
  10. end
  11. let(:variables) { { userId: gql.id(user) } }
  12. let(:query) do
  13. <<~QUERY
  14. query user($userId: ID, $userInternalId: Int) {
  15. user(user: { userId: $userId, userInternalId: $userInternalId }) {
  16. id
  17. firstname
  18. hasSecondaryOrganizations
  19. authorizations {
  20. username
  21. provider
  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: :authenticated do
  31. it 'has data' do
  32. expect(gql.result.data).to include(
  33. 'firstname' => user.firstname,
  34. 'authorizations' => [ { 'provider' => 'testprovider', 'username' => 'testuser' } ],
  35. )
  36. end
  37. context 'without user' do
  38. let(:user) { create(:user).tap(&:destroy) }
  39. it 'fetches no user' do
  40. expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
  41. end
  42. end
  43. context 'with internalId' do
  44. let(:variables) { { userInternalId: user.id } }
  45. it 'has data' do
  46. expect(gql.result.data).to include('firstname' => user.firstname)
  47. end
  48. it 'has no secondary organizations' do
  49. expect(gql.result.data).to include('hasSecondaryOrganizations' => false)
  50. end
  51. end
  52. context 'with secondardy organizations' do
  53. let(:organization) { create(:organization) }
  54. let(:secondary_organizations) { create_list(:organization, 2) }
  55. let(:user) { create(:user, organization_id: organization.id, organization_ids: secondary_organizations.map(&:id)) }
  56. let(:authenticated) { user }
  57. it 'has secondary organizations' do
  58. expect(gql.result.data).to include('hasSecondaryOrganizations' => true)
  59. end
  60. end
  61. context 'with customer' do
  62. context 'with myself' do
  63. let(:authenticated) { user }
  64. it 'has data' do
  65. expect(gql.result.data).to include('firstname' => user.firstname)
  66. end
  67. end
  68. context 'with another customer' do
  69. let(:authenticated) { create(:customer) }
  70. it 'raises an error' do
  71. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  72. end
  73. end
  74. end
  75. end
  76. it_behaves_like 'graphql responds with error if unauthenticated'
  77. end
  78. end