0
+require 'active_support'
0
+$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
0
+unless Hash.public_instance_methods.any? { |m| m =~ /to_query/ }
0
+ def to_query(key) #:nodoc:
0
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
0
+ def to_query(key) #:nodoc:
0
+ collect { |value| value.to_query("#{key}[]") } * '&'
0
+ def to_query(namespace = nil)
0
+ collect do |key, value|
0
+context "Viking Connection" do
0
+ specify "should load akismet engine" do
0
+ Viking.connect(:akismet, :api_key => 'foo', :blog => 'bar').class.should == Viking::Akismet
0
+ specify "should set default engine" do
0
+ Viking.default_engine = :akismet
0
+ Viking.connect_options = { :api_key => 'foo', :blog => 'bar' }
0
+ Viking.default_instance.class.should == Viking::Akismet
0
+ Viking.default_instance.options.should == Viking.connect_options
0
+ Viking.default_engine = nil
0
+ Viking.connect_options = nil
0
+ Viking.default_instance = nil
0
+ @viking = Viking.connect :akismet, :api_key => 'foo', :blog => 'bar'
0
+ specify "should verify api key" do
0
+ mock_akismet("/1.1/verify-key", {:key => :foo, :blog => :bar}).returns(stub(:body => 'valid'))
0
+ @viking.verified?.should == true # #verified? is called twice to make sure #verify_api_key is not called twice
0
+ @viking.verified?.should == true
0
+ specify "should show as unverified with api key" do
0
+ mock_akismet("/1.1/verify-key", {:key => :foo, :blog => :bar}).returns(stub(:body => 'invalid'))
0
+ @viking.verified?.should == false
0
+ @viking.verified?.should == false
0
+ specify "should check comment is valid" do
0
+ mock_akismet("/1.1/comment-check").returns(stub(:body => 'false'))
0
+ @viking.check_comment.should == {:message => 'false', :spam => false}
0
+ specify "should check comment is spam" do
0
+ mock_akismet("/1.1/comment-check").returns(stub(:body => 'blah'))
0
+ @viking.check_comment.should == {:message => 'blah', :spam => true}
0
+ specify "should mark comment as spam" do
0
+ mock_akismet("/1.1/submit-spam").returns(stub(:body => 'blah'))
0
+ @viking.mark_as_spam.should == {:message => 'blah'}
0
+ specify "should mark comment as ham" do
0
+ mock_akismet("/1.1/submit-ham").returns(stub(:body => 'blah'))
0
+ @viking.mark_as_ham.should == {:message => 'blah'}
0
+ def mock_akismet(url, options = {})
0
+ Net::HTTP.any_instance.expects(:post).with(url, options.merge(:blog => :bar).to_query, Viking::Akismet.standard_headers)
0
+ @viking = Viking.connect :defensio, :api_key => 'foo', :blog => 'bar'
0
+ specify "should build correct action url" do
0
+ @viking.send(:api_url, 'foo').should == '/blog/1.1/foo/foo.yaml'
0
+ specify "should verify api key" do
0
+ mock_defensio("validate-key", {}, {'status' => 'success'})
0
+ @viking.verified?.should == true # #verified? is called twice to make sure #verify_api_key is not called twice
0
+ @viking.verified?.should == true
0
+ specify "should show as unverified with api key" do
0
+ mock_defensio("validate-key", {}, {'status' => 'asdf'})
0
+ @viking.verified?.should == false
0
+ @viking.verified?.should == false
0
+ specify "should announce article" do
0
+ mock_defensio("announce-article", {}, {'message' => 'whatever'})
0
+ @viking.check_article.should == {:message => 'whatever'}
0
+ specify "should check comment is valid" do
0
+ mock_defensio("audit-comment", {}, {'spam' => false, 'spaminess' => 0.1})
0
+ @viking.check_comment.should == {:spaminess => 0.1, :spam => false}
0
+ specify "should mark comment as spam" do
0
+ mock_defensio("report-false-negatives", {:user_ip => '123'}, {'message' => 'blah'})
0
+ @viking.mark_as_spam(:user_ip => '123').should == {:message => 'blah'}
0
+ specify "should mark comment as ham" do
0
+ mock_defensio("report-false-positives", {}, {'message' => 'blah'})
0
+ @viking.mark_as_ham.should == {:message => 'blah'}
0
+ specify "should recover from bad defensio message" do
0
+ data = {'owner-url' => 'bar'}.to_query
0
+ response = {'nothing' => '1'}.to_yaml
0
+ Net::HTTP.any_instance.expects(:post).with(@viking.send(:api_url, 'announce-article'), data, Viking::Defensio.standard_headers).returns(stub(:body => response))
0
+ @viking.check_article.should == {:data => response, :status => 'fail'}
0
+ specify "should recover from bad yaml response" do
0
+ data = {'owner-url' => 'bar'}.to_query
0
+ Net::HTTP.any_instance.expects(:post).with(@viking.send(:api_url, 'announce-article'), data, Viking::Defensio.standard_headers).returns(stub(:body => response))
0
+ @viking.check_article.should == {:data => response, :status => 'fail'}
0
+ def mock_defensio(action, options = {}, response = {})
0
+ data = options.inject({'owner-url' => 'bar'}) do |memo, (key, value)|
0
+ memo[key.to_s.dasherize] = value
0
+ response = {'defensio-result' => response.stringify_keys}.to_yaml
0
+ Net::HTTP.any_instance.expects(:post).with(@viking.send(:api_url, action), data, Viking::Defensio.standard_headers).returns(stub(:body => response))
Comments
No one has commented yet.