Skip to content
Browse files

Add a test suite.

  • Loading branch information...
1 parent 0b582bd commit 4197bbacf2e649f14305d55142d799b0260e8ac1 @mcolyer committed Jun 6, 2013
Showing with 156 additions and 0 deletions.
  1. +2 −0 .rspec
  2. +5 −0 Gemfile
  3. +13 −0 Gemfile.lock
  4. +117 −0 spec/app_spec.rb
  5. +19 −0 spec/spec_helper.rb
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
5 Gemfile
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
gem 'rack-rpc'
gem 'hacklet'
+
+group :development do
+ gem 'rspec'
+ gem 'rack-test'
+end
View
13 Gemfile.lock
@@ -3,6 +3,7 @@ GEM
specs:
bindata (1.5.0)
builder (3.2.2)
+ diff-lcs (1.2.4)
hacklet (0.5.1)
bindata (~> 1.5.0)
serialport (~> 1.1.0)
@@ -11,6 +12,16 @@ GEM
rack-rpc (0.0.6)
builder (>= 2.1.2)
rack (>= 1.0)
+ rack-test (0.6.2)
+ rack (>= 1.0)
+ rspec (2.13.0)
+ rspec-core (~> 2.13.0)
+ rspec-expectations (~> 2.13.0)
+ rspec-mocks (~> 2.13.0)
+ rspec-core (2.13.1)
+ rspec-expectations (2.13.0)
+ diff-lcs (>= 1.1.3, < 2.0)
+ rspec-mocks (2.13.1)
serialport (1.1.0)
slop (3.4.5)
@@ -20,3 +31,5 @@ PLATFORMS
DEPENDENCIES
hacklet
rack-rpc
+ rack-test
+ rspec
View
117 spec/app_spec.rb
@@ -0,0 +1,117 @@
+require 'spec_helper'
+
+describe Server do
+ def app
+ base = proc do |env|
+ [ 200, {'Content-Type' => 'text/plain'}, ['hacklet'] ]
+ end
+ Rack::RPC::Endpoint.new(base, subject, :path => '/xmlrpc.php')
+ end
+
+ def build_request(user, password, command)
+ <<-END
+ <?xml version="1.0"?>
+ <methodCall>
+ <methodName>metaWeblog.newPost</methodName>
+ <params>
+ <param><value> <string/> </value></param>
+ <param><value> <string>#{user}</string> </value></param>
+ <param><value> <string>#{password}</string> </value></param>
+ <param><value> <struct>
+ <member> <name>title</name> <value> <string>#{command}</string> </value></member>
+ <member> <name>description</name> <value> <string>{"network": "0xc6d2", "socket":0}</string> </value></member>
+ <member> <name>post_status</name> <value> <string>publish</string> </value></member>
+ </struct> </value> </param>
+ <param> <value> <boolean>1</boolean> </value> </param>
+ </params>
+ </methodCall>
+ END
+ end
+
+ def response(status)
+ "<?xml version=\"1.0\" ?><methodResponse><params><param><value><i4>#{status}</i4></value></param></params></methodResponse>\n"
+ end
+
+ def xmlrpc_request(body)
+ post '/xmlrpc.php', {}, {'rack.input' => StringIO.new(body), 'CONTENT_TYPE' => 'text/xml'}
+ end
+
+ describe 'the default location' do
+ before do
+ get '/'
+ end
+
+ it 'is successful' do
+ last_response.should be_ok
+ end
+
+ it 'has a helpful message' do
+ last_response.body.should eq('hacklet')
+ end
+ end
+
+ describe 'controlling the hacklet' do
+ context 'with an invalid' do
+ before do
+ subject.stub(:configuration).and_return({ 'user' => 'b', 'password' => 'b' })
+ end
+
+ context 'username' do
+ let(:body) { build_request('a', 'b', 'on') }
+
+ before do
+ xmlrpc_request(body)
+ end
+
+ it 'fails' do
+ last_response.body.should eq(response(403))
+ end
+ end
+
+ context 'password' do
+ let(:body) { build_request('b', 'a', 'on') }
+
+ before do
+ xmlrpc_request(body)
+ end
+
+ it 'fails' do
+ last_response.body.should eq(response(403))
+ end
+ end
+ end
+
+ context 'with a valid credentials' do
+ before do
+ subject.stub(:configuration).and_return({ 'user' => 'user', 'password' => 'password' })
+ end
+
+ describe 'turning on' do
+ let(:body) { build_request('user', 'password', 'on') }
+
+ before do
+ subject.should_receive(:`).with('hacklet on -n 0xc6d2 -s 0').and_return('None')
+ xmlrpc_request(body)
+ end
+
+ it 'succeeds' do
+ last_response.body.should eq(response(200))
+ end
+ end
+
+ describe 'turning off' do
+ let(:body) { build_request('user', 'password', 'off') }
+
+ before do
+ subject.should_receive(:`).with('hacklet off -n 0xc6d2 -s 0').and_return('None')
+ xmlrpc_request(body)
+ end
+
+ it 'succeeds' do
+ last_response.body.should eq(response(200))
+ end
+ end
+ end
+ end
+
+end
View
19 spec/spec_helper.rb
@@ -0,0 +1,19 @@
+$:<< File.join(File.dirname(__FILE__), '..')
+
+require 'app'
+require 'rack/test'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+
+ # Include rack-test
+ config.include Rack::Test::Methods
+
+ # Run specs in random order to surface order dependencies. If you find an
+ # order dependency and want to debug it, you can fix the order by providing
+ # the seed, which is printed after each run.
+ # --seed 1234
+ config.order = 'random'
+end

0 comments on commit 4197bba

Please sign in to comment.
Something went wrong with that request. Please try again.