Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add a test suite.

  • Loading branch information...
commit 4197bbacf2e649f14305d55142d799b0260e8ac1 1 parent 0b582bd
Matt Colyer authored
2  .rspec
... ... @@ -0,0 +1,2 @@
  1 +--color
  2 +--format progress
5 Gemfile
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2 2
3 3 gem 'rack-rpc'
4 4 gem 'hacklet'
  5 +
  6 +group :development do
  7 + gem 'rspec'
  8 + gem 'rack-test'
  9 +end
13 Gemfile.lock
@@ -3,6 +3,7 @@ GEM
3 3 specs:
4 4 bindata (1.5.0)
5 5 builder (3.2.2)
  6 + diff-lcs (1.2.4)
6 7 hacklet (0.5.1)
7 8 bindata (~> 1.5.0)
8 9 serialport (~> 1.1.0)
@@ -11,6 +12,16 @@ GEM
11 12 rack-rpc (0.0.6)
12 13 builder (>= 2.1.2)
13 14 rack (>= 1.0)
  15 + rack-test (0.6.2)
  16 + rack (>= 1.0)
  17 + rspec (2.13.0)
  18 + rspec-core (~> 2.13.0)
  19 + rspec-expectations (~> 2.13.0)
  20 + rspec-mocks (~> 2.13.0)
  21 + rspec-core (2.13.1)
  22 + rspec-expectations (2.13.0)
  23 + diff-lcs (>= 1.1.3, < 2.0)
  24 + rspec-mocks (2.13.1)
14 25 serialport (1.1.0)
15 26 slop (3.4.5)
16 27
@@ -20,3 +31,5 @@ PLATFORMS
20 31 DEPENDENCIES
21 32 hacklet
22 33 rack-rpc
  34 + rack-test
  35 + rspec
117 spec/app_spec.rb
... ... @@ -0,0 +1,117 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Server do
  4 + def app
  5 + base = proc do |env|
  6 + [ 200, {'Content-Type' => 'text/plain'}, ['hacklet'] ]
  7 + end
  8 + Rack::RPC::Endpoint.new(base, subject, :path => '/xmlrpc.php')
  9 + end
  10 +
  11 + def build_request(user, password, command)
  12 + <<-END
  13 + <?xml version="1.0"?>
  14 + <methodCall>
  15 + <methodName>metaWeblog.newPost</methodName>
  16 + <params>
  17 + <param><value> <string/> </value></param>
  18 + <param><value> <string>#{user}</string> </value></param>
  19 + <param><value> <string>#{password}</string> </value></param>
  20 + <param><value> <struct>
  21 + <member> <name>title</name> <value> <string>#{command}</string> </value></member>
  22 + <member> <name>description</name> <value> <string>{"network": "0xc6d2", "socket":0}</string> </value></member>
  23 + <member> <name>post_status</name> <value> <string>publish</string> </value></member>
  24 + </struct> </value> </param>
  25 + <param> <value> <boolean>1</boolean> </value> </param>
  26 + </params>
  27 + </methodCall>
  28 + END
  29 + end
  30 +
  31 + def response(status)
  32 + "<?xml version=\"1.0\" ?><methodResponse><params><param><value><i4>#{status}</i4></value></param></params></methodResponse>\n"
  33 + end
  34 +
  35 + def xmlrpc_request(body)
  36 + post '/xmlrpc.php', {}, {'rack.input' => StringIO.new(body), 'CONTENT_TYPE' => 'text/xml'}
  37 + end
  38 +
  39 + describe 'the default location' do
  40 + before do
  41 + get '/'
  42 + end
  43 +
  44 + it 'is successful' do
  45 + last_response.should be_ok
  46 + end
  47 +
  48 + it 'has a helpful message' do
  49 + last_response.body.should eq('hacklet')
  50 + end
  51 + end
  52 +
  53 + describe 'controlling the hacklet' do
  54 + context 'with an invalid' do
  55 + before do
  56 + subject.stub(:configuration).and_return({ 'user' => 'b', 'password' => 'b' })
  57 + end
  58 +
  59 + context 'username' do
  60 + let(:body) { build_request('a', 'b', 'on') }
  61 +
  62 + before do
  63 + xmlrpc_request(body)
  64 + end
  65 +
  66 + it 'fails' do
  67 + last_response.body.should eq(response(403))
  68 + end
  69 + end
  70 +
  71 + context 'password' do
  72 + let(:body) { build_request('b', 'a', 'on') }
  73 +
  74 + before do
  75 + xmlrpc_request(body)
  76 + end
  77 +
  78 + it 'fails' do
  79 + last_response.body.should eq(response(403))
  80 + end
  81 + end
  82 + end
  83 +
  84 + context 'with a valid credentials' do
  85 + before do
  86 + subject.stub(:configuration).and_return({ 'user' => 'user', 'password' => 'password' })
  87 + end
  88 +
  89 + describe 'turning on' do
  90 + let(:body) { build_request('user', 'password', 'on') }
  91 +
  92 + before do
  93 + subject.should_receive(:`).with('hacklet on -n 0xc6d2 -s 0').and_return('None')
  94 + xmlrpc_request(body)
  95 + end
  96 +
  97 + it 'succeeds' do
  98 + last_response.body.should eq(response(200))
  99 + end
  100 + end
  101 +
  102 + describe 'turning off' do
  103 + let(:body) { build_request('user', 'password', 'off') }
  104 +
  105 + before do
  106 + subject.should_receive(:`).with('hacklet off -n 0xc6d2 -s 0').and_return('None')
  107 + xmlrpc_request(body)
  108 + end
  109 +
  110 + it 'succeeds' do
  111 + last_response.body.should eq(response(200))
  112 + end
  113 + end
  114 + end
  115 + end
  116 +
  117 +end
19 spec/spec_helper.rb
... ... @@ -0,0 +1,19 @@
  1 +$:<< File.join(File.dirname(__FILE__), '..')
  2 +
  3 +require 'app'
  4 +require 'rack/test'
  5 +
  6 +RSpec.configure do |config|
  7 + config.treat_symbols_as_metadata_keys_with_true_values = true
  8 + config.run_all_when_everything_filtered = true
  9 + config.filter_run :focus
  10 +
  11 + # Include rack-test
  12 + config.include Rack::Test::Methods
  13 +
  14 + # Run specs in random order to surface order dependencies. If you find an
  15 + # order dependency and want to debug it, you can fix the order by providing
  16 + # the seed, which is printed after each run.
  17 + # --seed 1234
  18 + config.order = 'random'
  19 +end

0 comments on commit 4197bba

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