[Tests] Improve/Add tests around network requests

This commit is contained in:
Earlopain 2024-04-27 13:57:51 +02:00
parent 75ea36b700
commit 58f9a18915
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
4 changed files with 65 additions and 41 deletions

View File

@ -6,59 +6,35 @@ class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest
context "The iqdb controller" do context "The iqdb controller" do
setup do setup do
IqdbProxy.stubs(:endpoint).returns("http://iqdb:5588") IqdbProxy.stubs(:endpoint).returns("http://iqdb:5588")
@user = create(:user) CloudflareService.stubs(:ips).returns([])
as(@user) do
@posts = create_list(:post, 2)
end
end end
context "show action" do context "show action" do
context "with a url parameter" do context "with a url parameter" do
setup do
create(:upload_whitelist, pattern: "*google.com")
@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 should "render a response" do
IqdbProxy.expects(:query_url).with(@url, nil).returns(@mocked_response) post = create(:post)
get_auth iqdb_queries_path, @user, params: @params create(:upload_whitelist, pattern: "https://google.com/*")
assert_select("#post_#{@posts[0].id}") stub_request(:get, "https://google.com/foo.jpg")
.to_return(body: file_fixture("test.jpg").read)
response = [{ "post_id" => post.id, "score" => 80 }]
stub_request(:post, "#{IqdbProxy.endpoint}/query").to_return_json(body: response)
get iqdb_queries_path, params: { url: "https://google.com/foo.jpg" }
assert_response :success
assert_select("#post_#{post.id}")
end end
end end
context "with a post_id parameter" do 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 iqdb" do should "redirect to iqdb" do
IqdbProxy.expects(:query_post).with(@posts[0], nil).returns(@mocked_response) post = create(:post)
get_auth iqdb_queries_path, @user, params: @params response = [{ "post_id" => post.id, "score" => 80 }]
assert_select("#post_#{@posts[0].id}") stub_request(:post, "#{IqdbProxy.endpoint}/query").to_return_json(body: response)
end Post.any_instance.stubs(:preview_file_path).returns(file_fixture("test.jpg"))
end
context "with matches" do get iqdb_queries_path, params: { post_id: post.id }
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_response :success
assert_select("#post_#{post.id}")
end end
end end
end end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
require "test_helper"
module DiscordReport
class AiburStatsTest < ActiveSupport::TestCase
test "it works" do
AiburStats.new.report # Prime cache
stats = AiburStats.new
stats.stubs(:webhook_url).returns("https://example.com")
stub_request(:post, "https://example.com/")
.with(body: /AIBUR report for/, headers: { "Content-Type" => "application/json" })
stats.post_webhook
end
end
end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
require "test_helper"
module DiscordReport
class JanitorStatsTest < ActiveSupport::TestCase
test "it works" do
JanitorStats.new.report # Prime cache
stats = JanitorStats.new
stats.stubs(:webhook_url).returns("https://example.com")
stub_request(:post, "https://example.com/")
.with(body: /Janitor report for/, headers: { "Content-Type" => "application/json" })
stats.post_webhook
end
end
end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
require "test_helper"
module DiscordReport
class ModeratorStatsTest < ActiveSupport::TestCase
test "it works" do
ModeratorStats.new.report # Prime cache
stats = ModeratorStats.new
stats.stubs(:webhook_url).returns("https://example.com")
stub_request(:post, "https://example.com/")
.with(body: /Moderator report for/, headers: { "Content-Type" => "application/json" })
stats.post_webhook
end
end
end