aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/requests-oauthlib/tests/test_oauth2_auth.py
blob: accb561ef6c760dadaa1b0585c38c17c979ba4e9 (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
from __future__ import unicode_literals
import unittest

from oauthlib.oauth2 import WebApplicationClient, MobileApplicationClient
from oauthlib.oauth2 import LegacyApplicationClient, BackendApplicationClient
from requests import Request
from requests_oauthlib import OAuth2


class OAuth2AuthTest(unittest.TestCase):
    def setUp(self):
        self.token = {
            "token_type": "Bearer",
            "access_token": "asdfoiw37850234lkjsdfsdf",
            "expires_in": "3600",
        }
        self.client_id = "foo"
        self.clients = [
            WebApplicationClient(self.client_id),
            MobileApplicationClient(self.client_id),
            LegacyApplicationClient(self.client_id),
            BackendApplicationClient(self.client_id),
        ]

    def test_add_token_to_url(self):
        url = "https://example.com/resource?foo=bar"
        new_url = url + "&access_token=" + self.token["access_token"]
        for client in self.clients:
            client.default_token_placement = "query"
            auth = OAuth2(client=client, token=self.token)
            r = Request("GET", url, auth=auth).prepare()
            self.assertEqual(r.url, new_url)

    def test_add_token_to_headers(self):
        token = "Bearer " + self.token["access_token"]
        for client in self.clients:
            auth = OAuth2(client=client, token=self.token)
            r = Request("GET", "https://i.b", auth=auth).prepare()
            self.assertEqual(r.headers["Authorization"], token)

    def test_add_token_to_body(self):
        body = "foo=bar"
        new_body = body + "&access_token=" + self.token["access_token"]
        for client in self.clients:
            client.default_token_placement = "body"
            auth = OAuth2(client=client, token=self.token)
            r = Request("GET", "https://i.b", data=body, auth=auth).prepare()
            self.assertEqual(r.body, new_body)

    def test_add_nonexisting_token(self):
        for client in self.clients:
            auth = OAuth2(client=client)
            r = Request("GET", "https://i.b", auth=auth)
            self.assertRaises(ValueError, r.prepare)