aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2012-03-22 17:44:34 +0000
committerPatrick J Cherry <patrick@bytemark.co.uk>2012-03-22 17:44:34 +0000
commitfb491aee8f771c2240b84b06ab95337123f7ec1a (patch)
tree5cb7ed1673f91e982d800a5dc7101979e70b4ace /test
parent887ab0cd350434b67b17c545e5975869bddd8b11 (diff)
parentdc443284c4b5f59a4447797f88730d9fe1bc0b45 (diff)
merge
Diffstat (limited to 'test')
-rw-r--r--test/tc_mauve_web_interface.rb175
-rw-r--r--test/test_mauve.rb1
-rw-r--r--test/th_mauve.rb2
-rw-r--r--test/th_mauve_resolv.rb8
4 files changed, 184 insertions, 2 deletions
diff --git a/test/tc_mauve_web_interface.rb b/test/tc_mauve_web_interface.rb
new file mode 100644
index 0000000..a120c37
--- /dev/null
+++ b/test/tc_mauve_web_interface.rb
@@ -0,0 +1,175 @@
+$:.unshift "../lib"
+
+require 'th_mauve'
+require 'th_mauve_resolv'
+
+require 'mauve/alert'
+require 'mauve/proto'
+require 'mauve/server'
+require 'mauve/configuration'
+require 'mauve/configuration_builder'
+require 'mauve/configuration_builders'
+
+require 'rack/test'
+
+ENV['RACK_ENV'] = 'test'
+
+class WebInterfaceTest < Mauve::UnitTest
+ include Rack::Test::Methods
+ include Mauve
+
+ SESSION_KEY="mauvealert"
+
+ class SessionData
+ def initialize(cookies)
+ @cookies = cookies
+ @data = cookies[WebInterfaceTest::SESSION_KEY]
+ if @data
+ @data = @data.unpack("m*").first
+ @data = Marshal.load(@data)
+ else
+ @data = {}
+ end
+ end
+
+ def [](key)
+ @data[key]
+ end
+
+ def []=(key, value)
+ @data[key] = value
+ session_data = Marshal.dump(@data)
+ session_data = [session_data].pack("m*")
+ @cookies.merge("#{WebInterfaceTest::SESSION_KEY}=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
+ raise "session variable not set" unless @cookies[WebInterfaceTest::SESSION_KEY] == session_data
+ end
+ end
+
+ def session
+ SessionData.new(rack_test_session.instance_variable_get(:@rack_mock_session).cookie_jar)
+ end
+
+ def setup
+ super
+ setup_database
+
+ #
+ # BytemarkAuth test users are:
+ #
+ # test1: ummVRu7qF
+ # test2: POKvBqLT7
+ #
+ config =<<EOF
+server {
+ hostname "localhost"
+ database "sqlite::memory:"
+ initial_sleep 0
+
+ web_interface {
+ document_root "#{File.expand_path(File.join(File.dirname(__FILE__),".."))}"
+ }
+}
+
+person ("test0") {
+ password "#{Digest::SHA1.new.hexdigest("password")}"
+ all { true }
+}
+
+person ("test1") {
+ password "#{Digest::SHA1.new.hexdigest("ummVRu7qF")}"
+ all { true }
+}
+
+source_list "example_hosts", %w(test-1.example.com test-2.example.com www.example.com www2.example.com)
+
+alert_group("test") {
+ includes{ in_source_list?("example_hosts") }
+
+ level LOW
+
+ notify("test1") {
+ every 10.minutes
+ }
+
+}
+
+alert_group("default") {
+ level URGENT
+
+ notify("test1") {
+ every 10.minutes
+ }
+}
+EOF
+
+ Configuration.current = ConfigurationBuilder.parse(config)
+ Server.instance.setup
+ end
+
+ def teardown
+ teardown_database
+ super
+ end
+
+ def app
+ Rack::Session::Cookie.new(WebInterface.new, :key => WebInterfaceTest::SESSION_KEY, :secret => "testing-1234")
+ end
+
+ def test_log_in
+ # Check we get the login page when going to "/" before logging in.
+ get '/'
+ follow_redirect! while last_response.redirect?
+ assert last_response.ok?
+ assert last_response.body.include?("Mauve: Login")
+ assert session['__FLASH__'].empty?
+
+ # Check we can access this page before logging in.
+ get '/alerts'
+ assert(session['__FLASH__'].has_key?(:error),"The flash error wasn't set following forbidden access")
+ follow_redirect! while last_response.redirect?
+ assert_equal(403, last_response.status, "The HTTP status wasn't 403")
+ assert last_response.body.include?("Mauve: Login")
+ assert session['__FLASH__'].empty?
+
+ #
+ # Try to falsify our login.
+ #
+ session['username'] = "test1"
+ get '/alerts'
+ assert(session['__FLASH__'].has_key?(:error),"The flash error wasn't set following forbidden access")
+ follow_redirect! while last_response.redirect?
+ assert_equal(403, last_response.status, "The HTTP status wasn't 403")
+ assert last_response.body.include?("Mauve: Login")
+ assert session['__FLASH__'].empty?
+
+ #
+ # OK login with a bad password
+ #
+ post '/login', :username => 'test1', :password => 'badpassword'
+ assert_equal(401, last_response.status, "A bad login did not produce a 401 response")
+ assert(last_response.body.include?("Mauve: Login"))
+ assert(session['__FLASH__'].has_key?(:error),"The flash error wasn't set")
+
+ post '/login', :username => 'test1', :password => 'ummVRu7qF'
+ follow_redirect! while last_response.redirect?
+ assert last_response.body.include?('Mauve: ')
+
+ get '/logout'
+ follow_redirect! while last_response.redirect?
+ assert last_response.ok?
+ end
+
+ def test_alerts_show_subject
+ post '/login', :username => 'test1', :password => 'ummVRu7qF'
+ follow_redirect! while last_response.redirect?
+ assert last_response.body.include?('Mauve: ')
+
+ a = Alert.new(:source => "www.example.com", :alert_id => "test_raise!")
+ a.raise!
+
+ get '/alerts/raised/subject'
+ end
+
+end
+
+
diff --git a/test/test_mauve.rb b/test/test_mauve.rb
index bdce3aa..ce83c6c 100644
--- a/test/test_mauve.rb
+++ b/test/test_mauve.rb
@@ -24,6 +24,7 @@ tc_mauve_people_list.rb
tc_mauve_person.rb
tc_mauve_source_list.rb
tc_mauve_time.rb
+tc_mauve_web_interface.rb
).each do |s|
require s
end
diff --git a/test/th_mauve.rb b/test/th_mauve.rb
index 7972b9d..2b97e64 100644
--- a/test/th_mauve.rb
+++ b/test/th_mauve.rb
@@ -69,7 +69,7 @@ module Mauve
@logger = Log4r::Logger.new 'Mauve'
@outputter = Mauve::TestOutputter.new("test")
@outputter.formatter = Log4r::PatternFormatter.new( :pattern => "%d %l %m" )
- @outputter.level = Log4r::WARN
+ @outputter.level = ($debug ? Log4r::DEBUG : Log4r::WARN)
@logger.outputters << @outputter
return @logger
end
diff --git a/test/th_mauve_resolv.rb b/test/th_mauve_resolv.rb
index 98b597b..10bdacc 100644
--- a/test/th_mauve_resolv.rb
+++ b/test/th_mauve_resolv.rb
@@ -14,13 +14,19 @@ module Mauve
alias_method :get_ips_for_without_testing, :get_ips_for
def get_ips_for_with_testing(host)
+
lookup = {
"test-1.example.com" => %w(1.2.3.4 2001:1:2:3::4),
"test-2.example.com" => %w(1.2.3.5 2001:1:2:3::5),
"www.example.com" => %w(1.2.3.4),
"www2.example.com" => %w(1.2.3.5 2001:2::2)
}
- lookup[host] || get_ips_for_without_testing(host)
+ if lookup.has_key?(host)
+ self.count += lookup[host].length if $debug
+ lookup[host]
+ else
+ get_ips_for_without_testing(host)
+ end
end
alias_method :get_ips_for, :get_ips_for_with_testing