Skip to content

Commit cc0c634

Browse files
committed
More tests
1 parent d34228a commit cc0c634

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

tests/test_cli.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import io
22
import json
33
import os
4-
from io import StringIO
5-
from unittest import mock
64

75
import pytest
86
from cryptography.exceptions import InvalidSignature
@@ -13,14 +11,11 @@
1311
from jwt_rsa.cli import parser
1412
from jwt_rsa.key_tester import main as verify
1513
from jwt_rsa.keygen import main as keygen
16-
from jwt_rsa.rsa import (
17-
generate_rsa, load_private_key, load_public_key, rsa_to_jwk,
18-
)
14+
from jwt_rsa.rsa import load_private_key, load_public_key
1915

2016

2117
def test_rsa_keygen(capsys):
22-
with mock.patch("sys.argv", ["jwt-rsa", "keygen", "--raw", "-o", "jwk"]):
23-
keygen(parser.parse_args())
18+
keygen(parser.parse_args(["keygen", "--raw", "-o", "jwk"]))
2419

2520
stdout, stderr = capsys.readouterr()
2621

@@ -57,8 +52,7 @@ def test_rsa_keygen(capsys):
5752

5853

5954
def test_pem_format(capsys):
60-
with mock.patch("sys.argv", ["jwt-rsa", "keygen", "-o", "pem"]):
61-
keygen(parser.parse_args())
55+
keygen(parser.parse_args(["keygen", "-o", "pem"]))
6256

6357
stdout, stderr = capsys.readouterr()
6458

@@ -160,29 +154,27 @@ def test_keygen_public_key_auto_naming(capsys, tmp_path):
160154
assert private_content != private_path.read_text()
161155

162156

163-
@pytest.mark.skip(reason="TODO")
164-
def test_rsa_verify(capsys):
165-
with mock.patch("sys.argv", ["jwt-rsa", "keygen"]):
166-
keygen(parser.parse_args())
157+
@pytest.mark.parametrize("fmt", ["jwk", "pem", "base64"])
158+
def test_rsa_verify(fmt, capsys, tmp_path):
159+
private_path = tmp_path / "private"
160+
public_path = tmp_path / "public"
167161

162+
keygen(parser.parse_args(["keygen", "-o", fmt, "-K", str(private_path), "-k", str(public_path)]))
163+
verify(parser.parse_args(["testkey", "-K", str(private_path), "-k", str(public_path)]))
168164
stdout, stderr = capsys.readouterr()
165+
assert "Signing OK" in stderr
166+
assert "Verifying OK" in stderr
169167

170-
with mock.patch("sys.stdin", StringIO(stdout)):
171-
verify(parser.parse_args())
172168

173-
174-
@pytest.mark.skip(reason="TODO")
175-
def test_rsa_verify_bad_key():
176-
private1, public1 = generate_rsa()
177-
private2, public2 = generate_rsa()
178-
179-
data = json.dumps(
180-
{
181-
"private_jwk": rsa_to_jwk(private1),
182-
"public_jwk": rsa_to_jwk(public2),
183-
}, indent=" ", sort_keys=True,
169+
@pytest.mark.parametrize("fmt", ["jwk", "pem", "base64"])
170+
def test_rsa_verify_bad_key(fmt, capsys, tmp_path):
171+
keys = (
172+
(tmp_path / "private1", tmp_path / "public1"),
173+
(tmp_path / "private2", tmp_path / "public2"),
184174
)
185175

186-
with mock.patch("sys.stdin", StringIO(data)):
187-
with pytest.raises(InvalidSignature):
188-
verify(parser.parse_args())
176+
for private_path, public_path in keys:
177+
keygen(parser.parse_args(["keygen", "-o", fmt, "-K", str(private_path), "-k", str(public_path)]))
178+
179+
with pytest.raises(InvalidSignature):
180+
verify(parser.parse_args(["testkey", "-K", str(keys[0][0]), "-k", str(keys[1][1])]))

tests/test_rsa.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_decode_only_ability():
116116
jwt = JWT(public)
117117
assert "foo" in jwt.decode(token)
118118

119-
with pytest.raises(RuntimeError):
119+
with pytest.raises(AttributeError):
120120
jwt.encode(foo=None)
121121

122122

@@ -128,7 +128,7 @@ def test_jwt_init():
128128

129129
assert JWT(public)
130130

131-
with pytest.raises(ValueError):
131+
with pytest.raises(TypeError):
132132
JWT(None)
133133

134134

0 commit comments

Comments
 (0)