File: test_proxy_digest_auth.py

package info (click to toggle)
python-requests-toolbelt 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 876 kB
  • sloc: python: 3,653; makefile: 166; sh: 7
file content (115 lines) | stat: -rw-r--r-- 4,092 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
"""Test proxy digest authentication."""

import unittest
try:
    from unittest import mock
except ImportError:
    import mock

import requests
from requests_toolbelt.auth import http_proxy_digest


class TestProxyDigestAuth(unittest.TestCase):
    """Tests for the ProxyDigestAuth class."""

    def setUp(self):
        """Set up variables for each test."""
        self.username = "username"
        self.password = "password"
        self.auth = http_proxy_digest.HTTPProxyDigestAuth(
            self.username, self.password
        )
        self.prepared_request = requests.Request(
            'GET',
            'http://host.org/index.html'
        ).prepare()

    def test_with_existing_nonce(self):
        """Test if it will generate Proxy-Auth header when nonce present.

        Digest authentication's correctness will not be tested here.
        """
        self.auth.last_nonce = "bH3FVAAAAAAg74rL3X8AAI3CyBAAAAAA"
        self.auth.chal = {
            'nonce': self.auth.last_nonce,
            'realm': 'testrealm@host.org',
            'qop': 'auth'
        }

        # prepared_request headers should be clear before calling auth
        assert self.prepared_request.headers.get('Proxy-Authorization') is None
        self.auth(self.prepared_request)
        assert self.prepared_request.headers['Proxy-Authorization'] is not None

    def test_no_challenge(self):
        """Test that a response containing no auth challenge is left alone."""
        connection = MockConnection()
        first_response = connection.make_response(self.prepared_request)
        first_response.status_code = 404

        assert self.auth.last_nonce == ''
        final_response = self.auth.handle_407(first_response)
        headers = final_response.request.headers
        assert self.auth.last_nonce == ''
        assert first_response is final_response
        assert headers.get('Proxy-Authorization') is None

    def test_digest_challenge(self):
        """Test a response with a digest auth challenge causes a new request.

        This ensures that the auth class generates a new request with a
        Proxy-Authorization header.

        Digest authentication's correctness will not be tested here.
        """
        connection = MockConnection()
        first_response = connection.make_response(self.prepared_request)
        first_response.status_code = 407
        first_response.headers['Proxy-Authenticate'] = (
            'Digest'
            ' realm="Fake Realm", nonce="oS6WVgAAAABw698CAAAAAHAk/HUAAAAA",'
            ' qop="auth", stale=false'
        )

        assert self.auth.last_nonce == ''
        final_response = self.auth.handle_407(first_response)
        headers = final_response.request.headers
        assert self.auth.last_nonce != ''
        assert first_response is not final_response
        assert headers.get('Proxy-Authorization') is not None

    def test_ntlm_challenge(self):
        """Test a response without a Digest auth challenge is left alone."""
        connection = MockConnection()
        first_response = connection.make_response(self.prepared_request)
        first_response.status_code = 407
        first_response.headers['Proxy-Authenticate'] = 'NTLM'

        assert self.auth.last_nonce == ''
        final_response = self.auth.handle_407(first_response)
        headers = final_response.request.headers
        assert self.auth.last_nonce == ''
        assert first_response is final_response
        assert headers.get('Proxy-Authorization') is None


class MockConnection(object):
    """Fake connection object."""

    def send(self, request, **kwargs):
        """Mock out the send method."""
        return self.make_response(request)

    def make_response(self, request):
        """Make a response for us based on the request."""
        response = requests.Response()
        response.status_code = 200
        response.request = request
        response.raw = mock.MagicMock()
        response.connection = self
        return response

if __name__ == '__main__':
    unittest.main()