search: refactor to pass in the current user explicitly.

This commit is contained in:
evazion
2022-09-22 04:16:28 -05:00
parent b56b6c554b
commit 88ac91f5f3
82 changed files with 233 additions and 280 deletions

View File

@@ -64,6 +64,12 @@ class ActiveSupport::TestCase
def as(user, &block)
CurrentUser.scoped(user, &block)
end
def assert_search_equals(expected_results, current_user: CurrentUser.user, **params)
results = subject.class.search(params, current_user)
assert_equal(Array(expected_results).map(&:id), results.ids)
end
end
class ActionDispatch::IntegrationTest

View File

@@ -5,23 +5,6 @@ class ApplicationRecordTest < ActiveSupport::TestCase
@tags = FactoryBot.create_list(:tag, 3, post_count: 1)
end
context "ApplicationRecord#search" do
should "support the id param" do
assert_equal([@tags.first], Tag.search(id: @tags.first.id))
end
should "support ranges in the id param" do
assert_equal(@tags.reverse, Tag.search(id: ">=1"))
assert_equal(@tags.reverse, Tag.search(id: "#{@tags[0].id}..#{@tags[2].id}"))
assert_equal(@tags.reverse, Tag.search(id: @tags.map(&:id).join(",")))
end
should "support the created_at and updated_at params" do
assert_equal(@tags.reverse, Tag.search(created_at: ">=#{@tags.first.created_at}"))
assert_equal(@tags.reverse, Tag.search(updated_at: ">=#{@tags.first.updated_at}"))
end
end
context "ApplicationRecord#parallel_each" do
context "in threaded mode" do
should "set CurrentUser correctly" do

View File

@@ -29,16 +29,16 @@ class ArtistCommentaryTest < ActiveSupport::TestCase
end
should "find the correct match" do
assert_equal([@artcomm1.id], ArtistCommentary.search(post_id: @post1.id.to_s).map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "foo").map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "f*").map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(post_tags_match: "artcomm1").map(&:id))
assert_search_equals(@artcomm1, post_id: @post1.id.to_s)
assert_search_equals(@artcomm1, text_matches: "foo")
assert_search_equals(@artcomm1, text_matches: "f*")
assert_search_equals(@artcomm1, post_tags_match: "artcomm1")
assert_equal([@artcomm1.id], ArtistCommentary.search(original_present: "yes").map(&:id))
assert_equal([@artcomm2.id], ArtistCommentary.search(original_present: "no").map(&:id))
assert_search_equals(@artcomm1, original_present: "yes")
assert_search_equals(@artcomm2, original_present: "no")
assert_equal([@artcomm1.id], ArtistCommentary.search(translated_present: "yes").map(&:id))
assert_equal([@artcomm2.id], ArtistCommentary.search(translated_present: "no").map(&:id))
assert_search_equals(@artcomm1, translated_present: "yes")
assert_search_equals(@artcomm2, translated_present: "no")
end
end

View File

