Skip to content

Commit

Permalink
Adding one more team_id filter to CheckSearch
Browse files Browse the repository at this point in the history
Just to be safe and avoid regressions from other parts of the code, be sure that at the end, the `team_id` filter is always applied on `CheckSearch` `media` queries.

Reference: CV2-4062.
  • Loading branch information
caiosba committed Dec 2, 2023
1 parent 468e334 commit a0db4a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/elastic_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/graphql_controller_12_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a0db4a1

Please sign in to comment.