-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modified the Authentication class to use the same token until i…
…t expires and request a new one via the refresh_token
- Loading branch information
1 parent
7ace524
commit 97a53f8
Showing
12 changed files
with
301 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Run Package Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
tags: | ||
- 'v*' | ||
pull_request: | ||
branches: | ||
- main | ||
release: | ||
types: [ created ] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [ 3.7, 3.8, 3.9, '3.10', 3.11 ] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Run tests | ||
env: | ||
TEST_CLIENT_ID: ${{ secrets.TEST_CLIENT_ID }} | ||
TEST_CLIENT_SECRET: ${{ secrets.TEST_CLIENT_SECRET }} | ||
run: | | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ numpy | |
pandas | ||
urllib3 | ||
progressbar2 | ||
pytest | ||
pytest-mock | ||
python-dotenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
|
||
client_id = os.environ.get("DERIBIT_CLIENT_ID") | ||
client_secret = os.environ.get("DERIBIT_CLIENT_SECRET") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import os | ||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from dotenv import load_dotenv | ||
|
||
from deribit_wrapper.authentication import Authentication | ||
from deribit_wrapper.exceptions import DeribitClientWarning | ||
|
||
load_dotenv() | ||
|
||
token_mock_response = { | ||
'access_token': 'new_access_token', | ||
'expires_in': 3600, | ||
'refresh_token': 'new_refresh_token', | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def auth_instance(): | ||
"""Fixture to create an Authentication instance with credentials loaded from environment variables.""" | ||
client_id = os.environ.get("TEST_CLIENT_ID") | ||
client_secret = os.environ.get("TEST_CLIENT_SECRET") | ||
return Authentication(env='test', client_id=client_id, client_secret=client_secret) | ||
|
||
|
||
def test_credentials_set_correctly(auth_instance): | ||
"""Test that client ID and client secret are set correctly from environment variables.""" | ||
assert auth_instance.client_id == os.environ.get("TEST_CLIENT_ID") | ||
assert auth_instance.client_secret == os.environ.get("TEST_CLIENT_SECRET") | ||
|
||
|
||
def test_warning_raised_when_credentials_not_provided(): | ||
"""Test that a warning is raised when credentials are not provided.""" | ||
with pytest.warns(DeribitClientWarning): | ||
Authentication(env='test') | ||
|
||
|
||
@patch('deribit_wrapper.authentication.Authentication._request') | ||
def test_authentication_process(mock_request, auth_instance): | ||
"""Test the authentication process, assuming successful token retrieval.""" | ||
# Mock the _request method to return a mock access token response | ||
mock_request.return_value = token_mock_response | ||
|
||
token = auth_instance.get_new_token() | ||
assert token == 'new_access_token' | ||
mock_request.assert_called_once() | ||
|
||
|
||
@patch('deribit_wrapper.authentication.Authentication._request', | ||
side_effect=Exception("Cannot generate new token without Client ID and Client Secret")) | ||
def test_authentication_failure_leads_to_exception(mock_request, auth_instance): | ||
"""Test that an exception is raised when the authentication request fails.""" | ||
with pytest.raises(Exception) as excinfo: | ||
auth_instance.get_new_token() | ||
assert "Cannot generate new token without Client ID and Client Secret" in str(excinfo.value) | ||
|
||
|
||
def test_get_new_token_retrieves_new_token(): | ||
mock_response = token_mock_response | ||
|
||
with patch('deribit_wrapper.authentication.Authentication._request') as mock_request, \ | ||
patch('deribit_wrapper.authentication.Authentication.create_new_scope', | ||
return_value='session:fixed_session_name') as mock_create_new_scope: | ||
mock_request.return_value = mock_response | ||
|
||
auth = Authentication(env='test', client_id='dummy_id', client_secret='dummy_secret') | ||
new_token = auth.get_new_token() | ||
|
||
assert new_token == 'new_access_token' | ||
mock_request.assert_called_once_with('/public/auth', { | ||
'grant_type': 'client_credentials', | ||
'client_id': 'dummy_id', | ||
'client_secret': 'dummy_secret', | ||
'scope': 'session:fixed_session_name', | ||
}) | ||
mock_create_new_scope.assert_called() | ||
|
||
|
||
class TestDeribitIntegration(TestCase): | ||
def setUp(self): | ||
env = 'test' | ||
client_id = os.environ.get('TEST_CLIENT_ID') | ||
client_secret = os.environ.get('TEST_CLIENT_SECRET') | ||
self.auth = Authentication(env=env, client_id=client_id, client_secret=client_secret) | ||
|
||
def test_get_new_token(self): | ||
token = self.auth.access_token | ||
self.assertIsNotNone(token) | ||
|
||
def test_get_time(self): | ||
time = self.auth.get_time() | ||
self.assertIsInstance(time, int) | ||
|
||
def test_get_api_version(self): | ||
version = self.auth.get_api_version() | ||
self.assertIsInstance(version, str) |
Oops, something went wrong.