Skip to content

Commit dfd22c0

Browse files
author
kabya
committed
test: add unit tests for token refresh header update (bug 1143)
1 parent c2a0965 commit dfd22c0

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ classifiers = [
1414
"Operating System :: OS Independent"
1515
]
1616

17+
1718
[tool.poetry.dependencies]
1819
python = "^3.9"
1920
postgrest = "1.1.1"

supabase/_async/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,14 @@ def _get_auth_headers(self, authorization: Optional[str] = None) -> Dict[str, st
323323
"Authorization": authorization,
324324
}
325325

326-
def _listen_to_auth_events(self, event: AuthChangeEvent, session: Optional[Session]):
326+
async def _listen_to_auth_events(self, event: AuthChangeEvent, session: Optional[Session]):
327327
original_auth = self._create_auth_header(self.supabase_key)
328328

329329
if self.options.headers.get("Authorization") == original_auth:
330330
return
331331

332-
access_token = self.supabase_key
332+
access_token = self.supabase_key # Default value to avoid unbound error
333+
333334
if event in ["SIGNED_IN", "TOKEN_REFRESHED", "SIGNED_OUT"]:
334335
self._postgrest = None
335336
self._storage = None
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
from supabase._async.client import create_client as create_async_client
3+
4+
class DummySession:
5+
def __init__(self, token):
6+
self.access_token = token
7+
8+
@pytest.mark.asyncio
9+
async def test_async_auth_header_updates(monkeypatch):
10+
client = await create_async_client("https://example.supabase.co", "svc-key")
11+
12+
# Fake realtime.set_auth
13+
called = {"token": None}
14+
async def fake_set_auth(token):
15+
called["token"] = token
16+
17+
monkeypatch.setattr(client.realtime, "set_auth", fake_set_auth)
18+
19+
client.options.headers["Authorization"] = "Bearer stale-token"
20+
session = DummySession("refreshed-token")
21+
22+
await client._listen_to_auth_events("TOKEN_REFRESHED", session)
23+
24+
assert client.options.headers["Authorization"] == "Bearer refreshed-token"
25+
assert called["token"] == "refreshed-token"
26+
assert client._postgrest is None
27+
assert client._storage is None
28+
assert client._functions is None
29+
30+
31+
@pytest.mark.asyncio
32+
async def test_async_no_update_if_original_token_used():
33+
client = await create_async_client("https://example.supabase.co", "svc-role-key")
34+
35+
original_auth = client._create_auth_header(client.supabase_key)
36+
client.options.headers["Authorization"] = original_auth
37+
38+
session = DummySession("some-other-token")
39+
await client._listen_to_auth_events("TOKEN_REFRESHED", session)
40+
41+
# Should skip the update logic
42+
assert client.options.headers["Authorization"] == original_auth

tests/_sync/test_auth_refresh_sync.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from supabase import create_client
2+
import pytest
3+
4+
class DummySession:
5+
def __init__(self, access_token):
6+
self.access_token = access_token
7+
8+
def test_token_refresh_updates_header(monkeypatch):
9+
supabase = create_client("https://test.supabase.co", "original-key")
10+
11+
session = DummySession("new-token-123")
12+
13+
# simulate stale token (pretend something else set a new one)
14+
supabase.options.headers["Authorization"] = "Bearer old-token"
15+
16+
supabase._listen_to_auth_events("TOKEN_REFRESHED", session)
17+
18+
assert supabase.options.headers["Authorization"] == "Bearer new-token-123"
19+
assert supabase._postgrest is None
20+
assert supabase._storage is None
21+
assert supabase._functions is None
22+
23+
def test_no_update_when_auth_unchanged():
24+
supabase = create_client("https://test.supabase.co", "svc-role-key")
25+
auth_header_before = supabase.options.headers["Authorization"]
26+
supabase._listen_to_auth_events("SIGNED_IN", DummySession("new-token"))
27+
auth_header_after = supabase.options.headers["Authorization"]
28+
29+
assert auth_header_before == auth_header_after

0 commit comments

Comments
 (0)