Skip to content

Commit b111a26

Browse files
fix up openfga - restrict to python3.10
1 parent ad92aaf commit b111a26

File tree

6 files changed

+174
-77
lines changed

6 files changed

+174
-77
lines changed

core/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Testcontainers Core
66
.. automodule:: testcontainers.core.container
77
:members:
88
:undoc-members:
9-
9+
1010
.. autoclass:: testcontainers.core.network.Network
1111
:members:
1212

modules/openfga/testcontainers/openfga/__init__.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313

14-
from typing import Optional, List
14+
from typing import Optional
1515

1616
import requests
17-
from openfga_sdk import ClientConfiguration
18-
from openfga_sdk.credentials import Credentials, CredentialConfiguration
19-
from openfga_sdk.sync import OpenFgaClient
17+
2018
from testcontainers.core.container import DockerContainer
2119
from testcontainers.core.waiting_utils import wait_container_is_ready
2220

21+
no_client = False
22+
try:
23+
from openfga_sdk import ClientConfiguration
24+
from openfga_sdk.credentials import CredentialConfiguration, Credentials
25+
from openfga_sdk.sync import OpenFgaClient
26+
except ImportError:
27+
no_client = True
28+
29+
class OpenFgaClient:
30+
pass
31+
32+
2333
_DEFAULT_RUN_COMMAND = "run"
2434

2535

@@ -41,13 +51,13 @@ class OpenFGAContainer(DockerContainer):
4151

4252
# pylint: disable=too-many-arguments
4353
def __init__(
44-
self,
45-
image: str = "openfga/openfga:latest",
46-
preshared_keys: Optional[List[str]] = None,
47-
playground_port: int = 3000,
48-
http_port: int = 8080,
49-
grpc_port: int = 8081,
50-
cmd: str = _DEFAULT_RUN_COMMAND,
54+
self,
55+
image: str = "openfga/openfga:latest",
56+
preshared_keys: Optional[list[str]] = None,
57+
playground_port: int = 3000,
58+
http_port: int = 8080,
59+
grpc_port: int = 8081,
60+
cmd: str = _DEFAULT_RUN_COMMAND,
5161
) -> None:
5262
super().__init__(image=image)
5363
self.preshared_keys = preshared_keys
@@ -60,7 +70,7 @@ def __init__(
6070
def _configure(self) -> None:
6171
if self.preshared_keys:
6272
self.cmd += " --authn-method=preshared"
63-
self.cmd += f" --authn-preshared-keys=\"{','.join(self.preshared_keys)}\""
73+
self.cmd += f' --authn-preshared-keys="{",".join(self.preshared_keys)}"'
6474
self.with_command(self.cmd)
6575

6676
def get_api_url(self) -> str:
@@ -77,14 +87,17 @@ def start(self) -> "OpenFGAContainer":
7787
self._readiness_probe()
7888
return self
7989

80-
def get_preshared_keys(self) -> Optional[List[str]]:
90+
def get_preshared_keys(self) -> Optional[list[str]]:
8191
return self.preshared_keys
8292

83-
def get_client(self) -> OpenFgaClient:
93+
def get_client(self) -> "OpenFgaClient":
94+
if no_client:
95+
raise NotImplementedError("failed to import openfga_sdk: is python < 3.10?")
96+
8497
credentials = None
8598
if preshared_keys := self.get_preshared_keys():
8699
credentials = Credentials(
87-
method='api_token',
100+
method="api_token",
88101
configuration=CredentialConfiguration(
89102
api_token=preshared_keys[0],
90103
),

modules/openfga/tests/test_openfga.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
import pytest
12
from testcontainers.openfga import OpenFGAContainer
3+
from sys import version_info
4+
25

36
def test_openfga():
7+
if version_info < (3, 10):
8+
with pytest.raises(NotImplementedError):
9+
_test_openfga()
10+
else:
11+
_test_openfga()
12+
13+
14+
def _test_openfga():
415
with OpenFGAContainer("openfga/openfga:v1.8.4") as openfga:
516
client = openfga.get_client()
617
assert client

0 commit comments

Comments
 (0)