Skip to content

Commit 8470bc3

Browse files
cristiprgdopry
authored andcommitted
test: add missing unit test for user_code_generator
1 parent 6ad6d78 commit 8470bc3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

oauth2_provider/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def user_code_generator(user_code_length: int = 8) -> str:
6565
6666
for our function we'll be using a base 32 character set
6767
"""
68+
if user_code_length < 1:
69+
raise ValueError("user_code_length needs to be greater than 0")
6870

6971
# base32 character space
7072
character_space = "0123456789ABCDEFGHIJKLMNOPQRSTUV"

tests/test_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from oauth2_provider import utils
24

35

@@ -25,3 +27,24 @@ def test_jwk_from_pem_caches_jwk():
2527
jwk3 = utils.jwk_from_pem(a_different_tiny_rsa_key)
2628

2729
assert jwk3 is not jwk1
30+
31+
32+
def test_user_code_generator():
33+
# Default argument, 8 characters
34+
user_code = utils.user_code_generator()
35+
assert isinstance(user_code, str)
36+
assert len(user_code) == 8
37+
38+
for character in user_code:
39+
assert character >= "0"
40+
assert character <= "V"
41+
42+
another_user_code = utils.user_code_generator()
43+
assert another_user_code != user_code
44+
45+
shorter_user_code = utils.user_code_generator(user_code_length=1)
46+
assert len(shorter_user_code) == 1
47+
48+
with pytest.raises(ValueError):
49+
utils.user_code_generator(user_code_length=0)
50+
utils.user_code_generator(user_code_length=-1)

0 commit comments

Comments
 (0)