summaryrefslogtreecommitdiffstats
path: root/vendor/gems/ruby-openid-2.1.4/test/test_discovery_manager.rb
blob: da966f3844fe33bd19db58ee9baa62248d97ae32 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
require 'test/unit'
require 'openid/consumer/discovery_manager'
require 'openid/extras'

require 'testutil'

module OpenID
  class TestDiscoveredServices < Test::Unit::TestCase
    def setup
      @starting_url = "http://starting.url.com/"
      @yadis_url = "http://starting.url.com/xrds"
      @services = ["bogus", "not_a_service"]

      @disco_services = Consumer::DiscoveredServices.new(@starting_url,
                                                         @yadis_url,
                                                         @services.dup)
    end

    def test_next
      assert_equal(@disco_services.next, @services[0])
      assert_equal(@disco_services.current, @services[0])

      assert_equal(@disco_services.next, @services[1])
      assert_equal(@disco_services.current, @services[1])

      assert_equal(@disco_services.next, nil)
      assert_equal(@disco_services.current, nil)
    end

    def test_for_url
      assert(@disco_services.for_url?(@starting_url))
      assert(@disco_services.for_url?(@yadis_url))

      assert(!@disco_services.for_url?(nil))
      assert(!@disco_services.for_url?("invalid"))
    end

    def test_started
      assert(!@disco_services.started?)
      @disco_services.next
      assert(@disco_services.started?)
      @disco_services.next
      assert(@disco_services.started?) 
      @disco_services.next
      assert(!@disco_services.started?)
    end

    def test_empty
      assert(Consumer::DiscoveredServices.new(nil, nil, []).empty?)

      assert(!@disco_services.empty?)

      @disco_services.next
      @disco_services.next

      assert(@disco_services.started?)
    end
  end

  # I need to be able to test the protected methods; this lets me do
  # that.
  class PassthroughDiscoveryManager < Consumer::DiscoveryManager
    def method_missing(m, *args)
      method(m).call(*args)
    end
  end

  class TestDiscoveryManager < Test::Unit::TestCase
    def setup
      @session = {}
      @url = "http://unittest.com/"
      @key_suffix = "testing"
      @yadis_url = "http://unittest.com/xrds"
      @manager = PassthroughDiscoveryManager.new(@session, @url, @key_suffix)
      @key = @manager.session_key
    end

    def test_construct
      # Make sure the default session key suffix is not nil.
      m = Consumer::DiscoveryManager.new(nil, nil)
      assert(!m.instance_variable_get("@session_key_suffix").nil?)

      m = Consumer::DiscoveryManager.new(nil, nil, "override")
      assert_equal(m.instance_variable_get("@session_key_suffix"), "override")
    end

    def test_get_next_service
      assert_equal(@session[@key], nil)

      next_service = @manager.get_next_service {
        [@yadis_url, ["one", "two", "three"]]
      }

      disco = @session[@key]
      assert_equal(disco.current, "one")
      assert_equal(next_service, "one")
      assert(disco.for_url?(@url))
      assert(disco.for_url?(@yadis_url))

      # The first two calls to get_next_service should return the
      # services in @disco.
      assert_equal(@manager.get_next_service, "two")
      assert_equal(@manager.get_next_service, "three")
      assert_equal(@session[@key], disco)

      # The manager is exhausted and should be deleted and a new one
      # should be created.
      @manager.get_next_service {
        [@yadis_url, ["four"]]
      }

      disco2 = @session[@key]
      assert_equal(disco2.current, "four")

      # create_manager may return a nil manager, in which case the
      # next service should be nil.
      @manager.extend(OpenID::InstanceDefExtension)
      @manager.instance_def(:create_manager) do |yadis_url, services|
        nil
      end

      result = @manager.get_next_service { |url|
        ["unused", []]
      }

      assert_equal(result, nil)
    end

    def test_cleanup
      # With no preexisting manager, cleanup() returns nil.
      assert_equal(@manager.cleanup, nil)

      # With a manager, it returns the manager's current service.
      disco = Consumer::DiscoveredServices.new(@url, @yadis_url, ["one", "two"])

      @session[@key] = disco
      assert_equal(@manager.cleanup, nil)
      assert_equal(@session[@key], nil)

      @session[@key] = disco
      disco.next
      assert_equal(@manager.cleanup, "one")
      assert_equal(@session[@key], nil)

      # The force parameter should be passed through to get_manager
      # and destroy_manager.
      force_value = "yo"
      testcase = self

      m = Consumer::DiscoveredServices.new(nil, nil, ["inner"])
      m.next

      @manager.extend(OpenID::InstanceDefExtension)
      @manager.instance_def(:get_manager) do |force|
        testcase.assert_equal(force, force_value)
        m
      end

      @manager.instance_def(:destroy_manager) do |force|
        testcase.assert_equal(force, force_value)
      end

      assert_equal("inner", @manager.cleanup(force_value))
    end

    def test_get_manager
      # get_manager should always return the loaded manager when
      # forced.
      @session[@key] = "bogus"
      assert_equal("bogus", @manager.get_manager(true))

      # When not forced, only managers for @url should be returned.
      disco = Consumer::DiscoveredServices.new(@url, @yadis_url, ["one"])
      @session[@key] = disco
      assert_equal(@manager.get_manager, disco)

      # Try to get_manager for a manger that doesn't manage @url:
      disco2 = Consumer::DiscoveredServices.new("http://not.this.url.com/",
                                                "http://other.yadis.url/", ["one"])
      @session[@key] = disco2
      assert_equal(@manager.get_manager, nil)
      assert_equal(@manager.get_manager(true), disco2)
    end

    def test_create_manager
      assert(@session[@key].nil?)

      services = ["created", "manager"]
      returned_disco = @manager.create_manager(@yadis_url, services)

      stored_disco = @session[@key]
      assert(stored_disco.for_url?(@yadis_url))
      assert_equal(stored_disco.next, "created")

      assert_equal(stored_disco, returned_disco)

      # Calling create_manager with a preexisting manager should
      # result in StandardError.
      assert_raise(StandardError) {
        @manager.create_manager(@yadis_url, services)
      }

      # create_manager should do nothing (and return nil) if given no
      # services.
      @session[@key] = nil
      result = @manager.create_manager(@yadis_url, [])
      assert(result.nil?)
      assert(@session[@key].nil?)
    end

    class DestroyCalledException < StandardError; end

    def test_destroy_manager
      # destroy_manager should remove the manager from the session,
      # forcibly if necessary.
      valid_disco = Consumer::DiscoveredServices.new(@url, @yadis_url, ["serv"])
      invalid_disco = Consumer::DiscoveredServices.new("http://not.mine.com/",
                                                       "http://different.url.com/",
                                                       ["serv"])

      @session[@key] = valid_disco
      @manager.destroy_manager
      assert(@session[@key].nil?)

      @session[@key] = invalid_disco
      @manager.destroy_manager
      assert_equal(@session[@key], invalid_disco)

      # Force destruction of manager, no matter which URLs it's for.
      @manager.destroy_manager(true)
      assert(@session[@key].nil?)
    end

    def test_session_key
      assert(@manager.session_key.ends_with?(
               @manager.instance_variable_get("@session_key_suffix")))
    end

    def test_store
      thing = "opaque"
      assert(@session[@key].nil?)
      @manager.store(thing)
      assert_equal(@session[@key], thing)
    end

    def test_load
      thing = "opaque"
      @session[@key] = thing
      assert_equal(@manager.load, thing)
    end

    def test_destroy!
      thing = "opaque"
      @manager.store(thing)
      assert_equal(@manager.load, thing)
      @manager.destroy!
      assert(@session[@key].nil?)
      assert(@manager.load.nil?)
    end
  end
end