diff --git a/test/controllers/form_controller_test.rb b/test/controllers/form_controller_test.rb index 98ea10cee..69d47de62 100644 --- a/test/controllers/form_controller_test.rb +++ b/test/controllers/form_controller_test.rb @@ -1,43 +1,16 @@ - require 'test_helper' -require 'rake' class FormControllerTest < ActionDispatch::IntegrationTest + include SearchindexHelper + setup do @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json', 'REMOTE_ADDR' => '1.2.3.4' } - if ENV['ES_URL'].present? - - #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - Setting.set('es_url', ENV['ES_URL']) - - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - Setting.set('es_index', ENV['ES_INDEX']) - end + configure_elasticsearch Ticket.destroy_all - # drop/create indexes - Setting.reload - Rake::Task.clear - Zammad::Application.load_tasks - Rake::Task['searchindex:rebuild'].execute - end - - teardown do - if ENV['ES_URL'].present? - Rake::Task['searchindex:drop'].execute - end + rebuild_searchindex end test '01 - get config call' do diff --git a/test/controllers/organization_controller_test.rb b/test/controllers/organization_controller_test.rb index 67e844c06..2aeebeb46 100644 --- a/test/controllers/organization_controller_test.rb +++ b/test/controllers/organization_controller_test.rb @@ -1,8 +1,8 @@ - require 'test_helper' -require 'rake' class OrganizationControllerTest < ActionDispatch::IntegrationTest + include SearchindexHelper + setup do # set accept header @@ -73,32 +73,11 @@ class OrganizationControllerTest < ActionDispatch::IntegrationTest organization_id: @organization.id, ) - # configure es - if ENV['ES_URL'].present? - #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - Setting.set('es_url', ENV['ES_URL']) - - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - Setting.set('es_index', ENV['ES_INDEX']) + configure_elasticsearch do travel 1.minute - # drop/create indexes - Rake::Task.clear - Zammad::Application.load_tasks - #Rake::Task["searchindex:drop"].execute - #Rake::Task["searchindex:create"].execute - Rake::Task['searchindex:rebuild'].execute + rebuild_searchindex # execute background jobs Scheduler.worker(true) diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rb index 06a54096b..2967c628c 100644 --- a/test/controllers/reports_controller_test.rb +++ b/test/controllers/reports_controller_test.rb @@ -1,7 +1,8 @@ require 'test_helper' -require 'rake' class ReportsControllerTest < ActionDispatch::IntegrationTest + include SearchindexHelper + setup do # set accept header @@ -69,31 +70,12 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest updated_by_id: 1, created_by_id: 1, ) - if ENV['ES_URL'].present? - #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - Setting.set('es_url', ENV['ES_URL']) - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - Setting.set('es_index', ENV['ES_INDEX']) + configure_elasticsearch do travel 1.minute - # drop/create indexes - Rake::Task.clear - Zammad::Application.load_tasks - #Rake::Task["searchindex:drop"].execute - #Rake::Task["searchindex:create"].execute - Rake::Task['searchindex:rebuild'].execute + rebuild_searchindex # execute background jobs Scheduler.worker(true) @@ -101,11 +83,6 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest sleep 6 end end - teardown do - if ENV['ES_URL'].present? - Rake::Task['searchindex:drop'].execute - end - end test '01.01 report example - admin access' do diff --git a/test/controllers/search_controller_test.rb b/test/controllers/search_controller_test.rb index e444485e9..6bfa3f726 100644 --- a/test/controllers/search_controller_test.rb +++ b/test/controllers/search_controller_test.rb @@ -1,8 +1,8 @@ - require 'test_helper' -require 'rake' class SearchControllerTest < ActionDispatch::IntegrationTest + include SearchindexHelper + setup do # set current user @@ -142,35 +142,11 @@ class SearchControllerTest < ActionDispatch::IntegrationTest type: Ticket::Article::Type.where(name: 'email').first, ) - # configure es - if ENV['ES_URL'].present? - #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - Setting.set('es_url', ENV['ES_URL']) - - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - Setting.set('es_index', ENV['ES_INDEX']) - - # set max attachment size in mb - Setting.set('es_attachment_max_size_in_mb', 1) + configure_elasticsearch do travel 1.minute - # drop/create indexes - Rake::Task.clear - Zammad::Application.load_tasks - #Rake::Task["searchindex:drop"].execute - #Rake::Task["searchindex:create"].execute - Rake::Task['searchindex:rebuild'].execute + rebuild_searchindex # execute background jobs Scheduler.worker(true) @@ -179,12 +155,6 @@ class SearchControllerTest < ActionDispatch::IntegrationTest end end - teardown do - if ENV['ES_URL'].present? - Rake::Task['searchindex:drop'].execute - end - end - test 'settings index with nobody' do params = { query: 'test 1234', diff --git a/test/controllers/user_controller_test.rb b/test/controllers/user_controller_test.rb index 1b810356f..a5d5ed638 100644 --- a/test/controllers/user_controller_test.rb +++ b/test/controllers/user_controller_test.rb @@ -1,8 +1,8 @@ - require 'test_helper' -require 'rake' class UserControllerTest < ActionDispatch::IntegrationTest + include SearchindexHelper + setup do # set accept header @@ -84,40 +84,19 @@ class UserControllerTest < ActionDispatch::IntegrationTest organization_id: @organization.id, ) - # configure es - if ENV['ES_URL'].present? - #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - Setting.set('es_url', ENV['ES_URL']) - - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - Setting.set('es_index', ENV['ES_INDEX']) + configure_elasticsearch do travel 1.minute - # drop/create indexes - Rake::Task.clear - Zammad::Application.load_tasks - #Rake::Task["searchindex:drop"].execute - #Rake::Task["searchindex:create"].execute - Rake::Task['searchindex:rebuild'].execute + rebuild_searchindex # execute background jobs Scheduler.worker(true) sleep 6 end - UserInfo.current_user_id = nil + UserInfo.current_user_id = nil end test 'user create tests - no user' do diff --git a/test/controllers/user_organization_controller_test.rb b/test/controllers/user_organization_controller_test.rb index b484d4faf..e2910a56a 100644 --- a/test/controllers/user_organization_controller_test.rb +++ b/test/controllers/user_organization_controller_test.rb @@ -1,8 +1,8 @@ - require 'test_helper' -require 'rake' class UserOrganizationControllerTest < ActionDispatch::IntegrationTest + include SearchindexHelper + setup do # set accept header @@ -84,39 +84,17 @@ class UserOrganizationControllerTest < ActionDispatch::IntegrationTest organization_id: @organization.id, ) - # configure es - if ENV['ES_URL'].present? - #fail "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - Setting.set('es_url', ENV['ES_URL']) - - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - Setting.set('es_index', ENV['ES_INDEX']) + configure_elasticsearch do travel 1.minute - # drop/create indexes - Rake::Task.clear - Zammad::Application.load_tasks - #Rake::Task["searchindex:drop"].execute - #Rake::Task["searchindex:create"].execute - Rake::Task['searchindex:rebuild'].execute + rebuild_searchindex # execute background jobs Scheduler.worker(true) sleep 6 end - end test 'user create tests - no user' do diff --git a/test/integration/elasticsearch_test.rb b/test/integration/elasticsearch_test.rb index 94d4991bc..ebd8eefe4 100644 --- a/test/integration/elasticsearch_test.rb +++ b/test/integration/elasticsearch_test.rb @@ -1,38 +1,13 @@ - require 'integration_test_helper' -require 'rake' class ElasticsearchTest < ActiveSupport::TestCase + include SearchindexHelper setup do - # set config - if ENV['ES_URL'].blank? - raise "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - end - Setting.set('es_url', ENV['ES_URL']) - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - Setting.set('es_index', ENV['ES_INDEX']) + configure_elasticsearch(required: true) - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - - # set max attachment size in mb - Setting.set('es_attachment_max_size_in_mb', 1) - - # drop/create indexes - Rake::Task.clear - Zammad::Application.load_tasks - #Rake::Task["searchindex:drop"].execute - #Rake::Task["searchindex:create"].execute - Rake::Task['searchindex:rebuild'].execute + rebuild_searchindex groups = Group.where(name: 'Users') roles = Role.where(name: 'Agent') @@ -100,12 +75,6 @@ class ElasticsearchTest < ActiveSupport::TestCase ) end - teardown do - if ENV['ES_URL'].present? - Rake::Task['searchindex:drop'].execute - end - end - # check search attributes test 'a - objects' do diff --git a/test/integration/report_test.rb b/test/integration/report_test.rb index 0ec471fd4..02d8c665a 100644 --- a/test/integration/report_test.rb +++ b/test/integration/report_test.rb @@ -1,37 +1,15 @@ - require 'integration_test_helper' -require 'rake' class ReportTest < ActiveSupport::TestCase + include SearchindexHelper setup do - # set config - if ENV['ES_URL'].blank? - raise "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" - end - Setting.set('es_url', ENV['ES_URL']) - if ENV['ES_INDEX_RAND'].present? - ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" - end - if ENV['ES_INDEX'].blank? - raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" - end - - # Setting.set('es_url', 'http://127.0.0.1:9200') - # Setting.set('es_index', 'estest.local_zammad') - # Setting.set('es_user', 'elasticsearch') - # Setting.set('es_password', 'zammad') - # Setting.set('es_attachment_max_size_in_mb', 1) + configure_elasticsearch(required: true) Ticket.destroy_all - # drop/create indexes - Rake::Task.clear - Zammad::Application.load_tasks - #Rake::Task["searchindex:drop"].execute - #Rake::Task["searchindex:create"].execute - Rake::Task['searchindex:rebuild'].execute + rebuild_searchindex group1 = Group.lookup(name: 'Users') group2 = Group.create_if_not_exists( @@ -275,13 +253,6 @@ class ReportTest < ActiveSupport::TestCase # execute background jobs Scheduler.worker(true) - - end - - teardown do - if ENV['ES_URL'].present? - Rake::Task['searchindex:drop'].execute - end end test 'compare' do diff --git a/test/integration_test_helper.rb b/test/integration_test_helper.rb index 9ded0c718..ec00050a4 100644 --- a/test/integration_test_helper.rb +++ b/test/integration_test_helper.rb @@ -3,6 +3,8 @@ require File.expand_path('../config/environment', __dir__) require 'rails/test_help' require 'cache' +require 'test_support_helper' + class ActiveSupport::TestCase # disable transactions / to work with own database connections for each thread diff --git a/test/support/searchindex_helper.rb b/test/support/searchindex_helper.rb new file mode 100644 index 000000000..a1a31217f --- /dev/null +++ b/test/support/searchindex_helper.rb @@ -0,0 +1,49 @@ +require 'rake' + +module SearchindexHelper + + def self.included(base) + + base.teardown do + next if ENV['ES_URL'].blank? + + Rake::Task['searchindex:drop'].execute + end + end + + def configure_elasticsearch(required: false) + if ENV['ES_URL'].blank? + return if !required + raise "ERROR: Need ES_URL - hint ES_URL='http://127.0.0.1:9200'" + end + + Setting.set('es_url', ENV['ES_URL']) + + # Setting.set('es_url', 'http://127.0.0.1:9200') + # Setting.set('es_index', 'estest.local_zammad') + # Setting.set('es_user', 'elasticsearch') + # Setting.set('es_password', 'zammad') + + if ENV['ES_INDEX_RAND'].present? + ENV['ES_INDEX'] = "es_index_#{rand(999_999_999)}" + end + if ENV['ES_INDEX'].blank? + raise "ERROR: Need ES_INDEX - hint ES_INDEX='estest.local_zammad'" + end + Setting.set('es_index', ENV['ES_INDEX']) + + # set max attachment size in mb + Setting.set('es_attachment_max_size_in_mb', 1) + + yield if block_given? + end + + def rebuild_searchindex + # drop/create indexes + Rake::Task.clear + Zammad::Application.load_tasks + #Rake::Task["searchindex:drop"].execute + #Rake::Task["searchindex:create"].execute + Rake::Task['searchindex:rebuild'].execute + end +end