"""Test entity models.""" from unittest.mock import MagicMock from owsrequest import request from marketing.models import entities def test_product_ownership(monkeypatch): """Test product ownership.""" http_response = MagicMock() http_response.status_code = 403 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) product_id = 2000 account_type = 'vendor' account_id = 5598 response = entities.is_product_owner(product_id, account_type, account_id) assert response.status == 403 request.head.assert_called_with( entities.PRODUCT_SERVICE, entities.PRODUCT_OWNERSHIP_RESOURCE.format( product_id=product_id, account_type=account_type, account_id=account_id)) def test_project_ownership(monkeypatch): """Test project ownership.""" http_response = MagicMock() http_response.status_code = 403 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) project_id = 1234 account_type = 'vendor' account_id = 5598 response = entities.is_project_owner(project_id, account_type, account_id) assert response.status == 403 request.head.assert_called_with( entities.PROJECT_SERVICE, '/ownership/{account_type}/{account_id}/project/{project_id}'.format( account_type=account_type, account_id=account_id, project_id=project_id)) def test_get_product_by_id(monkeypatch): """Test getting a product by its id.""" http_response = MagicMock() http_response.status_code = 200 http_response.json.return_value = {'product_id': 1} monkeypatch.setattr(request, 'get', MagicMock(return_value=http_response)) response = entities.get_product_by_id(1) assert response assert response.message is http_response.json.return_value def test_get_nonexistent_product_by_id(monkeypatch): """Test getting a nonexistent product by its id.""" http_response = MagicMock() http_response.status_code = 404 http_response.json.return_value = {'code': 'not_found'} product_id = 1 monkeypatch.setattr(request, 'get', MagicMock(return_value=http_response)) response = entities.get_product_by_id(product_id) assert not response assert not response.message assert response.errors is http_response.json.return_value request.get.assert_called_with( entities.PRODUCT_SERVICE, entities.PRODUCT_GETTER_RESOURCE.format(product_id=product_id)) def test_get_project_by_id(monkeypatch): """Test getting a project by its id.""" http_response = MagicMock() http_response.status_code = 200 http_response.json.return_value = {'project_id': 1} monkeypatch.setattr(request, 'get', MagicMock(return_value=http_response)) response = entities.get_project_by_id(1) assert response assert response.message is http_response.json.return_value def test_get_nonexistent_project_by_id(monkeypatch): """Test getting a nonexistent project by its id.""" http_response = MagicMock() http_response.status_code = 404 http_response.json.return_value = {'code': 'not_found'} project_id = 1 monkeypatch.setattr(request, 'get', MagicMock(return_value=http_response)) response = entities.get_project_by_id(project_id) assert not response assert not response.message assert response.errors is http_response.json.return_value request.get.assert_called_with( entities.PROJECT_SERVICE, entities.PROJECT_GETTER_RESOURCE.format(project_id=project_id)) def test_normalized_entity_type(): """Test getting a normalized entity type.""" for entity_type in entities.ENTITY_TYPES: assert ( entities.get_normalized_entity_type(entity_type) == entity_type) for entity_match, entity_type in entities.NORMALIZED_ENTITY_TYPES.items(): assert ( entities.get_normalized_entity_type(entity_match) == entity_type)