From 9553120a0270f1df708e31f5aefdec98cd06fd11 Mon Sep 17 00:00:00 2001 From: Kir Shatrov Date: Fri, 12 Apr 2013 23:55:09 +0300 Subject: [PATCH] Raise an error if api_key is nil or empty --- lib/yandex_cleanweb.rb | 14 ++++-- test/test_helper.rb | 1 + test/yandex_cleanweb_test.rb | 92 ++++++++++++++++++++++++++---------- yandex_cleanweb.gemspec | 1 + 4 files changed, 81 insertions(+), 27 deletions(-) diff --git a/lib/yandex_cleanweb.rb b/lib/yandex_cleanweb.rb index 07bc0e4..8860428 100644 --- a/lib/yandex_cleanweb.rb +++ b/lib/yandex_cleanweb.rb @@ -5,6 +5,8 @@ require "net/http" module YandexCleanweb + class NoApiKeyException < Exception; end + API_URL = 'http://cleanweb-api.yandex.ru/1.0/' class << self @@ -54,7 +56,7 @@ def valid_captcha?(request_id, captcha_id, value) def api_check_captcha(request_id, captcha_id, value) check_captcha_url = "#{API_URL}/check-captcha" params = { - :key => api_key, + :key => prepare_api_key, :id => request_id, :captcha => captcha_id, :value => value @@ -68,7 +70,7 @@ def api_check_captcha(request_id, captcha_id, value) def api_get_captcha(request_id) get_captcha_url = "#{API_URL}/get-captcha" - params = { :key => api_key, :id => request_id } + params = { :key => prepare_api_key, :id => request_id } uri = URI.parse(get_captcha_url) uri.query = URI.encode_www_form(params) @@ -77,7 +79,7 @@ def api_get_captcha(request_id) end def api_check_spam(options) - cleanweb_options = { :key => api_key } + cleanweb_options = { :key => prepare_api_key } if options[0].is_a?(String) # quick check cleanweb_options[:body_plain] = options[0] @@ -91,5 +93,11 @@ def api_check_spam(options) response = Net::HTTP.post_form(uri, cleanweb_options) response.body end + + def prepare_api_key + raise NoApiKeyException if api_key.nil? || api_key.empty? + + api_key + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 40a022e..a3b2ad5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,5 +3,6 @@ require "minitest/autorun" require "minitest/spec" require "minitest/pride" +require "minitest-spec-context" require "yandex_cleanweb" \ No newline at end of file diff --git a/test/yandex_cleanweb_test.rb b/test/yandex_cleanweb_test.rb index 2067ed4..5f8c156 100644 --- a/test/yandex_cleanweb_test.rb +++ b/test/yandex_cleanweb_test.rb @@ -2,47 +2,91 @@ require "test_helper" describe YandexCleanweb do - before do - YandexCleanweb.api_key = "cw.1.1.20121227T080449Z.51de1ee126e5ced6.f4f417fb55727520d7e39b00cf5393d4b1ca5e78" - end - describe "#spam?" do + context "without api key" do + before do + YandexCleanweb.api_key = nil + end - describe "simple check" do - it "works" do - YandexCleanweb.spam?("фраза").must_equal false - YandexCleanweb.spam?("недорого увеличение пениса проститутки").must_equal false + describe "#spam?" do + it "raise an error" do + -> { + YandexCleanweb.spam?("anything") + }.must_raise YandexCleanweb::NoApiKeyException end end - describe "advanced mode" do - it "workd" do - YandexCleanweb.spam?(:body_plain => "my text", :ip => "80.80.40.3").must_equal false + describe "#get_captcha" do + it "raise an error" do + -> { + YandexCleanweb.get_captcha("anything") + }.must_raise YandexCleanweb::NoApiKeyException end + end - it "for some html" do - result = YandexCleanweb.spam?(:body_html => "some spam spam link") - - result[:id].wont_be_empty - result[:links].must_be_empty + describe "#valid_captcha?" do + it "raise an error" do + -> { + YandexCleanweb.valid_captcha?("anything", "anything", 123) + }.must_raise YandexCleanweb::NoApiKeyException end + end + end + + context "with empty api key" do + before do + YandexCleanweb.api_key = "" end + it "raise an error" do + -> { + YandexCleanweb.spam?("anything") + }.must_raise YandexCleanweb::NoApiKeyException + end end - describe "#get_captcha + #valid_captcha?" do + context "with api key" do - it "works for not valid captchas" do - result = YandexCleanweb.spam?(:body_html => "some spam spam link") - captcha = YandexCleanweb.get_captcha(result[:id]) + before do + YandexCleanweb.api_key = "cw.1.1.20121227T080449Z.51de1ee126e5ced6.f4f417fb55727520d7e39b00cf5393d4b1ca5e78" + end - captcha[:url].wont_be_empty - captcha[:captcha].wont_be_empty + describe "#spam?" do - valid = YandexCleanweb.valid_captcha?(result[:id], captcha[:captcha], "1234") - valid.must_equal false + describe "simple check" do + it "works" do + YandexCleanweb.spam?("фраза").must_equal false + YandexCleanweb.spam?("недорого увеличение пениса проститутки").must_equal false + end + end + + describe "advanced mode" do + it "works" do + YandexCleanweb.spam?(:body_plain => "my text", :ip => "80.80.40.3").must_equal false + end + + it "with some html" do + result = YandexCleanweb.spam?(:body_html => "some spam spam link") + + result[:id].wont_be_empty + result[:links].must_be_empty + end + end end + describe "#get_captcha + #valid_captcha?" do + + it "works for not valid captchas" do + result = YandexCleanweb.spam?(:body_html => "some spam spam link") + captcha = YandexCleanweb.get_captcha(result[:id]) + + captcha[:url].wont_be_empty + captcha[:captcha].wont_be_empty + + valid = YandexCleanweb.valid_captcha?(result[:id], captcha[:captcha], "1234") + valid.must_equal false + end + end end end diff --git a/yandex_cleanweb.gemspec b/yandex_cleanweb.gemspec index d0ca6c2..af139b2 100644 --- a/yandex_cleanweb.gemspec +++ b/yandex_cleanweb.gemspec @@ -21,4 +21,5 @@ Gem::Specification.new do |gem| gem.add_development_dependency "rake" gem.add_development_dependency "minitest", "~> 3.0" + gem.add_development_dependency "minitest-spec-context" end