zammad_schema_global_id_spec.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::ZammadSchema, type: :graphql do
  4. describe '.id_from_object' do
  5. it 'generates GraphQL::ID values' do
  6. expect(described_class.id_from_object(Ticket.first)).to eq('gid://zammad/Ticket/1')
  7. end
  8. end
  9. describe '.object_from_id' do
  10. it 'resolves GraphQL::ID values' do
  11. expect(described_class.object_from_id('gid://zammad/Ticket/1')).to eq(Ticket.first)
  12. end
  13. end
  14. describe '.id_from_internal_id' do
  15. it 'resolves internal IDs to GraphQL::IDs' do
  16. expect(described_class.id_from_internal_id(Ticket, 1)).to eq('gid://zammad/Ticket/1')
  17. end
  18. it 'resolves internal IDs to GraphQL::IDs (with class name as string)' do
  19. expect(described_class.id_from_internal_id('Ticket', 1)).to eq('gid://zammad/Ticket/1')
  20. end
  21. end
  22. describe '.internal_id_from_id' do
  23. let(:id) { [*(1..999)].sample }
  24. let(:gid_string) { "gid://zammad/Ticket/#{id}" }
  25. it 'returns internal ID based on given global ID' do
  26. expect(described_class.internal_id_from_id(gid_string)).to eq(id)
  27. end
  28. it 'returns nil if class is not allowed' do
  29. expect(described_class.internal_id_from_id(gid_string, type: User)).to be_nil
  30. end
  31. it 'returns internal ID if class is allowed' do
  32. expect(described_class.internal_id_from_id(gid_string, type: Ticket)).to eq(id)
  33. end
  34. end
  35. describe '.internal_ids_from_ids' do
  36. let(:id) { [*(1..999)].sample }
  37. let(:gid_string) { "gid://zammad/Ticket/#{id}" }
  38. it 'returns internal IDs based on given global IDs' do
  39. expect(described_class.internal_ids_from_ids([gid_string])).to eq([id])
  40. end
  41. it 'returns internal IDs if class is allowed' do
  42. expect(described_class.internal_ids_from_ids([gid_string], type: Ticket)).to eq([id])
  43. end
  44. it 'skips item if class is not allowed' do
  45. expect(described_class.internal_ids_from_ids([gid_string], type: User)).to be_blank
  46. end
  47. end
  48. describe '.local_uris_from_ids' do
  49. let(:id) { [*(1..999)].sample }
  50. let(:gid_string) { "gid://zammad/Ticket/#{id}" }
  51. let(:gid) { GlobalID.new(gid_string) }
  52. it 'returns given global ID as GlobalID' do
  53. expect(described_class.local_uris_from_ids([gid_string])).to eq([gid])
  54. end
  55. it 'returns given global ID if class is allowed' do
  56. expect(described_class.local_uris_from_ids([gid_string], type: Ticket)).to eq([gid])
  57. end
  58. it 'skips item if class is not allowed' do
  59. expect(described_class.local_uris_from_ids([gid_string], type: User)).to be_blank
  60. end
  61. end
  62. end