-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
54 lines (38 loc) · 1.38 KB
/
conftest.py
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
from datetime import datetime, timedelta, timezone
import jwt
import pytest
from fastapi.testclient import TestClient
import typing as t
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from app.main import app
public_certificate_content = None
private_certificate_content = None
with open('public_key.pem', 'r') as file:
public_certificate_content = file.read()
with open('private_key.pem', 'r') as file:
private_certificate_content = file.read()
@pytest.fixture
def client():
"""
Get a TestClient instance that reads/write to the test database.
"""
yield TestClient(app)
@pytest.fixture
def mock_requests_get(mocker):
mock_get = mocker.patch("requests.get")
yield mock_get
@pytest.fixture
def mock_public_key():
return public_certificate_content
@pytest.fixture
def mock_jwt_token():
payload = {
"sub": "1234567890",
"name": "John Doe",
"iat": int((datetime.now(timezone.utc) - timedelta(hours=1)).timestamp()),
"exp": int((datetime.now(timezone.utc) + timedelta(hours=1)).timestamp())
}
certificate = load_pem_private_key(private_certificate_content.encode(), '123456'.encode())
signed_token = jwt.encode(payload, certificate, algorithm="RS256", headers={
"x5u": "https://example.com/cert.pem"})
return signed_token