'Test authorization failure for RFC1945/2617 compliance'

def testFail(self):

    auth = current.auth
    f = auth.permission.format
    assertEqual = self.assertEqual
    assertIn = self.assertIn
    try:
        auth.permission.format = 'html'
        auth.s3_impersonate(None)
        try:
            auth.permission.fail()
        except HTTP as e:
            assertEqual(e.status, 303)
            headers = e.headers
            assertIn('Location', headers)
            location = headers['Location'].split('?', 1)[0]
            assertEqual(location, URL(c='default', f='user', args=['login']))
        else:
            raise AssertionError('No    HTTP    status    raised')
        auth.permission.format = 'xml'
        auth.s3_impersonate(None)
        try:
            auth.permission.fail()
        except HTTP as e:
            assertEqual(e.status, 401)
            headers = e.headers
            assertIn('WWW-Authenticate', headers)
        else:
            raise AssertionError('No    HTTP    status    raised')
        auth.permission.format = 'xml'
        auth.s3_impersonate('admin@example.com')
        try:
            auth.permission.fail()
        except HTTP as e:
            assertEqual(e.status, 403)
            headers = e.headers
            self.assertNotIn('WWW-Authenticate', headers)
        else:
            raise AssertionError('No    HTTP    status    raised')
        msg = 's3_logged_in    must    not    raise    HTTP    Auth    challenge'
        auth.permission.format = 'xml'
        auth.s3_impersonate(None)
        try:
            basic = auth.s3_logged_in()
        except HTTP:
            raise AssertionError(msg)
        auth.permission.format = 'html'
        auth.s3_impersonate(None)
        try:
            basic = auth.s3_logged_in()
        except HTTP:
            raise AssertionError(msg)
    finally:
        auth.s3_impersonate(None)
        auth.permission.format = f
