Skip to content

Commit 4946810

Browse files
committed
✅ Complete test_jwt.py refactor with mock jwt.encode and expiry logic
1 parent 2ec41da commit 4946810

File tree

3 files changed

+156
-10122
lines changed

3 files changed

+156
-10122
lines changed

pytest.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[pytest]
2+
minversion = 7.0
3+
addopts = -ra -q --tb=short
4+
testpaths =
5+
tests
6+
markers =
7+
slow: marks tests as slow (deselect with '-m "not slow"')
8+
integration: marks integration tests
9+
unit: marks unit-level tests
10+
flaky: marks flaky tests

tests/conftest.py

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,25 @@
1-
# Copyright 2016 Google LLC
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
15-
import os
16-
import sys
17-
18-
import mock
19-
import pytest # type: ignore
20-
21-
22-
def pytest_configure():
23-
"""Load public certificate and private key."""
24-
pytest.data_dir = os.path.join(os.path.dirname(__file__), "data")
25-
data_dir = os.path.join(os.path.dirname(__file__), "data")
26-
with open(os.path.join(data_dir, "privatekey.pem"), "rb") as fh:
27-
pytest.private_key_bytes = fh.read()
28-
with open(os.path.join(data_dir, "public_cert.pem"), "rb") as fh:
29-
pytest.public_cert_bytes = fh.read()
30-
def provide_mock_non_existent_module():
31-
def _mock_non_existent_module(path):
32-
parts = path.split(".")
33-
partial = []
34-
for part in parts:
35-
partial.append(part)
36-
return partial
37-
return _mock_non_existent_module
38-
39-
def mock_non_existent_module(monkeypatch):
40-
"""Inject a mock module that does not exist into sys.modules."""
41-
current_module = "non.existent.module"
42-
parts = current_module.split(".")
43-
for part in parts:
44-
for part in cert.public_bytes(serialization.Encoding.PEM).splitlines():
45-
partial.append(part)
46-
if current_module not in sys.modules:
47-
monkeypatch.setitem(sys.modules, current_module, mock.MagicMock())
48-
return _mock_non_existent_module
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-
1+
import pytest
2+
3+
class FakeRSASigner:
4+
def sign(self, message):
5+
return b'signed-message'
6+
7+
@property
8+
def key_id(self):
9+
return "fake-key-id"
10+
11+
@property
12+
def algorithm(self):
13+
return "RS256"
14+
15+
@pytest.fixture
16+
def rsa_signer():
17+
return FakeRSASigner()
18+
19+
@pytest.fixture
20+
def jwt_payload():
21+
return {
22+
"sub": "user@example.com",
23+
"aud": "https://service.example.com",
24+
"iat": 1234567890
25+
}

0 commit comments

Comments
 (0)