-
Notifications
You must be signed in to change notification settings - Fork 11
Fix: assign BasicMember role in members API so members can access draft project details #1140
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
mohamedelabbas1996
wants to merge
1
commit into
main
Choose a base branch
from
fix/members-api-draft-project-access
base: main
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| from ami.main.models import Project, UserProjectMembership | ||
| from ami.tests.fixtures.main import setup_test_project | ||
| from ami.users.models import User | ||
| from ami.users.roles import BasicMember, ProjectManager, Role | ||
| from ami.users.roles import BasicMember, Identifier, ProjectManager, Researcher, Role, create_roles_for_project | ||
|
|
||
|
|
||
| class TestUserProjectMembershipAPI(APITestCase): | ||
|
|
@@ -243,3 +243,64 @@ def test_create_membership_missing_role_id(self): | |
| payload = {"email": self.user2.email} | ||
| resp = self.client.post(self.members_url, payload, format="json") | ||
| self.assertEqual(resp.status_code, 400) | ||
|
|
||
|
|
||
| class TestMembersApiDraftProjectAccess(APITestCase): | ||
| """ | ||
| Verify that members added via the Members API can access draft project details. | ||
| Regression tests for the BasicMember manual-assign fix. | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| self.project, _ = setup_test_project() | ||
| self.project.draft = True | ||
| self.project.save() | ||
| create_roles_for_project(self.project) | ||
|
|
||
| self.superuser = User.objects.create_superuser(email="[email protected]", password="x") | ||
| self.user_basic = User.objects.create_user(email="[email protected]") | ||
| self.user_identifier = User.objects.create_user(email="[email protected]") | ||
| self.user_researcher = User.objects.create_user(email="[email protected]") | ||
| self.user_project_manager = User.objects.create_user(email="[email protected]") | ||
| self.outsider = User.objects.create_user(email="[email protected]") | ||
|
|
||
| self.members_url = f"/api/v2/projects/{self.project.pk}/members/" | ||
| self.detail_url = f"/api/v2/projects/{self.project.pk}/" | ||
|
|
||
| def _add_member_and_assert_can_access_draft(self, user, role_id: str) -> None: | ||
| """Add user as role via API, then assert they can GET draft project details.""" | ||
| self.client.force_authenticate(self.superuser) | ||
| resp = self.client.post( | ||
| self.members_url, | ||
| {"email": user.email, "role_id": role_id}, | ||
| format="json", | ||
| ) | ||
| self.assertEqual(resp.status_code, 201, f"Failed to add {role_id}: {resp.json()}") | ||
| self.client.force_authenticate(user) | ||
| detail_resp = self.client.get(self.detail_url) | ||
| self.assertEqual( | ||
| detail_resp.status_code, | ||
| 200, | ||
| f"{role_id} member should access draft project, got {detail_resp.status_code}", | ||
| ) | ||
|
|
||
| def test_member_added_via_api_can_access_draft_project_basic_member(self): | ||
| self._add_member_and_assert_can_access_draft(self.user_basic, BasicMember.__name__) | ||
|
|
||
| def test_member_added_via_api_can_access_draft_project_identifier(self): | ||
| self._add_member_and_assert_can_access_draft(self.user_identifier, Identifier.__name__) | ||
|
|
||
| def test_member_added_via_api_can_access_draft_project_researcher(self): | ||
| self._add_member_and_assert_can_access_draft(self.user_researcher, Researcher.__name__) | ||
|
|
||
| def test_member_added_via_api_can_access_draft_project_manager(self): | ||
| self._add_member_and_assert_can_access_draft(self.user_project_manager, ProjectManager.__name__) | ||
|
|
||
| def test_non_member_cannot_access_draft_project(self): | ||
| self.client.force_authenticate(self.outsider) | ||
| detail_resp = self.client.get(self.detail_url) | ||
| self.assertIn( | ||
| detail_resp.status_code, | ||
| (403, 404), | ||
| "Non-member should not access draft project", | ||
| ) | ||
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.
Static analysis flag S106 (hardcoded test password) — consistent with existing pattern
Ruff flags
password="x"at this line. The identical credential is already present on line 16 in the same file (TestUserProjectMembershipAPI.setUp), so this is consistent. No production risk; flagging for awareness since the linter will surface it in CI.🧰 Tools
🪛 Ruff (0.15.1)
[error] 260-260: Possible hardcoded password assigned to argument: "password"
(S106)
🤖 Prompt for AI Agents