diff --git a/mergin/client.py b/mergin/client.py index ec6d155..101c62e 100644 --- a/mergin/client.py +++ b/mergin/client.py @@ -380,14 +380,18 @@ def server_type(self): try: resp = self.get("/config", validate_auth=False) config = json.load(resp) - if config["server_type"] == "ce": + stype = config.get("server_type") + if stype == "ce": self._server_type = ServerType.CE - elif config["server_type"] == "ee": + elif stype == "ee": self._server_type = ServerType.EE - elif config["server_type"] == "saas": + elif stype == "saas": self._server_type = ServerType.SAAS - except (ClientError, KeyError): - self._server_type = ServerType.OLD + except ClientError as e: + if getattr(e, "http_error", None) == 404: + self._server_type = ServerType.OLD + else: + raise return self._server_type diff --git a/mergin/test/test_client.py b/mergin/test/test_client.py index 58278b5..07cdeec 100644 --- a/mergin/test/test_client.py +++ b/mergin/test/test_client.py @@ -11,6 +11,8 @@ import sqlite3 import glob +from unittest.mock import patch, Mock + from .. import InvalidProject from ..client import ( MerginClient, @@ -3008,3 +3010,19 @@ def test_validate_auth(mc: MerginClient): # this should pass and not raise an error, as the client is able to re-login with pytest.raises(LoginError): mc_auth_token_login_wrong_password.validate_auth() + + +def test_server_type(mc): + """Test mc.server_type() method""" + # success + assert mc.server_type() == ServerType.SAAS + mc._server_type = None + with patch("mergin.client.MerginClient.get") as mock_client_get: + # 404 + mock_client_get.side_effect = ClientError(detail="Not found", http_error=404) + assert mc.server_type() == ServerType.OLD + mc._server_type = None + # 503 + mock_client_get.side_effect = ClientError(detail="Service unavailable", http_error=503) + with pytest.raises(ClientError, match="Service unavailable"): + mc.server_type()