Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add specs

  • Loading branch information...
commit a6ffc0529361d15903a379223ee0eaaac32e522e 1 parent 63a13cd
@jherdman jherdman authored
View
17 spec/core_ext/object_spec.rb
@@ -0,0 +1,17 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+describe Object do
+
+ describe '#to_param' do
+ it 'should return a representation of an object' do
+ 1.to_param.should == '1'
+ end
+ end
+
+ describe '#to_query' do
+ it 'should CGI escape an object and its associated key' do
+ 'foo'.to_query('bar').should == 'bar=foo'
+ end
+ end
+
+end
View
33 spec/core_ext/transformations_spec.rb
@@ -0,0 +1,33 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+describe Hash do
+
+ describe '#symbolize_keys' do
+ it 'should convert all keys to symbols' do
+ { 'foo' => 'bar', :baz => 1 }.symbolize_keys.should == { :foo => 'bar', :baz => 1 }
+ end
+
+ it 'should handle bad keys' do
+ { nil => 'bar' }.symbolize_keys[nil].should == 'bar'
+ end
+ end
+
+ describe '#dasherize_keys' do
+ it 'should convert all all underscores in keys to dashes' do
+ { 'foo_bar' => 'baz' }.dasherize_keys.should == { 'foo-bar' => 'baz' }
+ end
+ end
+
+ describe '#to_query' do
+ it 'should convert to a valid URI query' do
+ { :foo => 'baz', :bar => 1 }.to_query.should == 'bar=1&foo=baz'
+ end
+ end
+
+end
+
+describe Array, '#to_query' do
+ it 'should convert to a valid URI query' do
+ [:foo, :bar].to_query('baz').should == 'baz%5B%5D=foo&baz%5B%5D=bar'
+ end
+end
View
93 spec/lib/akismet_sepc.rb
@@ -0,0 +1,93 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+describe "Akismet" do
+
+ attr_accessor :akismet, :http
+
+ before(:each) do
+ self.akismet = Viking.connect('akismet', valid_options)
+ self.http = Net::HTTP.new("url")
+ Net::HTTP.stub!(:new).and_return(http)
+ end
+
+ after(:each) do
+ self.akismet = nil
+ self.http = nil
+ end
+
+ def valid_options
+ {
+ :blog => :foo,
+ :api_key => :bar
+ }
+ end
+
+ describe ".new" do
+ it "should not have a verified key when initialized" do
+ akismet.send(:verified_key).should be_false
+ end
+ end
+
+ describe '.url' do
+ it 'should return an URL for a request' do
+ Viking::Akismet.url('bar').should == '/1.1/bar'
+ end
+ end
+
+ describe "#verified?" do
+ it "should be verified when all parameters are provided" do
+ http.should_receive(:post).and_return(stub("response", :body => "valid"))
+
+ akismet.should be_verified # #verified? is called twice to make sure #verify_api_key is not called twice
+ akismet.should be_verified
+ end
+
+ it "should not be verified if Akismet doesn't validate" do
+ http.should_receive(:post).and_return(stub("response", :body => "invalid"))
+ akismet.should_not be_verified
+ end
+
+ it "should not be verified if its options are invalid" do
+ Viking.connect('akismet', {}).should_not be_verified
+ end
+ end
+
+ describe "#check_comment" do
+ it "should be false if the instance has invalid options" do
+ Viking.connect('akismet', {}).check_comment({}).should be_false
+ end
+
+ it "should be spam when the response body isn't a valid response" do
+ http.should_receive(:post).and_return(stub("response", :body => "invalid"))
+ akismet.check_comment(:user_ip => "127.0.0.1", :user_agent => "Mozilla").should == { :message => "invalid", :spam => true }
+ end
+
+ it "should not be spam when the response body is a valid response" do
+ http.should_receive(:post).and_return(stub("response", :body => "false"))
+ akismet.check_comment(:user_ip => "127.0.0.1", :user_agent => "Mozilla").should == { :message => "false", :spam => false }
+ end
+ end
+
+ describe "#mark_as_spam" do
+ it 'should be false if the instance has invalid options' do
+ Viking.connect('akismet', {}).mark_as_spam({}).should be_false
+ end
+
+ it 'should return the response body' do
+ http.should_receive(:post).and_return(stub('response', :body => "foo"))
+ akismet.mark_as_spam({}).should == { :message => "foo" }
+ end
+ end
+
+ describe '#mark_as_ham' do
+ it 'should be false if the instance has invalid options' do
+ Viking.connect('akismet', {}).mark_as_ham({}).should be_false
+ end
+
+ it 'should return the response body' do
+ http.should_receive(:post).and_return(stub('response', :body => "foo"))
+ akismet.mark_as_ham({}).should == { :message => "foo" }
+ end
+ end
+
+end
View
55 spec/lib/base_sepc.rb
@@ -0,0 +1,55 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+module BaseSpecHelper
+ def valid_base_options
+ {
+ :api_key => "1234abc",
+ :blog => "wiki.mysite.com"
+ }
+ end
+end
+
+describe Viking::Base do
+
+ attr_accessor :base
+
+ before(:each) do
+ self.base = Viking::Base.new({})
+ end
+
+ after(:each) do
+ self.base = nil
+ end
+
+ describe "#mark_as_spam_or_ham" do
+ it "should mark as spam when is_spam is true" do
+ base.should_receive(:mark_as_spam).and_return("I will be spam")
+ base.mark_as_spam_or_ham(true, {}).should == "I will be spam"
+ end
+
+ it "should mark as ham when is_spam is false" do
+ base.should_receive(:mark_as_ham).and_return("I will be ham")
+ base.mark_as_spam_or_ham(false, {}).should == "I will be ham"
+ end
+ end
+
+ describe "#invalid_options?" do
+ include BaseSpecHelper
+
+ it "should be false if the required options are non-nil" do
+ base.options = valid_base_options
+ base.should_not be_invalid_options
+ end
+
+ it "should be true if the options don't include an API key" do
+ base.options = valid_base_options.except(:api_key)
+ base.should be_invalid_options
+ end
+
+ it "should be true if the options don't include a blog address" do
+ base.options = valid_base_options.except(:blog)
+ base.should be_invalid_options
+ end
+ end
+
+end
View
135 spec/lib/defensio_spec.rb
@@ -0,0 +1,135 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+describe "Defensio" do
+
+ attr_accessor :defensio, :http
+
+ before(:each) do
+ self.defensio = Viking.connect('defensio', valid_options)
+ self.http = Net::HTTP.new('url')
+ Net::HTTP.stub!(:new).and_return(http)
+ end
+
+ after(:each) do
+ self.defensio = nil
+ self.http = nil
+ end
+
+ def valid_options
+ {
+ :blog => 'foo',
+ :api_key => 'bar'
+ }
+ end
+
+ def defensio_with_bad_options
+ Viking.connect('defensio', {})
+ end
+
+ describe '.new' do
+ it 'should not have verified options when initialized' do
+ defensio_with_bad_options.send(:verify_options).should == false
+ end
+ end
+
+ describe '#verified?' do
+ it "should be verified when all parameters are provided" do
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n status: success"))
+
+ defensio.should be_verified # called twice to make sure #validate-key is not called twice
+ defensio.should be_verified
+ end
+
+ it "should not be verified if Defensio doesn't validate" do
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n status: fail"))
+ defensio.should_not be_verified
+ end
+
+ it "should not be verified if its options are invalid" do
+ defensio_with_bad_options.should_not be_verified
+ end
+ end
+
+ describe '#check_article' do
+ it 'should be false if its options are invalid' do
+ defensio_with_bad_options.check_article({}).should be_false
+ end
+
+ it 'should check the article with Defensio' do
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
+ defensio.check_article({})[:foo].should == "bar"
+ end
+ end
+
+ describe '#check_comment' do
+ it 'should be false if its options are invalid' do
+ defensio_with_bad_options.check_comment({}).should be_false
+ end
+
+ it 'should raise a NoMethodError if options are provided without an article_date that responds to strftime' do
+ http.should_not_receive(:post)
+ lambda { defensio.check_comment(:article_date => nil) }.should raise_error(NoMethodError)
+ end
+
+ it 'should check the comment with Defensio' do
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
+ defensio.check_comment(:article_date => Time.now)[:foo].should == "bar"
+ end
+ end
+
+ describe '#mark_as_spam' do
+ it 'should be false if its options are invalid' do
+ defensio_with_bad_options.mark_as_spam({}).should be_false
+ end
+
+ it 'should mark the comments whose signatures are provided as spam' do
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
+ defensio.mark_as_spam(:signatures => "1,2,3")[:foo].should == "bar"
+ end
+ end
+
+ describe '#mark_as_ham' do
+ it 'should be false if its options are invalid' do
+ defensio_with_bad_options.mark_as_ham({}).should be_false
+ end
+
+ it 'should mark the comments whose signatures are provided as spam' do
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
+ defensio.mark_as_ham(:signatures => "1,2,3").should == { :foo => "bar" }
+ end
+ end
+
+ describe '#stats' do
+ it 'should be false if its options are invalid' do
+ defensio_with_bad_options.stats.should be_false
+ end
+
+ it 'should return stats about the blog' do
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
+ defensio.stats.should == { :foo => "bar" }
+ end
+ end
+
+ describe '#url' do
+ it 'should return an URL for the specified action' do
+ defensio.url('get-stats').should == '/blog/1.2/get-stats/bar.yaml'
+ end
+ end
+
+ describe '#process_response_body' do
+ it 'should return the defensio-response portion of the YAML response as a Hash' do
+ Viking::Defensio.publicize_methods do
+ v = Viking.connect('defensio', {})
+ v.process_response_body("defensio-result:\n foo: bar").should == { :foo => "bar" }
+ end
+ end
+
+ it 'should return the entire response with failure as the status if the response is not as expected' do
+ Viking::Defensio.publicize_methods do
+ v = Viking.connect('defensio', {})
+ v.process_response_body("foo:\n bar: baz").should == { :data => { 'foo' => { 'bar' => 'baz' } }, :status => "fail" }
+ end
+ end
+ end
+
+end
View
36 spec/lib/viking_spec.rb
@@ -0,0 +1,36 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+describe Viking do
+
+ describe ".connect" do
+ it "should load the Defensio engine" do
+ Viking.connect('defensio', {}).should be_a_kind_of(Viking::Defensio)
+ end
+
+ it "should load the Akismet engine" do
+ Viking.connect('akismet', {}).should be_a_kind_of(Viking::Akismet)
+ end
+
+ it "should be nil if the engine is nil" do
+ Viking.connect(nil, {}).should be_nil
+ end
+
+ it "should be nil if the engine is blank" do
+ Viking.connect('', {}).should be_nil
+ end
+ end
+
+ describe ".enabled?" do
+ it "should not be enabled if a default instance has not be initialized" do
+ Viking.should_not be_enabled
+ end
+
+ it "should be enabled if a default instance has been initialized" do
+ Viking.default_engine = 'defensio'
+ Viking.connect_options = '1234abc'
+
+ Viking.should be_enabled
+ end
+ end
+
+end
View
2  spec/spec.opts
@@ -0,0 +1,2 @@
+--colour
+--format specdoc
View
56 spec/spec_helper.rb
@@ -0,0 +1,56 @@
+begin
+ require 'spec'
+rescue LoadError
+ require 'rubygems'
+ gem 'rspec'
+ require 'spec'
+end
+
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'viking'
+
+# See http://blog.jayfields.com/2007/11/ruby-testing-private-methods.html
+class Class
+ def publicize_methods(instance=nil)
+ saved_private_instance_methods = self.private_instance_methods
+ self.class_eval { public *saved_private_instance_methods }
+ yield(instance)
+ ensure
+ self.class_eval { private *saved_private_instance_methods }
+ end
+end
+
+##
+# rSpec Hash additions.
+#
+# From
+# * http://wincent.com/knowledge-base/Fixtures_considered_harmful%3F
+# * Neil Rahilly
+class Hash
+ ##
+ # Filter keys out of a Hash.
+ #
+ # { :a => 1, :b => 2, :c => 3 }.except(:a)
+ # => { :b => 2, :c => 3 }
+ def except(*keys)
+ self.reject { |k,v| keys.include?(k || k.to_sym) }
+ end
+
+ ##
+ # Override some keys.
+ #
+ # { :a => 1, :b => 2, :c => 3 }.with(:a => 4)
+ # => { :a => 4, :b => 2, :c => 3 }
+ def with(overrides = {})
+ self.merge overrides
+ end
+
+ ##
+ # Returns a Hash with only the pairs identified by +keys+.
+ #
+ # { :a => 1, :b => 2, :c => 3 }.only(:a)
+ # => { :a => 1 }
+ def only(*keys)
+ self.reject { |k,v| !keys.include?(k || k.to_sym) }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.