@@ -2,14 +2,14 @@ require 'test_helper'
class ArtistTest < ActiveSupport::TestCase
def assert_artist_found(expected_name, source_url)
artists = Artist.search(url_matches: source_url).to_a
artists = Artist.search({ url_matches: source_url }, current_user: User.anonymous).to_a
assert_equal(1, artists.size)
assert_equal(expected_name, artists.first.name, "Testing URL: #{source_url}")
end
def assert_artist_not_found(source_url)
artists = Artist.search(url_matches: source_url).to_a
artists = Artist.search({ url_matches: source_url }, current_user: User.anonymous).to_a
assert_equal(0, artists.size, "Testing URL: #{source_url}")
end
@@ -455,50 +455,51 @@ class ArtistTest < ActiveSupport::TestCase
end
should "search on its name should return results" do
artist = FactoryBot.create(:artist, :name => "artist")
artist1 = create(:artist, name: "artist")
artist2 = create(:artist, name: "bkub")
assert_not_nil(Artist.search(:name => "artist").first)
assert_not_nil(Artist.search(:name_like => "artist").first)
assert_not_nil(Artist.search(:any_name_matches => "artist").first)
assert_not_nil(Artist.search(:any_name_matches => "/art/").first)
assert_search_equals(artist1, name: "artist")
assert_search_equals(artist1, name_like: "artist")
assert_search_equals(artist1, any_name_matches: "artist")
assert_search_equals(artist1, any_name_matches: "/art/")
end
should "search on other names should return matches" do
artist = FactoryBot.create(:artist, :name => "artist", :other_names_string => "aaa ccc_ddd")
artist = create(:artist, name: "artist", other_names_string: "aaa ccc_ddd")
assert_nil(Artist.search(any_other_name_like: "*artist*").first)
assert_not_nil(Artist.search(any_other_name_like: "*aaa*").first)
assert_not_nil(Artist.search(any_other_name_like: "*ccc_ddd*").first)
assert_not_nil(Artist.search(name: "artist").first)
assert_not_nil(Artist.search(:any_name_matches => "aaa").first)
assert_not_nil(Artist.search(:any_name_matches => "/a/").first)
assert_search_equals([], any_other_name_like: "*artist*")
assert_search_equals(artist, any_other_name_like: "*aaa*")
assert_search_equals(artist, any_other_name_like: "*ccc_ddd*")
assert_search_equals(artist, name: "artist")
assert_search_equals(artist, any_name_matches: "aaa")
assert_search_equals(artist, any_name_matches: "/a/")
end
should "search on group name and return matches" do
cat_or_fish = FactoryBot.create(:artist, :name => "cat_or_fish")
yuu = FactoryBot.create(:artist, :name => "yuu", :group_name => "cat_or_fish")
cat_or_fish = create(:artist, name: "cat_or_fish")
yuu = create(:artist, name: "yuu", group_name: "cat_or_fish")
assert_not_nil(Artist.search(:group_name => "cat_or_fish").first)
assert_not_nil(Artist.search(:any_name_matches => "cat_or_fish").first)
assert_not_nil(Artist.search(:any_name_matches => "/cat/").first)
assert_search_equals(yuu, group_name: "cat_or_fish")
assert_search_equals([yuu, cat_or_fish], any_name_matches: "cat_or_fish")
assert_search_equals([yuu, cat_or_fish], any_name_matches: "/cat/")
end
should "search on url and return matches" do
bkub = FactoryBot.create(:artist, name: "bkub", url_string: "http://bkub.com")
bkub = create(:artist, name: "bkub", url_string: "http://bkub.com")
assert_equal([bkub.id], Artist.search(url_matches: "bkub").map(&:id))
assert_equal([bkub.id], Artist.search(url_matches: "*bkub*").map(&:id))
assert_equal([bkub.id], Artist.search(url_matches: "/rifyu|bkub/").map(&:id))
assert_equal([bkub.id], Artist.search(url_matches: "http://bkub.com/test.jpg").map(&:id))
assert_search_equals(bkub, url_matches: "bkub")
assert_search_equals(bkub, url_matches: "*bkub*")
assert_search_equals(bkub, url_matches: "/rifyu|bkub/")
assert_search_equals(bkub, url_matches: "http://bkub.com/test.jpg")
end
should "search on has_tag and return matches" do
bkub = FactoryBot.create(:artist, name: "bkub")
none = FactoryBot.create(:artist, name: "none")
post = FactoryBot.create(:post, tag_string: "bkub")
bkub = create(:artist, name: "bkub")
none = create(:artist, name: "none")
post = create(:post, tag_string: "bkub")
assert_equal(bkub.id, Artist.search(has_tag: "true").first.id)
assert_equal(none.id, Artist.search(has_tag: "false").first.id)
assert_search_equals(bkub, has_tag: "true")
assert_search_equals(none, has_tag: "false")
end
should "revert to prior versions" do

View File

