diff --git a/spec/controllers/search_controller_spec.rb b/spec/controllers/search_controller_spec.rb index 04c2b8db477..0aaf369e646 100644 --- a/spec/controllers/search_controller_spec.rb +++ b/spec/controllers/search_controller_spec.rb @@ -17,6 +17,40 @@ describe SearchController do expect(data['posts'][0]['blurb']).to eq('this is my really awesome post') expect(data['topics'][0]['id']).to eq(my_post.topic_id) end + + it 'performs the query with a type filter' do + user = Fabricate(:user) + my_post = Fabricate(:post, raw: "#{user.username} is a cool person") + xhr :get, :query, term: user.username, type_filter: 'topic' + + expect(response).to be_success + data = JSON.parse(response.body) + + expect(data['posts'][0]['id']).to eq(my_post.id) + expect(data['users']).to be_blank + + xhr :get, :query, term: user.username, type_filter: 'user' + expect(response).to be_success + data = JSON.parse(response.body) + + expect(data['posts']).to be_blank + expect(data['users'][0]['id']).to eq(user.id) + end + + it "can search for id" do + user = Fabricate(:user) + my_post = Fabricate(:post, raw: "#{user.username} is a cool person") + xhr( + :get, + :query, + term: my_post.topic_id, + type_filter: 'topic', + search_for_id: true + ) + expect(response).to be_success + data = JSON.parse(response.body) + expect(data['topics'][0]['id']).to eq(my_post.topic_id) + end end context "#query" do @@ -51,49 +85,7 @@ describe SearchController do end end - - let(:search_context) { {type: 'user', id: 'eviltrout'} } - - pending "basics" do - let(:guardian) { Guardian.new } - let(:search) { mock() } - - before do - Guardian.stubs(:new).returns(guardian) - end - - it 'performs the query' do - Search.expects(:new).with('test', guardian: guardian).returns(search) - search.expects(:execute) - - xhr :get, :query, term: 'test' - end - - it 'performs the query with a filter' do - Search.expects(:new).with('test', guardian: guardian, type_filter: 'topic').returns(search) - search.expects(:execute) - - xhr :get, :query, term: 'test', type_filter: 'topic' - end - - it "performs the query and returns results including blurbs" do - Search.expects(:new).with('test', guardian: guardian, include_blurbs: true).returns(search) - search.expects(:execute) - - xhr :get, :query, term: 'test', include_blurbs: 'true' - end - - it 'performs the query with a filter and passes through search_for_id' do - Search.expects(:new).with('test', guardian: guardian, search_for_id: true, type_filter: 'topic').returns(search) - search.expects(:execute) - - xhr :get, :query, term: 'test', type_filter: 'topic', search_for_id: true - end - end - - - pending "search context" do - + context "search context" do it "raises an error with an invalid context type" do expect { xhr :get, :query, term: 'test', search_context: {type: 'security', id: 'hole'} @@ -108,7 +100,6 @@ describe SearchController do context "with a user" do let(:user) { Fabricate(:user) } - it "raises an error if the user can't see the context" do Guardian.any_instance.expects(:can_see?).with(user).returns(false) xhr :get, :query, term: 'test', search_context: {type: 'user', id: user.username} @@ -116,18 +107,11 @@ describe SearchController do end it 'performs the query with a search context' do - guardian = Guardian.new - Guardian.stubs(:new).returns(guardian) - - search = mock() - Search.expects(:new).with('test', guardian: guardian, search_context: user).returns(search) - search.expects(:execute) - xhr :get, :query, term: 'test', search_context: {type: 'user', id: user.username} + expect(response).to be_success end end + end - - end