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 import time
31 import unittest
32
33 from django.conf import settings
34 from django.test import TestCase
35 from openid.association import Association as OIDAssociation
36 from openid.store.nonce import SKEW
37
38 from madrona.openid.models import Association, Nonce
39 from madrona.openid.openid_store import DjangoOpenIDStore
40
42
46
48 assoc = OIDAssociation('handle', 'secret', 42, 600, 'HMAC-SHA1')
49 self.store.storeAssociation('server-url', assoc)
50
51 dbassoc = Association.objects.get(
52 server_url='server-url', handle='handle')
53 self.assertEquals(dbassoc.server_url, 'server-url')
54 self.assertEquals(dbassoc.handle, 'handle')
55 self.assertEquals(dbassoc.secret, 'secret'.encode('base-64'))
56 self.assertEquals(dbassoc.issued, 42)
57 self.assertEquals(dbassoc.lifetime, 600)
58 self.assertEquals(dbassoc.assoc_type, 'HMAC-SHA1')
59
61 timestamp = int(time.time())
62 self.store.storeAssociation(
63 'server-url', OIDAssociation('handle', 'secret', timestamp, 600,
64 'HMAC-SHA1'))
65 assoc = self.store.getAssociation('server-url', 'handle')
66 self.assertTrue(isinstance(assoc, OIDAssociation))
67
68 self.assertEquals(assoc.handle, 'handle')
69 self.assertEquals(assoc.secret, 'secret')
70 self.assertEquals(assoc.issued, timestamp)
71 self.assertEquals(assoc.lifetime, 600)
72 self.assertEquals(assoc.assoc_type, 'HMAC-SHA1')
73
75 assoc = self.store.getAssociation('server-url', 'unknown')
76 self.assertEquals(assoc, None)
77
79 lifetime = 600
80 timestamp = int(time.time()) - 2 * lifetime
81 self.store.storeAssociation(
82 'server-url', OIDAssociation('handle', 'secret', timestamp,
83 lifetime, 'HMAC-SHA1'))
84
85
86 assoc = self.store.getAssociation('server-url', 'handle')
87 self.assertEquals(assoc, None)
88 self.assertRaises(Association.DoesNotExist, Association.objects.get,
89 server_url='server-url', handle='handle')
90
92 timestamp = int(time.time())
93
94 self.store.storeAssociation(
95 'server-url', OIDAssociation('handle1', 'secret', timestamp + 1,
96 600, 'HMAC-SHA1'))
97 self.store.storeAssociation(
98 'server-url', OIDAssociation('handle2', 'secret', timestamp,
99 600, 'HMAC-SHA1'))
100
101
102 assoc = self.store.getAssociation('server-url', None)
103 self.assertNotEquals(assoc, None)
104 self.assertEquals(assoc.handle, 'handle1')
105 self.assertEquals(assoc.issued, timestamp + 1)
106
108 self.assertEquals(
109 self.store.removeAssociation('server-url', 'unknown'), False)
110
111 timestamp = int(time.time())
112 self.store.storeAssociation(
113 'server-url', OIDAssociation('handle', 'secret', timestamp, 600,
114 'HMAC-SHA1'))
115 self.assertEquals(
116 self.store.removeAssociation('server-url', 'handle'), True)
117 self.assertEquals(
118 self.store.getAssociation('server-url', 'handle'), None)
119
129
134
139
141 timestamp = time.time()
142 self.assertEqual(
143 self.store.useNonce('server1', timestamp, 'salt1'), True)
144 self.assertEqual(
145 self.store.useNonce('server2', timestamp, 'salt2'), True)
146 self.assertEqual(
147 self.store.useNonce('server3', timestamp, 'salt3'), True)
148 self.assertEqual(Nonce.objects.count(), 3)
149
150 self.assertEqual(
151 self.store.cleanupNonces(_now=timestamp + 2 * SKEW), 3)
152 self.assertEqual(Nonce.objects.count(), 0)
153
154
155 self.assertEqual(
156 self.store.useNonce('server1', timestamp, 'salt1'), True)
157 self.assertEqual(
158 self.store.cleanupNonces(_now=timestamp + 2 * SKEW), 1)
159 self.assertEqual(
160 self.store.cleanupNonces(_now=timestamp + 2 * SKEW), 0)
161
163 timestamp = int(time.time()) - 100
164 self.store.storeAssociation(
165 'server-url', OIDAssociation('handle1', 'secret', timestamp,
166 50, 'HMAC-SHA1'))
167 self.store.storeAssociation(
168 'server-url', OIDAssociation('handle2', 'secret', timestamp,
169 200, 'HMAC-SHA1'))
170
171 self.assertEquals(self.store.cleanupAssociations(), 1)
172
173
174 self.assertNotEqual(self.store.getAssociation('server-url', 'handle2'),
175 None)
176
178 return unittest.TestLoader().loadTestsFromName(__name__)
179