"""Tests for OwsMoneyhub client.""" import datetime from unittest.mock import patch import httpx from owsclient.test import OwsClientMock import pytest from src.connectors.ows_moneyhub import OwsMoneyhub from src.constants.constants import DEFAULT_HEADERS from src.constants.constants import FileType from src.utils.custom_dataclasses import InternalAttachmentPayload from src.utils.exceptions import OwsMoneyhubException FAKE_SERVICE = 'fake-service' ACCOUNT_ID = 1 STATEMENT_PERIOD_ID = 1 @patch('src.connectors.ows_moneyhub.OwsMoneyhub.post') def test_create_internal_statement_attachment(mock_moneyhub): """Testing creating internal attachment requests.""" path = f'/statement-attachment/account/{ACCOUNT_ID}/statement-period/{STATEMENT_PERIOD_ID}/internal-attachment' # noqa: E501 OwsMoneyhub._service = FAKE_SERVICE payload = InternalAttachmentPayload( file_type=FileType.CSV, file_location='fake-location', upload_date=datetime.datetime(2024, 12, 12), ) OwsMoneyhub.create_internal_statement_attachment(ACCOUNT_ID, STATEMENT_PERIOD_ID, payload) mock_moneyhub.assert_called_with(path, payload.__dict__) def test_post(ows_client_mock: OwsClientMock): """Testing POSt requests.""" path = f'/statement-attachment/account/{ACCOUNT_ID}/statement-period/{STATEMENT_PERIOD_ID}/internal-attachment' # noqa: E501 OwsMoneyhub._service = FAKE_SERVICE payload = InternalAttachmentPayload( file_type=FileType.CSV, file_location='fake-location', upload_date=datetime.datetime(2024, 12, 12), ) ows_client_mock.post(FAKE_SERVICE, path=path).mock( return_value=httpx.Response(status_code=200, json=payload) ) res = OwsMoneyhub.post(path, payload) assert res == payload def test_request_error(ows_client_mock: OwsClientMock): """Test a request that fails.""" path = f'/statement-attachment/account/{ACCOUNT_ID}/statement-period/{STATEMENT_PERIOD_ID}/internal-attachment' # noqa: E501 text = 'Forbidden: Unauthorized access' message = f'{FAKE_SERVICE} error: 401 response from post {path}: {text}' OwsMoneyhub._service = FAKE_SERVICE ows_client_mock.post(FAKE_SERVICE, path=path).mock( return_value=httpx.Response(status_code=401, text=text) ) with pytest.raises(OwsMoneyhubException) as excinfo: OwsMoneyhub.post(path, body={}) assert excinfo.value.message == message @patch('src.connectors.ows_moneyhub.OWS_CLIENT_TOKEN', new=None) def test_get_request_headers_without_token(): """Test getting request headers without a token set.""" result = OwsMoneyhub._get_request_headers() assert result == DEFAULT_HEADERS @patch('src.connectors.ows_moneyhub.OWS_CLIENT_TOKEN', new='abcdef') def test_get_request_headers_with_token(): """Test getting request headers with a token set.""" expected = DEFAULT_HEADERS | {'authorization': 'Bearer abcdef'} result = OwsMoneyhub._get_request_headers() assert result == expected