Skip to content

Commit

Permalink
THREESCALE-10856: Searchd: Filter Accounts by provider id (#3873)
Browse files Browse the repository at this point in the history
* Filter accounts by provider ID

* Write test for the fix

---------

Co-authored-by: Aleksandar N. Kostadinov <akostadinov@gmail.com>
  • Loading branch information
jlledom and akostadinov authored Aug 30, 2024
1 parent 7d803a7 commit 9afab5e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/account/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def search_ids(query, options = {})
per_page: ThreeScale::Search::Helpers::SPHINX_PAGE_SIZE_INFINITE,
ignore_scopes: true, with: { })

if (tenant_id = User.tenant_id)
options.deep_merge!(with: { tenant_id: tenant_id })
if (account_id = User.current&.account_id)
options.deep_merge!(with: { provider_account_id: account_id })
end

search(ThinkingSphinx::Query.escape(query), options)
Expand Down
31 changes: 29 additions & 2 deletions test/unit/account/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ class Account::SearchTest < ActiveSupport::TestCase
with: { }
}, Account.buyers.search_ids('foo').options)

User.expects(:tenant_id).returns(42)
user = FactoryBot.build(:user, account_id: 42)
User.expects(:current).returns(user)

assert_equal({
ids_only: true, per_page: 1_000_000, star: true,
ignore_scopes: true, classes: [Account],
with: { tenant_id: 42 }
with: { provider_account_id: 42 }
}, Account.providers.search_ids('foo').options)
end

Expand Down Expand Up @@ -142,6 +143,32 @@ class Account::SearchTest < ActiveSupport::TestCase
Account.scope_search(:query => '')
end

test 'search as master user only returns providers and not buyers' do
ThinkingSphinx::Test.rt_run do
perform_enqueued_jobs only: SphinxAccountIndexationWorker do
FactoryBot.create_list(:provider_account, 3, :with_a_buyer)
end
user = Account.master.first_admin!
User.expects(:current).returns(user)

results = Account.scope_search({query: 'company'})

assert_equal 3, results.size
end
end

test 'search as no user returns providers and buyers' do
ThinkingSphinx::Test.rt_run do
perform_enqueued_jobs only: SphinxAccountIndexationWorker do
FactoryBot.create_list(:provider_account, 3, :with_a_buyer)
end

results = Account.scope_search({query: 'company'})

assert_equal 6, results.size
end
end

test 'by_created_within' do
ThinkingSphinx::Search.expects(:new).never
provider = FactoryBot.create(:simple_provider)
Expand Down

0 comments on commit 9afab5e

Please sign in to comment.