-
-
Notifications
You must be signed in to change notification settings - Fork 382
fix(user auth context): do not overwrite provided client options Authorization header #766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,14 @@ | ||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import os | ||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||
| from unittest.mock import MagicMock | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| from supabase import Client, create_client | ||||||||||||||||||||||
| from supabase.lib.client_options import ClientOptions | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @pytest.mark.xfail( | ||||||||||||||||||||||
| reason="None of these values should be able to instantiate a client object" | ||||||||||||||||||||||
|
|
@@ -12,6 +17,75 @@ | |||||||||||||||||||||
| @pytest.mark.parametrize("key", ["", None, "valeefgpoqwjgpj", 139, -1, {}, []]) | ||||||||||||||||||||||
| def test_incorrect_values_dont_instantiate_client(url: Any, key: Any) -> None: | ||||||||||||||||||||||
| """Ensure we can't instantiate client with invalid values.""" | ||||||||||||||||||||||
| from supabase import Client, create_client | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| _: Client = create_client(url, key) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_uses_key_as_authorization_header_by_default() -> None: | ||||||||||||||||||||||
| url = os.environ.get("SUPABASE_TEST_URL") | ||||||||||||||||||||||
| key = os.environ.get("SUPABASE_TEST_KEY") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| client = create_client(url, key) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.options.headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.options.headers.get("Authorization") == f"Bearer {key}" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.postgrest.session.headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.postgrest.session.headers.get("Authorization") == f"Bearer {key}" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.auth._headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.auth._headers.get("Authorization") == f"Bearer {key}" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.storage.session.headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.storage.session.headers.get("Authorization") == f"Bearer {key}" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_supports_setting_a_global_authorization_header() -> None: | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Please add a test case to verify the behavior when an invalid JWT is provided in the This test would help ensure that the system behaves as expected when an invalid JWT is used, potentially rejecting the request or handling it gracefully.
Suggested change
|
||||||||||||||||||||||
| url = os.environ.get("SUPABASE_TEST_URL") | ||||||||||||||||||||||
| key = os.environ.get("SUPABASE_TEST_KEY") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| authorization = f"Bearer secretuserjwt" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| options = ClientOptions(headers={"Authorization": authorization}) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| client = create_client(url, key, options) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.options.headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.options.headers.get("Authorization") == authorization | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.postgrest.session.headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.postgrest.session.headers.get("Authorization") == authorization | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.auth._headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.auth._headers.get("Authorization") == authorization | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert client.storage.session.headers.get("apiKey") == key | ||||||||||||||||||||||
| assert client.storage.session.headers.get("Authorization") == authorization | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def test_updates_the_authorization_header_on_auth_events() -> None: | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add a test to verify header updates when the auth event is 'SIGNED_OUT'. This would ensure that headers are correctly reset or handled when a user signs out.
Suggested change
|
||||||||||||||||||||||
| url = os.environ.get("SUPABASE_TEST_URL") | ||||||||||||||||||||||
| key = os.environ.get("SUPABASE_TEST_KEY") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| client = create_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") | ||||||||||||||||||||||
| 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 | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Consider adding a test case for when the
Authorizationheader is explicitly set toNone.This would help ensure that the default behavior is correctly handled even when the header is explicitly set to
None, rather than just not provided.