"""Tests for check_ownership logic.""" import flexmock from oto import response import pytest from assets.logic import ownership from assets.models import ows_product from assets.models import ows_track @pytest.fixture def correct_ownership_data(): """Fixture with correct check_ownership params.""" return { 'account_type': 'vendor', 'account_id': 123, 'product_id': 321 } @pytest.fixture def correct_track_ownership_data(): """Fixture with correct check_track_ownership ownership params.""" return { 'track_id': 111, 'account_type': 'vendor', 'account_id': 123 } def test_check_ownership_success(correct_ownership_data): """Test for successful check_ownership call.""" (flexmock(ows_product) .should_receive('check_ownership') .and_return(response.Response())) result = ownership.check_ownership(**correct_ownership_data) assert result assert result.status == 200 def test_check_ownership_fail(correct_ownership_data): """Test for check_ownership fail.""" (flexmock(ows_product) .should_receive('check_ownership') .and_return(response.create_error_response( 'not_owned_code', 'Not owned', status=403))) result = ownership.check_ownership(**correct_ownership_data) assert not result assert result.status == 403 def test_check_track_ownership_success(correct_track_ownership_data): """Test for check track ownership.""" expected_product_id = 10 (flexmock(ows_track) .should_receive('get_track_by_id') .with_args(correct_track_ownership_data['track_id']) .and_return(response.Response({ 'product_id': expected_product_id})) .once()) (flexmock(ows_product) .should_receive('check_ownership') .with_args( correct_track_ownership_data['account_type'], correct_track_ownership_data['account_id'], expected_product_id) .and_return(response.Response()) .once()) result = ownership.check_track_ownership(**correct_track_ownership_data) assert result assert result.status == 200 def test_check_track_ownership_fail(correct_track_ownership_data): """Test for check track ownership fail.""" expected_product_id = 10 (flexmock(ows_track) .should_receive('get_track_by_id') .with_args(correct_track_ownership_data['track_id']) .and_return(response.Response({ 'product_id': expected_product_id})) .once()) (flexmock(ows_product) .should_receive('check_ownership') .with_args( correct_track_ownership_data['account_type'], correct_track_ownership_data['account_id'], expected_product_id) .and_return(response.create_error_response( 'not_owned_code', 'Not owned', status=403)) .once()) result = ownership.check_track_ownership(**correct_track_ownership_data) assert not result assert result.status == 403 def test_check_track_ownership_error(correct_track_ownership_data): """Test for check track ownership with error in ows_track.""" (flexmock(ows_track) .should_receive('get_track_by_id') .with_args(correct_track_ownership_data['track_id']) .and_return(response.create_not_found_response()) .once()) (flexmock(ows_product) .should_receive('check_ownership') .never()) result = ownership.check_track_ownership(**correct_track_ownership_data) assert not result assert result.status == 404