comparison .svn/pristine/2d/2d84dac3d9e6ebc1cf62421fe475b32d70661abb.svn-base @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents
children
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
1 require File.dirname(__FILE__) + '/test_helper'
2 require File.dirname(__FILE__) + '/../lib/open_id_authentication/mem_cache_store'
3
4 # Mock MemCacheStore with MemoryStore for testing
5 class OpenIdAuthentication::MemCacheStore < OpenID::Store::Interface
6 def initialize(*addresses)
7 @connection = ActiveSupport::Cache::MemoryStore.new
8 end
9 end
10
11 class MemCacheStoreTest < Test::Unit::TestCase
12 ALLOWED_HANDLE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
13
14 def setup
15 @store = OpenIdAuthentication::MemCacheStore.new
16 end
17
18 def test_store
19 server_url = "http://www.myopenid.com/openid"
20 assoc = gen_assoc(0)
21
22 # Make sure that a missing association returns no result
23 assert_retrieve(server_url)
24
25 # Check that after storage, getting returns the same result
26 @store.store_association(server_url, assoc)
27 assert_retrieve(server_url, nil, assoc)
28
29 # more than once
30 assert_retrieve(server_url, nil, assoc)
31
32 # Storing more than once has no ill effect
33 @store.store_association(server_url, assoc)
34 assert_retrieve(server_url, nil, assoc)
35
36 # Removing an association that does not exist returns not present
37 assert_remove(server_url, assoc.handle + 'x', false)
38
39 # Removing an association that does not exist returns not present
40 assert_remove(server_url + 'x', assoc.handle, false)
41
42 # Removing an association that is present returns present
43 assert_remove(server_url, assoc.handle, true)
44
45 # but not present on subsequent calls
46 assert_remove(server_url, assoc.handle, false)
47
48 # Put assoc back in the store
49 @store.store_association(server_url, assoc)
50
51 # More recent and expires after assoc
52 assoc2 = gen_assoc(1)
53 @store.store_association(server_url, assoc2)
54
55 # After storing an association with a different handle, but the
56 # same server_url, the handle with the later expiration is returned.
57 assert_retrieve(server_url, nil, assoc2)
58
59 # We can still retrieve the older association
60 assert_retrieve(server_url, assoc.handle, assoc)
61
62 # Plus we can retrieve the association with the later expiration
63 # explicitly
64 assert_retrieve(server_url, assoc2.handle, assoc2)
65
66 # More recent, and expires earlier than assoc2 or assoc. Make sure
67 # that we're picking the one with the latest issued date and not
68 # taking into account the expiration.
69 assoc3 = gen_assoc(2, 100)
70 @store.store_association(server_url, assoc3)
71
72 assert_retrieve(server_url, nil, assoc3)
73 assert_retrieve(server_url, assoc.handle, assoc)
74 assert_retrieve(server_url, assoc2.handle, assoc2)
75 assert_retrieve(server_url, assoc3.handle, assoc3)
76
77 assert_remove(server_url, assoc2.handle, true)
78
79 assert_retrieve(server_url, nil, assoc3)
80 assert_retrieve(server_url, assoc.handle, assoc)
81 assert_retrieve(server_url, assoc2.handle, nil)
82 assert_retrieve(server_url, assoc3.handle, assoc3)
83
84 assert_remove(server_url, assoc2.handle, false)
85 assert_remove(server_url, assoc3.handle, true)
86
87 assert_retrieve(server_url, nil, assoc)
88 assert_retrieve(server_url, assoc.handle, assoc)
89 assert_retrieve(server_url, assoc2.handle, nil)
90 assert_retrieve(server_url, assoc3.handle, nil)
91
92 assert_remove(server_url, assoc2.handle, false)
93 assert_remove(server_url, assoc.handle, true)
94 assert_remove(server_url, assoc3.handle, false)
95
96 assert_retrieve(server_url, nil, nil)
97 assert_retrieve(server_url, assoc.handle, nil)
98 assert_retrieve(server_url, assoc2.handle, nil)
99 assert_retrieve(server_url, assoc3.handle, nil)
100
101 assert_remove(server_url, assoc2.handle, false)
102 assert_remove(server_url, assoc.handle, false)
103 assert_remove(server_url, assoc3.handle, false)
104 end
105
106 def test_nonce
107 server_url = "http://www.myopenid.com/openid"
108
109 [server_url, ''].each do |url|
110 nonce1 = OpenID::Nonce::mk_nonce
111
112 assert_nonce(nonce1, true, url, "#{url}: nonce allowed by default")
113 assert_nonce(nonce1, false, url, "#{url}: nonce not allowed twice")
114 assert_nonce(nonce1, false, url, "#{url}: nonce not allowed third time")
115
116 # old nonces shouldn't pass
117 old_nonce = OpenID::Nonce::mk_nonce(3600)
118 assert_nonce(old_nonce, false, url, "Old nonce #{old_nonce.inspect} passed")
119 end
120 end
121
122 private
123 def gen_assoc(issued, lifetime = 600)
124 secret = OpenID::CryptUtil.random_string(20, nil)
125 handle = OpenID::CryptUtil.random_string(128, ALLOWED_HANDLE)
126 OpenID::Association.new(handle, secret, Time.now + issued, lifetime, 'HMAC-SHA1')
127 end
128
129 def assert_retrieve(url, handle = nil, expected = nil)
130 assoc = @store.get_association(url, handle)
131
132 if expected.nil?
133 assert_nil(assoc)
134 else
135 assert_equal(expected, assoc)
136 assert_equal(expected.handle, assoc.handle)
137 assert_equal(expected.secret, assoc.secret)
138 end
139 end
140
141 def assert_remove(url, handle, expected)
142 present = @store.remove_association(url, handle)
143 assert_equal(expected, present)
144 end
145
146 def assert_nonce(nonce, expected, server_url, msg = "")
147 stamp, salt = OpenID::Nonce::split_nonce(nonce)
148 actual = @store.use_nonce(server_url, stamp, salt)
149 assert_equal(expected, actual, msg)
150 end
151 end