iqdb: switch to Danbooru::Http.
This commit is contained in:
@@ -3,61 +3,33 @@ require 'test_helper'
|
||||
class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest
|
||||
context "The iqdb controller" do
|
||||
setup do
|
||||
Danbooru.config.stubs(:iqdbs_server).returns("https://karasuma.donmai.us")
|
||||
@user = create(:user)
|
||||
@posts = as(@user) { create_list(:post, 2) }
|
||||
@post = as(@user) { create(:post) }
|
||||
end
|
||||
|
||||
context "show action" do
|
||||
context "with a url parameter" do
|
||||
setup do
|
||||
@url = "https://google.com"
|
||||
@params = { url: @url }
|
||||
@mocked_response = [{
|
||||
"post" => @posts[0],
|
||||
"post_id" => @posts[0].id,
|
||||
"score" => 1
|
||||
}]
|
||||
end
|
||||
|
||||
should "render a response" do
|
||||
IqdbProxy.expects(:query).returns(@mocked_response)
|
||||
get_auth iqdb_queries_path, @user, as: :javascript, params: @params
|
||||
@url = "https://google.com"
|
||||
@matches = [{ "post_id" => @post.id, "width" => 128, "height" => 128, "score" => 95.0 }]
|
||||
mock_iqdb_matches(@matches)
|
||||
|
||||
get_auth iqdb_queries_path, @user, as: :javascript, params: { url: @url }
|
||||
|
||||
assert_response :success
|
||||
assert_select("#post_#{@posts[0].id}")
|
||||
assert_select("#post_#{@post.id}")
|
||||
end
|
||||
end
|
||||
|
||||
context "with a post_id parameter" do
|
||||
setup do
|
||||
@params = { post_id: @posts[0].id }
|
||||
@url = @posts[0].preview_file_url
|
||||
@mocked_response = [{
|
||||
"post" => @posts[0],
|
||||
"post_id" => @posts[0].id,
|
||||
"score" => 1
|
||||
}]
|
||||
end
|
||||
|
||||
should "redirect to iqdbs" do
|
||||
IqdbProxy.expects(:query).returns(@mocked_response)
|
||||
get_auth iqdb_queries_path, @user, params: @params
|
||||
@matches = [{ "post_id" => @post.id, "width" => 128, "height" => 128, "score" => 95.0 }]
|
||||
mock_iqdb_matches(@matches)
|
||||
|
||||
get_auth iqdb_queries_path, @user, params: { post_id: @post.id }
|
||||
|
||||
assert_response :success
|
||||
assert_select("#post_#{@posts[0].id}")
|
||||
end
|
||||
end
|
||||
|
||||
context "with matches" do
|
||||
setup do
|
||||
json = @posts.map {|x| {"post_id" => x.id, "score" => 1}}.to_json
|
||||
@params = { matches: json }
|
||||
end
|
||||
|
||||
should "render with matches" do
|
||||
get_auth iqdb_queries_path, @user, params: @params
|
||||
assert_response :success
|
||||
assert_select("#post_#{@post.id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,11 +21,9 @@ module IqdbTestHelper
|
||||
Danbooru.config.stubs(:iqdbs_server).returns("http://localhost:3004")
|
||||
end
|
||||
|
||||
def mock_iqdb_matches!(post_or_source, matches)
|
||||
source = post_or_source.is_a?(Post) ? post_or_source.preview_file_url : post_or_source
|
||||
url = "http://localhost:3004/similar?key=hunter2&url=#{CGI.escape source}&ref"
|
||||
body = matches.map { |post| { post_id: post.id } }.to_json
|
||||
|
||||
stub_request(:get, url).to_return(body: body)
|
||||
def mock_iqdb_matches(matches)
|
||||
Danbooru.config.stubs(:iqdbs_server).returns("http://localhost:3004")
|
||||
response = HTTP::Response.new(status: 200, body: matches.to_json, headers: { "Content-Type": "application/json" }, version: "1.1")
|
||||
HTTP::Client.any_instance.stubs(:post).returns(response)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user