diff --git a/tests/test_central.py b/tests/test_central.py index 2dfcd34a8..22b00c386 100644 --- a/tests/test_central.py +++ b/tests/test_central.py @@ -24,6 +24,9 @@ import requests import segno +from osm_fieldwork.OdkCentral import OdkCentral +from osm_fieldwork.OdkCentralAsync import OdkCentral as OdkCentralAsync + testdata_dir = Path(__file__).parent / "testdata" @@ -224,3 +227,26 @@ def test_form_fields(odk_form_cleanup): "binary": None, "selectMultiple": None, } + + +def test_invalid_connection_sync(): + """Test case when connection to Central fails, sync code.""" + central = OdkCentral("https://proxy", "test@hotosm.org", "Password1234") + assert central.listProjects() == [] + + with pytest.raises(ConnectionError, match="Failed to connect to Central. Is the URL valid?"): + OdkCentral("https://somerandominvalidurl546456546.xyz", "test@hotosm.org", "Password1234") + + with pytest.raises(ConnectionError, match="ODK credentials are invalid, or may have changed. Please update them."): + OdkCentral("https://proxy", "thisuser@notexist.org", "Password1234") + + +async def test_invalid_connection_async(): + """Test case when connection to Central fails, async code.""" + with pytest.raises(ConnectionError, match="Failed to connect to Central. Is the URL valid?"): + async with OdkCentralAsync("https://somerandominvalidurl546456546.xyz", "test@hotosm.org", "Password1234"): + pass + + with pytest.raises(ConnectionError, match="ODK credentials are invalid, or may have changed. Please update them."): + async with OdkCentralAsync("https://proxy", "thisuser@notexist.org", "Password1234"): + pass