-
Notifications
You must be signed in to change notification settings - Fork 515
fix: prevent signup blocking invitation signups #5507
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0dac490
fix: added-invitation-permission-in-is-signup-allowed
Zaimwa9 7db2b3f
fix: parametrized-tests-and-show-error-in-frontend
Zaimwa9 73e5711
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f5c0b6f
fix: misc-improvements
Zaimwa9 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| from django.conf import settings | ||
| from django.views import View | ||
| from rest_framework.exceptions import PermissionDenied | ||
| from rest_framework.permissions import AllowAny, IsAuthenticated | ||
| from rest_framework.request import Request | ||
|
|
||
| from organisations.invites.models import Invite, InviteLink | ||
|
|
||
|
|
||
| class CurrentUser(IsAuthenticated): | ||
| """ | ||
|
|
@@ -17,8 +20,27 @@ def has_object_permission(self, request, view, obj): # type: ignore[no-untyped- | |
|
|
||
|
|
||
| class IsSignupAllowed(AllowAny): | ||
| message = "Signing up without an invitation is disabled. Please contact your administrator." | ||
|
|
||
| def has_permission(self, request: Request, view: View) -> bool: | ||
| return not settings.PREVENT_SIGNUP | ||
| if not settings.PREVENT_SIGNUP: | ||
| return True | ||
|
|
||
| email = request.data.get("email") | ||
| if email and Invite.objects.filter(email__iexact=email).exists(): | ||
| return True | ||
|
|
||
| invite_hash = request.data.get("invite_hash") | ||
| if invite_hash: | ||
| try: | ||
| invite_link = InviteLink.objects.get(hash=invite_hash) | ||
| except InviteLink.DoesNotExist: | ||
| pass | ||
|
|
||
| if not invite_link.is_expired: | ||
| return True | ||
|
|
||
| raise PermissionDenied(self.message) | ||
|
Contributor
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. Following returning |
||
|
|
||
|
|
||
| class IsPasswordLoginAllowed(AllowAny): | ||
|
|
||
121 changes: 121 additions & 0 deletions
121
api/tests/unit/custom_auth/test_unit_custom_auth_permissions.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,121 @@ | ||
| from datetime import timedelta | ||
| from typing import Any, Callable | ||
|
|
||
| import pytest | ||
| from django.urls import reverse | ||
| from django.utils import timezone | ||
| from rest_framework import status | ||
| from rest_framework.test import ( # type: ignore[attr-defined] | ||
| APIClient, | ||
| override_settings, | ||
| ) | ||
|
|
||
| from organisations.invites.models import Invite, InviteLink | ||
| from organisations.models import Organisation | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def signup_data() -> dict[str, Any]: | ||
| return { | ||
| "email": "test@example.com", | ||
| "password": "testpass123", | ||
| "first_name": "Test", | ||
| "last_name": "User", | ||
| } | ||
|
|
||
|
|
||
| def test_signup_allowed_when_prevent_signup_disabled( | ||
| api_client: APIClient, signup_data: dict[str, Any], db: None | ||
| ) -> None: | ||
| # Given | ||
| url = reverse("api-v1:custom_auth:ffadminuser-list") | ||
|
|
||
| # When | ||
| response = api_client.post(url, data=signup_data) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_201_CREATED | ||
|
|
||
|
|
||
| @override_settings(PREVENT_SIGNUP=True) # type: ignore[misc] | ||
| def test_signup_blocked_when_prevent_signup_enabled_and_no_invitation( | ||
| api_client: APIClient, signup_data: dict[str, Any], db: None | ||
| ) -> None: | ||
| # Given | ||
| url = reverse("api-v1:custom_auth:ffadminuser-list") | ||
|
|
||
| # When | ||
| response = api_client.post(url, data=signup_data) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_403_FORBIDDEN | ||
| assert ( | ||
| str(response.data["detail"]) | ||
| == "Signing up without an invitation is disabled. Please contact your administrator." | ||
| ) | ||
|
|
||
|
|
||
| @override_settings(PREVENT_SIGNUP=True) # type: ignore[misc] | ||
| def test_signup_allowed_with_email_invite( | ||
| api_client: APIClient, signup_data: dict[str, Any], db: None | ||
| ) -> None: | ||
| # Given | ||
| organisation = Organisation.objects.create(name="Test Org") | ||
| Invite.objects.create(email=signup_data["email"], organisation=organisation) | ||
| url = reverse("api-v1:custom_auth:ffadminuser-list") | ||
|
|
||
| # When | ||
| response = api_client.post(url, data=signup_data) | ||
|
|
||
| # Then | ||
| assert response.status_code == status.HTTP_201_CREATED | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "get_invite_hash, expected_status, expected_detail", | ||
| [ | ||
| ( | ||
| lambda organisation: InviteLink.objects.create( | ||
| organisation=organisation | ||
| ).hash, | ||
| status.HTTP_201_CREATED, | ||
| None, | ||
| ), | ||
| ( | ||
| lambda _: "invalid-hash", | ||
| status.HTTP_403_FORBIDDEN, | ||
| "Signing up without an invitation is disabled. Please contact your administrator.", | ||
| ), | ||
| ( | ||
| lambda organisation: InviteLink.objects.create( | ||
| organisation=organisation, | ||
| expires_at=timezone.now() - timedelta(days=1), | ||
| ), | ||
| status.HTTP_403_FORBIDDEN, | ||
| "Signing up without an invitation is disabled. Please contact your administrator.", | ||
| ), | ||
| ], | ||
| ) | ||
| @override_settings(PREVENT_SIGNUP=True) # type: ignore[misc] | ||
| def test_signup_with_invite_hash_behavior( | ||
| api_client: APIClient, | ||
| signup_data: dict[str, Any], | ||
| db: None, | ||
| get_invite_hash: Callable[[Organisation], str], | ||
| expected_status: int, | ||
| expected_detail: str, | ||
| organisation: Organisation, | ||
| ) -> None: | ||
| # Given | ||
| invite_hash = get_invite_hash(organisation) | ||
|
|
||
| signup_data_with_hash = {**signup_data, "invite_hash": invite_hash} | ||
| url = reverse("api-v1:custom_auth:ffadminuser-list") | ||
|
|
||
| # When | ||
| response = api_client.post(url, data=signup_data_with_hash) | ||
|
|
||
| # Then | ||
| assert response.status_code == expected_status | ||
| if expected_detail: | ||
| assert str(response.data["detail"]) == expected_detail |
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
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.