-
Notifications
You must be signed in to change notification settings - Fork 6
[clientpython] feat(pyoaev): add multi-tenancy (#205) #208
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
Draft
Megafredo
wants to merge
2
commits into
release/current
Choose a base branch
from
feat/205-multi-tenancy
base: release/current
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/bdd/constraints/multi_tenant_api_routing_constraint.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| Feature: URL normalization in OpenAEV client | ||
|
|
||
| Scenario Outline: URL normalization combines base_url and path correctly | ||
| Given an OpenAEV client with base_url "<base_url>" | ||
| When I build the URL for "<path>" | ||
| Then the resulting URL should be "<expected>" | ||
|
|
||
| Examples: | ||
| | base_url | path | expected | | ||
| | base_url | path | base_url/api/path | | ||
| | base_url/ | /path | base_url/api/path | | ||
| | base_url// | //path | base_url/api/path | |
38 changes: 38 additions & 0 deletions
38
test/bdd/constraints/test_multi_tenant_api_routing_constraint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import pytest | ||
|
|
||
| from pyoaev import OpenAEV | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "base_url, input_path, expected", | ||
| [ | ||
| ( | ||
| "base_url", | ||
| "path", | ||
| "base_url/api/path", | ||
| ), | ||
| ( | ||
| "base_url/", | ||
| "/path", | ||
| "base_url/api/path", | ||
| ), | ||
| ( | ||
| "base_url//", | ||
| "//path", | ||
| "base_url/api/path", | ||
| ), | ||
| ], | ||
| ids=[ | ||
| "clean-base-url-and-relative-path", | ||
| "base-url-trailing-slash", | ||
| "base-url-double-slash-and-path-double-slash", | ||
| ], | ||
| ) | ||
| def test_url_normalization(base_url, input_path, expected): | ||
| client = OpenAEV( | ||
| url=base_url, | ||
| token="token", | ||
| tenant_id=None, | ||
| ) | ||
| result = client._build_url(input_path) | ||
| assert result == expected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| Feature: Multi-tenant API routing in OpenAEV client | ||
|
|
||
| Scenario: Full URL bypasses tenant routing | ||
| Given an OpenAEV client with tenant_id "2cffad3a-0001-4078-b0e2-ef74274022c3" | ||
| When I build the URL for "https://external.service/api/path" | ||
| Then the resulting URL should be "https://external.service/api/path" | ||
|
|
||
| Scenario Outline: Relative path routing behavior | ||
| Given an OpenAEV client with tenant_id "<tenant_id>" | ||
| When I build the URL for "/path" | ||
| Then the resulting URL should be "<output>" | ||
|
|
||
| Examples: | ||
| | tenant_id | output | | ||
| | None | base_url/api/path | | ||
| | 2cffad3a-0001-4078-b0e2-ef74274022c3 | base_url/api/tenants/2cffad3a-0001-4078-b0e2-ef74274022c3/path | | ||
|
|
12 changes: 12 additions & 0 deletions
12
test/bdd/features/multi_tenant_base_daemon_propagation.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| Feature: Tenant propagation in BaseDaemon API client initialization | ||
|
|
||
| Scenario Outline: BaseDaemon propagates tenant_id correctly from configuration | ||
| Given a daemon configuration with <tenant_id> | ||
| When the BaseDaemon is initialized | ||
| Then the API client should be created with tenant_id "<expected_tenant_id>" | ||
|
|
||
| Examples: | ||
| | tenant_id | expected_tenant_id | | ||
| | Missing tenant key | None | | ||
| | None | None | | ||
| | 2cffad3a-0001-4078-b0e2-ef74274022c3 | 2cffad3a-0001-4078-b0e2-ef74274022c3 | |
12 changes: 12 additions & 0 deletions
12
test/bdd/features/multi_tenant_endpoint_search_targets.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| Feature: searchTargets API routing with and without tenant_id | ||
|
|
||
| Scenario Outline: searchTargets routing behavior | ||
| Given an OpenAEV client with tenant_id "<tenant_id>" | ||
| And a valid SearchPaginationInput | ||
| When I call searchTargets on endpoint | ||
| Then the request URL should be "<expected_url>" | ||
|
|
||
| Examples: | ||
| | tenant_id | expected_url | | ||
| | None | url/api/endpoints/targets | | ||
| | 2cffad3a-0001-4078-b0e2-ef74274022c3 | url/api/tenants/2cffad3a-0001-4078-b0e2-ef74274022c3/endpoints/targets | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from uuid import UUID | ||
|
|
||
| import pytest | ||
|
|
||
| from pyoaev import OpenAEV | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "tenant_id, path, expected", | ||
| [ | ||
| ( | ||
| None, | ||
| "/path", | ||
| "base_url/api/path", | ||
| ), | ||
| ( | ||
| UUID("2cffad3a-0001-4078-b0e2-ef74274022c3"), | ||
| "/path", | ||
| "base_url/api/tenants/2cffad3a-0001-4078-b0e2-ef74274022c3/path", | ||
| ), | ||
| ( | ||
| None, | ||
| "https://external.service/api/path", | ||
| "https://external.service/api/path", | ||
| ), | ||
| ( | ||
| UUID("2cffad3a-0001-4078-b0e2-ef74274022c3"), | ||
| "https://external.service/api/path", | ||
| "https://external.service/api/path", | ||
| ), | ||
| ], | ||
| ids=[ | ||
| "legacy-relative-path", | ||
| "tenant-relative-path", | ||
| "legacy-full-url-bypass", | ||
| "tenant-full-url-bypass", | ||
| ], | ||
| ) | ||
| def test_build_url_behavior(tenant_id, path, expected): | ||
| client = OpenAEV( | ||
| "base_url", | ||
| "token", | ||
| tenant_id=tenant_id, | ||
| ) | ||
| result = client._build_url(path) | ||
| assert result == expected |
72 changes: 72 additions & 0 deletions
72
test/bdd/features/test_multi_tenant_base_daemon_propagation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from unittest.mock import MagicMock | ||
| from uuid import UUID | ||
|
|
||
| import pytest | ||
|
|
||
| from pyoaev.daemons.base_daemon import BaseDaemon | ||
|
|
||
|
|
||
| class DummyDaemon(BaseDaemon): | ||
| def _setup(self): | ||
| pass | ||
|
|
||
| def _start_loop(self): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "config_map, expected_tenant", | ||
| [ | ||
| ( | ||
| { | ||
| "openaev_url": "url", | ||
| "openaev_token": "token", | ||
| }, | ||
| None, | ||
| ), | ||
| ( | ||
| { | ||
| "openaev_url": "url", | ||
| "openaev_token": "token", | ||
| "openaev_tenant_id": None, | ||
| }, | ||
| None, | ||
| ), | ||
| ( | ||
| { | ||
| "openaev_url": "url", | ||
| "openaev_token": "token", | ||
| "openaev_tenant_id": UUID("2cffad3a-0001-4078-b0e2-ef74274022c3"), | ||
| }, | ||
| UUID("2cffad3a-0001-4078-b0e2-ef74274022c3"), | ||
| ), | ||
| ], | ||
| ids=[ | ||
| "missing_tenant_key", | ||
| "explicit_none_tenant", | ||
| "valid_uuid_tenant", | ||
| ], | ||
| ) | ||
| def test_default_api_client_propagates_tenant_id( | ||
| monkeypatch, config_map, expected_tenant | ||
| ): | ||
| captured = {} | ||
|
|
||
| def fake_client(url, token, tenant_id=None): | ||
| captured["url"] = url | ||
| captured["token"] = token | ||
| captured["tenant_id"] = tenant_id | ||
| return MagicMock() | ||
|
|
||
| monkeypatch.setattr("pyoaev.daemons.base_daemon.OpenAEV", fake_client) | ||
|
|
||
| config = MagicMock() | ||
| config.get.side_effect = lambda key: config_map.get(key) | ||
|
|
||
| daemon = DummyDaemon(configuration=config) | ||
| assert daemon.api is not None | ||
|
|
||
| assert "openaev_tenant_id" in config_map or expected_tenant is None | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look like, it's an always So it doesn't really test 'if the tenant is propagated'. |
||
| assert captured["url"] == "url" | ||
| assert captured["token"] == "token" | ||
| assert captured["tenant_id"] == expected_tenant | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could add a constraint 'tenant_id is not a valid uuid4' ?