From 87a6e68f6e29a8755e031a83d2022106e216d69e Mon Sep 17 00:00:00 2001 From: lawrencecchen Date: Tue, 27 Jul 2021 16:41:35 -0700 Subject: [PATCH 1/2] add set_auth to gotrue client --- gotrue/client.py | 5 +++++ tests/test_gotrue.py | 22 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/gotrue/client.py b/gotrue/client.py index ab135b75..2ec827be 100644 --- a/gotrue/client.py +++ b/gotrue/client.py @@ -127,6 +127,11 @@ def update(self, **attributes) -> Dict[str, Any]: self._notify_all_subscribers("USER_UPDATED") return data + def set_auth(self, access_token: str): + """Overrides the JWT on the current client. The JWT will then be sent in all subsequent network requests.""" + self._save_session({**self.current_session, "access_token": access_token, "token_type": "bearer", "user": None}) + return self.current_session + def get_session_from_url(self, store_session: bool): """Gets the session data from a URL string.""" raise NotImplementedError( diff --git a/tests/test_gotrue.py b/tests/test_gotrue.py index cdb85d95..df637a2d 100644 --- a/tests/test_gotrue.py +++ b/tests/test_gotrue.py @@ -1,3 +1,4 @@ +from gotrue.client import Client import os import random import string @@ -35,7 +36,7 @@ def client(): ) -def test_user_auth_flow(client): +def test_user_auth_flow(client: Client): """Ensures user can sign up, log out and log into their account.""" random_email: str = f"{_random_string(10)}@supamail.com" random_password: str = _random_string(20) @@ -53,7 +54,7 @@ def test_user_auth_flow(client): assert client.current_session is not None -def test_get_user_and_session_methods(client): +def test_get_user_and_session_methods(client: Client): """Ensure we can get the current user and session via the getters.""" # Create a random user. random_email: str = f"{_random_string(10)}@supamail.com" @@ -65,7 +66,7 @@ def test_get_user_and_session_methods(client): assert client.session() is not None -def test_refresh_session(client): +def test_refresh_session(client: Client): """Test user can signup/in and refresh their session.""" # Create a random user. random_email: str = f"{_random_string(10)}@supamail.com" @@ -81,9 +82,22 @@ def test_refresh_session(client): assert client.current_session is not None -def test_send_magic_link(client): +def test_send_magic_link(client: Client): """Tests client can send a magic link to email address.""" random_email: str = f"{_random_string(10)}@supamail.com" # We send a magic link if no password is supplied with the email. data = client.sign_in(email=random_email) assert data.get("status_code") == 200 + + +def test_set_auth(client: Client): + """Test client can override the access_token""" + random_email: str = f"{_random_string(10)}@supamail.com" + random_password: str = _random_string(20) + user = client.sign_up(email=random_email, password=random_password) + _assert_authenticated_user(user) + + mock_access_token = _random_string(20) + client.set_auth(mock_access_token) + new_session = client.session() + assert new_session["access_token"] == mock_access_token From 757714bdd53d4a0eb8a2d33b1999470f1648d543 Mon Sep 17 00:00:00 2001 From: lawrencecchen Date: Tue, 27 Jul 2021 17:05:47 -0700 Subject: [PATCH 2/2] remove lib from gitignore --- .gitignore | 2 +- gotrue/.gitignore | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 960ddc32..fa6217cc 100644 --- a/.gitignore +++ b/.gitignore @@ -38,7 +38,7 @@ dist/ downloads/ eggs/ .eggs/ -lib/ +/lib/ lib64/ parts/ sdist/ diff --git a/gotrue/.gitignore b/gotrue/.gitignore index 1a173698..c6814621 100644 --- a/gotrue/.gitignore +++ b/gotrue/.gitignore @@ -29,7 +29,7 @@ dist/ downloads/ eggs/ .eggs/ -lib/ +/lib/ lib64/ parts/ sdist/ @@ -123,4 +123,4 @@ venv.bak/ # mypy .mypy_cache/ .dmypy.json -dmypy.json \ No newline at end of file +dmypy.json