forked from mirrors/catstodon
Add coverage for api/v1/peers/search
endpoint and extract controller query to Instance scope (#28796)
This commit is contained in:
parent
2115bc52e4
commit
0b853678a4
3 changed files with 71 additions and 4 deletions
|
@ -27,7 +27,7 @@ class Api::V1::Peers::SearchController < Api::BaseController
|
||||||
@domains = InstancesIndex.query(function_score: {
|
@domains = InstancesIndex.query(function_score: {
|
||||||
query: {
|
query: {
|
||||||
prefix: {
|
prefix: {
|
||||||
domain: TagManager.instance.normalize_domain(params[:q].strip),
|
domain: normalized_domain,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -37,11 +37,18 @@ class Api::V1::Peers::SearchController < Api::BaseController
|
||||||
},
|
},
|
||||||
}).limit(10).pluck(:domain)
|
}).limit(10).pluck(:domain)
|
||||||
else
|
else
|
||||||
domain = params[:q].strip
|
domain = normalized_domain
|
||||||
domain = TagManager.instance.normalize_domain(domain)
|
@domains = Instance.searchable.domain_starts_with(domain).limit(10).pluck(:domain)
|
||||||
@domains = Instance.searchable.where(Instance.arel_table[:domain].matches("#{Instance.sanitize_sql_like(domain)}%", false, true)).limit(10).pluck(:domain)
|
|
||||||
end
|
end
|
||||||
rescue Addressable::URI::InvalidURIError
|
rescue Addressable::URI::InvalidURIError
|
||||||
@domains = []
|
@domains = []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def normalized_domain
|
||||||
|
TagManager.instance.normalize_domain(query_value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def query_value
|
||||||
|
params[:q].strip
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,6 +23,7 @@ class Instance < ApplicationRecord
|
||||||
|
|
||||||
scope :searchable, -> { where.not(domain: DomainBlock.select(:domain)) }
|
scope :searchable, -> { where.not(domain: DomainBlock.select(:domain)) }
|
||||||
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
|
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
|
||||||
|
scope :domain_starts_with, ->(value) { where(arel_table[:domain].matches("#{sanitize_sql_like(value)}%", false, true)) }
|
||||||
scope :by_domain_and_subdomains, ->(domain) { where("reverse('.' || domain) LIKE reverse(?)", "%.#{domain}") }
|
scope :by_domain_and_subdomains, ->(domain) { where("reverse('.' || domain) LIKE reverse(?)", "%.#{domain}") }
|
||||||
|
|
||||||
def self.refresh
|
def self.refresh
|
||||||
|
|
59
spec/requests/api/v1/peers/search_spec.rb
Normal file
59
spec/requests/api/v1/peers/search_spec.rb
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe 'API Peers Search' do
|
||||||
|
describe 'GET /api/v1/peers/search' do
|
||||||
|
context 'when peers api is disabled' do
|
||||||
|
before do
|
||||||
|
Setting.peers_api_enabled = false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns http not found response' do
|
||||||
|
get '/api/v1/peers/search'
|
||||||
|
|
||||||
|
expect(response)
|
||||||
|
.to have_http_status(404)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with no search param' do
|
||||||
|
it 'returns http success and empty response' do
|
||||||
|
get '/api/v1/peers/search'
|
||||||
|
|
||||||
|
expect(response)
|
||||||
|
.to have_http_status(200)
|
||||||
|
expect(body_as_json)
|
||||||
|
.to be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with invalid search param' do
|
||||||
|
it 'returns http success and empty response' do
|
||||||
|
get '/api/v1/peers/search', params: { q: 'ftp://Invalid-Host!!.valüe' }
|
||||||
|
|
||||||
|
expect(response)
|
||||||
|
.to have_http_status(200)
|
||||||
|
expect(body_as_json)
|
||||||
|
.to be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with search param' do
|
||||||
|
let!(:account) { Fabricate(:account, domain: 'host.example') }
|
||||||
|
|
||||||
|
before { Instance.refresh }
|
||||||
|
|
||||||
|
it 'returns http success and json with known domains' do
|
||||||
|
get '/api/v1/peers/search', params: { q: 'host.example' }
|
||||||
|
|
||||||
|
expect(response)
|
||||||
|
.to have_http_status(200)
|
||||||
|
expect(body_as_json.size)
|
||||||
|
.to eq(1)
|
||||||
|
expect(body_as_json.first)
|
||||||
|
.to eq(account.domain)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue