Commit c314baee authored by Tanya Pazitny's avatar Tanya Pazitny

Merge branch 'egb-refactor-essearch-api-test' into 'master'

Refactor advanced_global_advanced_syntax_search_spec.rb to use API only

See merge request gitlab-org/gitlab!26021
parents 06125c35 0b7d6ca4
...@@ -39,6 +39,7 @@ module QA ...@@ -39,6 +39,7 @@ module QA
autoload :MailHog, 'qa/runtime/mail_hog' autoload :MailHog, 'qa/runtime/mail_hog'
autoload :IPAddress, 'qa/runtime/ip_address' autoload :IPAddress, 'qa/runtime/ip_address'
autoload :Search, 'qa/runtime/search' autoload :Search, 'qa/runtime/search'
autoload :Project, 'qa/runtime/project'
autoload :ApplicationSettings, 'qa/runtime/application_settings' autoload :ApplicationSettings, 'qa/runtime/application_settings'
module API module API
......
# frozen_string_literal: true
module QA
module Runtime
module Project
extend self
extend Support::Api
def create_project(project_name, api_client, project_description = 'default')
project = Resource::Project.fabricate_via_api! do |project|
project.add_name_uuid = false
project.name = project_name
project.description = project_description
project.api_client = api_client
project.visibility = 'public'
end
project
end
def push_file_to_project(target_project, file_name, file_content)
Resource::Repository::ProjectPush.fabricate! do |push|
push.project = target_project
push.file_name = file_name
push.file_content = file_content
end
end
def set_project_visibility(api_client, project_id, visibility)
request = Runtime::API::Request.new(api_client, "/projects/#{project_id}")
response = put request.url, visibility: visibility
response.code.equal?(QA::Support::Api::HTTP_STATUS_OK)
end
end
end
end
...@@ -42,6 +42,22 @@ module QA ...@@ -42,6 +42,22 @@ module QA
end end
end end
def elasticsearch_on?(api_client)
elasticsearch_state_request = Runtime::API::Request.new(api_client, '/application/settings')
response = get elasticsearch_state_request.url
parse_body(response)[:elasticsearch_search] && parse_body(response)[:elasticsearch_indexing]
end
def disable_elasticsearch(api_client)
disable_elasticsearch_request = Runtime::API::Request.new(api_client, '/application/settings')
put disable_elasticsearch_request.url, elasticsearch_search: false, elasticsearch_indexing: false
end
def create_search_request(api_client, scope, search_term)
Runtime::API::Request.new(api_client, '/search', scope: scope, search: search_term)
end
def find_code(file_name, search_term) def find_code(file_name, search_term)
find_target_in_scope('blobs', search_term) do |record| find_target_in_scope('blobs', search_term) do |record|
record[:filename] == file_name && record[:data].include?(search_term) record[:filename] == file_name && record[:data].include?(search_term)
......
# frozen_string_literal: true
require 'securerandom'
module QA
context 'Enablement:Search' do
include Support::Api
describe 'Elasticsearch advanced global search with advanced syntax', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do
before(:all) do
@api_client = Runtime::API::Client.new(:gitlab)
@project_name_suffix = SecureRandom.hex(8)
@elasticsearch_original_state_on = Runtime::Search.elasticsearch_on?(@api_client)
unless @elasticsearch_original_state_on
QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api!
sleep(60)
# wait for the change to propagate before inserting records or else
# Gitlab::CurrentSettings.elasticsearch_indexing and
# Elastic::ApplicationVersionedSearch::searchable? will be false
# this sleep can be removed after we're able to query logs via the API
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end
@project = Runtime::Project.create_project("es-adv-global-search-#{@project_name_suffix}",
@api_client,
"This is a unique project description #{@project_name_suffix}")
Runtime::Project.push_file_to_project(@project, 'elasticsearch.rb', "elasticsearch: #{SecureRandom.hex(8)}")
end
after(:all) do
if !@elasticsearch_original_state_on && !@api_client.nil?
Runtime::Search.disable_elasticsearch(@api_client)
end
end
context 'when searching for projects using advanced syntax' do
it 'searches in the project name' do
expect_search_to_find_project("es-adv-*#{@project_name_suffix}")
end
it 'searches in the project description' do
expect_search_to_find_project("unique +#{@project_name_suffix}")
end
end
private
def expect_search_to_find_project(search_term)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get Runtime::Search.create_search_request(@api_client, 'projects', search_term).url
expect_status(QA::Support::Api::HTTP_STATUS_OK)
raise 'Empty search result returned' if json_body.empty?
expect(json_body[0][:name]).to eq(@project.name)
end
end
end
end
end
...@@ -3,55 +3,52 @@ ...@@ -3,55 +3,52 @@
require 'securerandom' require 'securerandom'
module QA module QA
context 'Enablement - Search' do context 'Enablement:Search' do
include Support::Api
describe 'When using elasticsearch API to search for a known blob', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do describe 'When using elasticsearch API to search for a known blob', :orchestrated, :elasticsearch, :requires_admin, quarantine: { type: :new } do
before(:all) do before(:all) do
@project_name = "api-es-#{SecureRandom.hex(8)}"
@project_file_content = "elasticsearch: #{SecureRandom.hex(8)}"
@api_client = Runtime::API::Client.new(:gitlab) @api_client = Runtime::API::Client.new(:gitlab)
@unauthorized_user = Resource::User.fabricate_or_use('unauthorized_user', "unauthorized_user_password") @project_file_content = "elasticsearch: #{SecureRandom.hex(8)}"
@unauthorized_api_client = Runtime::API::Client.new(user: @unauthorized_user) non_member_user = Resource::User.fabricate_or_use('non_member_user', 'non_member_user_password')
@non_member_api_client = Runtime::API::Client.new(user: non_member_user)
elasticsearch_state_request = Runtime::API::Request.new(@api_client, '/application/settings') @elasticsearch_original_state_on = Runtime::Search.elasticsearch_on?(@api_client)
response = get elasticsearch_state_request.url
if response.to_s.match(/"elasticsearch_search":true/) && response.to_s.match(/"elasticsearch_indexing":true/)
@elasticsearch_original_state_on = true
end
if @elasticsearch_original_state_on.nil? unless @elasticsearch_original_state_on
QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api! QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api!
sleep(60) # wait for the change to propagate before inserting records or else Gitlab::CurrentSettings.elasticsearch_indexing and Elastic::ApplicationVersionedSearch::searchable? will be false sleep(60)
# this sleep can be removed after we're able to query logs via the API as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395 # wait for the change to propagate before inserting records or else
# Gitlab::CurrentSettings.elasticsearch_indexing and
# Elastic::ApplicationVersionedSearch::searchable? will be false
# this sleep can be removed after we're able to query logs via the API
# as per this issue https://gitlab.com/gitlab-org/quality/team-tasks/issues/395
end end
@project = create_project @project = Runtime::Project.create_project("api-es-#{SecureRandom.hex(8)}", @api_client)
Runtime::Project.push_file_to_project(@project, 'README.md', @project_file_content)
end end
after(:all) do after(:all) do
if !@elasticsearch_original_state_on && !@api_client.nil? if !@elasticsearch_original_state_on && !@api_client.nil?
disable_elasticsearch_request = Runtime::API::Request.new(@api_client, '/application/settings') Runtime::Search.disable_elasticsearch(@api_client)
put disable_elasticsearch_request.url, elasticsearch_search: false, elasticsearch_indexing: false
end end
end end
it 'searches public project and finds a blob as an unauthorized user' do it 'searches public project and finds a blob as an non-member user' do
sucessful_search(@unauthorized_api_client) successful_search(@non_member_api_client)
end end
describe 'When searching a private repository' do describe 'When searching a private repository' do
before(:all) do before(:all) do
set_project_visibility('private') Runtime::Project.set_project_visibility(@api_client, @project.id, 'private')
end end
it 'finds a blob as an authorized user' do it 'finds a blob as an authorized user' do
sucessful_search(@api_client) successful_search(@api_client)
end end
it 'does not find a blob as an unauthorized user' do it 'does not find a blob as an non-member user' do
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get create_search_request(@unauthorized_api_client).url get Runtime::Search.create_search_request(@non_member_api_client, 'blobs', @project_file_content).url
expect_status(QA::Support::Api::HTTP_STATUS_OK) expect_status(QA::Support::Api::HTTP_STATUS_OK)
expect(json_body).to be_empty expect(json_body).to be_empty
end end
...@@ -60,45 +57,17 @@ module QA ...@@ -60,45 +57,17 @@ module QA
private private
def create_project def successful_search(api_client)
project = Resource::Project.fabricate_via_api! do |project|
project.name = @project_name
project.api_client = @api_client
end
Resource::Repository::ProjectPush.fabricate! do |push|
push.project = project
push.file_name = 'README.md'
push.file_content = @project_file_content
push.commit_message = 'Add README.md'
end
project
end
def create_search_request(api_client)
Runtime::API::Request.new(api_client, '/search', scope: 'blobs', search: "#{@project_file_content}")
end
def sucessful_search(api_client)
QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do QA::Support::Retrier.retry_on_exception(max_attempts: 10, sleep_interval: 3) do
get create_search_request(api_client).url get Runtime::Search.create_search_request(api_client, 'blobs', @project_file_content).url
expect_status(QA::Support::Api::HTTP_STATUS_OK) expect_status(QA::Support::Api::HTTP_STATUS_OK)
if json_body.empty? raise 'Empty search result returned' if json_body.empty?
raise 'Empty search result returned'
end
expect(json_body[0][:data]).to match(@project_file_content) expect(json_body[0][:data]).to match(@project_file_content)
expect(json_body[0][:project_id]).to equal(@project.id) expect(json_body[0][:project_id]).to equal(@project.id)
end end
end end
def set_project_visibility(visibility)
request = Runtime::API::Request.new(@api_client, "/projects/#{@project.id}")
put request.url, visibility: visibility
expect_status(QA::Support::Api::HTTP_STATUS_OK)
end
end end
end end
end end
# frozen_string_literal: true
module QA
context 'Create' do
describe 'Elasticsearch advanced global search with advanced syntax', :orchestrated, :elasticsearch, :requires_admin do
let(:project_name_suffix) { SecureRandom.hex(8) }
let(:project_file_name) { 'elasticsearch.rb' }
let(:project_file_content) { "elasticsearch: #{SecureRandom.hex(8)}" }
let(:project) do
Resource::Project.fabricate_via_api! do |project|
project.add_name_uuid = false
project.name = "es-adv-global-search-#{project_name_suffix}"
project.description = "This is a unique project description #{project_name_suffix}"
end
end
before(:context) do
QA::EE::Resource::Settings::Elasticsearch.fabricate_via_api! do |es|
es.user = QA::Resource::User.new.tap do |user|
user.username = QA::Runtime::User.admin_username
user.password = QA::Runtime::User.admin_password
end
es.api_client = Runtime::API::Client.as_admin
end
Runtime::Search.assert_elasticsearch_responding
end
before do
Resource::Repository::ProjectPush.fabricate! do |push|
push.project = project
push.file_name = project_file_name
push.file_content = project_file_content
end
Flow::Login.sign_in
end
context 'when searching for projects using advanced syntax' do
it 'searches in the project name', retry: 3 do
expect_search_to_find_project("es-adv-*#{project_name_suffix}")
end
it 'searches in the project description', retry: 3 do
expect_search_to_find_project("unique +#{project_name_suffix}")
end
end
def expect_search_to_find_project(search_term)
expect { Runtime::Search.find_project(project, search_term) }.not_to raise_error
Page::Main::Menu.perform do |menu|
menu.search_for(search_term)
end
Page::Search::Results.perform do |results|
results.switch_to_projects
results.retry_on_exception(reload: true, sleep_interval: 10) do
expect(results).to have_content("Advanced search functionality is enabled")
expect(results).to have_project(project.name)
end
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment