/ee/spec/models/concerns/elastic/issue_spec.rb

https://gitlab.com/klml/gitlab-ee · Ruby · 173 lines · 128 code · 40 blank · 5 comment · 0 complexity · 33044ea2c83bef8cb199cdcaf9f3f5bb MD5 · raw file

  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Issue, :elastic do
  4. before do
  5. stub_ee_application_setting(elasticsearch_search: true, elasticsearch_indexing: true)
  6. end
  7. let(:project) { create :project, :public }
  8. let(:admin) { create :user, :admin }
  9. context 'when limited indexing is on' do
  10. let_it_be(:project) { create :project, name: 'test1' }
  11. let_it_be(:issue) { create :issue, project: project}
  12. before do
  13. stub_ee_application_setting(elasticsearch_limit_indexing: true)
  14. end
  15. context 'when the project is not enabled specifically' do
  16. describe '#searchable?' do
  17. it 'returns false' do
  18. expect(issue.searchable?).to be_falsey
  19. end
  20. end
  21. end
  22. context 'when a project is enabled specifically' do
  23. before do
  24. create :elasticsearch_indexed_project, project: project
  25. end
  26. describe '#searchable?' do
  27. it 'returns true' do
  28. expect(issue.searchable?).to be_truthy
  29. end
  30. end
  31. end
  32. context 'when a group is enabled' do
  33. let_it_be(:group) { create(:group) }
  34. before do
  35. create :elasticsearch_indexed_namespace, namespace: group
  36. end
  37. describe '#searchable?' do
  38. it 'returns true' do
  39. project = create :project, name: 'test1', group: group
  40. issue = create :issue, project: project
  41. expect(issue.searchable?).to be_truthy
  42. end
  43. end
  44. end
  45. end
  46. it "searches issues", :aggregate_failures do
  47. Sidekiq::Testing.inline! do
  48. create :issue, title: 'bla-bla term1', project: project
  49. create :issue, description: 'bla-bla term2', project: project
  50. create :issue, project: project
  51. # The issue I have no access to except as an administrator
  52. create :issue, title: 'bla-bla term3', project: create(:project, :private)
  53. ensure_elasticsearch_index!
  54. end
  55. options = { project_ids: [project.id] }
  56. expect(described_class.elastic_search('(term1 | term2 | term3) +bla-bla', options: options).total_count).to eq(2)
  57. expect(described_class.elastic_search(Issue.last.to_reference, options: options).total_count).to eq(1)
  58. expect(described_class.elastic_search('bla-bla', options: { project_ids: :any, public_and_internal_projects: true }).total_count).to eq(3)
  59. end
  60. it "names elasticsearch queries" do
  61. described_class.elastic_search('*').total_count
  62. assert_named_queries('doc:is_a:issue',
  63. 'issue:match:search_terms',
  64. 'issue:authorized:project')
  65. end
  66. it "searches by iid and scopes to type: issue only" do
  67. issue = nil
  68. Sidekiq::Testing.inline! do
  69. issue = create :issue, title: 'bla-bla issue', project: project
  70. create :issue, description: 'term2 in description', project: project
  71. # MergeRequest with the same iid should not be found in Issue search
  72. create :merge_request, title: 'bla-bla', source_project: project, iid: issue.iid
  73. ensure_elasticsearch_index!
  74. end
  75. # User needs to be admin or the MergeRequest would just be filtered by
  76. # confidential: false
  77. options = { project_ids: [project.id], current_user: admin }
  78. results = described_class.elastic_search("##{issue.iid}", options: options)
  79. expect(results.total_count).to eq(1)
  80. expect(results.first.title).to eq('bla-bla issue')
  81. end
  82. it "returns json with all needed elements" do
  83. assignee = create(:user)
  84. project = create(:project, :internal)
  85. issue = create :issue, project: project, assignees: [assignee]
  86. expected_hash = issue.attributes.extract!(
  87. 'id',
  88. 'iid',
  89. 'title',
  90. 'description',
  91. 'created_at',
  92. 'updated_at',
  93. 'project_id',
  94. 'author_id',
  95. 'confidential'
  96. ).merge({
  97. 'type' => issue.es_type,
  98. 'state' => issue.state
  99. })
  100. expected_hash['assignee_id'] = [assignee.id]
  101. expected_hash['issues_access_level'] = ProjectFeature::ENABLED
  102. expected_hash['visibility_level'] = Gitlab::VisibilityLevel::INTERNAL
  103. expect(issue.__elasticsearch__.as_indexed_json).to eq(expected_hash)
  104. end
  105. it 'handles a project missing project_feature', :aggregate_failures do
  106. issue = create :issue, project: project
  107. allow(issue.project).to receive(:project_feature).and_return(nil)
  108. expect { issue.__elasticsearch__.as_indexed_json }.not_to raise_error
  109. expect(issue.__elasticsearch__.as_indexed_json['issues_access_level']).to eq(ProjectFeature::PRIVATE)
  110. end
  111. context 'field length limits' do
  112. context 'when there is an elasticsearch_indexed_field_length limit' do
  113. it 'truncates to the default plan limit' do
  114. stub_ee_application_setting(elasticsearch_indexed_field_length_limit: 10)
  115. issue = create :issue, description: 'The description is too long'
  116. indexed_json = issue.__elasticsearch__.as_indexed_json
  117. expect(indexed_json['description']).to eq('The descri')
  118. end
  119. end
  120. context 'when the elasticsearch_indexed_field_length limit is 0' do
  121. it 'does not truncate the fields' do
  122. stub_ee_application_setting(elasticsearch_indexed_field_length_limit: 0)
  123. issue = create :issue, description: 'The description is too long'
  124. indexed_json = issue.__elasticsearch__.as_indexed_json
  125. expect(indexed_json['description']).to eq('The description is too long')
  126. end
  127. end
  128. end
  129. it_behaves_like 'no results when the user cannot read cross project' do
  130. let(:record1) { create(:issue, project: project, title: 'test-issue') }
  131. let(:record2) { create(:issue, project: project2, title: 'test-issue') }
  132. end
  133. end