Search API Endpoint and unit test complete
This commit is contained in:
parent
2f432eb560
commit
7dba616e4e
4 changed files with 190 additions and 0 deletions
31
app/controllers/api/v1/search_controller.rb
Normal file
31
app/controllers/api/v1/search_controller.rb
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class SearchController < Api::V1::BaseController
|
||||
before_action do
|
||||
require_access_token %w[read]
|
||||
end
|
||||
|
||||
rescue_from ActionController::ParameterMissing, RuntimeError do
|
||||
render json: I18n.t("api.endpoint_errors.search.cant_process"), status: :unprocessable_entity
|
||||
end
|
||||
|
||||
def user_index
|
||||
parameters = params.permit(:tag, :name_or_handle)
|
||||
raise RuntimeError if parameters.keys.length != 1
|
||||
people = if params.has_key?(:tag)
|
||||
Person.profile_tagged_with(params[:tag])
|
||||
else
|
||||
Person.search(params[:name_or_handle], current_user)
|
||||
end
|
||||
render json: people.map {|p| PersonPresenter.new(p).as_api_json }
|
||||
end
|
||||
|
||||
def post_index
|
||||
posts = Stream::Tag.new(current_user, params.require(:tag)).posts
|
||||
render json: posts.map {|p| PostPresenter.new(p).as_api_response }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -985,6 +985,8 @@ en:
|
|||
users:
|
||||
cant_update: "Failed to update the user settings"
|
||||
not_found: "User not found"
|
||||
search:
|
||||
cant_process: "Search request could not be processed"
|
||||
tags:
|
||||
cant_process: "Failed to process the tag followings request"
|
||||
|
||||
|
|
|
|||
|
|
@ -249,6 +249,8 @@ Rails.application.routes.draw do
|
|||
get :posts
|
||||
end
|
||||
resources :tag_followings, only: %i[index create destroy]
|
||||
get "search/users" => "search#user_index", :as => "user_index"
|
||||
get "search/posts" => "search#post_index", :as => "post_index"
|
||||
get "streams/activity" => "streams#activity", :as => "activity_stream"
|
||||
get "streams/main" => "streams#multi", :as => "stream"
|
||||
get "streams/tags" => "streams#followed_tags", :as => "followed_tags_stream"
|
||||
|
|
|
|||
155
spec/integration/api/search_controller_spec.rb
Normal file
155
spec/integration/api/search_controller_spec.rb
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::V1::SearchController do
|
||||
let(:auth) { FactoryGirl.create(:auth_with_read_and_write) }
|
||||
let!(:access_token) { auth.create_access_token.to_s }
|
||||
let(:auth_read_only) { FactoryGirl.create(:auth_with_read) }
|
||||
let!(:access_token_read_only) { auth_read_only.create_access_token.to_s }
|
||||
|
||||
describe "#user_index" do
|
||||
before do
|
||||
@searchable_user = FactoryGirl.create(
|
||||
:person,
|
||||
diaspora_handle: "findable@example.org",
|
||||
profile: FactoryGirl.build(:profile, first_name: "Terry", last_name: "Smith")
|
||||
)
|
||||
|
||||
@closed_user = FactoryGirl.create(
|
||||
:person,
|
||||
closed_account: true,
|
||||
profile: FactoryGirl.build(:profile, first_name: "Closed", last_name: "Account")
|
||||
)
|
||||
@unsearchable_user = FactoryGirl.create(
|
||||
:person,
|
||||
diaspora_handle: "unsearchable@example.org",
|
||||
profile: FactoryGirl.build(
|
||||
:profile,
|
||||
first_name: "Unsearchable",
|
||||
last_name: "Person",
|
||||
searchable: false
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
it "succeeds by tag" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {tag: "one", access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
users = JSON.parse(response.body)
|
||||
expect(users.length).to eq(14)
|
||||
end
|
||||
|
||||
it "succeeds by name" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {name_or_handle: "Terry", access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
users = JSON.parse(response.body)
|
||||
expect(users.length).to eq(1)
|
||||
end
|
||||
|
||||
it "succeeds by handle" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {name_or_handle: "findable", access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
users = JSON.parse(response.body)
|
||||
expect(users.length).to eq(1)
|
||||
end
|
||||
|
||||
it "doesn't return closed accounts" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {name_or_handle: "Closed", access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
users = JSON.parse(response.body)
|
||||
expect(users.length).to eq(0)
|
||||
end
|
||||
|
||||
it "doesn't return hidden accounts" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {name_or_handle: "unsearchable@example.org", access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
users = JSON.parse(response.body)
|
||||
expect(users.length).to eq(0)
|
||||
end
|
||||
|
||||
it "fails if ask for both" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {tag: "tag1", name_or_handle: "name", access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(422)
|
||||
expect(response.body).to eq(I18n.t("api.endpoint_errors.search.cant_process"))
|
||||
end
|
||||
|
||||
it "fails with no fields" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(422)
|
||||
expect(response.body).to eq(I18n.t("api.endpoint_errors.search.cant_process"))
|
||||
end
|
||||
|
||||
it "fails with bad credentials" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {tag: "tag1", access_token: "999_999_999"}
|
||||
)
|
||||
expect(response.status).to eq(401)
|
||||
end
|
||||
end
|
||||
|
||||
describe "post_index" do
|
||||
before do
|
||||
@user_post = auth.user.post(
|
||||
:status_message,
|
||||
text: "This is a status message #tag1 #tag2",
|
||||
public: true
|
||||
)
|
||||
|
||||
@eve_post = eve.post(
|
||||
:status_message,
|
||||
text: "This is Eve's status message #tag2 #tag3",
|
||||
public: true
|
||||
)
|
||||
end
|
||||
|
||||
it "succeeds by tag" do
|
||||
get(
|
||||
"/api/v1/search/posts",
|
||||
params: {tag: "tag2", access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
posts = JSON.parse(response.body)
|
||||
expect(posts.length).to eq(2)
|
||||
end
|
||||
|
||||
it "fails with missing parameters" do
|
||||
get(
|
||||
"/api/v1/search/posts",
|
||||
params: {access_token: access_token}
|
||||
)
|
||||
expect(response.status).to eq(422)
|
||||
expect(response.body).to eq(I18n.t("api.endpoint_errors.search.cant_process"))
|
||||
end
|
||||
|
||||
it "fails with bad credentials" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
params: {tag: "tag1", access_token: "999_999_999"}
|
||||
)
|
||||
expect(response.status).to eq(401)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue