Merge branch 'develop' into feature/provider_refactoring

This commit is contained in:
Igor Radovanovic
2023-08-02 11:36:55 +02:00
11 changed files with 1337 additions and 28 deletions

View File

@@ -1,8 +1,10 @@
import json
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch
import pytest
import requests
from jose import JWTError, jwt
from openbb_terminal.core.session import hub_model
@@ -24,6 +26,18 @@ TEST_HEADER_TOKEN = [
]
def create_token(delta: int = 0):
"""Create a JWT token with a payload that expires in `delta` days."""
return jwt.encode(
claims={
"some": "claim",
"exp": (datetime.today() + timedelta(days=delta)).timestamp(),
},
key="secret",
algorithm="HS256",
)
@pytest.mark.parametrize("email, password", TEST_EMAIL_PASSWORD)
def test_create_session_success(email, password):
with patch("requests.post") as mock_post:
@@ -62,10 +76,39 @@ def test_create_session_exception(email, password):
assert response is None
# Github actions fails this test so I hard coded the test tokens
# expired: create_token(-10)
# valid: create_token(3600) this will fail in 2033 contact me by then
@pytest.mark.parametrize(
("test_type", "token"),
[
("invalid", "random"),
(
"expired",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoiY2xhaW0iLCJleHAiOjE2ODk1MjgyMTEuMTQ3MjgxfQ.W6ElBpX19SToo3vAwfV7U9S-LdKELXzvoTD6grMVh9I",
),
(
"valid",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoiY2xhaW0iLCJleHAiOjIwMDE0MzIyMDkuNzEzODIxfQ.lSS9OAtwpzqma7xiP3vMDdrDaeCj8vsWKwRC1lSiRFA",
),
],
)
def test_check_token_expiration(test_type, token):
if test_type == "invalid":
with pytest.raises(JWTError):
hub_model.check_token_expiration(token)
elif test_type == "expired":
with pytest.raises(jwt.ExpiredSignatureError):
hub_model.check_token_expiration(token)
elif test_type == "valid":
hub_model.check_token_expiration(token)
@pytest.mark.parametrize("token", [("test_token")])
def test_create_session_from_token_success(token):
def test_create_session_from_token_success(mocker, token):
with patch("requests.post") as mock_post:
mock_post.return_value.json.return_value = TEST_RESPONSE
mocker.patch("openbb_terminal.core.session.hub_model.check_token_expiration")
response = hub_model.create_session_from_token(token)
assert response.json() == TEST_RESPONSE