@@ -1,10 +1,6 @@
require 'test_helper'
class ArtistURLTest < ActiveSupport::TestCase
def assert_search_equals(results, conditions)
assert_equal(results.map(&:id), subject.search(conditions).map(&:id))
end
context "An artist url" do
setup do
CurrentUser.user = FactoryBot.create(:user)
@@ -173,8 +169,6 @@ class ArtistURLTest < ActiveSupport::TestCase
end
context "#search method" do
subject { ArtistURL }
should "work" do
@bkub = create(:artist, name: "bkub", is_deleted: false, url_string: "https://bkub.com")
@masao = create(:artist, name: "masao", is_deleted: true, url_string: "-https://masao.com")

View File

@@ -51,22 +51,9 @@ class BanTest < ActiveSupport::TestCase
context "Searching for a ban" do
should "find a given ban" do
CurrentUser.user = FactoryBot.create(:admin_user)
ban = create(:ban)
user = FactoryBot.create(:user)
ban = FactoryBot.create(:ban, user: user)
params = {
user_name: user.name,
banner_name: ban.banner.name,
reason: ban.reason,
expired: false,
order: :id_desc
}
bans = Ban.search(params)
assert_equal(1, bans.length)
assert_equal(ban.id, bans.first.id)
assert_search_equals(ban, user_name: ban.user.name, banner_name: ban.banner.name, reason: ban.reason, expired: false, order: :id_desc)
end
context "by user id" do

View File

@@ -780,8 +780,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
end
should "work" do
assert_equal([@bur2.id, @bur1.id], BulkUpdateRequest.search.map(&:id))
assert_equal([@bur1.id], BulkUpdateRequest.search(user_name: @admin.name, approver_name: @admin.name, status: "approved").map(&:id))
assert_search_equals(@bur1, user_name: @admin.name, approver_name: @admin.name, status: "approved")
end
end
end

View File

@@ -141,17 +141,13 @@ class CommentTest < ActiveSupport::TestCase
c2 = FactoryBot.create(:comment, :body => "aaa ddd")
c3 = FactoryBot.create(:comment, :body => "eee")
matches = Comment.search(body_matches: "aaa")
assert_equal(2, matches.count)
assert_equal(c2.id, matches.all[0].id)
assert_equal(c1.id, matches.all[1].id)
assert_search_equals([c2, c1], body_matches: "aaa")
end
should "default to id_desc order when searched with no options specified" do
comms = FactoryBot.create_list(:comment, 3)
matches = Comment.search({})
assert_equal([comms[2].id, comms[1].id, comms[0].id], matches.map(&:id))
assert_search_equals([comms[2], comms[1], comms[0]])
end
context "that is edited by a moderator" do

View File

@@ -1,12 +1,6 @@
require 'test_helper'
class SearchableTest < ActiveSupport::TestCase
def assert_search_equals(results, current_user: User.anonymous, **params)
as(current_user) do
assert_equal(Array(results).map(&:id), subject.search(**params).ids)
end
end
context "#search method" do
subject { Post }
@@ -19,7 +13,7 @@ class SearchableTest < ActiveSupport::TestCase
context "for a nonexistent attribute" do
should "raise an error" do
assert_raises(ArgumentError) do
Post.search_attributes({ answer: 42 }, :answer)
Post.search_attributes({ answer: 42 }, [:answer], current_user: User.anonymous)
end
end
end

View File

@@ -37,27 +37,19 @@ class DmailTest < ActiveSupport::TestCase
context "search" do
should "return results based on title contents" do
dmail = FactoryBot.create(:dmail, :title => "xxx", :owner => @user)
dmail = create(:dmail, title: "xxx", owner: @user)
matches = Dmail.search(title_matches: "x*")
assert_equal([dmail.id], matches.map(&:id))
matches = Dmail.search(title_matches: "X*")
assert_equal([dmail.id], matches.map(&:id))
matches = Dmail.search(message_matches: "xxx")
assert_equal([dmail.id], matches.map(&:id))
matches = Dmail.search(message_matches: "aaa")
assert(matches.empty?)
assert_search_equals(dmail, title_matches: "x*")
assert_search_equals(dmail, title_matches: "X*")
assert_search_equals(dmail, message_matches: "xxx")
assert_search_equals([], message_matches: "aaa")
end
should "return results based on body contents" do
dmail = FactoryBot.create(:dmail, :body => "xxx", :owner => @user)
matches = Dmail.search(message_matches: "xxx")
assert(matches.any?)
matches = Dmail.search(message_matches: "aaa")
assert(matches.empty?)
dmail = create(:dmail, body: "xxx", owner: @user)
assert_search_equals(dmail, message_matches: "xxx")
assert_search_equals([], message_matches: "aaa")
end
end

