diff --git a/backend/app/controllers/posts_controller.rb b/backend/app/controllers/posts_controller.rb index 74d720c..5546f3c 100644 --- a/backend/app/controllers/posts_controller.rb +++ b/backend/app/controllers/posts_controller.rb @@ -47,8 +47,8 @@ class PostsController < ApplicationController .preload(tags: { tag_name: :wiki_page }) .with_attached_thumbnail - q = q.where('posts.url LIKE ?', "%#{ url }%") if url - q = q.where('posts.title LIKE ?', "%#{ title }%") if title + q = q.where('posts.url LIKE ?', "%#{ Post.sanitize_sql_like(url).tr('*?', '%_') }%") if url + q = q.where('posts.title LIKE ?', "%#{ Post.sanitize_sql_like(title).tr('*?', '%_') }%") if title if original_created_from q = q.where('posts.original_created_before > ?', original_created_from) end diff --git a/backend/spec/requests/posts_spec.rb b/backend/spec/requests/posts_spec.rb index 120a221..c5c0f04 100644 --- a/backend/spec/requests/posts_spec.rb +++ b/backend/spec/requests/posts_spec.rb @@ -287,6 +287,35 @@ RSpec.describe 'Posts API', type: :request do end end + [:title, :url].each do |field| + context "when #{field} contains wildcards" do + let!(:search_posts) do + ['needle%25end', 'needleX25end', 'needle_end', 'needleXend', 'needleABend'].map do |value| + Post.create!(uploaded_user: user, title: "prefix #{value} suffix", + url: "https://example.com/prefix-#{value}-suffix") + end + end + + { + 'needle%25end' => [0], + 'needle_end' => [2], + 'needle*end' => [0, 1, 2, 3, 4], + 'needle?end' => [2, 3], + 'needle%*end' => [0], + 'needle_?nd' => [2] + }.each do |query, expected_indices| + it "matches #{query.inspect} using only * and ? as wildcards" do + get '/posts', params: { field => query } + + expect(response).to have_http_status(:ok) + expected_ids = expected_indices.map { |i| search_posts[i].id } + expect(json.fetch('posts').map { |p| p['id'] }).to match_array(expected_ids) + expect(json.fetch('count')).to eq(expected_ids.size) + end + end + end + end + context 'when created_from/created_to are provided' do let(:t_created_hit) { Time.zone.local(2010, 1, 5, 12, 0, 0) } let(:t_created_miss) { Time.zone.local(2012, 1, 5, 12, 0, 0) }