"""Functional tests for terms and conditions endpoints.""" from unittest.mock import patch from collaborator.constants import error from tests.testutils import db, mock_auth @db.test_schema_default_seed def test_get_terms_and_conditions(auth_client, mocker, mock_account): """Test get terms and conditions for vendor.""" mock_auth(mocker, mock_account.id) mocker.patch( "collaborator.models.ows.ows_account.has_direct_payments", return_value=True ) result = auth_client.get( "/terms-and-conditions", query_string={"vendor_id": mock_account.id} ) assert result.status_code == 200 assert result.json["latest_version"] == 2 assert result.json["agreed_version"] == 2 # From seed data @db.test_schema_default_seed def test_get_terms_and_conditions_no_direct_payments(auth_client, mocker, mock_account): """Test get terms and conditions returns None for vendor without direct payments.""" mock_auth(mocker, mock_account.id) with patch( "collaborator.models.ows.ows_account.has_direct_payments", return_value=False ): result = auth_client.get( "/terms-and-conditions", query_string={"vendor_id": mock_account.id} ) assert result.status_code == 200 assert result.json is None @db.test_schema_default_seed def test_get_terms_and_conditions_missing_vendor_id(auth_client, mocker, mock_account): """Test get terms and conditions with missing vendor_id.""" mock_auth(mocker, mock_account.id) result = auth_client.get("/terms-and-conditions") assert result.status_code == 400 def test_get_terms_and_conditions_missing_profile_headers(test_client): """Test get terms and conditions without auth headers.""" result = test_client.get( "/terms-and-conditions?vendor_id=123", headers=[], ) assert result.status_code == 400 assert result.json["code"] == error.ERROR_CODE_BAD_GRASS_REQUEST @db.test_schema_default_seed def test_get_terms_and_conditions_vendor_authorization_failure( auth_client, mocker, mock_account ): """Test get terms and conditions when vendor authorization fails.""" mock_auth(mocker, mock_account.id) with patch("collaborator.utils.helpers.check_vendors_authorization") as mock_check: mock_check.side_effect = Exception("Authorization failed") result = auth_client.get( "/terms-and-conditions", query_string={"vendor_id": mock_account.id} ) assert result.status_code == 500 # Would be caught by error handler