# encoding: utf-8
require "test_helper"
describe YandexCleanweb do
context "without api key" do
before do
YandexCleanweb.api_key = nil
end
describe "#spam?" do
it "raise an error" do
-> {
YandexCleanweb.spam?("anything")
}.must_raise YandexCleanweb::NoApiKeyException
end
end
describe "#spam!" do
it "raise an error" do
-> {
YandexCleanweb.spam!("anything")
}.must_raise YandexCleanweb::NoApiKeyException
end
end
describe "#get_captcha" do
it "raise an error" do
-> {
YandexCleanweb.get_captcha("anything")
}.must_raise YandexCleanweb::NoApiKeyException
end
end
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
it "raise an error" do
-> {
YandexCleanweb.spam!("anything")
}.must_raise YandexCleanweb::NoApiKeyException
end
end
context "with api key" do
before do
YandexCleanweb.api_key = "cw.1.1.20121227T080449Z.51de1ee126e5ced6.f4f417fb55727520d7e39b00cf5393d4b1ca5e78"
end
describe "#spam?" do
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
result = YandexCleanweb.spam?(body_plain: "my text", ip: "80.80.40.3")
result[:id].wont_be_empty
result[:links].must_be_empty
YandexCleanweb.spam?(body_plain: "my text").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 "#spam!" do
describe "simple check" do
it "works" do
YandexCleanweb.spam!("другая тестовая фраза").must_equal true
YandexCleanweb.spam!("недорого увеличение пениса проститутки без смс и регистрации").must_equal true
end
it "with some html" do
result = YandexCleanweb.spam!(body_html: "some spam spam link")
result.must_equal true
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
describe "raises BadResponseException in case of empty result" do
before do
FakeWeb.clean_registry
end
it do
FakeWeb.register_uri(:post, "http://cleanweb-api.yandex.ru/1.0/check-spam", body: "")
proc { YandexCleanweb.spam?(body_plain: "any text") }.must_raise(YandexCleanweb::BadResponseException)
end
end
end
end