configuration_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Mutations::System::Import::Configuration, type: :graphql do
  4. context 'when validation system import configuration' do
  5. let(:mutation) do
  6. <<~MUTATION
  7. mutation systemImportConfiguration($configuration: SystemImportConfigurationInput!) {
  8. systemImportConfiguration(configuration: $configuration) {
  9. success
  10. errors {
  11. message
  12. field
  13. }
  14. }
  15. }
  16. MUTATION
  17. end
  18. context 'with valid configuration' do
  19. let(:variables) do
  20. {
  21. configuration: {
  22. url: 'https://ticket.freshdesk.com',
  23. secret: 'freshdesk_api_token',
  24. source: 'freshdesk',
  25. }
  26. }
  27. end
  28. it 'succeeds' do
  29. mock = Service::System::Import::ApplyFreshdeskConfiguration
  30. allow_any_instance_of(mock).to receive(:execute).and_return(true)
  31. gql.execute(mutation, variables: variables)
  32. expect(gql.result.data).to include({ 'success' => true })
  33. end
  34. end
  35. shared_examples 'missing required configuration parameter' do
  36. it 'raises an error' do
  37. gql.execute(mutation, variables: variables)
  38. expect { gql.result.data }.to raise_error(RuntimeError, %r{#{parameter}})
  39. end
  40. end
  41. %w[url source].each do |p|
  42. context "when no #{p} is provided" do
  43. let(:variables) do
  44. config = {
  45. configuration: {
  46. url: 'https://ticket.otrs.com',
  47. source: 'otrs'
  48. }
  49. }
  50. config[:configuration].delete(p.to_sym)
  51. config
  52. end
  53. let(:parameter) { p }
  54. it_behaves_like 'missing required configuration parameter'
  55. end
  56. end
  57. context 'when invalid url is provided' do
  58. let(:variables) do
  59. {
  60. configuration: {
  61. url: 'gopher://ticket.otrs.com',
  62. source: 'otrs'
  63. }
  64. }
  65. end
  66. it 'raises an error' do
  67. gql.execute(mutation, variables: variables)
  68. expect { gql.result.data }.to raise_error(RuntimeError, %r{URI scheme must be HTTP or HTTPS})
  69. end
  70. end
  71. context 'when url is not reachable' do
  72. let(:variables) do
  73. {
  74. configuration: {
  75. url: 'https://ticket.freshdesk.com',
  76. secret: 'freshdesk_api_token',
  77. source: 'freshdesk'
  78. }
  79. }
  80. end
  81. it 'returns an error' do
  82. mock = Service::System::Import::ApplyFreshdeskConfiguration
  83. error = Service::System::Import::ApplyFreshdeskConfiguration::UnreachableError
  84. allow_any_instance_of(mock).to receive(:reachable!).and_raise(error, 'The hostname could not be found.')
  85. gql.execute(mutation, variables: variables)
  86. expect(gql.result.data).to include({ 'errors' => [{ 'message' => 'The hostname could not be found.', 'field' => 'url' }] })
  87. end
  88. end
  89. context 'when url is inaccessible' do
  90. let(:variables) do
  91. {
  92. configuration: {
  93. url: 'https://ticket.zendesk.com',
  94. username: 'zendesk',
  95. secret: 'zendesk_api_token',
  96. source: 'zendesk'
  97. }
  98. }
  99. end
  100. it 'returns an error' do
  101. mock = Service::System::Import::ApplyZendeskConfiguration
  102. error = Service::System::Import::ApplyZendeskConfiguration::InaccessibleError
  103. allow_any_instance_of(mock).to receive(:reachable!).and_return(nil)
  104. allow_any_instance_of(mock).to receive(:accessible!).and_raise(error, 'The provided credentials are invalid.')
  105. gql.execute(mutation, variables: variables)
  106. expect(gql.result.data).to include({
  107. 'errors' => [
  108. { 'message' => 'The provided credentials are invalid.', 'field' => 'secret' },
  109. { 'message' => 'The provided credentials are invalid.', 'field' => 'username' }
  110. ]
  111. })
  112. end
  113. end
  114. end
  115. end