Permalink
Browse files

Added new tests, asserting Digest in header

  • Loading branch information...
1 parent 3fa1c0c commit 7d3a32d7593272ca75fded9fab9cb78c08d05ff8 almad committed Dec 16, 2008
Showing with 44 additions and 3 deletions.
  1. +3 −2 djangohttpdigest/digest.py
  2. +40 −0 djangohttpdigest/tests/test_digest.py
  3. +1 −1 testproject/testapi/views.py
@@ -14,8 +14,9 @@ def parse_authorization_header(header):
# digest is marked as part of header and causes problem
# parsing, so remove its
- if header.startswith('Digest '):
- header = header[len('Digest '):]
+ if not header.startswith('Digest '):
+ raise ValueError("Header do not start with Digest")
+ header = header[len('Digest '):]
# Convert the auth params to a dict
items = urllib2.parse_http_list(header)
@@ -12,6 +12,12 @@ class TestDigestor(TestCase):
def setUp(self):
self.digestor = Digestor(realm='testrealm', method='GET', path='/testapi/simpleprotected/')
+ self.auth_string = 'Digest username="rpgpedia", realm="extproject", nonce="1cc6ab869fca869c2c085d78a3729a66", uri="/extproject/project/fc8afe5e-da35-4fe2-a991-7b26c829cde5/user/rpgpedia/salt/", response="69ead146a246cd51bbd076244d2e455b", opaque="ToDoMoveThisToSettings", algorithm="MD5", qop=auth, nc=00000001, cnonce="a84f8e6cfcd50a75"'
+
+ def _assertKeyEquals(self, key, value):
+ parsed_header = self.digestor.parse_authorization_header(self.auth_string)
+ self.assertTrue(parsed_header.has_key(key))
+ self.assertEquals(value, parsed_header[key])
def test_get_digest_challenge(self):
challenge = self.digestor.get_digest_challenge()
@@ -22,6 +28,40 @@ def test_get_digest_challenge(self):
else:
self.fail("Challenge %s does not match urllib2's regexp" % challenge)
+ def test_proper_parsing_username(self):
+ self._assertKeyEquals('username', 'rpgpedia')
+
+ def test_proper_parsing_realm(self):
+ self._assertKeyEquals('realm', 'extproject')
+
+ def test_proper_parsing_nonce(self):
+ self._assertKeyEquals('nonce', '1cc6ab869fca869c2c085d78a3729a66')
+
+ def test_proper_parsing_uri(self):
+ self._assertKeyEquals('uri', '/extproject/project/fc8afe5e-da35-4fe2-a991-7b26c829cde5/user/rpgpedia/salt/')
+
+ def test_proper_parsing_response(self):
+ self._assertKeyEquals('response', '69ead146a246cd51bbd076244d2e455b')
+
+ def test_proper_parsing_opaque(self):
+ self._assertKeyEquals('opaque', 'ToDoMoveThisToSettings')
+
+ def test_proper_parsing_algorithm(self):
+ self._assertKeyEquals('algorithm', 'MD5')
+
+ def test_proper_parsing_qop(self):
+ self._assertKeyEquals('qop', 'auth')
+
+ def test_proper_parsing_nc(self):
+ self._assertKeyEquals('nc', '00000001')
+
+ def test_proper_parsing_cnonce(self):
+ self._assertKeyEquals('cnonce', 'a84f8e6cfcd50a75')
+
+ def test_bad_parsing_missing_digest(self):
+ auth_string = 'username="rpgpedia", realm="extproject", nonce="1cc6ab869fca869c2c085d78a3729a66", uri="/extproject/project/fc8afe5e-da35-4fe2-a991-7b26c829cde5/user/rpgpedia/salt/", response="69ead146a246cd51bbd076244d2e455b", opaque="ToDoMoveThisToSettings", algorithm="MD5", qop=auth, nc=00000001, cnonce="a84f8e6cfcd50a75"'
+ self.assertRaises(ValueError, lambda:self.digestor.parse_authorization_header(auth_string))
+
class TestSimpleDigest(TestCase):
environment = {
@@ -23,4 +23,4 @@ def modelprotected(request):
"""
Example of model-protected site.
"""
- return HttpResponse('')
+ return HttpResponse('')

0 comments on commit 7d3a32d

Please sign in to comment.