File: test_pkce.py

package info (click to toggle)
python-pkce 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 84 kB
  • sloc: python: 80; makefile: 7
file content (43 lines) | stat: -rw-r--r-- 1,310 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
import random
import string

import pkce
import pytest


CORRECT_LENGTHS = range(43, 129)
INCORRECT_LENGTHS = list(range(43)) + list(range(129, 200))


@pytest.mark.parametrize('length', CORRECT_LENGTHS)
def test_generate_code_verifier(length):
    verifier1 = pkce.generate_code_verifier(length)
    verifier2 = pkce.generate_code_verifier(length)
    assert len(verifier1) == length
    assert len(verifier2) == length
    assert verifier1 != verifier2


@pytest.mark.parametrize('length', INCORRECT_LENGTHS)
def test_generate_code_verifier_too_short_or_too_long(length):
    with pytest.raises(ValueError):
        pkce.generate_code_verifier(length)


@pytest.mark.parametrize('length', INCORRECT_LENGTHS)
def test_generate_pkce_pair_too_short_or_too_long(length):
    with pytest.raises(ValueError):
        pkce.generate_pkce_pair(length)


def test_get_code_challenge():
    abc = 'abcdefghijklmnopqrstuvwxyz'
    challenge = pkce.get_code_challenge(abc + abc.upper())
    assert challenge == 'OWQpS2ZGE3mNGkd-uK0CEYtI0MVzjEJ2EyAvLtEjtfE'


@pytest.mark.parametrize('length', INCORRECT_LENGTHS)
def test_get_code_challenge_too_short_or_too_long(length):
    verifier = ''.join(random.choices(string.ascii_letters, k=length))
    with pytest.raises(ValueError):
        pkce.get_code_challenge(verifier)