Skip to content

Commit

Permalink
fix: 401 error during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-Dijoux committed Jan 23, 2024
1 parent 6eb70e2 commit b7b58a5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 11 additions & 9 deletions src/business_logic/formation/scrap/utils/get_onisep_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()} "
)


Expand Down
1 change: 0 additions & 1 deletion src/models/formation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/tests/fixtures/service.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b7b58a5

Please sign in to comment.