View File

@@ -142,9 +142,10 @@ class ForumPostTest < ActiveSupport::TestCase
end
should "be searchable by body content" do
post = FactoryBot.create(:forum_post, :topic_id => @topic.id, :body => "xxx")
assert_equal(1, ForumPost.search(body_matches: "xxx").count)
assert_equal(0, ForumPost.search(body_matches: "aaa").count)
post = create(:forum_post, topic: @topic, body: "xxx")
assert_search_equals(post, body_matches: "xxx")
assert_search_equals([], body_matches: "aaa")
end
should "initialize its creator" do

View File

@@ -44,13 +44,13 @@ class ForumTopicTest < ActiveSupport::TestCase
end
should "be searchable by title" do
assert_equal(1, ForumTopic.search(title: "xxx").count)
assert_equal(0, ForumTopic.search(title: "aaa").count)
assert_search_equals(@topic, title: "xxx")
assert_search_equals([], title: "aaa")
end
should "be searchable by category id" do
assert_equal(1, ForumTopic.search(:category_id => 0).count)
assert_equal(0, ForumTopic.search(:category_id => 1).count)
assert_search_equals(@topic, category_id: 0)
assert_search_equals([], category_id: 1)
end
should "initialize its creator" do

View File

@@ -135,13 +135,13 @@ class NoteTest < ActiveSupport::TestCase
context "where the body contains the string 'aaa'" do
should "return a hit" do
assert_equal(1, Note.search(body_matches: "aaa").count)
assert_search_equals(@note, body_matches: "aaa")
end
end
context "where the body contains the string 'bbb'" do
should "return no hits" do
assert_equal(0, Note.search(body_matches: "bbb").count)
assert_search_equals([], body_matches: "bbb")
end
end
end

View File

