aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/requests-oauthlib/tests/test_core.py
blob: 09cd0f021213ec43d566810afae0578402453917 (plain) (blame)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
import requests
import requests_oauthlib
import oauthlib
import os.path
from io import StringIO
import unittest

from unittest import mock


@mock.patch("oauthlib.oauth1.rfc5849.generate_timestamp")
@mock.patch("oauthlib.oauth1.rfc5849.generate_nonce")
class OAuth1Test(unittest.TestCase):
    def testFormEncoded(self, generate_nonce, generate_timestamp):
        """OAuth1 assumes form encoded if content type is not specified."""
        generate_nonce.return_value = "abc"
        generate_timestamp.return_value = "1"
        oauth = requests_oauthlib.OAuth1("client_key")
        headers = {"Content-type": "application/x-www-form-urlencoded"}
        r = requests.Request(
            method="POST",
            url="http://a.b/path?query=retain",
            auth=oauth,
            data="this=really&is=&+form=encoded",
            headers=headers,
        )
        a = r.prepare()

        self.assertEqual(a.url, "http://a.b/path?query=retain")
        self.assertEqual(a.body, b"this=really&is=&+form=encoded")
        self.assertEqual(
            a.headers.get("Content-Type"), b"application/x-www-form-urlencoded"
        )

        # guess content-type
        r = requests.Request(
            method="POST",
            url="http://a.b/path?query=retain",
            auth=oauth,
            data="this=really&is=&+form=encoded",
        )
        b = r.prepare()
        self.assertEqual(b.url, "http://a.b/path?query=retain")
        self.assertEqual(b.body, b"this=really&is=&+form=encoded")
        self.assertEqual(
            b.headers.get("Content-Type"), b"application/x-www-form-urlencoded"
        )

        self.assertEqual(a.headers.get("Authorization"), b.headers.get("Authorization"))

    def testNonFormEncoded(self, generate_nonce, generate_timestamp):
        """OAuth signature only depend on body if it is form encoded."""
        generate_nonce.return_value = "abc"
        generate_timestamp.return_value = "1"
        oauth = requests_oauthlib.OAuth1("client_key")

        r = requests.Request(
            method="POST",
            url="http://a.b/path?query=retain",
            auth=oauth,
            data="this really is not form encoded",
        )
        a = r.prepare()

        r = requests.Request(
            method="POST", url="http://a.b/path?query=retain", auth=oauth
        )
        b = r.prepare()

        self.assertEqual(a.headers.get("Authorization"), b.headers.get("Authorization"))

        r = requests.Request(
            method="POST",
            url="http://a.b/path?query=retain",
            auth=oauth,
            files={"test": StringIO("hello")},
        )
        c = r.prepare()

        self.assertEqual(b.headers.get("Authorization"), c.headers.get("Authorization"))

    @unittest.skip("test uses real http://httpbin.org")
    def testCanPostBinaryData(self, generate_nonce, generate_timestamp):
        """
        Test we can post binary data. Should prevent regression of the
        UnicodeDecodeError issue.
        """
        generate_nonce.return_value = "abc"
        generate_timestamp.return_value = "1"
        oauth = requests_oauthlib.OAuth1("client_key")
        import yatest.common
        dirname = yatest.common.test_source_path()
        fname = os.path.join(dirname, "test.bin")

        with open(fname, "rb") as f:
            r = requests.post(
                "http://httpbin.org/post",
                data={"hi": "there"},
                files={"media": (os.path.basename(f.name), f)},
                headers={"content-type": "application/octet-stream"},
                auth=oauth,
            )
            self.assertEqual(r.status_code, 200)

    @unittest.skip("test uses real http://httpbin.org")
    def test_url_is_native_str(self, generate_nonce, generate_timestamp):
        """
        Test that the URL is always a native string.
        """
        generate_nonce.return_value = "abc"
        generate_timestamp.return_value = "1"
        oauth = requests_oauthlib.OAuth1("client_key")

        r = requests.get("http://httpbin.org/get", auth=oauth)
        self.assertIsInstance(r.request.url, str)

    @unittest.skip("test uses real http://httpbin.org")
    def test_content_type_override(self, generate_nonce, generate_timestamp):
        """
        Content type should only be guessed if none is given.
        """
        generate_nonce.return_value = "abc"
        generate_timestamp.return_value = "1"
        oauth = requests_oauthlib.OAuth1("client_key")
        data = "a"
        r = requests.post("http://httpbin.org/get", data=data, auth=oauth)
        self.assertEqual(
            r.request.headers.get("Content-Type"), b"application/x-www-form-urlencoded"
        )
        r = requests.post(
            "http://httpbin.org/get",
            auth=oauth,
            data=data,
            headers={"Content-type": "application/json"},
        )
        self.assertEqual(r.request.headers.get("Content-Type"), b"application/json")

    def test_register_client_class(self, generate_timestamp, generate_nonce):
        class ClientSubclass(oauthlib.oauth1.Client):
            pass

        self.assertTrue(hasattr(requests_oauthlib.OAuth1, "client_class"))

        self.assertEqual(requests_oauthlib.OAuth1.client_class, oauthlib.oauth1.Client)

        normal = requests_oauthlib.OAuth1("client_key")

        self.assertIsInstance(normal.client, oauthlib.oauth1.Client)
        self.assertNotIsInstance(normal.client, ClientSubclass)

        requests_oauthlib.OAuth1.client_class = ClientSubclass

        self.assertEqual(requests_oauthlib.OAuth1.client_class, ClientSubclass)

        custom = requests_oauthlib.OAuth1("client_key")

        self.assertIsInstance(custom.client, oauthlib.oauth1.Client)
        self.assertIsInstance(custom.client, ClientSubclass)

        overridden = requests_oauthlib.OAuth1(
            "client_key", client_class=oauthlib.oauth1.Client
        )

        self.assertIsInstance(overridden.client, oauthlib.oauth1.Client)
        self.assertNotIsInstance(normal.client, ClientSubclass)