File: test_server.py

package info (click to toggle)
python-oauthlib 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,420 kB
  • sloc: python: 11,599; sh: 16; makefile: 5
file content (113 lines) | stat: -rw-r--r-- 4,463 bytes parent folder | download
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
import json
from unittest import mock

from oauthlib.oauth2.rfc8628.endpoints import DeviceAuthorizationEndpoint
from oauthlib.oauth2.rfc8628.request_validator import RequestValidator

from tests.unittest import TestCase


class DeviceAuthorizationEndpointTest(TestCase):
    def _configure_endpoint(
        self, interval=None, verification_uri_complete=None, user_code_generator=None
    ):
        self.endpoint = DeviceAuthorizationEndpoint(
            request_validator=mock.MagicMock(spec=RequestValidator),
            verification_uri=self.verification_uri,
            interval=interval,
            verification_uri_complete=verification_uri_complete,
            user_code_generator=user_code_generator,
        )

    def setUp(self):
        self.request_validator = mock.MagicMock(spec=RequestValidator)
        self.verification_uri = "http://i.b/l/verify"
        self.uri = "http://i.b/l"
        self.http_method = "POST"
        self.body = "client_id=abc"
        self.headers = {"Content-Type": "application/x-www-form-urlencoded"}
        self._configure_endpoint()

    def response_payload(self):
        return self.uri, self.http_method, self.body, self.headers

    @mock.patch("oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token")
    def test_device_authorization_grant(self, generate_token):
        generate_token.side_effect = ["abc", "def"]
        _, body, status_code = self.endpoint.create_device_authorization_response(
            *self.response_payload()
        )
        expected_payload = {
            "verification_uri": "http://i.b/l/verify",
            "user_code": "abc",
            "device_code": "def",
            "expires_in": 1800,
        }
        self.assertEqual(200, status_code)
        self.assertEqual(body, expected_payload)

    @mock.patch(
        "oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
        lambda: "abc",
    )
    def test_device_authorization_grant_interval(self):
        self._configure_endpoint(interval=5)
        _, body, _ = self.endpoint.create_device_authorization_response(*self.response_payload())
        self.assertEqual(5, body["interval"])

    @mock.patch(
        "oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
        lambda: "abc",
    )
    def test_device_authorization_grant_interval_with_zero(self):
        self._configure_endpoint(interval=0)
        _, body, _ = self.endpoint.create_device_authorization_response(*self.response_payload())
        self.assertEqual(0, body["interval"])

    @mock.patch(
        "oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
        lambda: "abc",
    )
    def test_device_authorization_grant_verify_url_complete_string(self):
        self._configure_endpoint(verification_uri_complete="http://i.l/v?user_code={user_code}")
        _, body, _ = self.endpoint.create_device_authorization_response(*self.response_payload())
        self.assertEqual(
            "http://i.l/v?user_code=abc",
            body["verification_uri_complete"],
        )

    @mock.patch(
        "oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
        lambda: "abc",
    )
    def test_device_authorization_grant_verify_url_complete_callable(self):
        self._configure_endpoint(verification_uri_complete=lambda u: f"http://i.l/v?user_code={u}")
        _, body, _ = self.endpoint.create_device_authorization_response(*self.response_payload())
        self.assertEqual(
            "http://i.l/v?user_code=abc",
            body["verification_uri_complete"],
        )

    @mock.patch(
        "oauthlib.oauth2.rfc8628.endpoints.device_authorization.generate_token",
        lambda: "abc",
    )
    def test_device_authorization_grant_user_gode_generator(self):
        def user_code():
            """
            A friendly user code the device can display and the user
            can type in. It's up to the device how
            this code should be displayed. e.g 123-456
            """
            return "123456"

        self._configure_endpoint(
            verification_uri_complete=lambda u: f"http://i.l/v?user_code={u}",
            user_code_generator=user_code,
        )

        _, body, _ = self.endpoint.create_device_authorization_response(*self.response_payload())
        self.assertEqual(
            "http://i.l/v?user_code=123456",
            body["verification_uri_complete"],
        )