docbrown/spec/services/search/user_spec.rb
Molly Struve 385e167f74
Clear Elasticsearch Data Instead of Resetting Entire Index for Specs (#7602)
* Clear Elasticsearch Data Instead of Resetting Entire Index for Specs

* only reset for specs that are messing with index creation and deletion, recreate indexes before and after those

* add back elasticsearch meta info to specs and update data update specs

* update serializer specs

* update search and rest of specs with new meta data tag
2020-04-30 11:59:10 -04:00

49 lines
1.8 KiB
Ruby

require "rails_helper"
RSpec.describe Search::User, type: :service do
it "defines INDEX_NAME, INDEX_ALIAS, and MAPPINGS", :aggregate_failures do
expect(described_class::INDEX_NAME).not_to be_nil
expect(described_class::INDEX_ALIAS).not_to be_nil
expect(described_class::MAPPINGS).not_to be_nil
end
describe "::search_documents", elasticsearch: "User" do
let(:user1) { create(:user) }
let(:user2) { create(:user) }
it "parses user document hits from search response" do
mock_search_response = { "hits" => { "hits" => {} } }
allow(described_class).to receive(:search) { mock_search_response }
described_class.search_documents(params: {})
expect(described_class).to have_received(:search).with(body: a_kind_of(Hash))
end
context "with a query" do
it "searches by search_fields" do
allow(user1).to receive(:available_for).and_return("ruby")
allow(user2).to receive(:employer_name).and_return("Ruby Tuesday")
index_documents([user1, user2])
query_params = { size: 5, search_fields: "ruby" }
user_docs = described_class.search_documents(params: query_params)
expect(user_docs.count).to eq(2)
doc_ids = user_docs.map { |t| t.dig("id") }
expect(doc_ids).to include(user1.id, user2.id)
end
end
context "with a filter" do
it "searches by excluding roles" do
user1.add_role(:admin)
user2.add_role(:banned)
index_documents([user1, user2])
query_params = { size: 5, exclude_roles: ["banned"] }
user_docs = described_class.search_documents(params: query_params)
expect(user_docs.count).to eq(1)
doc_ids = user_docs.map { |t| t.dig("id") }
expect(doc_ids).to match_array([user1.id])
end
end
end
end