"""Test for ows_product model.""" from unittest.mock import MagicMock from owsrequest import request import pytest from asset_file_details.models import ows_product @pytest.fixture def test_upc(): """Return upc string for testing.""" return '012345678912' @pytest.fixture def test_correlation_id(): """Return upc string for testing.""" return '12346789' def test_check_ownership_vendor_is_owner( mocker, test_upc, test_correlation_id): """Test that the call is successful when given vendor owns the product.""" vendor_id = 7123 mocker.patch.object( request, 'post', return_value=MagicMock(status_code=200)) ownership_response = ows_product.check_ownership( test_upc, 'vendor', vendor_id, test_correlation_id) request.post.assert_not_called assert ownership_response.status == 200 def test_check_ownership_vendor_is_not_owner( mocker, test_upc, test_correlation_id): """Test that the call fails when given vendor does not own the product.""" vendor_id = 7123 mocker.patch.object( request, 'post', return_value=MagicMock(status_code=403)) ownership_response = ows_product.check_ownership( test_upc, 'vendor', vendor_id, test_correlation_id) assert ownership_response.status == 403 assert ownership_response.errors == { 'code': 'authorization_error', 'message': 'Product is not owned by account'} def test_check_ownership_subaccount_is_owner( mocker, test_upc, test_correlation_id): """Test that the call is successful when given subaccount owns product.""" subaccount_id = 4128 mocker.patch.object( request, 'post', return_value=MagicMock(status_code=200)) ownership_response = ows_product.check_ownership( test_upc, 'subaccount', subaccount_id, test_correlation_id) request.post.assert_not_called assert ownership_response.status == 200 def test_check_ownership_subaccount_is_not_owner( mocker, test_upc, test_correlation_id): """Test that the call fails when given subaccount does not own product.""" subaccount_id = 4128 mocker.patch.object( request, 'post', return_value=MagicMock(status_code=403)) ownership_response = ows_product.check_ownership( test_upc, 'subaccount', subaccount_id, test_correlation_id) assert ownership_response.status == 403 assert ownership_response.errors == { 'code': 'authorization_error', 'message': 'Product is not owned by account'} def test_check_ownership_invalid_account_type( mocker, test_upc, test_correlation_id): """Test that the call fails when given an unrecognized account type.""" mocker.spy(request, 'post') ownership_response = ows_product.check_ownership( test_upc, 'subcoconut', 13, test_correlation_id) request.post.assert_not_called assert ownership_response.status == 400 assert ownership_response.errors == { 'code': 'bad_request', 'message': 'Invalid account type'}