Skip to content

Commit 6c419f3

Browse files
authored
Merge pull request #278 from MerginMaps/fix_server_type
Return OLD server only for 404 resp
2 parents d6e8a16 + 59e04fe commit 6c419f3

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

mergin/client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,18 @@ def server_type(self):
380380
try:
381381
resp = self.get("/config", validate_auth=False)
382382
config = json.load(resp)
383-
if config["server_type"] == "ce":
383+
stype = config.get("server_type")
384+
if stype == "ce":
384385
self._server_type = ServerType.CE
385-
elif config["server_type"] == "ee":
386+
elif stype == "ee":
386387
self._server_type = ServerType.EE
387-
elif config["server_type"] == "saas":
388+
elif stype == "saas":
388389
self._server_type = ServerType.SAAS
389-
except (ClientError, KeyError):
390-
self._server_type = ServerType.OLD
390+
except ClientError as e:
391+
if getattr(e, "http_error", None) == 404:
392+
self._server_type = ServerType.OLD
393+
else:
394+
raise
391395

392396
return self._server_type
393397

mergin/test/test_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import sqlite3
1212
import glob
1313

14+
from unittest.mock import patch, Mock
15+
1416
from .. import InvalidProject
1517
from ..client import (
1618
MerginClient,
@@ -3008,3 +3010,19 @@ def test_validate_auth(mc: MerginClient):
30083010
# this should pass and not raise an error, as the client is able to re-login
30093011
with pytest.raises(LoginError):
30103012
mc_auth_token_login_wrong_password.validate_auth()
3013+
3014+
3015+
def test_server_type(mc):
3016+
"""Test mc.server_type() method"""
3017+
# success
3018+
assert mc.server_type() == ServerType.SAAS
3019+
mc._server_type = None
3020+
with patch("mergin.client.MerginClient.get") as mock_client_get:
3021+
# 404
3022+
mock_client_get.side_effect = ClientError(detail="Not found", http_error=404)
3023+
assert mc.server_type() == ServerType.OLD
3024+
mc._server_type = None
3025+
# 503
3026+
mock_client_get.side_effect = ClientError(detail="Service unavailable", http_error=503)
3027+
with pytest.raises(ClientError, match="Service unavailable"):
3028+
mc.server_type()

0 commit comments

Comments
 (0)