Skip to content

Commit

Permalink
fix: allow registration via invite link if ALLOW_REGISTRATION_WITHOUT…
Browse files Browse the repository at this point in the history
…_INVITE is False (#2731)
  • Loading branch information
matthewelwell authored Sep 5, 2023
1 parent 33e7c17 commit 73705d5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
6 changes: 3 additions & 3 deletions api/custom_auth/oauth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def _get_user(self, user_data: dict):
existing_user = UserModel.objects.filter(email=email).first()

if not existing_user:
sign_up_type = self.validated_data.get("sign_up_type")
if not (
settings.ALLOW_REGISTRATION_WITHOUT_INVITE
or sign_up_type == SignUpType.INVITE_LINK.value
or Invite.objects.filter(email=email).exists()
):
raise PermissionDenied(USER_REGISTRATION_WITHOUT_INVITE_ERROR_MESSAGE)

return UserModel.objects.create(
**user_data, sign_up_type=self.validated_data.get("sign_up_type")
)
return UserModel.objects.create(**user_data, sign_up_type=sign_up_type)

return existing_user

Expand Down
31 changes: 31 additions & 0 deletions api/custom_auth/oauth/tests/test_unit_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
from django.contrib.auth import get_user_model
from django.test import RequestFactory
from django.utils import timezone
from pytest_django.fixtures import SettingsWrapper
from pytest_mock import MockerFixture
from rest_framework.authtoken.models import Token

from custom_auth.oauth.serializers import (
GithubLoginSerializer,
GoogleLoginSerializer,
OAuthLoginSerializer,
)
from users.models import SignUpType

UserModel = get_user_model()

Expand Down Expand Up @@ -131,3 +134,31 @@ def test_OAuthLoginSerializer_calls_is_authentication_method_valid_correctly_if_
email=user_email,
raise_exception=True,
)


def test_OAuthLoginSerializer_allows_registration_if_sign_up_type_is_invite_link(
settings: SettingsWrapper, rf: RequestFactory, mocker: MockerFixture, db: None
):
# Given
settings.ALLOW_REGISTRATION_WITHOUT_INVITE = False

request = rf.post("/api/v1/auth/users/")
user_email = "[email protected]"

serializer = OAuthLoginSerializer(
data={
"access_token": "some_token",
"sign_up_type": SignUpType.INVITE_LINK.value,
},
context={"request": request},
)
# monkey patch the get_user_info method to return the mock user data
serializer.get_user_info = lambda: {"email": user_email}

serializer.is_valid(raise_exception=True)

# When
user = serializer.save()

# Then
assert user
3 changes: 2 additions & 1 deletion api/custom_auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from organisations.invites.models import Invite
from users.constants import DEFAULT_DELETE_ORPHAN_ORGANISATIONS_VALUE
from users.models import FFAdminUser
from users.models import FFAdminUser, SignUpType

from .constants import USER_REGISTRATION_WITHOUT_INVITE_ERROR_MESSAGE

Expand Down Expand Up @@ -64,6 +64,7 @@ def get_key(instance):
def save(self, **kwargs):
if not (
settings.ALLOW_REGISTRATION_WITHOUT_INVITE
or self.validated_data.get("sign_up_type") == SignUpType.INVITE_LINK.value
or Invite.objects.filter(email=self.validated_data.get("email"))
):
raise PermissionDenied(USER_REGISTRATION_WITHOUT_INVITE_ERROR_MESSAGE)
Expand Down
30 changes: 29 additions & 1 deletion api/custom_auth/tests/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.test import RequestFactory
from pytest_django.fixtures import SettingsWrapper

from custom_auth.serializers import CustomUserCreateSerializer
from users.models import FFAdminUser
from users.models import FFAdminUser, SignUpType

user_dict = {
"email": "[email protected]",
Expand Down Expand Up @@ -53,3 +56,28 @@ def test_CustomUserCreateSerializer_calls_is_authentication_method_valid_correct
email=user_dict["email"],
raise_exception=True,
)


def test_CustomUserCreateSerializer_allows_registration_if_sign_up_type_is_invite_link(
db: None,
settings: SettingsWrapper,
rf: RequestFactory,
) -> None:
# Given
settings.ALLOW_REGISTRATION_WITHOUT_INVITE = False

data = {
**user_dict,
"sign_up_type": SignUpType.INVITE_LINK.value,
}

serializer = CustomUserCreateSerializer(
data=data, context={"request": rf.post("/v1/auth/users/")}
)
assert serializer.is_valid()

# When
user = serializer.save()

# Then
assert user

3 comments on commit 73705d5

@vercel
Copy link

@vercel vercel bot commented on 73705d5 Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 73705d5 Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs.bullet-train.io
docs-flagsmith.vercel.app
docs.flagsmith.com

@vercel
Copy link

@vercel vercel bot commented on 73705d5 Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.