Skip to content

Commit a1ce295

Browse files
committed
add test cases for validate_auth()
1 parent db37a2e commit a1ce295

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

mergin/test/test_client.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import tempfile
66
import subprocess
77
import shutil
8-
from datetime import datetime, timedelta, date
8+
from datetime import datetime, timedelta, date, timezone
99
import pytest
1010
import pytz
1111
import sqlite3
@@ -14,6 +14,7 @@
1414
from .. import InvalidProject
1515
from ..client import (
1616
MerginClient,
17+
AuthTokenExpiredError,
1718
ClientError,
1819
MerginProject,
1920
LoginError,
@@ -2910,3 +2911,61 @@ def test_do_request_error_handling(mc: MerginClient):
29102911

29112912
assert e.value.http_error == 400
29122913
assert "Passwords must be at least 8 characters long." in e.value.detail
2914+
2915+
2916+
def test_validate_auth(mc: MerginClient):
2917+
"""Test validate authentication under different scenarios."""
2918+
2919+
# ----- Client without authentication -----
2920+
mc_not_auth = MerginClient(SERVER_URL)
2921+
2922+
with pytest.raises(ClientError) as e:
2923+
mc_not_auth.validate_auth()
2924+
2925+
assert e.value.detail == "Missing login or password"
2926+
2927+
# ----- Client with token -----
2928+
# create a client with valid auth token based on other MerginClient instance, but not with username/password
2929+
mc_auth_token = MerginClient(SERVER_URL, auth_token=mc._auth_session["token"])
2930+
2931+
# this should pass and not raise an error
2932+
mc_auth_token.validate_auth()
2933+
2934+
# manually set expire date to the past to simulate expired token
2935+
mc_auth_token._auth_session["expire"] = datetime.now(timezone.utc) - timedelta(days=1)
2936+
2937+
# check that this raises an error
2938+
with pytest.raises(AuthTokenExpiredError):
2939+
mc_auth_token.validate_auth()
2940+
2941+
# ----- Client with token and username/password -----
2942+
# create a client with valid auth token based on other MerginClient instance with username/password that allows relogin if the token is expired
2943+
mc_auth_token_login = MerginClient(
2944+
SERVER_URL, auth_token=mc._auth_session["token"], login=API_USER, password=USER_PWD
2945+
)
2946+
2947+
# this should pass and not raise an error
2948+
mc_auth_token_login.validate_auth()
2949+
2950+
# manually set expire date to the past to simulate expired token
2951+
mc_auth_token_login._auth_session["expire"] = datetime.now(timezone.utc) - timedelta(days=1)
2952+
2953+
# this should pass and not raise an error, as the client is able to re-login
2954+
mc_auth_token_login.validate_auth()
2955+
2956+
# ----- Client with token and username/WRONG password -----
2957+
# create a client with valid auth token based on other MerginClient instance with username and WRONG password
2958+
# that does NOT allow relogin if the token is expired
2959+
mc_auth_token_login_wrong_password = MerginClient(
2960+
SERVER_URL, auth_token=mc._auth_session["token"], login=API_USER, password="WRONG_PASSWORD"
2961+
)
2962+
2963+
# this should pass and not raise an error
2964+
mc_auth_token_login_wrong_password.validate_auth()
2965+
2966+
# manually set expire date to the past to simulate expired token
2967+
mc_auth_token_login_wrong_password._auth_session["expire"] = datetime.now(timezone.utc) - timedelta(days=1)
2968+
2969+
# this should pass and not raise an error, as the client is able to re-login
2970+
with pytest.raises(LoginError):
2971+
mc_auth_token_login_wrong_password.validate_auth()

0 commit comments

Comments
 (0)