Spaces:
Running
Running
Upload folder using huggingface_hub (part 2)
Browse files- tests/test_auth.py +29 -0
- tests/test_geo_automatic.py +7 -1
tests/test_auth.py
CHANGED
|
@@ -23,6 +23,10 @@ def _make_token(sub=SUB, secret=SECRET, expires_delta=3600, headers=None):
|
|
| 23 |
return pyjwt.encode(payload, secret, algorithm="HS256", headers=headers or {})
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def _creds(token: str) -> HTTPAuthorizationCredentials:
|
| 27 |
return HTTPAuthorizationCredentials(scheme="Bearer", credentials=token)
|
| 28 |
|
|
@@ -84,6 +88,31 @@ class TestGetUserId:
|
|
| 84 |
assert seen["token"] == "aaa.bbb.ccc"
|
| 85 |
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
class TestRequireUserId:
|
| 88 |
def test_raises_401_on_unverifiable_token(self, monkeypatch):
|
| 89 |
async def fake_verify(token: str):
|
|
|
|
| 23 |
return pyjwt.encode(payload, secret, algorithm="HS256", headers=headers or {})
|
| 24 |
|
| 25 |
|
| 26 |
+
async def _async_identity(value: str, identity: str):
|
| 27 |
+
return identity
|
| 28 |
+
|
| 29 |
+
|
| 30 |
def _creds(token: str) -> HTTPAuthorizationCredentials:
|
| 31 |
return HTTPAuthorizationCredentials(scheme="Bearer", credentials=token)
|
| 32 |
|
|
|
|
| 88 |
assert seen["token"] == "aaa.bbb.ccc"
|
| 89 |
|
| 90 |
|
| 91 |
+
def test_falls_back_to_supabase_when_legacy_secret_does_not_match_live_session(self, monkeypatch):
|
| 92 |
+
monkeypatch.setenv("SUPABASE_JWT_SECRET", SECRET)
|
| 93 |
+
token = _make_token(secret="rotated-or-asymmetric-signing-key")
|
| 94 |
+
seen = {}
|
| 95 |
+
|
| 96 |
+
async def fake_verify(value: str):
|
| 97 |
+
seen["token"] = value
|
| 98 |
+
return SUB
|
| 99 |
+
|
| 100 |
+
monkeypatch.setattr(auth, "_verify_via_supabase", fake_verify)
|
| 101 |
+
assert asyncio.run(auth.get_user_id(_creds(token))) == SUB
|
| 102 |
+
assert seen["token"] == token
|
| 103 |
+
|
| 104 |
+
def test_falls_back_to_supabase_for_asymmetric_jwt(self, monkeypatch):
|
| 105 |
+
monkeypatch.setenv("SUPABASE_JWT_SECRET", SECRET)
|
| 106 |
+
# The JWT payload/signature are intentionally not trusted here; the
|
| 107 |
+
# mocked authoritative verifier stands in for Supabase Auth /user.
|
| 108 |
+
import base64, json
|
| 109 |
+
header = base64.urlsafe_b64encode(json.dumps({"alg": "ES256", "typ": "JWT"}).encode()).decode().rstrip("=")
|
| 110 |
+
payload = base64.urlsafe_b64encode(json.dumps({"sub": SUB, "exp": int(time.time()) + 3600}).encode()).decode().rstrip("=")
|
| 111 |
+
token = f"{header}.{payload}.signature"
|
| 112 |
+
monkeypatch.setattr(auth, "_verify_via_supabase", lambda value: _async_identity(value, SUB))
|
| 113 |
+
assert asyncio.run(auth.get_user_id(_creds(token))) == SUB
|
| 114 |
+
|
| 115 |
+
|
| 116 |
class TestRequireUserId:
|
| 117 |
def test_raises_401_on_unverifiable_token(self, monkeypatch):
|
| 118 |
async def fake_verify(token: str):
|
tests/test_geo_automatic.py
CHANGED
|
@@ -137,7 +137,11 @@ def test_empty_R_preflight_output_is_not_missing_package(monkeypatch):
|
|
| 137 |
|
| 138 |
|
| 139 |
def test_full_queue_execution_does_not_require_manual_QC_checkpoint(tmp_path, monkeypatch):
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
monkeypatch.setattr(durable, 'readiness', lambda *a: {'available': True})
|
| 142 |
counts = tmp_path / 'counts.tsv'; counts.write_text('gene\ts1\ng1\t1\n')
|
| 143 |
meta = tmp_path / 'metadata.tsv'; meta.write_text('sample\tcondition\ns1\ta\n')
|
|
@@ -149,3 +153,5 @@ def test_full_queue_execution_does_not_require_manual_QC_checkpoint(tmp_path, mo
|
|
| 149 |
assert captured['stage'] == 'full' and captured['checkpoint_path'] is None
|
| 150 |
with durable.db() as db:
|
| 151 |
assert db.execute('SELECT state FROM jobs WHERE id=?', (job_id,)).fetchone()['state'] == 'SUCCEEDED'
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
|
| 139 |
def test_full_queue_execution_does_not_require_manual_QC_checkpoint(tmp_path, monkeypatch):
|
| 140 |
+
# The production queue deliberately rejects ephemeral /tmp storage. Keep
|
| 141 |
+
# this local-queue unit test on a dedicated persistent-looking sandbox path.
|
| 142 |
+
queue_root = Path.cwd() / '.test-expression-queue'
|
| 143 |
+
monkeypatch.setenv('BIONEXUS_EXPRESSION_BACKEND', 'local')
|
| 144 |
+
monkeypatch.setenv('BIONEXUS_EXPRESSION_ROOT', str(queue_root))
|
| 145 |
monkeypatch.setattr(durable, 'readiness', lambda *a: {'available': True})
|
| 146 |
counts = tmp_path / 'counts.tsv'; counts.write_text('gene\ts1\ng1\t1\n')
|
| 147 |
meta = tmp_path / 'metadata.tsv'; meta.write_text('sample\tcondition\ns1\ta\n')
|
|
|
|
| 153 |
assert captured['stage'] == 'full' and captured['checkpoint_path'] is None
|
| 154 |
with durable.db() as db:
|
| 155 |
assert db.execute('SELECT state FROM jobs WHERE id=?', (job_id,)).fetchone()['state'] == 'SUCCEEDED'
|
| 156 |
+
import shutil
|
| 157 |
+
shutil.rmtree(queue_root, ignore_errors=True)
|