Skip to content

Commit

Permalink
Merge pull request #3 from lawrencecchen/main
Browse files Browse the repository at this point in the history
add set_auth to gotrue client
  • Loading branch information
J0 committed Jul 28, 2021
2 parents 8189c49 + 757714b commit 49c092e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
/lib/
lib64/
parts/
sdist/
Expand Down
4 changes: 2 additions & 2 deletions gotrue/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
/lib/
lib64/
parts/
sdist/
Expand Down Expand Up @@ -123,4 +123,4 @@ venv.bak/
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
dmypy.json
5 changes: 5 additions & 0 deletions gotrue/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 18 additions & 4 deletions tests/test_gotrue.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from gotrue.client import Client
import os
import random
import string
Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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

0 comments on commit 49c092e

Please sign in to comment.