From 00df0b7bdae07c3e761ff3493aaa461c8c2e71dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Tue, 11 Feb 2020 10:27:46 +0100 Subject: [PATCH] API: add new route to search for tags --- app/controllers/api/v1/search_controller.rb | 10 ++++ config/routes.rb | 1 + .../integration/api/search_controller_spec.rb | 50 ++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index c3464a194..d30fcf185 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -23,6 +23,12 @@ module Api render_paged_api_response posts_page end + def tag_index + tags_page = index_pager(tags_query).response + tags_page[:data] = tags_page[:data].pluck(:name) + render_paged_api_response tags_page + end + private def time_pager(query, query_time_field, data_time_field) @@ -53,6 +59,10 @@ module Api Stream::Tag.new(nil, params.require(:tag)).posts end end + + def tags_query + ActsAsTaggableOn::Tag.autocomplete(params.require(:query)) + end end end end diff --git a/config/routes.rb b/config/routes.rb index 502edc0c5..4651d6db6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -259,6 +259,7 @@ Rails.application.routes.draw do 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 "search/tags" => "search#tag_index", :as => "tag_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" diff --git a/spec/integration/api/search_controller_spec.rb b/spec/integration/api/search_controller_spec.rb index 67e11ad8d..46524a32e 100644 --- a/spec/integration/api/search_controller_spec.rb +++ b/spec/integration/api/search_controller_spec.rb @@ -213,13 +213,61 @@ describe Api::V1::SearchController do it "fails with bad credentials" do get( - "/api/v1/search/users", + "/api/v1/search/posts", params: {tag: "tag1", access_token: invalid_token} ) expect(response.status).to eq(401) end end + describe "tag_index" do + before do + FactoryGirl.create(:tag, name: "apipartyone") + FactoryGirl.create(:tag, name: "apipartytwo") + FactoryGirl.create(:tag, name: "apipartythree") + end + + it "succeeds" do + get( + "/api/v1/search/tags", + params: {query: "apiparty", access_token: access_token_public_only_read_only} + ) + expect(response.status).to eq(200) + tags = response_body_data(response) + expect(tags.size).to eq(3) + + expect(tags.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/tags") + end + + it "does a prefix search" do + get( + "/api/v1/search/tags", + params: {query: "apipartyt", access_token: access_token_public_only_read_only} + ) + expect(response.status).to eq(200) + tags = response_body_data(response) + expect(tags.size).to eq(2) + + expect(tags.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/tags") + end + + it "fails with missing parameters" do + get( + "/api/v1/search/tags", + params: {access_token: access_token} + ) + confirm_api_error(response, 422, "Search request could not be processed") + end + + it "fails with bad credentials" do + get( + "/api/v1/search/tags", + params: {query: "apiparty", access_token: invalid_token} + ) + expect(response.status).to eq(401) + end + end + def response_body_data(response) JSON.parse(response.body) end