Package madrona :: Package openid :: Package tests :: Module test_store
[hide private]

Source Code for Module madrona.openid.tests.test_store

  1  #!/usr/bin/env python 
  2  # django-openid-auth -  OpenID integration for django.contrib.auth 
  3  # 
  4  # Copyright (C) 2009 Canonical Ltd. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  # * Redistributions of source code must retain the above copyright 
 11  # notice, this list of conditions and the following disclaimer. 
 12  # 
 13  # * Redistributions in binary form must reproduce the above copyright 
 14  # notice, this list of conditions and the following disclaimer in the 
 15  # documentation and/or other materials provided with the distribution. 
 16  # 
 17  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 18  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 19  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 20  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 21  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 22  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 23  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 24  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 25  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 26  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 27  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 28  # POSSIBILITY OF SUCH DAMAGE. 
 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   
41 -class OpenIDStoreTests(TestCase):
42
43 - def setUp(self):
44 super(OpenIDStoreTests, self).setUp() 45 self.store = DjangoOpenIDStore()
46
47 - def test_storeAssociation(self):
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
60 - def test_getAssociation(self):
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 # The association is not returned, and is removed from the database. 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 # The newest handle is returned. 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
107 - def test_removeAssociation(self):
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
120 - def test_useNonce(self):
121 timestamp = time.time() 122 # The nonce can only be used once. 123 self.assertEqual( 124 self.store.useNonce('server-url', timestamp, 'salt'), True) 125 self.assertEqual( 126 self.store.useNonce('server-url', timestamp, 'salt'), False) 127 self.assertEqual( 128 self.store.useNonce('server-url', timestamp, 'salt'), False)
129
130 - def test_useNonce_expired(self):
131 timestamp = time.time() - 2 * SKEW 132 self.assertEqual( 133 self.store.useNonce('server-url', timestamp, 'salt'), False)
134
135 - def test_useNonce_future(self):
136 timestamp = time.time() + 2 * SKEW 137 self.assertEqual( 138 self.store.useNonce('server-url', timestamp, 'salt'), False)
139
140 - def test_cleanupNonces(self):
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 # The nonces have now been cleared: 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
162 - def test_cleanupAssociations(self):
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 # The second (non-expired) association is left behind. 174 self.assertNotEqual(self.store.getAssociation('server-url', 'handle2'), 175 None)
176
177 -def suite():
178 return unittest.TestLoader().loadTestsFromName(__name__)
179