preview_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Copyright (C) 2013-2023 Zammad Foundation, https://zammad-foundation.org/
  3. require 'rails_helper'
  4. RSpec.describe Service::ExternalDataSource::Preview do
  5. describe '#execute', db_adapter: :postgresql do
  6. context 'with ElasticSearch', searchindex: true do
  7. let(:data_option) do
  8. create(:object_manager_attribute_autocompletion_ajax_external_data_source, :elastic_search)
  9. .data_option
  10. end
  11. let(:searchterm) { SecureRandom.uuid }
  12. let(:user1) { create(:agent, firstname: searchterm) }
  13. let(:user2) { create(:agent, firstname: searchterm) }
  14. before do
  15. user1
  16. user2
  17. searchindex_model_reload([User])
  18. end
  19. it 'returns search results' do
  20. result = described_class.new.execute(data_option: data_option, render_context: {}, term: searchterm)
  21. expect(result).to include(
  22. success: true,
  23. data: eq([
  24. { value: user1.id.to_s, label: user1.email },
  25. { value: user2.id.to_s, label: user2.email }
  26. ])
  27. )
  28. end
  29. end
  30. context 'with mocked response' do
  31. let(:instance) { described_class.new }
  32. let(:json_response) do
  33. {
  34. 'deadend' => 'yes',
  35. 'results' => {
  36. 'items' => [
  37. { 'data' => { 'id' => 1, 'name' => 'name 1' } },
  38. { 'data' => { 'id' => 2, 'name' => 'name 2' } },
  39. { 'data' => { 'id' => 3, 'name' => false } },
  40. { 'data' => { 'id' => 4, 'name' => true } },
  41. ]
  42. }
  43. }
  44. end
  45. let(:list_key) { 'results.items' }
  46. let(:value_key) { 'data.id' }
  47. let(:label_key) { 'data.name' }
  48. let(:data_option) do
  49. create(:object_manager_attribute_autocompletion_ajax_external_data_source,
  50. list_key: list_key,
  51. value_key: value_key,
  52. label_key: label_key)
  53. .data_option
  54. end
  55. before do
  56. allow_any_instance_of(ExternalDataSource)
  57. .to receive(:fetch_json)
  58. .and_return(json_response)
  59. end
  60. it 'returns correct data' do
  61. result = instance.execute(data_option: data_option, render_context: {}, term: 'term')
  62. expect(result).to include(
  63. success: true,
  64. data: eq([
  65. { value: 1, label: 'name 1' },
  66. { value: 2, label: 'name 2' },
  67. { value: 3, label: false },
  68. { value: 4, label: true },
  69. ])
  70. )
  71. end
  72. context 'when list parsing fails' do
  73. let(:list_key) { 'deadend' }
  74. it 'raises error' do
  75. result = instance.execute(data_option: data_option, render_context: {}, term: 'term')
  76. expect(result).to include(
  77. success: false,
  78. error: 'Search result list key "deadend" is not an array.',
  79. response_body: json_response,
  80. parsed_items: be_nil
  81. )
  82. end
  83. end
  84. context 'when item parsing fails' do
  85. let(:label_key) { 'nonexistant' }
  86. it 'raises error' do
  87. result = instance.execute(data_option: data_option, render_context: {}, term: 'term')
  88. expect(result).to include(
  89. success: false,
  90. error: 'Search result label key "nonexistant" was not found.',
  91. response_body: json_response,
  92. parsed_items: json_response.dig('results', 'items')
  93. )
  94. end
  95. end
  96. end
  97. end
  98. end