@@ -18,11 +18,11 @@ class PoolTest < ActiveSupport::TestCase
assert_equal(@pool.id, Pool.find_by_name("test pool").id)
assert_equal([@pool.id], Pool.search(name_contains: "test pool").map(&:id))
assert_equal([@pool.id], Pool.search(name_contains: "tes").map(&:id))
assert_equal([@pool.id], Pool.search(name_matches: "test pool").map(&:id))
assert_equal([@pool.id], Pool.search(name_matches: "testing pool").map(&:id))
assert_equal([], Pool.search(name_matches: "tes").map(&:id))
assert_search_equals(@pool, name_contains: "test pool")
assert_search_equals(@pool, name_contains: "tes")
assert_search_equals(@pool, name_matches: "test pool")
assert_search_equals(@pool, name_matches: "testing pool")
assert_search_equals([], name_matches: "tes")
end
should "find pools by post id" do
@@ -31,8 +31,8 @@ class PoolTest < ActiveSupport::TestCase
@post1 = create(:post, tag_string: "pool:pool1")
@post2 = create(:post, tag_string: "pool:pool2")
assert_equal([@pool1.id], Pool.search(post_ids_include_any: @post1.id).pluck(:id))
assert_equal([@pool2.id, @pool1.id], Pool.search(post_ids_include_any: "#{@post1.id} #{@post2.id}").pluck(:id))
assert_search_equals(@pool1, post_ids_include_any: @post1.id)
assert_search_equals([@pool2, @pool1], post_ids_include_any: "#{@post1.id} #{@post2.id}")
end
should "find pools by post id count" do
@@ -41,7 +41,7 @@ class PoolTest < ActiveSupport::TestCase
@post1 = create(:post, tag_string: "pool:pool1")
@post2 = create(:post, tag_string: "pool:pool1")
assert_equal([@pool1.id], Pool.search(post_id_count: 2).pluck(:id))
assert_search_equals(@pool1, post_id_count: 2)
end
should "find pools by post tags" do
@@ -51,13 +51,13 @@ class PoolTest < ActiveSupport::TestCase
@post2 = create(:post, tag_string: "pool:pool1 fumimi")
@post3 = create(:post, tag_string: "pool:pool2 bkub fumimi")
assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "bkub").pluck(:id))
assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "fumimi").pluck(:id))
assert_equal([@pool2.id], Pool.search(post_tags_match: "bkub fumimi").pluck(:id))
assert_search_equals([@pool2, @pool1], post_tags_match: "bkub")
assert_search_equals([@pool2, @pool1], post_tags_match: "fumimi")
assert_search_equals(@pool2, post_tags_match: "bkub fumimi")
assert_equal(2, Pool.search(post_tags_match: "bkub").count)
assert_equal(2, Pool.search(post_tags_match: "fumimi").count)
assert_equal(1, Pool.search(post_tags_match: "bkub fumimi").count)
assert_equal(2, Pool.search({ post_tags_match: "bkub" }, current_user: User.anonymous).count)
assert_equal(2, Pool.search({ post_tags_match: "fumimi" }, current_user: User.anonymous).count)
assert_equal(1, Pool.search({ post_tags_match: "bkub fumimi" }, current_user: User.anonymous).count)
end
end

View File

@@ -95,9 +95,8 @@ class PostApprovalTest < ActiveSupport::TestCase
CurrentUser.scoped(@approver) do
@post.update!(tag_string: "touhou")
@approval = create(:post_approval, post: @post, user: @approver)
@approvals = PostApproval.search(user_name: @approver.name, post_tags_match: "touhou", post_id: @post.id)
assert_equal([@approval.id], @approvals.map(&:id))
assert_search_equals(@approval, user_name: @approver.name, post_tags_match: "touhou", post_id: @post.id)
end
end
end

View File

@@ -79,9 +79,8 @@ class PostDisapprovalTest < ActiveSupport::TestCase
disapproval1 = FactoryBot.create(:post_disapproval, user: @approver, post: @post1, reason: "breaks_rules")
disapproval2 = FactoryBot.create(:post_disapproval, user: @approver, post: @post2, reason: "poor_quality", message: "bad anatomy")
assert_equal([disapproval1.id], PostDisapproval.search(reason: "breaks_rules").pluck(:id))
assert_equal([disapproval2.id], PostDisapproval.search(message: "bad anatomy").pluck(:id))
assert_equal([disapproval1.id, disapproval2.id], PostDisapproval.where(user: @approver).pluck(:id))
assert_search_equals([disapproval1], reason: "breaks_rules")
assert_search_equals([disapproval2], message: "bad anatomy")
end
end
end

View File

@@ -294,9 +294,9 @@ class UserTest < ActiveSupport::TestCase
user2.save(validate: false)
user3.save(validate: false)
assert_equal([user2.id, user1.id], User.search(name: "foo*").map(&:id))
assert_equal([user2.id], User.search(name: "foo\*bar").map(&:id))
assert_equal([user3.id], User.search(name: "bar\\\*baz").map(&:id))
assert_search_equals([user2, user1], name: "foo*")
assert_search_equals(user2, name: "foo\*bar")
assert_search_equals(user3, name: "bar\\\*baz")
end
end

View File

@@ -20,8 +20,7 @@ class WikiPageTest < ActiveSupport::TestCase
end
should "search other names with wildcards" do
matches = WikiPage.search(other_names_match: "fo*")
assert_equal([@wiki_page.id], matches.map(&:id))
assert_search_equals(@wiki_page, other_names_match: "fo*")
end
should "create versions" do