Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Raise an error if api_key is nil or empty
  • Loading branch information
kirs committed Apr 12, 2013
1 parent eb45599 commit 9553120
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 27 deletions.
14 changes: 11 additions & 3 deletions lib/yandex_cleanweb.rb
Expand Up @@ -5,6 +5,8 @@
require "net/http"

module YandexCleanweb
class NoApiKeyException < Exception; end

API_URL = 'http://cleanweb-api.yandex.ru/1.0/'

class << self
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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]
Expand All @@ -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
1 change: 1 addition & 0 deletions test/test_helper.rb
Expand Up @@ -3,5 +3,6 @@
require "minitest/autorun"
require "minitest/spec"
require "minitest/pride"
require "minitest-spec-context"

require "yandex_cleanweb"
92 changes: 68 additions & 24 deletions test/yandex_cleanweb_test.rb
Expand Up @@ -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 <a href='http://spam.com'>spam link</a>")

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 <a href='http://spam.com'>spam link</a>")
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 <a href='http://spam.com'>spam link</a>")

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 <a href='http://spam.com'>spam link</a>")
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
1 change: 1 addition & 0 deletions yandex_cleanweb.gemspec
Expand Up @@ -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

0 comments on commit 9553120

Please sign in to comment.