Go to the documentation of this file.00001
00002 from __future__ import unicode_literals
00003 from webtest.debugapp import DebugApp
00004 from tests.compat import unittest
00005 from base64 import b64decode
00006 from webtest.compat import to_bytes
00007
00008 import webtest
00009
00010
00011 class TestAuthorization(unittest.TestCase):
00012
00013 def callFUT(self):
00014 return webtest.TestApp(DebugApp())
00015
00016 def test_basic_authorization(self):
00017 app = self.callFUT()
00018 authorization = ('Basic', ['gawel', 'passwd'])
00019 app.authorization = authorization
00020
00021 self.assertIn('HTTP_AUTHORIZATION', app.extra_environ)
00022 self.assertEquals(app.authorization, authorization)
00023
00024 resp = app.get('/')
00025 resp.mustcontain('HTTP_AUTHORIZATION: Basic Z2F3ZWw6cGFzc3dk')
00026 header = resp.request.environ['HTTP_AUTHORIZATION']
00027 self.assertTrue(header.startswith('Basic '))
00028 authtype, value = header.split(' ')
00029 auth = (authtype,
00030 b64decode(to_bytes(value)).decode('latin1').split(':'))
00031 self.assertEquals(authorization, auth)
00032
00033 app.authorization = None
00034 self.assertNotIn('HTTP_AUTHORIZATION', app.extra_environ)
00035
00036 def test_invalid(self):
00037 app = self.callFUT()
00038 self.assertRaises(ValueError, app.set_authorization, ())
00039 self.assertRaises(ValueError, app.set_authorization, '')
00040 self.assertRaises(ValueError, app.set_authorization, ('Basic', ''))
00041 self.assertRaises(ValueError, app.set_authorization, ('Basic', ()))