Skip to content

Commit

Permalink
fix: async set_auth for realtime in auth event listener (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks authored Sep 26, 2024
1 parent 948eb60 commit 5e34512
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tests_only:

build_sync:
poetry run unasync supabase tests
sed -i 's/asyncio.create_task(self.realtime.set_auth(access_token))//g' supabase/_sync/client.py
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ tests = 'poetry_scripts:run_tests'

[tool.poetry.group.dev.dependencies]
unasync-cli = { git = "https://github.com/supabase-community/unasync-cli.git", branch = "main" }
pytest-asyncio = "^0.24.0"

[tool.pytest.ini_options]
asyncio_mode = "auto"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
5 changes: 2 additions & 3 deletions supabase/_async/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import re
from typing import Any, Dict, List, Optional, Union

Expand Down Expand Up @@ -296,9 +297,7 @@ def _listen_to_auth_events(
access_token = session.access_token if session else self.supabase_key

self.options.headers["Authorization"] = self._create_auth_header(access_token)

# set_auth is a coroutine, how to handle this?
self.realtime.set_auth(access_token)
asyncio.create_task(self.realtime.set_auth(access_token))


async def create_client(
Expand Down
3 changes: 0 additions & 3 deletions supabase/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ def _listen_to_auth_events(

self.options.headers["Authorization"] = self._create_auth_header(access_token)

# set_auth is a coroutine, how to handle this?
self.realtime.set_auth(access_token)


def create_client(
supabase_url: str,
Expand Down
38 changes: 38 additions & 0 deletions tests/_async/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
from unittest.mock import AsyncMock, MagicMock

from supabase import create_async_client


async def test_updates_the_authorization_header_on_auth_events() -> None:
url = os.environ.get("SUPABASE_TEST_URL")
key = os.environ.get("SUPABASE_TEST_KEY")

client = await create_async_client(url, key)

assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == f"Bearer {key}"

mock_session = MagicMock(access_token="secretuserjwt")
realtime_mock = AsyncMock()
client.realtime = realtime_mock

client._listen_to_auth_events("SIGNED_IN", mock_session)

updated_authorization = f"Bearer {mock_session.access_token}"

assert client.options.headers.get("apiKey") == key
assert client.options.headers.get("Authorization") == updated_authorization

assert client.postgrest.session.headers.get("apiKey") == key
assert (
client.postgrest.session.headers.get("Authorization") == updated_authorization
)

assert client.auth._headers.get("apiKey") == key
assert client.auth._headers.get("Authorization") == updated_authorization

assert client.storage.session.headers.get("apiKey") == key
assert client.storage.session.headers.get("Authorization") == updated_authorization

realtime_mock.set_auth.assert_called_once_with(mock_session.access_token)
2 changes: 0 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,3 @@ def test_updates_the_authorization_header_on_auth_events() -> None:

assert client.storage.session.headers.get("apiKey") == key
assert client.storage.session.headers.get("Authorization") == updated_authorization

realtime_mock.set_auth.assert_called_once_with(mock_session.access_token)

0 comments on commit 5e34512

Please sign in to comment.