From cbee779500583eea15a7656709c8325c1e7222db Mon Sep 17 00:00:00 2001 From: spwoodcock Date: Fri, 19 Apr 2024 16:26:04 +0100 Subject: [PATCH] test: add tests if connecting for if odk central fails --- tests/test_central.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_central.py b/tests/test_central.py index 2dfcd34a..22b00c38 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