From c6da482fbdeeda0ab4e78ac71afa25b4d8715496 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Fri, 6 Oct 2023 11:06:49 +0100 Subject: [PATCH] add http basic header to integration tests --- .../platform-api-integration-test.yml | 3 ++ .../crypto/integration/test_crypto_api.py | 12 ++----- .../integration/test_econometrics_api.py | 31 ++++++------------- .../economy/integration/test_economy_api.py | 13 ++------ .../integration/test_fixedincome_api.py | 12 ++----- .../forex/integration/test_forex_api.py | 12 ++----- .../futures/integration/test_futures_api.py | 12 ++----- .../news/integration/test_news_api.py | 12 ++----- .../extensions/qa/integration/test_qa_api.py | 31 ++++++------------- .../stocks/integration/test_stocks_api.py | 12 ++----- .../extensions/ta/integration/test_ta_api.py | 31 ++++++------------- .../extensions/tests/utils/helpers.py | 13 ++++++++ .../utils/integration_tests_api_generator.py | 13 ++------ openbb_platform/tests/conftest.py | 4 ++- 14 files changed, 66 insertions(+), 145 deletions(-) create mode 100644 openbb_platform/extensions/tests/utils/helpers.py diff --git a/.github/workflows/platform-api-integration-test.yml b/.github/workflows/platform-api-integration-test.yml index 2822c967f22..2a13e447a72 100644 --- a/.github/workflows/platform-api-integration-test.yml +++ b/.github/workflows/platform-api-integration-test.yml @@ -15,6 +15,9 @@ jobs: runs-on: ubuntu-latest env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + OPENBB_API_AUTH: true + OPENBB_API_USERNAME: 'USER' + OPENBB_API_PASSWORD: 'PASS' steps: - name: Slack Notification - Starting diff --git a/openbb_platform/extensions/crypto/integration/test_crypto_api.py b/openbb_platform/extensions/crypto/integration/test_crypto_api.py index 8bec440e201..4e6d773702e 100644 --- a/openbb_platform/extensions/crypto/integration/test_crypto_api.py +++ b/openbb_platform/extensions/crypto/integration/test_crypto_api.py @@ -1,20 +1,12 @@ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} @pytest.mark.parametrize( diff --git a/openbb_platform/extensions/econometrics/integration/test_econometrics_api.py b/openbb_platform/extensions/econometrics/integration/test_econometrics_api.py index 482e070fb65..55558ed639a 100644 --- a/openbb_platform/extensions/econometrics/integration/test_econometrics_api.py +++ b/openbb_platform/extensions/econometrics/integration/test_econometrics_api.py @@ -1,37 +1,22 @@ import json import random +from extensions.tests.utils.helpers import get_auth import pytest import requests from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - -def auth_header(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} - - @pytest.fixture(scope="session") def headers(): - h = {} - auth = auth_header() - h.update(auth) - return h + return {"Authorization": get_auth()} -def get_data(menu: str, symbol: str, provider: str): +def get_data(menu: str, symbol: str, provider: str, headers): """Randomly pick a symbol and a provider and get data from the selected menu.""" url = f"http://0.0.0.0:8000/api/v1/{menu}/load?symbol={symbol}&provider={provider}" - result = requests.get(url, headers=auth_header(), timeout=10) + result = requests.get(url, headers=headers, timeout=10) return result.json()["results"] @@ -45,7 +30,9 @@ def get_stocks_data(): symbol = random.choice(["AAPL", "NVDA", "MSFT", "TSLA", "AMZN", "V"]) # noqa: S311 provider = random.choice(["fmp", "intrinio", "polygon", "yfinance"]) # noqa: S311 - data["stocks_data"] = get_data("stocks", symbol=symbol, provider=provider) + data["stocks_data"] = get_data( + "stocks", symbol=symbol, provider=provider, headers=headers + ) return data["stocks_data"] @@ -57,7 +44,9 @@ def get_crypto_data(): symbol = random.choice(["BTC"]) # noqa: S311 provider = random.choice(["fmp"]) # noqa: S311 - data["crypto_data"] = get_data(menu="crypto", symbol=symbol, provider=provider) + data["crypto_data"] = get_data( + menu="crypto", symbol=symbol, provider=provider, headers=headers + ) return data["crypto_data"] diff --git a/openbb_platform/extensions/economy/integration/test_economy_api.py b/openbb_platform/extensions/economy/integration/test_economy_api.py index d7e98bf85ce..20353c98390 100644 --- a/openbb_platform/extensions/economy/integration/test_economy_api.py +++ b/openbb_platform/extensions/economy/integration/test_economy_api.py @@ -1,20 +1,13 @@ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} + return {"Authorization": get_auth()} @pytest.mark.parametrize( diff --git a/openbb_platform/extensions/fixedincome/integration/test_fixedincome_api.py b/openbb_platform/extensions/fixedincome/integration/test_fixedincome_api.py index cb750ba3fa3..4cd9a44484b 100644 --- a/openbb_platform/extensions/fixedincome/integration/test_fixedincome_api.py +++ b/openbb_platform/extensions/fixedincome/integration/test_fixedincome_api.py @@ -1,20 +1,12 @@ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} @pytest.mark.parametrize( diff --git a/openbb_platform/extensions/forex/integration/test_forex_api.py b/openbb_platform/extensions/forex/integration/test_forex_api.py index c1438b736a6..c7327baff89 100644 --- a/openbb_platform/extensions/forex/integration/test_forex_api.py +++ b/openbb_platform/extensions/forex/integration/test_forex_api.py @@ -1,20 +1,12 @@ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} @pytest.mark.parametrize( diff --git a/openbb_platform/extensions/futures/integration/test_futures_api.py b/openbb_platform/extensions/futures/integration/test_futures_api.py index 99fdff6096b..56427dd9ba3 100644 --- a/openbb_platform/extensions/futures/integration/test_futures_api.py +++ b/openbb_platform/extensions/futures/integration/test_futures_api.py @@ -1,20 +1,12 @@ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} @pytest.mark.parametrize( diff --git a/openbb_platform/extensions/news/integration/test_news_api.py b/openbb_platform/extensions/news/integration/test_news_api.py index 63fa4662819..ecd8c5a6a66 100644 --- a/openbb_platform/extensions/news/integration/test_news_api.py +++ b/openbb_platform/extensions/news/integration/test_news_api.py @@ -1,20 +1,12 @@ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} @pytest.mark.parametrize( diff --git a/openbb_platform/extensions/qa/integration/test_qa_api.py b/openbb_platform/extensions/qa/integration/test_qa_api.py index c82441a2bbc..cd5a90714f7 100644 --- a/openbb_platform/extensions/qa/integration/test_qa_api.py +++ b/openbb_platform/extensions/qa/integration/test_qa_api.py @@ -3,35 +3,20 @@ import random import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - -def auth_header(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} - - @pytest.fixture(scope="session") def headers(): - h = {} - auth = auth_header() - h.update(auth) - return h + return {"Authorization": get_auth()} -def get_data(menu: str, symbol: str, provider: str): +def get_data(menu: str, symbol: str, provider: str, headers): """Randomly pick a symbol and a provider and get data from the selected menu.""" url = f"http://0.0.0.0:8000/api/v1/{menu}/load?symbol={symbol}&provider={provider}" - result = requests.get(url, headers=auth_header(), timeout=10) + result = requests.get(url, headers=headers, timeout=10) return result.json()["results"] @@ -45,7 +30,9 @@ def get_stocks_data(): symbol = random.choice(["AAPL", "NVDA", "MSFT", "TSLA", "AMZN", "V"]) # noqa: S311 provider = random.choice(["fmp", "intrinio", "polygon", "yfinance"]) # noqa: S311 - data["stocks_data"] = get_data("stocks", symbol=symbol, provider=provider) + data["stocks_data"] = get_data( + "stocks", symbol=symbol, provider=provider, headers=headers + ) return data["stocks_data"] @@ -57,7 +44,9 @@ def get_crypto_data(): symbol = random.choice(["BTC"]) # noqa: S311 provider = random.choice(["fmp"]) # noqa: S311 - data["crypto_data"] = get_data(menu="crypto", symbol=symbol, provider=provider) + data["crypto_data"] = get_data( + menu="crypto", symbol=symbol, provider=provider, headers=headers + ) return data["crypto_data"] diff --git a/openbb_platform/extensions/stocks/integration/test_stocks_api.py b/openbb_platform/extensions/stocks/integration/test_stocks_api.py index 705c932df12..6a60224d20b 100644 --- a/openbb_platform/extensions/stocks/integration/test_stocks_api.py +++ b/openbb_platform/extensions/stocks/integration/test_stocks_api.py @@ -1,20 +1,12 @@ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} @pytest.mark.parametrize( diff --git a/openbb_platform/extensions/ta/integration/test_ta_api.py b/openbb_platform/extensions/ta/integration/test_ta_api.py index 6d4086405df..cf82a5e0b8a 100644 --- a/openbb_platform/extensions/ta/integration/test_ta_api.py +++ b/openbb_platform/extensions/ta/integration/test_ta_api.py @@ -3,35 +3,20 @@ import random import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - -def auth_header(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} - - @pytest.fixture(scope="session") def headers(): - h = {} - auth = auth_header() - h.update(auth) - return h + return {"Authorization": get_auth()} -def get_data(menu: str, symbol: str, provider: str): +def get_data(menu: str, symbol: str, provider: str, headers): """Randomly pick a symbol and a provider and get data from the selected menu.""" url = f"http://0.0.0.0:8000/api/v1/{menu}/load?symbol={symbol}&provider={provider}" - result = requests.get(url, headers=auth_header(), timeout=10) + result = requests.get(url, headers=headers, timeout=10) return result.json()["results"] @@ -45,7 +30,9 @@ def get_stocks_data(): symbol = random.choice(["AAPL", "NVDA", "MSFT", "TSLA", "AMZN", "V"]) # noqa: S311 provider = random.choice(["fmp", "intrinio", "polygon", "yfinance"]) # noqa: S311 - data["stocks_data"] = get_data("stocks", symbol=symbol, provider=provider) + data["stocks_data"] = get_data( + "stocks", symbol=symbol, provider=provider, headers=headers + ) return data["stocks_data"] @@ -57,7 +44,9 @@ def get_crypto_data(): symbol = random.choice(["BTC"]) # noqa: S311 provider = random.choice(["fmp"]) # noqa: S311 - data["crypto_data"] = get_data(menu="crypto", symbol=symbol, provider=provider) + data["crypto_data"] = get_data( + menu="crypto", symbol=symbol, provider=provider, headers=headers + ) return data["crypto_data"] diff --git a/openbb_platform/extensions/tests/utils/helpers.py b/openbb_platform/extensions/tests/utils/helpers.py new file mode 100644 index 00000000000..b4c307b542a --- /dev/null +++ b/openbb_platform/extensions/tests/utils/helpers.py @@ -0,0 +1,13 @@ +import base64 + +from openbb_core.env import Env + + +def get_auth() -> str: + """Returns the basic auth header.""" + + userpass = f"{Env().API_USERNAME}:{Env().API_PASSWORD}" + userpass_bytes = userpass.encode("ascii") + base64_bytes = base64.b64encode(userpass_bytes) + + return f"Basic {base64_bytes.decode('ascii')}" diff --git a/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py b/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py index f838da743a2..5923fa84dbe 100644 --- a/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py +++ b/openbb_platform/extensions/tests/utils/integration_tests_api_generator.py @@ -24,21 +24,12 @@ def write_init_test_template(http_method: str, path: str): template += """ import pytest import requests +from extensions.tests.utils.helpers import get_auth from openbb_provider.utils.helpers import get_querystring - -def get_token(): - return requests.post( - "http://0.0.0.0:8000/api/v1/account/token", - data={"username": "openbb", "password": "openbb"}, - timeout=10, - ) - - @pytest.fixture(scope="session") def headers(): - access_token = get_token().json()["access_token"] - return {"Authorization": f"Bearer {access_token}"} + return {"Authorization": get_auth()} """ with open(path, "w") as f: diff --git a/openbb_platform/tests/conftest.py b/openbb_platform/tests/conftest.py index 2c3d415748d..7d19c5f6572 100644 --- a/openbb_platform/tests/conftest.py +++ b/openbb_platform/tests/conftest.py @@ -1,2 +1,4 @@ # TODO: See if we can dynamically import the test modules under the openbb_platform -# file structure. +# f + +# TODO: Add global fixture for headers here