Permalink
Browse files

Specs for basic authentication

Had to drop down to a lower-level API,
since the default sets the user/pass
at load time, and I want to change it
dynamically.
  • Loading branch information...
1 parent 295cfa3 commit 525041cced9f56b742a17d5cc3687f136b3913fb @dce dce committed Nov 27, 2012
Showing with 49 additions and 3 deletions.
  1. +9 −3 app/controllers/stat_board/stats_controller.rb
  2. +40 −0 spec/requests/stat_board_spec.rb
@@ -2,8 +2,14 @@
module StatBoard
class StatsController < ApplicationController
- http_basic_authenticate_with :name => StatBoard.username,
- :password => StatBoard.password,
- :if => lambda { StatBoard.username && StatBoard.password }
+ before_filter :basic_authenticate, :if => lambda { StatBoard.username && StatBoard.password }
+
+ private
+
+ def basic_authenticate
+ authenticate_or_request_with_http_basic do |name, password|
+ name == StatBoard.username && password == StatBoard.password
+ end
+ end
end
end
@@ -72,5 +72,45 @@
visit "/stats"
page.should have_content("App Stats")
end
+
+ after do
+ StatBoard.title = nil
+ end
+ end
+
+ describe "with auth credentials" do
+ before do
+ StatBoard.username = "user"
+ StatBoard.password = "pass"
+ end
+
+ it "gets a request for authentication if none provided" do
+ visit "/stats"
+ page.status_code.should == 401
+ end
+
+ it "gets a request for authentication if bad details provided" do
+ page.driver.browser.authorize("user", "wrong")
+ visit "/stats"
+ page.status_code.should == 401
+ end
+
+ it "sees the stat board if correct details provided" do
+ page.driver.browser.authorize("user", "pass")
+ visit "/stats"
+ puts page.body
+ page.should have_content("StatBoard")
+ end
+
+ it "does not need details if only partial credentials specified" do
+ StatBoard.password = nil
+ visit "/stats"
+ page.should have_content("StatBoard")
+ end
+
+ after do
+ StatBoard.username = nil
+ StatBoard.password = nil
+ end
end
end

0 comments on commit 525041c

Please sign in to comment.