Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 23c167e

Browse files
fix: delete_user returns Exception event if response is Ok (#68)
* fix: delete_user returns Exception event if response is Ok * 'Refactored by Sourcery' (#69) Co-authored-by: Sourcery AI <> * chore: format code Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
1 parent e1acdf3 commit 23c167e

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

gotrue/_async/api.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(
3131
self.url = url
3232
self.headers = headers
3333
self.cookie_options = cookie_options
34-
self.http_client = http_client if http_client else AsyncClient()
34+
self.http_client = http_client or AsyncClient()
3535

3636
async def __aenter__(self) -> AsyncGoTrueAPI:
3737
return self
@@ -238,10 +238,9 @@ async def sign_in_with_phone(
238238
error : APIError
239239
If an error occurs
240240
"""
241-
headers = self.headers
242-
query_string = "?grant_type=password"
243241
data = {"phone": phone, "password": password}
244-
url = f"{self.url}/token{query_string}"
242+
url = f"{self.url}/token?grant_type=password"
243+
headers = self.headers
245244
response = await self.http_client.post(url, json=data, headers=headers)
246245
return Session.parse_response(response)
247246

@@ -529,7 +528,7 @@ async def update_user(
529528
response = await self.http_client.put(url, json=data, headers=headers)
530529
return User.parse_response(response)
531530

532-
async def delete_user(self, *, uid: str, jwt: str) -> User:
531+
async def delete_user(self, *, uid: str, jwt: str) -> None:
533532
"""Delete a user. Requires a `service_role` key.
534533
535534
This function should only be called on a server.
@@ -555,7 +554,7 @@ async def delete_user(self, *, uid: str, jwt: str) -> User:
555554
headers = self._create_request_headers(jwt=jwt)
556555
url = f"{self.url}/admin/users/{uid}"
557556
response = await self.http_client.delete(url, headers=headers)
558-
return User.parse_response(response)
557+
return check_response(response)
559558

560559
async def refresh_access_token(self, *, refresh_token: str) -> Session:
561560
"""Generates a new JWT.
@@ -575,10 +574,9 @@ async def refresh_access_token(self, *, refresh_token: str) -> Session:
575574
error : APIError
576575
If an error occurs
577576
"""
578-
headers = self.headers
579-
query_string = "?grant_type=refresh_token"
580577
data = {"refresh_token": refresh_token}
581-
url = f"{self.url}/token{query_string}"
578+
url = f"{self.url}/token?grant_type=refresh_token"
579+
headers = self.headers
582580
response = await self.http_client.post(url, json=data, headers=headers)
583581
return Session.parse_response(response)
584582

gotrue/_sync/api.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(
3131
self.url = url
3232
self.headers = headers
3333
self.cookie_options = cookie_options
34-
self.http_client = http_client if http_client else SyncClient()
34+
self.http_client = http_client or SyncClient()
3535

3636
def __enter__(self) -> SyncGoTrueAPI:
3737
return self
@@ -238,10 +238,9 @@ def sign_in_with_phone(
238238
error : APIError
239239
If an error occurs
240240
"""
241-
headers = self.headers
242-
query_string = "?grant_type=password"
243241
data = {"phone": phone, "password": password}
244-
url = f"{self.url}/token{query_string}"
242+
url = f"{self.url}/token?grant_type=password"
243+
headers = self.headers
245244
response = self.http_client.post(url, json=data, headers=headers)
246245
return Session.parse_response(response)
247246

@@ -529,7 +528,7 @@ def update_user(
529528
response = self.http_client.put(url, json=data, headers=headers)
530529
return User.parse_response(response)
531530

532-
def delete_user(self, *, uid: str, jwt: str) -> User:
531+
def delete_user(self, *, uid: str, jwt: str) -> None:
533532
"""Delete a user. Requires a `service_role` key.
534533
535534
This function should only be called on a server.
@@ -555,7 +554,7 @@ def delete_user(self, *, uid: str, jwt: str) -> User:
555554
headers = self._create_request_headers(jwt=jwt)
556555
url = f"{self.url}/admin/users/{uid}"
557556
response = self.http_client.delete(url, headers=headers)
558-
return User.parse_response(response)
557+
return check_response(response)
559558

560559
def refresh_access_token(self, *, refresh_token: str) -> Session:
561560
"""Generates a new JWT.
@@ -575,10 +574,9 @@ def refresh_access_token(self, *, refresh_token: str) -> Session:
575574
error : APIError
576575
If an error occurs
577576
"""
578-
headers = self.headers
579-
query_string = "?grant_type=refresh_token"
580577
data = {"refresh_token": refresh_token}
581-
url = f"{self.url}/token{query_string}"
578+
url = f"{self.url}/token?grant_type=refresh_token"
579+
headers = self.headers
582580
response = self.http_client.post(url, json=data, headers=headers)
583581
return Session.parse_response(response)
584582

tests/_async/test_api_with_auto_confirm_enabled.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from gotrue.types import CookieOptions, Session, User
99

1010
GOTRUE_URL = "http://localhost:9998"
11-
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwicm9sZSI6InN1cGFiYXNlX2FkbWluIiwiaWF0IjoxNTE2MjM5MDIyfQ.0sOtTSTfPv5oPZxsjvBO249FI4S4p0ymHoIZ6H6z9Y8" # noqa: E501
11+
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIiwiaWF0IjoxNjQyMjMyNzUwfQ.TUR8Zu05TtNR25L42soA2trZpc4oBR8-9Pv5r5bvls8" # noqa: E501
1212

1313

1414
@pytest.fixture(name="api")
@@ -52,3 +52,14 @@ async def test_get_user(api: AsyncGoTrueAPI):
5252
assert isinstance(response, User)
5353
except Exception as e:
5454
assert False, str(e)
55+
56+
57+
@pytest.mark.asyncio
58+
@pytest.mark.depends(on=[test_get_user.__name__])
59+
async def test_delete_user(api: AsyncGoTrueAPI):
60+
try:
61+
jwt = valid_session.access_token if valid_session else ""
62+
user = await api.get_user(jwt=jwt)
63+
await api.delete_user(uid=str(user.id), jwt=TOKEN)
64+
except Exception as e:
65+
assert False, str(e)

tests/_sync/test_api_with_auto_confirm_enabled.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from gotrue.types import CookieOptions, Session, User
99

1010
GOTRUE_URL = "http://localhost:9998"
11-
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwicm9sZSI6InN1cGFiYXNlX2FkbWluIiwiaWF0IjoxNTE2MjM5MDIyfQ.0sOtTSTfPv5oPZxsjvBO249FI4S4p0ymHoIZ6H6z9Y8" # noqa: E501
11+
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIiwiaWF0IjoxNjQyMjMyNzUwfQ.TUR8Zu05TtNR25L42soA2trZpc4oBR8-9Pv5r5bvls8" # noqa: E501
1212

1313

1414
@pytest.fixture(name="api")
@@ -52,3 +52,14 @@ def test_get_user(api: SyncGoTrueAPI):
5252
assert isinstance(response, User)
5353
except Exception as e:
5454
assert False, str(e)
55+
56+
57+
@pytest.mark.asyncio
58+
@pytest.mark.depends(on=[test_get_user.__name__])
59+
def test_delete_user(api: SyncGoTrueAPI):
60+
try:
61+
jwt = valid_session.access_token if valid_session else ""
62+
user = api.get_user(jwt=jwt)
63+
api.delete_user(uid=str(user.id), jwt=TOKEN)
64+
except Exception as e:
65+
assert False, str(e)

0 commit comments

Comments
 (0)