forked from RedHatInsights/insights-rbac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request RedHatInsights#1459 from astrozzc/user_id
[RHCLOUD-37452] Get user id if not set
- Loading branch information
Showing
5 changed files
with
114 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
"""Tenant management business logic.""" | ||
|
||
from typing import Callable, Optional | ||
|
||
from django.conf import settings | ||
from management.relation_replicator.relation_replicator import RelationReplicator | ||
from management.tenant_service.tenant_service import TenantBootstrapService | ||
from management.tenant_service.v1 import V1TenantBootstrapService | ||
from management.tenant_service.v2 import V2TenantBootstrapService | ||
|
||
from api.models import User | ||
|
||
|
||
def get_tenant_bootstrap_service(replicator: RelationReplicator) -> "TenantBootstrapService": | ||
def get_tenant_bootstrap_service( | ||
replicator: RelationReplicator, get_user_id: Optional[Callable[[User], str]] = None | ||
) -> "TenantBootstrapService": | ||
"""Get a TenantBootstrapService instance based on settings.""" | ||
return V2TenantBootstrapService(replicator) if settings.V2_BOOTSTRAP_TENANT else V1TenantBootstrapService() | ||
return ( | ||
V2TenantBootstrapService(replicator, get_user_id=get_user_id) | ||
if settings.V2_BOOTSTRAP_TENANT | ||
else V1TenantBootstrapService() | ||
) |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -776,6 +776,45 @@ def test_bootstraps_tenants_if_not_existing(self): | |
), | ||
) | ||
|
||
@patch( | ||
"management.principal.proxy.PrincipalProxy.request_filtered_principals", | ||
return_value={ | ||
"status_code": 200, | ||
"data": [ | ||
{ | ||
"username": "user_1", | ||
"email": "[email protected]", | ||
"first_name": "user", | ||
"last_name": "test", | ||
"user_id": "u1", | ||
"org_id": "12345", | ||
} | ||
], | ||
}, | ||
) | ||
def test_bootstraps_tenants_if_user_id_is_missing(self, _): | ||
with patch("rbac.middleware.OutboxReplicator", new=partial(InMemoryRelationReplicator, self._tuples)): | ||
# Change the user's org so we create a new tenant | ||
self.request.user.org_id = "12345" | ||
self.org_id = "12345" | ||
self.request.user.id = None | ||
mock_request = self.request | ||
tenant_cache = TenantCache() | ||
tenant_cache.delete_tenant(self.org_id) | ||
middleware = IdentityHeaderMiddleware(get_response=IdentityHeaderMiddleware.get_tenant) | ||
result = middleware.get_tenant(Tenant, "localhost", mock_request) | ||
self.assertEqual(result.org_id, mock_request.user.org_id) | ||
tenant = Tenant.objects.get(org_id=self.org_id) | ||
self.assertIsNotNone(tenant) | ||
mapping = TenantMapping.objects.get(tenant=tenant) | ||
self.assertIsNotNone(mapping) | ||
workspaces = list(Workspace.objects.filter(tenant=tenant)) | ||
self.assertEqual(len(workspaces), 2) | ||
default = Workspace.objects.get(type=Workspace.Types.DEFAULT, tenant=tenant) | ||
self.assertIsNotNone(default) | ||
root = Workspace.objects.get(type=Workspace.Types.ROOT, tenant=tenant) | ||
self.assertIsNotNone(root) | ||
|
||
|
||
@override_settings(V2_BOOTSTRAP_TENANT=True) | ||
class V2IdentityHeaderMiddlewareTest(IdentityHeaderMiddlewareTest): | ||
|