Permalink
Browse files

add test suite to check basic functionality

  • Loading branch information...
1 parent 508d350 commit 9b5007833df1ee81bcdaf1f90b9a6dd4e7a90e70 @garethr committed Nov 2, 2011
Showing with 85 additions and 3 deletions.
  1. +8 −2 Gemfile
  2. +11 −0 Gemfile.lock
  3. +9 −0 Rakefile
  4. +2 −1 lib/web-facter.rb
  5. +46 −0 test/integration_test.rb
  6. +9 −0 test/test_helper.rb
View
10 Gemfile
@@ -1,6 +1,12 @@
-# A sample Gemfile
-source "http://rubygems.org"
+source :rubygems
gem "facter"
gem "rack"
+gem "rake"
gem "parseconfig"
+
+group :test do
+ gem "test-unit"
+ gem "rack-test"
+ gem "mocha"
+end
View
@@ -2,13 +2,24 @@ 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)
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
facter
+ mocha
parseconfig
rack
+ rack-test
+ rake
+ test-unit
View
@@ -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
@@ -15,7 +15,8 @@ def call(env)
def add_auth(conf)
application = Rack::Auth::Basic.new(self) do |username, password|
- username_check = conf.get_value('username') ? conf.get_value('username') == username : true
+ 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
View
@@ -0,0 +1,46 @@
+require "test_helper"
+require "web-facter"
+
+class SearchTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ WebFacter::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)
+ assert json.keys.include?("architecture")
+ 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")
+ WebFacter::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
@@ -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

0 comments on commit 9b50078

Please sign in to comment.