-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'prevent-user-registration-without-invite' into 'develop'
Prevent user registration without invite See merge request flagsmith/bullet-train-api!365
- Loading branch information
Showing
9 changed files
with
240 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
USER_REGISTRATION_WITHOUT_INVITE_ERROR_MESSAGE = ( | ||
"User registration without an invite is disabled for this installation." | ||
) |
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
from unittest import mock | ||
|
||
from django.test import override_settings | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
from organisations.invites.models import Invite | ||
from organisations.models import Organisation | ||
|
||
|
||
@mock.patch("custom_auth.oauth.serializers.get_user_info") | ||
@override_settings(ALLOW_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_cannot_register_with_google_without_invite_if_registration_disabled( | ||
mock_get_user_info, db | ||
): | ||
# Given | ||
url = reverse(f"api-v1:custom_auth:oauth:google-oauth-login") | ||
client = APIClient() | ||
|
||
email = "[email protected]" | ||
mock_get_user_info.return_value = {"email": email} | ||
|
||
# When | ||
response = client.post(url, data={"access_token": "some-token"}) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
|
||
@mock.patch("custom_auth.oauth.serializers.GithubUser") | ||
@override_settings(ALLOW_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_cannot_register_with_github_without_invite_if_registration_disabled( | ||
MockGithubUser, db | ||
): | ||
# Given | ||
url = reverse(f"api-v1:custom_auth:oauth:github-oauth-login") | ||
client = APIClient() | ||
|
||
email = "[email protected]" | ||
mock_github_user = mock.MagicMock() | ||
MockGithubUser.return_value = mock_github_user | ||
mock_github_user.get_user_info.return_value = {"email": email} | ||
|
||
# When | ||
response = client.post(url, data={"access_token": "some-token"}) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
|
||
@mock.patch("custom_auth.oauth.serializers.get_user_info") | ||
@override_settings(ALLOW_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_can_register_with_google_with_invite_if_registration_disabled( | ||
mock_get_user_info, db | ||
): | ||
# Given | ||
url = reverse(f"api-v1:custom_auth:oauth:google-oauth-login") | ||
client = APIClient() | ||
|
||
email = "[email protected]" | ||
mock_get_user_info.return_value = {"email": email} | ||
organisation = Organisation.objects.create(name="Test Org") | ||
Invite.objects.create(organisation=organisation, email=email) | ||
|
||
# When | ||
response = client.post(url, data={"access_token": "some-token"}) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
|
||
|
||
@mock.patch("custom_auth.oauth.serializers.GithubUser") | ||
@override_settings(ALLOW_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_can_register_with_github_with_invite_if_registration_disabled( | ||
MockGithubUser, db | ||
): | ||
# Given | ||
url = reverse(f"api-v1:custom_auth:oauth:github-oauth-login") | ||
client = APIClient() | ||
|
||
email = "[email protected]" | ||
mock_github_user = mock.MagicMock() | ||
MockGithubUser.return_value = mock_github_user | ||
mock_github_user.get_user_info.return_value = {"email": email} | ||
organisation = Organisation.objects.create(name="Test Org") | ||
Invite.objects.create(organisation=organisation, email=email) | ||
|
||
# When | ||
response = client.post(url, data={"access_token": "some-token"}) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
|
||
|
||
@mock.patch("custom_auth.oauth.serializers.get_user_info") | ||
@override_settings(ALLOW_OAUTH_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_can_login_with_google_if_registration_disabled( | ||
mock_get_user_info, db, django_user_model | ||
): | ||
# Given | ||
url = reverse(f"api-v1:custom_auth:oauth:google-oauth-login") | ||
client = APIClient() | ||
|
||
email = "[email protected]" | ||
mock_get_user_info.return_value = {"email": email} | ||
django_user_model.objects.create(email=email) | ||
|
||
# When | ||
response = client.post(url, data={"access_token": "some-token"}) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
assert "key" in response.json() | ||
|
||
|
||
@mock.patch("custom_auth.oauth.serializers.GithubUser") | ||
@override_settings(ALLOW_OAUTH_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_can_login_with_github_if_registration_disabled( | ||
MockGithubUser, db, django_user_model | ||
): | ||
# Given | ||
url = reverse(f"api-v1:custom_auth:oauth:github-oauth-login") | ||
client = APIClient() | ||
|
||
email = "[email protected]" | ||
mock_github_user = mock.MagicMock() | ||
MockGithubUser.return_value = mock_github_user | ||
mock_github_user.get_user_info.return_value = {"email": email} | ||
django_user_model.objects.create(email=email) | ||
|
||
# When | ||
response = client.post(url, data={"access_token": "some-token"}) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_200_OK | ||
assert "key" in response.json() |
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 |
---|---|---|
@@ -1,21 +1,27 @@ | ||
import re | ||
|
||
import time | ||
from collections import ChainMap | ||
from unittest import mock | ||
|
||
import pyotp | ||
from django.conf import settings | ||
from django.core import mail | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase, override_settings | ||
from rest_framework.test import APIClient, APITestCase, override_settings | ||
|
||
from organisations.invites.models import Invite | ||
from organisations.models import Organisation | ||
from users.models import FFAdminUser | ||
|
||
|
||
class AuthIntegrationTestCase(APITestCase): | ||
test_email = "[email protected]" | ||
password = FFAdminUser.objects.make_random_password() | ||
|
||
def setUp(self) -> None: | ||
self.organisation = Organisation.objects.create(name="Test Organisation") | ||
|
||
def tearDown(self) -> None: | ||
FFAdminUser.objects.all().delete() | ||
|
||
|
@@ -93,6 +99,41 @@ def test_register_and_login_workflows(self): | |
assert new_login_response.status_code == status.HTTP_200_OK | ||
assert new_login_response.json()["key"] | ||
|
||
@override_settings(ALLOW_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_cannot_register_without_invite_if_disabled(self): | ||
# Given | ||
register_data = { | ||
"email": self.test_email, | ||
"password": self.password, | ||
"first_name": "test", | ||
"last_name": "register", | ||
} | ||
|
||
# When | ||
url = reverse("api-v1:custom_auth:ffadminuser-list") | ||
response = self.client.post(url, data=register_data) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_403_FORBIDDEN | ||
|
||
@override_settings(ALLOW_REGISTRATION_WITHOUT_INVITE=False) | ||
def test_can_register_with_invite_if_registration_disabled_without_invite(self): | ||
# Given | ||
register_data = { | ||
"email": self.test_email, | ||
"password": self.password, | ||
"first_name": "test", | ||
"last_name": "register", | ||
} | ||
Invite.objects.create(email=self.test_email, organisation=self.organisation) | ||
|
||
# When | ||
url = reverse("api-v1:custom_auth:ffadminuser-list") | ||
response = self.client.post(url, data=register_data) | ||
|
||
# Then | ||
assert response.status_code == status.HTTP_201_CREATED | ||
|
||
@override_settings( | ||
DJOSER=ChainMap( | ||
{"SEND_ACTIVATION_EMAIL": True, "SEND_CONFIRMATION_EMAIL": False}, | ||
|