Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor based on web-facter, add simple tests and new configuration …

…options
  • Loading branch information...
commit 0b1bca9171334f9034bf7017631d4dbe5c0560b5 1 parent 41c0a3a
@garethr authored
View
8 Gemfile
@@ -2,3 +2,11 @@ source "http://rubygems.org"
gem "rack"
gem "puppet", ">= 2.7.0"
+gem "rake"
+gem "parseconfig"
+
+group :test do
+ gem "test-unit"
+ gem "rack-test"
+ gem "mocha"
+end
View
13 Gemfile.lock
@@ -2,13 +2,26 @@ GEM
remote: http://rubygems.org/
specs:
facter (1.6.2)
+ metaclass (0.0.1)
+ mocha (0.10.0)
+ metaclass (~> 0.0.1)
+ parseconfig (0.5.2)
puppet (2.7.6)
facter (>= 1.5.1)
rack (1.3.5)
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rake (0.9.2.2)
+ test-unit (2.4.0)
PLATFORMS
ruby
DEPENDENCIES
+ mocha
+ parseconfig
puppet (>= 2.7.0)
rack
+ rack-test
+ rake
+ test-unit
View
9 Rakefile
@@ -1 +1,10 @@
require "bundler/gem_tasks"
+require "rake/testtask"
+
+Rake::TestTask.new do |t|
+ t.libs << "test"
+ t.test_files = FileList["test/**/*_test.rb"]
+ t.verbose = true
+end
+
+task :default => :test
View
14 bin/web-puppet
@@ -4,7 +4,7 @@ require 'rack'
require 'optparse'
require 'web-puppet'
-options = {}
+options = {:daemonize => true, :port => 9295, :config => false}
optparse = OptionParser.new do |opts|
opts.banner = 'Usage: web-puppet [options] ...'
@@ -12,18 +12,17 @@ optparse = OptionParser.new do |opts|
opts.separator ''
opts.separator 'Configuration options:'
- options[:daemonize] = true
opts.on( '--no-daemonize', "Don't daemonize the web server process") do |username|
options[:daemonize] = false
end
- options[:port] = 9295
opts.on( '-p', '--port PORT', 'The port to run web-puppet on') do |port|
options[:port] = port
end
- opts.separator ""
- opts.separator "Common options:"
+ opts.on( '-c', '--config FILE', 'The file to load with configuration options') do |file|
+ options[:config] = file
+ end
opts.on_tail('-h', '--help', 'Display this screen' ) do
puts opts
@@ -33,9 +32,12 @@ end
begin
optparse.parse!
- Rack::Server.new(:app => WebPuppet::App.new, :Port => options[:port], :daemonize => options[:daemonize]).start
+ WebPuppet::App.run!(options)
rescue OptionParser::InvalidArgument, OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
+rescue Errno::EACCES
+ puts $!.to_s
+ exit
end
View
28 lib/web-puppet.rb
@@ -31,5 +31,33 @@ def call env
response.write JSON.pretty_generate(data)
response.finish
end
+
+ def add_auth(conf)
+ application = Rack::Auth::Basic.new(self) do |username, password|
+ stored_username = conf.get_value('username')
+ username_check = stored_username ? stored_username == username : true
+ password_check = conf.get_value('password') == password
+ username_check && password_check
+ end
+ application.realm = 'Web Puppet'
+ application
+ end
+
+ def self.run!(options)
+ application = self.new
+
+ daemonize = options[:daemonize]
+ port = options[:port]
+
+ if options[:config]
+ conf = ParseConfig.new(options[:config])
+ application = application.add_auth(conf) if conf.get_value('password')
+ daemonize = conf.get_value('daemonize') ? conf.get_value('daemonize') == "true" : daemonize
+ port = conf.get_value('port') ? conf.get_value('port') : port
+ end
+
+ Rack::Server.new(:app => application, :Port => port, :daemonize => daemonize).start
+
+ end
end
end
View
45 test/integration_test.rb
@@ -0,0 +1,45 @@
+require "test_helper"
+require "web-puppet"
+
+class AppTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ WebPuppet::App.new
+ end
+
+ def test_view_renders_successfully
+ get "/"
+ assert last_response.ok?
+ end
+
+ def test_view_returns_json
+ get "/"
+ json = JSON.load(last_response.body)
+ end
+
+end
+
+class AuthenticationTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ conf = mock()
+ conf.expects(:get_value).with('username').returns("admin")
+ conf.expects(:get_value).with('password').returns("password")
+ WebPuppet::App.new().add_auth(conf)
+ end
+
+ def test_view_requires_authentication
+ authorize 'evil', 'password'
+ get "/"
+ assert_equal 401, last_response.status
+ end
+
+ def test_view_renders_with_auth_details
+ authorize 'admin', 'password'
+ get "/"
+ assert last_response.ok?
+ end
+
+end
View
9 test/test_helper.rb
@@ -0,0 +1,9 @@
+ENV['RACK_ENV'] = 'test'
+
+require "test/unit"
+require "rack/test"
+require "mocha"
+
+%w[ lib ].each do |path|
+ $:.unshift path unless $:.include?(path)
+end
View
5 web-puppet.gemspec
@@ -20,4 +20,9 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "rack"
s.add_runtime_dependency "puppet", ">= 2.7.0"
+ s.add_runtime_dependency "parseconfig"
+
+ s.add_development_dependency "test-unit"
+ s.add_development_dependency "rack-test"
+ s.add_development_dependency "mocha"
end
Please sign in to comment.
Something went wrong with that request. Please try again.