"""Test PATCH /v2/vendor//service_tier.""" import time from typing import Any import pytest import requests from tests.integration import config UNTIERED_UUID = 'c069ab15-9370-4bd6-8ad1-895a64ae2ad8' DIY_TIER_1_UUID = '5f2bd4fc-df94-4f35-97d3-ef23f8573279' @pytest.fixture() def theorchard_vendor_for_service_tier() -> dict[str, Any]: return { 'is_distributor': False, 'name': f'ows-account integration-test service-tier {time.time()}', 'owner': 'odd', 'company_brand': 'theorchard', 'service_tier_uuid': UNTIERED_UUID, 'payment_currency': 'USD', } @pytest.fixture() def created_vendor_uuid( bearer_token_contract_admin_theorchard: str, theorchard_vendor_for_service_tier: dict[str, Any], ) -> str: """Create a vendor and return its UUID.""" headers = {'Authorization': f'Bearer {bearer_token_contract_admin_theorchard}'} resp = requests.post( f'{config.QA_BASE_URL}/v2/vendors', json=theorchard_vendor_for_service_tier, headers=headers, ) assert resp.status_code == 200, resp.text return resp.json()['vendor_uuid'] def test_update_service_tier_success( bearer_token_account_admin_theorchard: str, created_vendor_uuid: str, ) -> None: """PATCH /v2/vendor//service_tier replaces the tier.""" headers = {'Authorization': f'Bearer {bearer_token_account_admin_theorchard}'} resp = requests.patch( f'{config.QA_BASE_URL}/v2/vendor/{created_vendor_uuid}/service_tier', json={'service_tier_uuid': DIY_TIER_1_UUID}, headers=headers, ) assert resp.status_code == 200, resp.text assert resp.json()['vendor_uuid'] == created_vendor_uuid @pytest.mark.parametrize( 'payload,expected_status_code', [ pytest.param({}, 400, id='missing service_tier_uuid'), pytest.param({'service_tier_uuid': None}, 400, id='null service_tier_uuid'), pytest.param( {'service_tier_uuid': '00000000-0000-0000-0000-000000000000'}, 400, id='unknown service_tier_uuid', ), ], ) def test_update_service_tier_validation_errors( bearer_token_account_admin_theorchard: str, created_vendor_uuid: str, payload: dict[str, Any], expected_status_code: int, ) -> None: """Bad payloads return 400.""" headers = {'Authorization': f'Bearer {bearer_token_account_admin_theorchard}'} resp = requests.patch( f'{config.QA_BASE_URL}/v2/vendor/{created_vendor_uuid}/service_tier', json=payload, headers=headers, ) assert resp.status_code == expected_status_code, resp.text def test_update_service_tier_forbidden_contract_admin( bearer_token_contract_admin_theorchard: str, created_vendor_uuid: str, ) -> None: """Contract admin lacks update:service_tier on account.""" headers = {'Authorization': f'Bearer {bearer_token_contract_admin_theorchard}'} resp = requests.patch( f'{config.QA_BASE_URL}/v2/vendor/{created_vendor_uuid}/service_tier', json={'service_tier_uuid': DIY_TIER_1_UUID}, headers=headers, ) assert resp.status_code == 403, resp.text assert resp.json() == {'code': 'authorization_error', 'message': 'Forbidden'} def test_update_service_tier_no_auth() -> None: """No auth header returns 401.""" resp = requests.patch( f'{config.QA_BASE_URL}/v2/vendor/fff741c2-6def-4493-bfdf-c2bcb1128e02/service_tier', json={'service_tier_uuid': DIY_TIER_1_UUID}, ) assert resp.status_code == 401, resp.text