diff --git a/lib/check_search.rb b/lib/check_search.rb index 29cdd37fa1..c0ef5a78ac 100644 --- a/lib/check_search.rb +++ b/lib/check_search.rb @@ -65,7 +65,7 @@ def team_condition(team_id = nil) is_shared = FeedTeam.where(feed_id: @feed.id, team_id: Team.current&.id, shared: true).last is_shared ? feed_teams : [0] # Invalidate the query if the current team is not sharing content else - [team_id || Team.current&.id].compact + [team_id || Team.current&.id].compact.flatten end end @@ -105,6 +105,7 @@ def class_name end def medias + return ProjectMedia.none if @options['team_id'].blank? return [] unless !media_types_filter.blank? && index_exists? return @medias if @medias if should_hit_elasticsearch? @@ -117,7 +118,7 @@ def medias else @medias = get_pg_results end - @medias + @medias.where(team_id: @options['team_id'].map(&:to_i)) # Safe check: Be sure that `team_id` filter is always applied end def project_medias diff --git a/test/controllers/elastic_search_test.rb b/test/controllers/elastic_search_test.rb index bff98d6e63..4c669225c6 100644 --- a/test/controllers/elastic_search_test.rb +++ b/test/controllers/elastic_search_test.rb @@ -264,7 +264,7 @@ def setup test "should ensure project_medias to be an alias of medias" do pm = create_project_media cs = CheckSearch.new('{}', nil, pm.team_id) - assert_equal cs.medias, cs.project_medias + assert_equal cs.medias.to_a, cs.project_medias.to_a end test "should get search id" do diff --git a/test/controllers/graphql_controller_12_test.rb b/test/controllers/graphql_controller_12_test.rb index 96423ddb66..c4cbf1f025 100644 --- a/test/controllers/graphql_controller_12_test.rb +++ b/test/controllers/graphql_controller_12_test.rb @@ -235,4 +235,27 @@ def teardown assert_response :success assert_nil JSON.parse(@response.body).dig('data', 'feed_team') end + + test "should always apply team filter on search" do + setup_elasticsearch + t1 = create_team + t2 = create_team + pm1 = create_project_media team: t1, quote: 'Test 1', disable_es_callbacks: false + pm2 = create_project_media team: t2, quote: 'Test 2', disable_es_callbacks: false + sleep 2 # Wait for content to be indexed + + authenticate_with_user(@u) + + # ElasticSearch + query = 'query { search(query: "{\"keyword\":\"Test\",\"operator\":\"or\"}") { number_of_results } }' + post :create, params: { query: query, team: t1.slug } + assert_response :success + assert_equal 1, JSON.parse(@response.body).dig('data', 'search', 'number_of_results') + + # PostgreSQL + query = 'query { search(query: "{\"operator\":\"or\"}") { number_of_results } }' + post :create, params: { query: query, team: t1.slug } + assert_response :success + assert_equal 1, JSON.parse(@response.body).dig('data', 'search', 'number_of_results') + end end