"""Test ows-account.""" import json from http.client import INTERNAL_SERVER_ERROR, NOT_FOUND, OK import pytest import requests from owsrequest import request from pytest_mock import MockerFixture from video.exceptions import InvalidRequest, SubaccountNotFound from video.models.ows import account from tests.unit.factories.ows.subaccount import SubaccountFactory subaccount_a = SubaccountFactory.build() def test_get_subaccount_success(mocker: MockerFixture) -> None: """Test get_subaccount success.""" ows_account_response = requests.Response() ows_account_response.status_code = OK ows_account_response._content = json.dumps( { "vendor_id": subaccount_a.vendor_id, "subaccount_id": subaccount_a.subaccount_id, "subaccount_name": subaccount_a.name, "description": subaccount_a.description, "country_id": subaccount_a.country_id, } ).encode() mocker.patch.object( request, "process", return_value=ows_account_response, autospec=True ) subaccount = account.get_subaccount(subaccount_a.subaccount_id) assert subaccount == subaccount_a.to_dict() def test_get_subaccount_not_found(mocker: MockerFixture) -> None: """Test get_subaccount raises SubaccountNotFound on 404.""" ows_account_response = requests.Response() ows_account_response.status_code = NOT_FOUND mocker.patch.object( request, "process", return_value=ows_account_response, autospec=True ) with pytest.raises(SubaccountNotFound): account.get_subaccount(subaccount_a.subaccount_id) def test_get_subaccount_error(mocker: MockerFixture) -> None: """Test get_subaccount raises InvalidRequest on non-404 upstream error.""" ows_account_response = requests.Response() ows_account_response.status_code = INTERNAL_SERVER_ERROR mocker.patch.object( request, "process", return_value=ows_account_response, autospec=True ) with pytest.raises(InvalidRequest): account.get_subaccount(subaccount_a.subaccount_id)