Connect platform tenant creation to tenant-engine
This commit is contained in:
parent
6589294ae7
commit
c0da589dbe
2 changed files with 56 additions and 8 deletions
|
|
@ -22,11 +22,11 @@ class HTTPTenantManagementAdapter:
|
|||
correlation_id: str,
|
||||
) -> TenantProvisioningResult:
|
||||
request = Request(
|
||||
self.base_url + "/v1/tenants",
|
||||
self.base_url + "/tenants",
|
||||
data=json.dumps({
|
||||
"tenant": tenant, "display_name": display_name,
|
||||
"idempotency_key": idempotency_key,
|
||||
"correlation_id": correlation_id,
|
||||
"tenant_id": tenant,
|
||||
"identifier": tenant,
|
||||
"actor": "tenant-engine",
|
||||
}).encode(),
|
||||
headers={
|
||||
"Authorization": f"Bearer {self.bearer_token}",
|
||||
|
|
@ -41,12 +41,16 @@ class HTTPTenantManagementAdapter:
|
|||
result = json.loads(response.read())
|
||||
except HTTPError as exc:
|
||||
exc.read(4096)
|
||||
if exc.code == 409:
|
||||
return TenantProvisioningResult(
|
||||
tenant=tenant, status="existing", resumed=True, external_ref=tenant,
|
||||
)
|
||||
raise RuntimeError(f"tenant authority failed ({exc.code})") from exc
|
||||
except URLError as exc:
|
||||
raise RuntimeError("tenant authority unavailable") from exc
|
||||
return TenantProvisioningResult(
|
||||
tenant=str(result.get("tenant") or tenant),
|
||||
status=str(result["status"]),
|
||||
resumed=bool(result.get("resumed", False)),
|
||||
external_ref=str(result["external_ref"]) if result.get("external_ref") else None,
|
||||
tenant=str(result.get("identifier") or tenant),
|
||||
status="created",
|
||||
resumed=False,
|
||||
external_ref=str(result.get("tenant_id") or tenant),
|
||||
)
|
||||
|
|
|
|||
44
tests/test_tenant_management_adapter.py
Normal file
44
tests/test_tenant_management_adapter.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import io
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from user_engine.adapters.tenant_management import HTTPTenantManagementAdapter
|
||||
|
||||
|
||||
class _Response(io.BytesIO):
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *_args):
|
||||
self.close()
|
||||
|
||||
|
||||
class TenantManagementAdapterTests(unittest.TestCase):
|
||||
def test_uses_tenant_engine_contract(self):
|
||||
body = _Response(json.dumps({
|
||||
"tenant_id": "tenant:friendly:new",
|
||||
"identifier": "tenant:friendly:new",
|
||||
"grouping": "friendly",
|
||||
}).encode())
|
||||
adapter = HTTPTenantManagementAdapter(
|
||||
base_url="http://tenant-engine", bearer_token="opaque"
|
||||
)
|
||||
with patch("user_engine.adapters.tenant_management.urlopen", return_value=body) as call:
|
||||
result = adapter.create_tenant(
|
||||
tenant="tenant:friendly:new", display_name="New",
|
||||
idempotency_key="tenant-create-123", correlation_id="corr-1",
|
||||
)
|
||||
request = call.call_args.args[0]
|
||||
self.assertEqual(request.full_url, "http://tenant-engine/tenants")
|
||||
self.assertEqual(json.loads(request.data), {
|
||||
"tenant_id": "tenant:friendly:new",
|
||||
"identifier": "tenant:friendly:new",
|
||||
"actor": "tenant-engine",
|
||||
})
|
||||
self.assertEqual(result.status, "created")
|
||||
self.assertEqual(result.external_ref, "tenant:friendly:new")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue