aboutsummaryrefslogtreecommitdiff
path: root/test/tc_mauve_calendar_interface.rb
blob: ddf4039fc34486e41961df70eb662cacacdb5589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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