File: test_assertion_session.py

package info (click to toggle)
python-authlib 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,016 kB
  • sloc: python: 26,998; makefile: 53; sh: 14
file content (69 lines) | stat: -rw-r--r-- 1,902 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
import time
from unittest import TestCase
from unittest import mock

import pytest

from authlib.integrations.requests_client import AssertionSession


class AssertionSessionTest(TestCase):
    def setUp(self):
        self.token = {
            "token_type": "Bearer",
            "access_token": "a",
            "refresh_token": "b",
            "expires_in": "3600",
            "expires_at": int(time.time()) + 3600,
        }

    def test_refresh_token(self):
        def verifier(r, **kwargs):
            resp = mock.MagicMock()
            resp.status_code = 200
            if r.url == "https://i.b/token":
                assert "assertion=" in r.body
                resp.json = lambda: self.token
            return resp

        sess = AssertionSession(
            "https://i.b/token",
            issuer="foo",
            subject="foo",
            audience="foo",
            alg="HS256",
            key="secret",
        )
        sess.send = verifier
        sess.get("https://i.b")

        # trigger more case
        now = int(time.time())
        sess = AssertionSession(
            "https://i.b/token",
            issuer="foo",
            subject=None,
            audience="foo",
            issued_at=now,
            expires_at=now + 3600,
            header={"alg": "HS256"},
            key="secret",
            scope="email",
            claims={"test_mode": "true"},
        )
        sess.send = verifier
        sess.get("https://i.b")
        # trigger for branch test case
        sess.get("https://i.b")

    def test_without_alg(self):
        sess = AssertionSession(
            "https://i.b/token",
            grant_type=AssertionSession.JWT_BEARER_GRANT_TYPE,
            issuer="foo",
            subject="foo",
            audience="foo",
            key="secret",
        )
        with pytest.raises(ValueError):
            sess.get("https://i.b")