File: test_token.py

package info (click to toggle)
python-redis 6.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,432 kB
  • sloc: python: 60,318; sh: 179; makefile: 128
file content (76 lines) | stat: -rw-r--r-- 2,592 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
70
71
72
73
74
75
76
from datetime import datetime, timezone

import pytest
from redis.auth.err import InvalidTokenSchemaErr
from redis.auth.token import JWToken, SimpleToken


class TestToken:
    def test_simple_token(self):
        token = SimpleToken(
            "value",
            (datetime.now(timezone.utc).timestamp() * 1000) + 1000,
            (datetime.now(timezone.utc).timestamp() * 1000),
            {"key": "value"},
        )

        assert token.ttl() == pytest.approx(1000, 10)
        assert token.is_expired() is False
        assert token.try_get("key") == "value"
        assert token.get_value() == "value"
        assert token.get_expires_at_ms() == pytest.approx(
            (datetime.now(timezone.utc).timestamp() * 1000) + 100, 10
        )
        assert token.get_received_at_ms() == pytest.approx(
            (datetime.now(timezone.utc).timestamp() * 1000), 10
        )

        token = SimpleToken(
            "value",
            -1,
            (datetime.now(timezone.utc).timestamp() * 1000),
            {"key": "value"},
        )

        assert token.ttl() == -1
        assert token.is_expired() is False
        assert token.get_expires_at_ms() == -1

    def test_jwt_token(self):
        jwt = pytest.importorskip("jwt")

        token = {
            "exp": datetime.now(timezone.utc).timestamp() + 100,
            "iat": datetime.now(timezone.utc).timestamp(),
            "key": "value",
        }
        encoded = jwt.encode(token, "secret", algorithm="HS256")
        jwt_token = JWToken(encoded)

        assert jwt_token.ttl() == pytest.approx(100000, 10)
        assert jwt_token.is_expired() is False
        assert jwt_token.try_get("key") == "value"
        assert jwt_token.get_value() == encoded
        assert jwt_token.get_expires_at_ms() == pytest.approx(
            (datetime.now(timezone.utc).timestamp() * 1000) + 100000, 10
        )
        assert jwt_token.get_received_at_ms() == pytest.approx(
            (datetime.now(timezone.utc).timestamp() * 1000), 10
        )

        token = {
            "exp": -1,
            "iat": datetime.now(timezone.utc).timestamp(),
            "key": "value",
        }
        encoded = jwt.encode(token, "secret", algorithm="HS256")
        jwt_token = JWToken(encoded)

        assert jwt_token.ttl() == -1
        assert jwt_token.is_expired() is False
        assert jwt_token.get_expires_at_ms() == -1000

        with pytest.raises(InvalidTokenSchemaErr):
            token = {"key": "value"}
            encoded = jwt.encode(token, "secret", algorithm="HS256")
            JWToken(encoded)