Repository navigation
UN-4398 [MISC] Ask the active auth service for roles in backend tests #2372
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea6864b
test: ask the active auth service for roles instead of hardcoding them
chandrasekharan-zipstack 885e3c6
test: address self-review round 1 on the auth seam helpers
chandrasekharan-zipstack a1076b1
Merge origin/main into fix/test-seam-auth-fixtures
chandrasekharan-zipstack 4c67232
Merge branch 'main' into fix/test-seam-auth-fixtures
chandrasekharan-zipstack ef1611c
Merge branch 'main' into fix/test-seam-auth-fixtures
chandrasekharan-zipstack 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,15 +7,14 @@ | |
|
|
||
| import secrets | ||
| from types import SimpleNamespace | ||
| from unittest.mock import patch | ||
|
|
||
| from django.test import TestCase | ||
| from tenant_account_v2.models import OrganizationMember | ||
| from utils.tests.auth_seam import admin_role, member_role | ||
| from utils.user_context import UserContext | ||
|
|
||
| from account_v2.authentication_controller import AuthenticationController | ||
| from account_v2.custom_exceptions import Forbidden | ||
| from account_v2.enums import UserRole | ||
| from account_v2.models import Organization, User | ||
|
|
||
|
|
||
|
|
@@ -32,34 +31,25 @@ def setUp(self) -> None: | |
| name="org-a", display_name="Org A", organization_id="org-a" | ||
| ) | ||
| UserContext.set_organization_identifier(self.org.organization_id) | ||
| # Pin the built-in auth service: role vocabulary is plugin-defined, so | ||
| # UserRole below is only meaningful while no auth plugin is installed. | ||
| registry = patch( | ||
| "account_v2.authentication_controller." | ||
| "AuthenticationPluginRegistry.is_plugin_available", | ||
| return_value=False, | ||
| ) | ||
| registry.start() | ||
| self.addCleanup(registry.stop) | ||
| self.controller = AuthenticationController() | ||
| self.admin = _make_member(self.org, "[email protected]", UserRole.ADMIN.value) | ||
| self.admin = _make_member(self.org, "[email protected]", admin_role()) | ||
|
|
||
| def test_sole_admin_cannot_be_demoted(self) -> None: | ||
| with self.assertRaises(Forbidden): | ||
| self.controller._ensure_not_last_admin_demotion( | ||
| email=self.admin.user.email, new_role=UserRole.USER.value | ||
| email=self.admin.user.email, new_role=member_role() | ||
| ) | ||
|
|
||
| def test_demotion_allowed_when_another_admin_remains(self) -> None: | ||
| _make_member(self.org, "[email protected]", UserRole.ADMIN.value) | ||
| _make_member(self.org, "[email protected]", admin_role()) | ||
| self.controller._ensure_not_last_admin_demotion( | ||
| email=self.admin.user.email, new_role=UserRole.USER.value | ||
| email=self.admin.user.email, new_role=member_role() | ||
| ) | ||
|
|
||
| def test_non_admin_role_change_is_unaffected(self) -> None: | ||
| member = _make_member(self.org, "[email protected]", UserRole.USER.value) | ||
| member = _make_member(self.org, "[email protected]", member_role()) | ||
| self.controller._ensure_not_last_admin_demotion( | ||
| email=member.user.email, new_role=UserRole.USER.value | ||
| email=member.user.email, new_role=member_role() | ||
| ) | ||
|
|
||
| def test_add_user_role_blocks_sole_admin_demotion(self) -> None: | ||
|
|
@@ -69,7 +59,7 @@ def test_add_user_role_blocks_sole_admin_demotion(self) -> None: | |
| request, | ||
| self.org.organization_id, | ||
| self.admin.user.email, | ||
| UserRole.USER.value, | ||
| member_role(), | ||
| ) | ||
|
|
||
| def test_remove_user_role_blocks_sole_admin_demotion(self) -> None: | ||
|
|
@@ -79,5 +69,5 @@ def test_remove_user_role_blocks_sole_admin_demotion(self) -> None: | |
| request, | ||
| self.org.organization_id, | ||
| self.admin.user.email, | ||
| UserRole.ADMIN.value, | ||
| admin_role(), | ||
| ) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,23 +9,16 @@ | |
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
| from unittest.mock import patch | ||
|
|
||
| from account_v2.models import Organization, User | ||
| from permissions.permission import IsOwner | ||
| from rest_framework.test import APIRequestFactory | ||
| from rest_framework.views import APIView | ||
| from tenant_account_v2.models import OrganizationMember | ||
| from utils.tests.auth_seam import admin_role, member_role | ||
| from utils.user_context import UserContext | ||
| from workflow_manager.workflow_v2.models.workflow import Workflow | ||
|
|
||
| # Admin resolution is auth-plugin backed, so patch it to a deterministic | ||
| # predicate — the ``role="admin"`` membership row alone does not make | ||
| # ``_is_organization_admin`` true. | ||
| _ADMIN_PREDICATE = ( | ||
| "tenant_account_v2.organization_member_service." | ||
| "OrganizationMemberService.is_user_organization_admin" | ||
| ) | ||
| from permissions.permission import IsOwner | ||
|
|
||
|
|
||
| def make_user(email: str, **kwargs: Any) -> User: | ||
|
|
@@ -120,7 +113,7 @@ class ResourceSpec: | |
|
|
||
|
|
||
| class CoOwnerOrgTestMixin: | ||
| """setUp helper: one org, members, and a deterministic admin predicate.""" | ||
| """setUp helper: one org and its members, admin by the active auth service's role.""" | ||
|
|
||
| def _seed_org(self) -> None: | ||
| self.org = Organization.objects.create( | ||
|
|
@@ -135,18 +128,11 @@ def _seed_org(self) -> None: | |
| self.stranger = make_user("[email protected]") # NOT an org member | ||
| for user in (self.owner, self.coowner, self.viewer, self.outsider): | ||
| OrganizationMember.objects.create( | ||
| organization=self.org, user=user, role="user" | ||
| organization=self.org, user=user, role=member_role() | ||
| ) | ||
| OrganizationMember.objects.create( | ||
| organization=self.org, user=self.admin, role="admin" | ||
| ) | ||
| patcher = patch( | ||
| _ADMIN_PREDICATE, | ||
| side_effect=lambda u: getattr(u, "email", None) == self.admin.email, | ||
| organization=self.org, user=self.admin, role=admin_role() | ||
| ) | ||
| patcher.start() | ||
| # addCleanup is provided by TestCase (this is a mixin combined with it). | ||
| self.addCleanup(patcher.stop) # type: ignore[attr-defined] | ||
|
|
||
| def _is_owner_perm(self, user: User, obj: object) -> bool: | ||
| """Run ``IsOwner.has_object_permission`` for ``user`` against ``obj``.""" | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.