Skip to content

Convert context into query string for FGA queries #438

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

Merged
merged 4 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
cast,
)
from unittest.mock import AsyncMock, MagicMock
import urllib.parse

import httpx
import pytest
Expand Down Expand Up @@ -162,6 +163,14 @@ def inner(
def capture_and_mock(*args, **kwargs):
request_kwargs.update(kwargs)

# Capture full URL with encoded params while keeping original URL
if kwargs and "params" in kwargs and kwargs["params"]:
# Convert params to query string with proper URL encoding
query_string = urllib.parse.urlencode(
kwargs["params"], doseq=True, quote_via=urllib.parse.quote_plus
)
request_kwargs.update({"full_url": f"{kwargs['url']}?{query_string}"})

return httpx.Response(
status_code=status_code,
headers=headers,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_fga.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,22 @@ def test_query(self, mock_query_response, mock_http_client_with_response):
warrant_token="warrant_token",
)
assert response.dict(exclude_none=True) == mock_query_response

def test_query_with_context(
self, mock_query_response, capture_and_mock_http_client_request
):
request_kwargs = capture_and_mock_http_client_request(
self.http_client, mock_query_response, 200
)

response = self.fga.query(
q="select member of type user for permission:view-docs",
order="asc",
warrant_token="warrant_token",
context={"region": "us", "subscription": "pro"},
)

assert request_kwargs["url"] == "https://api.workos.test/fga/v1/query"
expected_full_url = "https://api.workos.test/fga/v1/query?q=select+member+of+type+user+for+permission%3Aview-docs&limit=10&order=asc&context=%7B%22region%22%3A+%22us%22%2C+%22subscription%22%3A+%22pro%22%7D"
assert request_kwargs["full_url"] == expected_full_url
assert response.dict(exclude_none=True) == mock_query_response

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make an assertion on what the request URL looks like? (i.e. it should include a string-ified version of the JSON context)

8 changes: 7 additions & 1 deletion workos/fga.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Any, Mapping, Optional, Protocol, Sequence
from workos.types.fga import (
CheckOperation,
Expand Down Expand Up @@ -621,11 +622,16 @@ def query(
"after": after,
"context": context,
}
parsed_list_params = {
key: json.dumps(value) if key == "context" and value is not None else value
for key, value in list_params.items()
if value is not None
}

response = self._http_client.request(
"fga/v1/query",
method=REQUEST_METHOD_GET,
params=list_params,
params=parsed_list_params,
headers={"Warrant-Token": warrant_token} if warrant_token else None,
)

Expand Down