aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2012-04-25 17:15:49 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2012-04-25 17:15:49 +0100
commite959c0fe4c887154bbe28c31324fef2975cbe467 (patch)
tree3088c7a1f389944d613e57b551b452f7ec83181d /test
parent5fff12fc11cb8b02a44fd40ed78fa9d196f269d7 (diff)
Big update.
* Max acknowledgement time is now specified in the config * Calendar interface improved. * holiday_url no longer used -- replaced by notify_when_on_holiday! * added notify_when_off_sick! * Added ability for the calendar to be queried for a list of bank holdays. * Added ability for Time to be given a list of bank holidays to check against. * PeopleLists can now be a Proc, allowing downloading of lists * Person is no longer a struct * Moved the method_missing bit into ObjectBuilder from various sub classes. * Added tests for the calendar interface * Updated tests in other bits.
Diffstat (limited to 'test')
-rw-r--r--test/tc_mauve_alert.rb3
-rw-r--r--test/tc_mauve_authentication.rb18
-rw-r--r--test/tc_mauve_calendar_interface.rb155
-rw-r--r--test/tc_mauve_notification.rb6
-rw-r--r--test/tc_mauve_people_list.rb25
-rw-r--r--test/tc_mauve_time.rb7
-rw-r--r--test/th_mauve.rb5
7 files changed, 204 insertions, 15 deletions
diff --git a/test/tc_mauve_alert.rb b/test/tc_mauve_alert.rb
index 2b8af6f..3af9075 100644
--- a/test/tc_mauve_alert.rb
+++ b/test/tc_mauve_alert.rb
@@ -95,8 +95,7 @@ EOF
end
def test_acknowledge!
- person = Mauve::Person.new
- person.username = "test-user"
+ person = Mauve::Person.new("test-user")
Server.instance.setup
diff --git a/test/tc_mauve_authentication.rb b/test/tc_mauve_authentication.rb
index d0f2d4f..c364aca 100644
--- a/test/tc_mauve_authentication.rb
+++ b/test/tc_mauve_authentication.rb
@@ -26,9 +26,7 @@ class TcMauveAuthentication < Mauve::UnitTest
def test_default_auth_always_fails
config=<<EOF
-server {
- failed_login_delay 0
-}
+failed_login_delay 0
EOF
Configuration.current = ConfigurationBuilder.parse(config)
@@ -43,9 +41,7 @@ EOF
def test_local_auth
config=<<EOF
-server {
- failed_login_delay 0
-}
+failed_login_delay 0
person ("test") {
password "#{Digest::SHA1.new.hexdigest("password")}"
@@ -70,9 +66,7 @@ EOF
def test_local_auth
config=<<EOF
-server {
- failed_login_delay 0
-}
+failed_login_delay 0
person ("nopass") { }
@@ -97,10 +91,8 @@ EOF
# test2: POKvBqLT7
#
config=<<EOF
-server {
- failed_login_delay 0
- bytemark_auth_url "https://auth.bytemark.co.uk/"
-}
+failed_login_delay 0
+bytemark_auth_url "https://auth.bytemark.co.uk/"
person ("test1") { }
diff --git a/test/tc_mauve_calendar_interface.rb b/test/tc_mauve_calendar_interface.rb
new file mode 100644
index 0000000..ddf4039
--- /dev/null
+++ b/test/tc_mauve_calendar_interface.rb
@@ -0,0 +1,155 @@
+$:.unshift "../lib"
+
+require 'th_mauve'
+require 'mauve/calendar_interface'
+require 'mauve/configuration_builder'
+require 'mauve/configuration'
+require 'webmock'
+#
+# Ugh webmock is annoying.
+WebMock.allow_net_connect!
+
+class TcMauveCalendarInterface < Mauve::UnitTest
+
+ include WebMock::API
+ include Mauve
+
+ def setup
+ WebMock.disable_net_connect!
+ super
+ end
+
+ def teardown
+ WebMock.reset!
+ WebMock.allow_net_connect!
+ super
+ end
+
+ def test_get_attendees
+ attendees = %w(test1 test2)
+ stub_request(:get, "http://localhost/calendar/api/attendees/support_shift/2011-08-01T00:00:00").
+ to_return(:status => 200, :body => YAML.dump(attendees))
+
+ config =<<EOF
+bytemark_calendar_url "http://localhost/calendar"
+EOF
+
+ Configuration.current = ConfigurationBuilder.parse(config)
+
+ assert_equal(attendees, CalendarInterface.get_attendees("support_shift"))
+ end
+
+ def test_is_user_on_holiday?
+ attendees = %w(test1 test2)
+ stub_request(:get, "http://localhost/calendar/api/attendees/staff_holiday/2011-08-01T00:00:00").
+ to_return(:status => 200, :body => YAML.dump(attendees))
+
+
+ config =<<EOF
+bytemark_calendar_url "http://localhost/calendar"
+EOF
+
+ Configuration.current = ConfigurationBuilder.parse(config)
+
+ assert(CalendarInterface.is_user_on_holiday?("test1"))
+ assert(!CalendarInterface.is_user_on_holiday?("test3"))
+ end
+
+ def test_is_user_off_sick?
+ attendees = %w(test1 test2)
+ stub_request(:get, "http://localhost/calendar/api/attendees/sick_period/2011-08-01T00:00:00").
+ to_return(:status => 200, :body => YAML.dump(attendees))
+
+ config =<<EOF
+bytemark_calendar_url "http://localhost/calendar"
+EOF
+
+ Configuration.current = ConfigurationBuilder.parse(config)
+
+ assert(CalendarInterface.is_user_off_sick?("test1"))
+ assert(!CalendarInterface.is_user_off_sick?("test3"))
+ end
+
+ def test_do_get
+ url = "http://localhost/"
+
+ #
+ # This sets up two redirects, followed by the answer (below)
+ #
+ 2.times do |x|
+ next_url = url + "#{x}/"
+ stub_request(:get, url).
+ to_return(:status => 301, :body => nil, :headers => {:location => next_url})
+ url = next_url
+ end
+
+ #
+ # And finally the answer.
+ #
+ stub_request(:get, url).
+ to_return(:status => 200, :body => "OK!", :headers => {})
+
+ #
+ # Now do_get should return "OK!" when the maximum number of redirects is set to two.
+ #
+ result = nil
+ assert_nothing_raised{ result = CalendarInterface.__send__(:do_get, "http://localhost/", 2) }
+ assert_equal("OK!",result)
+
+ #
+ # do_get should return nil when the maximum number of redirects is set to two.
+ #
+ assert_nothing_raised{ result = CalendarInterface.__send__(:do_get, "http://localhost/", 1) }
+ assert_nil(result)
+
+ #
+ # Pop the warning about the redirect off the end of the log.
+ #
+ logger_pop
+ end
+
+ def test_do_get_with_cache
+ url = "http://localhost/"
+
+ #
+ # This stubs the request to give out the time
+ #
+ stub_request(:get, url).
+ to_return( lambda{ {:status => 200, :body => YAML.dump(Time.now), :headers => {}} } )
+
+ #
+ # This reponse should not be cached, the cache-until paramter is "now"
+ #
+ assert_equal(Time.now, CalendarInterface.__send__(:do_get_with_cache, url, Time.now))
+
+ #
+ # Since the last request wasn't cached, the next one should give back
+ # "now", and should be cached for the next 10 seconds.
+ #
+ Timecop.freeze(Time.now + 5)
+ assert_equal(Time.now, CalendarInterface.__send__(:do_get_with_cache, url, Time.now + 10))
+
+ #
+ # This should have been cached from the last query.
+ #
+ Timecop.freeze(Time.now + 5)
+ assert_equal(Time.now - 5, CalendarInterface.__send__(:do_get_with_cache, url, Time.now + 10))
+
+ #
+ # Finally, this should now have expired from the cache.
+ #
+ Timecop.freeze(Time.now + 5)
+ assert_equal(Time.now, CalendarInterface.__send__(:do_get_with_cache, url, Time.now + 10))
+
+ Timecop.freeze(Time.now + 50)
+ cache = CalendarInterface.__send__(:clean_cache)
+ assert(cache.empty?)
+ end
+
+
+end
+
+
+
+
+
diff --git a/test/tc_mauve_notification.rb b/test/tc_mauve_notification.rb
index a447207..d0c2bab 100644
--- a/test/tc_mauve_notification.rb
+++ b/test/tc_mauve_notification.rb
@@ -442,9 +442,15 @@ people_list("testers", %w(test1 test2)) {
alert_group("test") {
level URGENT
+
notify("test1")
notify("test2")
notify("testers")
+
+ notify("testers") {
+ every 60
+ during { hours_in_day (3) }
+ }
}
EOF
diff --git a/test/tc_mauve_people_list.rb b/test/tc_mauve_people_list.rb
index 2d5da6d..fd8bda9 100644
--- a/test/tc_mauve_people_list.rb
+++ b/test/tc_mauve_people_list.rb
@@ -117,4 +117,29 @@ EOF
end
end
+
+ def test_dynamic_people_list
+ #
+ # Allows us to pick up notifications sent.
+ #
+ $sent_notifications = []
+
+ config =<<EOF
+
+person "test1"
+
+person "test2"
+
+#
+# This should oscillate between test1 and test2.
+#
+people_list "testers", lambda { $ans = ($ans == "test1" ? "test2" : "test1") }
+
+EOF
+ Configuration.current = ConfigurationBuilder.parse(config)
+ people_list = Configuration.current.people_lists["testers"]
+ assert_equal([Configuration.current.people["test1"]], people_list.people)
+ assert_equal([Configuration.current.people["test2"]], people_list.people)
+ end
+
end
diff --git a/test/tc_mauve_time.rb b/test/tc_mauve_time.rb
index 7a8fefa..6e5989b 100644
--- a/test/tc_mauve_time.rb
+++ b/test/tc_mauve_time.rb
@@ -35,8 +35,15 @@ class TestMauveTime < Mauve::UnitTest
assert_equal(hour_1, t.in_x_hours(1,"working"))
assert_equal(hour_0, t.in_x_hours(0,"working"))
+ end
+
+ def test_bank_holiday?
+ x = Time.now
+ assert(!x.bank_holiday?)
+ x.bank_holidays << Date.new(x.year, x.month, x.day)
+ assert(x.bank_holiday?)
end
diff --git a/test/th_mauve.rb b/test/th_mauve.rb
index ce29e11..99eaaed 100644
--- a/test/th_mauve.rb
+++ b/test/th_mauve.rb
@@ -55,6 +55,7 @@ module Mauve
def setup
reset_all_singletons
+ reset_mauve_configuration
setup_logger
setup_time
end
@@ -108,6 +109,10 @@ module Mauve
Timecop.return
end
+ def reset_mauve_configuration
+ Mauve::Configuration.current = Mauve::Configuration.new
+ end
+
def reset_all_singletons
Mauve.constants.collect{|const| Mauve.const_get(const)}.each do |klass|
next unless klass.respond_to?(:instance)