vikinggem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe Object do
4
+
5
+ describe '#to_param' do
6
+ it 'should return a representation of an object' do
7
+ 1.to_param.should == '1'
8
+ end
9
+ end
10
+
11
+ describe '#to_query' do
12
+ it 'should CGI escape an object and its associated key' do
13
+ 'foo'.to_query('bar').should == 'bar=foo'
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe Hash do
4
+
5
+ describe '#symbolize_keys' do
6
+ it 'should convert all keys to symbols' do
7
+ { 'foo' => 'bar', :baz => 1 }.symbolize_keys.should == { :foo => 'bar', :baz => 1 }
8
+ end
9
+
10
+ it 'should handle bad keys' do
11
+ { nil => 'bar' }.symbolize_keys[nil].should == 'bar'
12
+ end
13
+ end
14
+
15
+ describe '#dasherize_keys' do
16
+ it 'should convert all all underscores in keys to dashes' do
17
+ { 'foo_bar' => 'baz' }.dasherize_keys.should == { 'foo-bar' => 'baz' }
18
+ end
19
+ end
20
+
21
+ describe '#to_query' do
22
+ it 'should convert to a valid URI query' do
23
+ { :foo => 'baz', :bar => 1 }.to_query.should == 'bar=1&foo=baz'
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ describe Array, '#to_query' do
30
+ it 'should convert to a valid URI query' do
31
+ [:foo, :bar].to_query('baz').should == 'baz%5B%5D=foo&baz%5B%5D=bar'
32
+ end
33
+ end
@@ -0,0 +1,93 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Akismet" do
4
+
5
+ attr_accessor :akismet, :http
6
+
7
+ before(:each) do
8
+ self.akismet = Viking.connect('akismet', valid_options)
9
+ self.http = Net::HTTP.new("url")
10
+ Net::HTTP.stub!(:new).and_return(http)
11
+ end
12
+
13
+ after(:each) do
14
+ self.akismet = nil
15
+ self.http = nil
16
+ end
17
+
18
+ def valid_options
19
+ {
20
+ :blog => :foo,
21
+ :api_key => :bar
22
+ }
23
+ end
24
+
25
+ describe ".new" do
26
+ it "should not have a verified key when initialized" do
27
+ akismet.send(:verified_key).should be_false
28
+ end
29
+ end
30
+
31
+ describe '.url' do
32
+ it 'should return an URL for a request' do
33
+ Viking::Akismet.url('bar').should == '/1.1/bar'
34
+ end
35
+ end
36
+
37
+ describe "#verified?" do
38
+ it "should be verified when all parameters are provided" do
39
+ http.should_receive(:post).and_return(stub("response", :body => "valid"))
40
+
41
+ akismet.should be_verified # #verified? is called twice to make sure #verify_api_key is not called twice
42
+ akismet.should be_verified
43
+ end
44
+
45
+ it "should not be verified if Akismet doesn't validate" do
46
+ http.should_receive(:post).and_return(stub("response", :body => "invalid"))
47
+ akismet.should_not be_verified
48
+ end
49
+
50
+ it "should not be verified if its options are invalid" do
51
+ Viking.connect('akismet', {}).should_not be_verified
52
+ end
53
+ end
54
+
55
+ describe "#check_comment" do
56
+ it "should be false if the instance has invalid options" do
57
+ Viking.connect('akismet', {}).check_comment({}).should be_false
58
+ end
59
+
60
+ it "should be spam when the response body isn't a valid response" do
61
+ http.should_receive(:post).and_return(stub("response", :body => "invalid"))
62
+ akismet.check_comment(:user_ip => "127.0.0.1", :user_agent => "Mozilla").should == { :message => "invalid", :spam => true }
63
+ end
64
+
65
+ it "should not be spam when the response body is a valid response" do
66
+ http.should_receive(:post).and_return(stub("response", :body => "false"))
67
+ akismet.check_comment(:user_ip => "127.0.0.1", :user_agent => "Mozilla").should == { :message => "false", :spam => false }
68
+ end
69
+ end
70
+
71
+ describe "#mark_as_spam" do
72
+ it 'should be false if the instance has invalid options' do
73
+ Viking.connect('akismet', {}).mark_as_spam({}).should be_false
74
+ end
75
+
76
+ it 'should return the response body' do
77
+ http.should_receive(:post).and_return(stub('response', :body => "foo"))
78
+ akismet.mark_as_spam({}).should == { :message => "foo" }
79
+ end
80
+ end
81
+
82
+ describe '#mark_as_ham' do
83
+ it 'should be false if the instance has invalid options' do
84
+ Viking.connect('akismet', {}).mark_as_ham({}).should be_false
85
+ end
86
+
87
+ it 'should return the response body' do
88
+ http.should_receive(:post).and_return(stub('response', :body => "foo"))
89
+ akismet.mark_as_ham({}).should == { :message => "foo" }
90
+ end
91
+ end
92
+
93
+ end
@@ -0,0 +1,55 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ module BaseSpecHelper
4
+ def valid_base_options
5
+ {
6
+ :api_key => "1234abc",
7
+ :blog => "wiki.mysite.com"
8
+ }
9
+ end
10
+ end
11
+
12
+ describe Viking::Base do
13
+
14
+ attr_accessor :base
15
+
16
+ before(:each) do
17
+ self.base = Viking::Base.new({})
18
+ end
19
+
20
+ after(:each) do
21
+ self.base = nil
22
+ end
23
+
24
+ describe "#mark_as_spam_or_ham" do
25
+ it "should mark as spam when is_spam is true" do
26
+ base.should_receive(:mark_as_spam).and_return("I will be spam")
27
+ base.mark_as_spam_or_ham(true, {}).should == "I will be spam"
28
+ end
29
+
30
+ it "should mark as ham when is_spam is false" do
31
+ base.should_receive(:mark_as_ham).and_return("I will be ham")
32
+ base.mark_as_spam_or_ham(false, {}).should == "I will be ham"
33
+ end
34
+ end
35
+
36
+ describe "#invalid_options?" do
37
+ include BaseSpecHelper
38
+
39
+ it "should be false if the required options are non-nil" do
40
+ base.options = valid_base_options
41
+ base.should_not be_invalid_options
42
+ end
43
+
44
+ it "should be true if the options don't include an API key" do
45
+ base.options = valid_base_options.except(:api_key)
46
+ base.should be_invalid_options
47
+ end
48
+
49
+ it "should be true if the options don't include a blog address" do
50
+ base.options = valid_base_options.except(:blog)
51
+ base.should be_invalid_options
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,135 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe "Defensio" do
4
+
5
+ attr_accessor :defensio, :http
6
+
7
+ before(:each) do
8
+ self.defensio = Viking.connect('defensio', valid_options)
9
+ self.http = Net::HTTP.new('url')
10
+ Net::HTTP.stub!(:new).and_return(http)
11
+ end
12
+
13
+ after(:each) do
14
+ self.defensio = nil
15
+ self.http = nil
16
+ end
17
+
18
+ def valid_options
19
+ {
20
+ :blog => 'foo',
21
+ :api_key => 'bar'
22
+ }
23
+ end
24
+
25
+ def defensio_with_bad_options
26
+ Viking.connect('defensio', {})
27
+ end
28
+
29
+ describe '.new' do
30
+ it 'should not have verified options when initialized' do
31
+ defensio_with_bad_options.send(:verify_options).should == false
32
+ end
33
+ end
34
+
35
+ describe '#verified?' do
36
+ it "should be verified when all parameters are provided" do
37
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n status: success"))
38
+
39
+ defensio.should be_verified # called twice to make sure #validate-key is not called twice
40
+ defensio.should be_verified
41
+ end
42
+
43
+ it "should not be verified if Defensio doesn't validate" do
44
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n status: fail"))
45
+ defensio.should_not be_verified
46
+ end
47
+
48
+ it "should not be verified if its options are invalid" do
49
+ defensio_with_bad_options.should_not be_verified
50
+ end
51
+ end
52
+
53
+ describe '#check_article' do
54
+ it 'should be false if its options are invalid' do
55
+ defensio_with_bad_options.check_article({}).should be_false
56
+ end
57
+
58
+ it 'should check the article with Defensio' do
59
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
60
+ defensio.check_article({})[:foo].should == "bar"
61
+ end
62
+ end
63
+
64
+ describe '#check_comment' do
65
+ it 'should be false if its options are invalid' do
66
+ defensio_with_bad_options.check_comment({}).should be_false
67
+ end
68
+
69
+ it 'should raise a NoMethodError if options are provided without an article_date that responds to strftime' do
70
+ http.should_not_receive(:post)
71
+ lambda { defensio.check_comment(:article_date => nil) }.should raise_error(NoMethodError)
72
+ end
73
+
74
+ it 'should check the comment with Defensio' do
75
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
76
+ defensio.check_comment(:article_date => Time.now)[:foo].should == "bar"
77
+ end
78
+ end
79
+
80
+ describe '#mark_as_spam' do
81
+ it 'should be false if its options are invalid' do
82
+ defensio_with_bad_options.mark_as_spam({}).should be_false
83
+ end
84
+
85
+ it 'should mark the comments whose signatures are provided as spam' do
86
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
87
+ defensio.mark_as_spam(:signatures => "1,2,3")[:foo].should == "bar"
88
+ end
89
+ end
90
+
91
+ describe '#mark_as_ham' do
92
+ it 'should be false if its options are invalid' do
93
+ defensio_with_bad_options.mark_as_ham({}).should be_false
94
+ end
95
+
96
+ it 'should mark the comments whose signatures are provided as spam' do
97
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
98
+ defensio.mark_as_ham(:signatures => "1,2,3").should == { :foo => "bar" }
99
+ end
100
+ end
101
+
102
+ describe '#stats' do
103
+ it 'should be false if its options are invalid' do
104
+ defensio_with_bad_options.stats.should be_false
105
+ end
106
+
107
+ it 'should return stats about the blog' do
108
+ http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
109
+ defensio.stats.should == { :foo => "bar" }
110
+ end
111
+ end
112
+
113
+ describe '#url' do
114
+ it 'should return an URL for the specified action' do
115
+ defensio.url('get-stats').should == '/blog/1.2/get-stats/bar.yaml'
116
+ end
117
+ end
118
+
119
+ describe '#process_response_body' do
120
+ it 'should return the defensio-response portion of the YAML response as a Hash' do
121
+ Viking::Defensio.publicize_methods do
122
+ v = Viking.connect('defensio', {})
123
+ v.process_response_body("defensio-result:\n foo: bar").should == { :foo => "bar" }
124
+ end
125
+ end
126
+
127
+ it 'should return the entire response with failure as the status if the response is not as expected' do
128
+ Viking::Defensio.publicize_methods do
129
+ v = Viking.connect('defensio', {})
130
+ v.process_response_body("foo:\n bar: baz").should == { :data => { 'foo' => { 'bar' => 'baz' } }, :status => "fail" }
131
+ end
132
+ end
133
+ end
134
+
135
+ end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+
3
+ describe Viking do
4
+
5
+ describe ".connect" do
6
+ it "should load the Defensio engine" do
7
+ Viking.connect('defensio', {}).should be_a_kind_of(Viking::Defensio)
8
+ end
9
+
10
+ it "should load the Akismet engine" do
11
+ Viking.connect('akismet', {}).should be_a_kind_of(Viking::Akismet)
12
+ end
13
+
14
+ it "should be nil if the engine is nil" do
15
+ Viking.connect(nil, {}).should be_nil
16
+ end
17
+
18
+ it "should be nil if the engine is blank" do
19
+ Viking.connect('', {}).should be_nil
20
+ end
21
+ end
22
+
23
+ describe ".enabled?" do
24
+ it "should not be enabled if a default instance has not be initialized" do
25
+ Viking.should_not be_enabled
26
+ end
27
+
28
+ it "should be enabled if a default instance has been initialized" do
29
+ Viking.default_engine = 'defensio'
30
+ Viking.connect_options = '1234abc'
31
+
32
+ Viking.should be_enabled
33
+ end
34
+ end
35
+
36
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format specdoc
@@ -0,0 +1,56 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'viking'
11
+
12
+ # See http://blog.jayfields.com/2007/11/ruby-testing-private-methods.html
13
+ class Class
14
+ def publicize_methods(instance=nil)
15
+ saved_private_instance_methods = self.private_instance_methods
16
+ self.class_eval { public *saved_private_instance_methods }
17
+ yield(instance)
18
+ ensure
19
+ self.class_eval { private *saved_private_instance_methods }
20
+ end
21
+ end
22
+
23
+ ##
24
+ # rSpec Hash additions.
25
+ #
26
+ # From
27
+ # * http://wincent.com/knowledge-base/Fixtures_considered_harmful%3F
28
+ # * Neil Rahilly
29
+ class Hash
30
+ ##
31
+ # Filter keys out of a Hash.
32
+ #
33
+ # { :a => 1, :b => 2, :c => 3 }.except(:a)
34
+ # => { :b => 2, :c => 3 }
35
+ def except(*keys)
36
+ self.reject { |k,v| keys.include?(k || k.to_sym) }
37
+ end
38
+
39
+ ##
40
+ # Override some keys.
41
+ #
42
+ # { :a => 1, :b => 2, :c => 3 }.with(:a => 4)
43
+ # => { :a => 4, :b => 2, :c => 3 }
44
+ def with(overrides = {})
45
+ self.merge overrides
46
+ end
47
+
48
+ ##
49
+ # Returns a Hash with only the pairs identified by +keys+.
50
+ #
51
+ # { :a => 1, :b => 2, :c => 3 }.only(:a)
52
+ # => { :a => 1 }
53
+ def only(*keys)
54
+ self.reject { |k,v| !keys.include?(k || k.to_sym) }
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vikinggem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Herdman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-22 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Gemified version of the Viking plugin
17
+ email:
18
+ - james.herdman@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - History.txt
25
+ - Manifest.txt
26
+ - README.txt
27
+ files:
28
+ - .autotest
29
+ - .gitignore
30
+ - History.txt
31
+ - Manifest.txt
32
+ - README.markdown
33
+ - README.txt
34
+ - Rakefile
35
+ - config/requirements.rb
36
+ - lib/core_ext/object.rb
37
+ - lib/core_ext/transformations.rb
38
+ - lib/viking.rb
39
+ - lib/viking/akismet.rb
40
+ - lib/viking/base.rb
41
+ - lib/viking/defensio.rb
42
+ - lib/viking/version.rb
43
+ - lib/viking/viking.rb
44
+ - setup.rb
45
+ - spec/core_ext/object_spec.rb
46
+ - spec/core_ext/transformations_spec.rb
47
+ - spec/lib/akismet_spec.rb
48
+ - spec/lib/base_spec.rb
49
+ - spec/lib/defensio_spec.rb
50
+ - spec/lib/viking_spec.rb
51
+ - spec/spec.opts
52
+ - spec/spec_helper.rb
53
+ has_rdoc: true
54
+ homepage: http://vikinggem.rubyforge.org
55
+ post_install_message: ""
56
+ rdoc_options:
57
+ - --main
58
+ - README.txt
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: vikinggem
76
+ rubygems_version: 1.1.1
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: Gemified version of the Viking plugin
80
+ test_files: []
81
+