From b7b58a52b2f619f34eadc22ba7533bddd864907e Mon Sep 17 00:00:00 2001 From: Angel-Dijoux Date: Tue, 23 Jan 2024 23:32:54 +0100 Subject: [PATCH] fix: 401 error during tests --- .../tests/unit/test_connect_to_onisep_api.py | 51 ++++++++++--------- .../scrap/tests/unit/test_get_token_onisep.py | 32 ++++++++++++ .../formation/scrap/utils/get_onisep_data.py | 20 ++++---- src/models/formation.py | 1 - src/tests/fixtures/service.py | 5 +- 5 files changed, 73 insertions(+), 36 deletions(-) create mode 100644 src/business_logic/formation/scrap/tests/unit/test_get_token_onisep.py diff --git a/src/business_logic/formation/scrap/tests/unit/test_connect_to_onisep_api.py b/src/business_logic/formation/scrap/tests/unit/test_connect_to_onisep_api.py index 31395db..b20792d 100644 --- a/src/business_logic/formation/scrap/tests/unit/test_connect_to_onisep_api.py +++ b/src/business_logic/formation/scrap/tests/unit/test_connect_to_onisep_api.py @@ -1,44 +1,45 @@ from unittest.mock import MagicMock, patch import pytest + from src.business_logic.formation.exceptions import NoOnisepAPIException from src.business_logic.formation.scrap.utils.get_onisep_data import ( HeaderKey, get_raw_data, ) from src.business_logic.formation.scrap.utils.get_onisep_token import BearerToken - from src.constants.http_status_codes import ( HTTP_200_OK, - HTTP_401_UNAUTHORIZED, HTTP_500_INTERNAL_SERVER_ERROR, ) -MOKED_TOKEN: dict[HeaderKey, BearerToken | str] = { - HeaderKey.APPLICATION_ID.value: ("ONISEP_APP_ID"), - HeaderKey.AUTHORIZATION.value: "Bearer mockedTokenForTest", + +HEADERS: dict[HeaderKey, BearerToken | str] = { + HeaderKey.APPLICATION_ID.value: "ONISEP_APP_ID", + HeaderKey.AUTHORIZATION.value: "Bearer TEST", } @pytest.fixture -def mock_onisep_token(): +def mock_onisep_request(): with patch( - "src.business_logic.formation.scrap.utils.get_onisep_data.HEADERS" - ) as mock_token: - yield mock_token + "src.business_logic.formation.scrap.utils.get_onisep_data.requests.get" + ) as mock_requests_get: + yield mock_requests_get @pytest.fixture -def mock_onisep_request(): +def mock_onisep_token(): with patch( - "src.business_logic.formation.scrap.utils.get_onisep_data.requests.get" + "src.business_logic.formation.scrap.utils.get_onisep_data._get_headers" ) as mock_requests_get: yield mock_requests_get -def test_get_onisep_data_successful(mock_onisep_token, mock_onisep_request): +def test_get_onisep_data_successful(mock_onisep_request, mock_onisep_token): # Arrange - mock_onisep_token.return_value = MOKED_TOKEN + mock_onisep_token.status_code = HTTP_200_OK + mock_onisep_token.json.return_value = HEADERS mock_response = MagicMock() mock_response.status_code = HTTP_200_OK @@ -51,22 +52,24 @@ def test_get_onisep_data_successful(mock_onisep_token, mock_onisep_request): def test_get_onisep_data_retry_after_unauthorized( - mock_onisep_token, mock_onisep_request + mock_onisep_request, mock_onisep_token ): # Arrange - mock_onisep_token.return_value = MOKED_TOKEN + unauthorized_response = mock_onisep_request.return_value + unauthorized_response.status_code = 401 - unauthorized_response = MagicMock() - unauthorized_response.status_code = HTTP_401_UNAUTHORIZED - authorized_response = MagicMock() - authorized_response.status_code = HTTP_200_OK - authorized_response.json.return_value = {"total": "5173"} + successful_response = mock_onisep_request.return_value + successful_response.status_code = 200 + successful_response.json.return_value = {"total": "5173"} - # Set up the responses for the two requests - mock_onisep_request.side_effect = [unauthorized_response, authorized_response] + mock_onisep_token.return_value = {"total": "5173"} - # Act - result = get_raw_data("SHR") + with patch( + "src.business_logic.formation.scrap.utils.get_onisep_data.get_token" + ) as mock_get_token: + mock_get_token.return_value = "Bearer TEST" + # Act + result = get_raw_data("SHR") # Assert assert result == {"total": "5173"} diff --git a/src/business_logic/formation/scrap/tests/unit/test_get_token_onisep.py b/src/business_logic/formation/scrap/tests/unit/test_get_token_onisep.py new file mode 100644 index 0000000..21b16f1 --- /dev/null +++ b/src/business_logic/formation/scrap/tests/unit/test_get_token_onisep.py @@ -0,0 +1,32 @@ +import pytest +from unittest.mock import patch +from src.business_logic.formation.exceptions import NoOnisepAPIException + +from src.business_logic.formation.scrap.utils.get_onisep_token import ( + get_token, +) +from src.constants.http_status_codes import HTTP_200_OK, HTTP_404_NOT_FOUND + + +@pytest.fixture +def mock_onisep_token(): + with patch( + "src.business_logic.formation.scrap.utils.get_onisep_token._send_request" + ) as mock_requests_get: + yield mock_requests_get + + +def test_get_token_success(mock_onisep_token): + mock_onisep_token.return_value.status_code = HTTP_200_OK + mock_onisep_token.return_value.json.return_value = {"token": "fake_token"} + + token = get_token() + + assert token == "Bearer fake_token" + + +def test_get_token_failure(mock_onisep_token): + mock_onisep_token.return_value.status_code = HTTP_404_NOT_FOUND + + with pytest.raises(NoOnisepAPIException): + get_token() diff --git a/src/business_logic/formation/scrap/utils/get_onisep_data.py b/src/business_logic/formation/scrap/utils/get_onisep_data.py index 43fdd5a..6082d37 100644 --- a/src/business_logic/formation/scrap/utils/get_onisep_data.py +++ b/src/business_logic/formation/scrap/utils/get_onisep_data.py @@ -21,27 +21,29 @@ class HeaderKey(Enum): ONISEP_URL = "https://api.opendata.onisep.fr/api/1.0/dataset/" -HEADERS: dict[HeaderKey, BearerToken | str] = { - HeaderKey.APPLICATION_ID.value: os.environ.get("ONISEP_APP_ID"), - HeaderKey.AUTHORIZATION.value: get_token(), -} + +def _get_headers() -> dict[HeaderKey, BearerToken | str]: + return { + HeaderKey.APPLICATION_ID.value: os.environ.get("ONISEP_APP_ID"), + HeaderKey.AUTHORIZATION.value: get_token(), + } + DATASET = "5fa591127f501" def get_onisep_data(params: str) -> dict: url = ONISEP_URL + DATASET + params - print("SSSSSSSSSSSSSSSS", HEADERS) - response = requests.get(url, headers=HEADERS) + response = requests.get(url, headers=_get_headers()) if response.status_code == HTTP_200_OK: return response.json() if response.status_code == HTTP_401_UNAUTHORIZED: - HEADERS[HeaderKey.AUTHORIZATION.value] = get_token() - response = requests.get(url, headers=HEADERS) + _get_headers()[HeaderKey.AUTHORIZATION.value] = get_token() + response = requests.get(url, headers=_get_headers()) if response.status_code == HTTP_200_OK: return response.json() raise NoOnisepAPIException( - f"\n status: {response.status_code} \n message : Onisep API is down. \n dataset : {DATASET} \n headers : {HEADERS} " + f"\n status: {response.status_code} \n message : Onisep API is down. \n dataset : {DATASET} \n headers : {_get_headers()} " ) diff --git a/src/models/formation.py b/src/models/formation.py index 4da8427..4211647 100644 --- a/src/models/formation.py +++ b/src/models/formation.py @@ -1,6 +1,5 @@ import uuid -from sqlalchemy import Column, Integer, String, Text import strawberry from src.models.base_model import BaseModel from src.models.helpers.UUIDType import UUIDType diff --git a/src/tests/fixtures/service.py b/src/tests/fixtures/service.py index 61cab1d..a67d1b4 100644 --- a/src/tests/fixtures/service.py +++ b/src/tests/fixtures/service.py @@ -1,12 +1,13 @@ from typing import Generator + import pytest from flask import Flask -from sqlalchemy.orm import Session from flask.testing import FlaskClient -from flask_sqlalchemy import SQLAlchemy from flask_jwt_extended import ( create_access_token, ) +from flask_sqlalchemy import SQLAlchemy +from sqlalchemy.orm import Session from src import create_app from src import db as _db