File: test_authenticator.py

package info (click to toggle)
python-asyncprawcore 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,328 kB
  • sloc: python: 2,224; makefile: 4
file content (76 lines) | stat: -rw-r--r-- 3,104 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
"""Test for subclasses of asyncprawcore.auth.BaseAuthenticator class."""

import pytest

import asyncprawcore

from . import UnitTest


class TestTrustedAuthenticator(UnitTest):
    @pytest.fixture
    def trusted_authenticator(self, trusted_authenticator):
        trusted_authenticator.redirect_uri = pytest.placeholders.redirect_uri
        return trusted_authenticator

    def test_authorize_url(self, trusted_authenticator):
        url = trusted_authenticator.authorize_url("permanent", ["identity", "read"], "a_state")
        assert f"client_id={pytest.placeholders.client_id}" in url
        assert "duration=permanent" in url
        assert "response_type=code" in url
        assert "scope=identity+read" in url
        assert "state=a_state" in url

    def test_authorize_url__fail_with_implicit(self, trusted_authenticator):
        with pytest.raises(asyncprawcore.InvalidInvocation):
            trusted_authenticator.authorize_url("temporary", ["identity", "read"], "a_state", implicit=True)

    def test_authorize_url__fail_without_redirect_uri(self, trusted_authenticator):
        trusted_authenticator.redirect_uri = None
        with pytest.raises(asyncprawcore.InvalidInvocation):
            trusted_authenticator.authorize_url(
                "permanent",
                ["identity"],
                "...",
            )


class TestUntrustedAuthenticator(UnitTest):
    @pytest.fixture
    def untrusted_authenticator(self, untrusted_authenticator):
        untrusted_authenticator.redirect_uri = pytest.placeholders.redirect_uri
        return untrusted_authenticator

    def test_authorize_url__code(self, untrusted_authenticator):
        url = untrusted_authenticator.authorize_url("permanent", ["identity", "read"], "a_state")
        assert f"client_id={pytest.placeholders.client_id}" in url
        assert "duration=permanent" in url
        assert "response_type=code" in url
        assert "scope=identity+read" in url
        assert "state=a_state" in url

    def test_authorize_url__fail_with_token_and_permanent(self, untrusted_authenticator):
        with pytest.raises(asyncprawcore.InvalidInvocation):
            untrusted_authenticator.authorize_url(
                "permanent",
                ["identity", "read"],
                "a_state",
                implicit=True,
            )

    def test_authorize_url__fail_without_redirect_uri(self, untrusted_authenticator):
        untrusted_authenticator.redirect_uri = None
        with pytest.raises(asyncprawcore.InvalidInvocation):
            untrusted_authenticator.authorize_url(
                "temporary",
                ["identity"],
                "...",
            )

    def test_authorize_url__token(self, untrusted_authenticator):
        url = untrusted_authenticator.authorize_url("temporary", ["identity", "read"], "a_state", implicit=True)
        assert f"client_id={pytest.placeholders.client_id}" in url
        assert "duration=temporary" in url
        assert "response_type=token" in url
        assert "scope=identity+read" in url
        assert "state=a_state" in url