From 3edd814be564d287cd5f26becc593448d2985cfb Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 6 Feb 2017 16:06:23 -0600 Subject: [PATCH] tests: add iqdb queries controller tests. --- test/factories/post.rb | 1 + .../iqdb_queries_controller_test.rb | 33 +++++++++++++++++++ test/helpers/iqdb_test_helper.rb | 11 +++++++ test/unit/post_test.rb | 2 +- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/functional/iqdb_queries_controller_test.rb diff --git a/test/factories/post.rb b/test/factories/post.rb index ec0e8f4a4..2e13bb190 100644 --- a/test/factories/post.rb +++ b/test/factories/post.rb @@ -13,5 +13,6 @@ FactoryGirl.define do image_height 1000 file_size 2000 rating "q" + source { FFaker::Internet.http_url } end end diff --git a/test/functional/iqdb_queries_controller_test.rb b/test/functional/iqdb_queries_controller_test.rb new file mode 100644 index 000000000..a9dd869f1 --- /dev/null +++ b/test/functional/iqdb_queries_controller_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' +require 'helpers/iqdb_test_helper' + +class IqdbQueriesControllerTest < ActionController::TestCase + include IqdbTestHelper + + context "The iqdb controller" do + setup do + @user = FactoryGirl.create(:user) + CurrentUser.user = @user + CurrentUser.ip_addr = "127.0.0.1" + + @posts = FactoryGirl.create_list(:post, 2) + mock_iqdb_service! + end + + context "create action" do + should "render with a post_id" do + mock_iqdb_matches!(@posts[0], @posts) + post :create, { post_id: @posts[0].id, format: "js" }, { user_id: @user.id } + + assert_response :success + end + + should "render with an url" do + mock_iqdb_matches!(@posts[0].source, @posts) + post :create, { url: @posts[0].source }, { user_id: @user.id } + + assert_response :success + end + end + end +end diff --git a/test/helpers/iqdb_test_helper.rb b/test/helpers/iqdb_test_helper.rb index a9fed0cc5..f34a3cdb8 100644 --- a/test/helpers/iqdb_test_helper.rb +++ b/test/helpers/iqdb_test_helper.rb @@ -16,5 +16,16 @@ module IqdbTestHelper service = mock_sqs_service.new Post.stubs(:iqdb_sqs_service).returns(service) + + Danbooru.config.stubs(:iqdbs_auth_key).returns("hunter2") + 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.complete_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 + + FakeWeb.register_uri(:get, url, body: body) end end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 9104eb3ab..4fb307df4 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -2039,7 +2039,7 @@ class PostTest < ActiveSupport::TestCase context "a post that has been updated" do setup do - @post = FactoryGirl.create(:post, :rating => "q", :tag_string => "aaa") + @post = FactoryGirl.create(:post, :rating => "q", :tag_string => "aaa", :source => nil) @post.stubs(:merge_version?).returns(false) @post.update_attributes(:tag_string => "aaa bbb ccc ddd") @post.update_attributes(:tag_string => "bbb xxx yyy", :source => "xyz")