Skip to content

Commit

Permalink
Add REGISTER_FLOW_ENABLED setting (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed Mar 23, 2022
1 parent a7b813e commit 15e947f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions rest_registration/api/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def register(request):
'''
Register new user.
'''
if not registration_settings.REGISTER_FLOW_ENABLED:
raise Http404()
serializer_class = registration_settings.REGISTER_SERIALIZER_CLASS
serializer = serializer_class(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
Expand Down
10 changes: 10 additions & 0 deletions rest_registration/settings_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def __new__(
]

REGISTER_SETTINGS_FIELDS = [
Field(
'REGISTER_FLOW_ENABLED',
default=True,
help=dedent("""\
If enabled, then users are able to register (create new account).
One can disable it if for instance accounts should not be registered
by external users but rather should be created only by admin user.
"""),
),
Field(
'REGISTER_SERIALIZER_CLASS',
default='rest_registration.api.serializers.DefaultRegisterUserSerializer', # noqa: E501,
Expand Down
26 changes: 24 additions & 2 deletions tests/api/views/register/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
from rest_framework import status

from rest_registration.signers.register import RegisterSigner
from tests.helpers.api_views import assert_response_status_is_created
from tests.helpers.api_views import (
assert_response_is_not_found,
assert_response_status_is_created
)
from tests.helpers.constants import REGISTER_VERIFICATION_URL, VERIFICATION_FROM_EMAIL
from tests.helpers.email import assert_one_email_sent, capture_sent_emails
from tests.helpers.email import (
assert_no_email_sent,
assert_one_email_sent,
capture_sent_emails
)
from tests.helpers.settings import override_rest_registration_settings
from tests.helpers.text import assert_one_url_line_in_text
from tests.helpers.timer import capture_time
Expand Down Expand Up @@ -432,6 +439,21 @@ def test_ok_when_faulty_verification_template_selector(
assert_valid_register_verification_email(sent_email, user, timer)


@pytest.mark.django_db
@override_rest_registration_settings({
'REGISTER_FLOW_ENABLED': False,
})
def test_fail_when_register_flow_disabled(
settings_with_register_verification,
api_view_provider, api_factory):
data = _get_register_user_data(password='testpassword')
request = api_factory.create_post_request(data)
with capture_sent_emails() as sent_emails:
response = api_view_provider.view_func(request)
assert_response_is_not_found(response)
assert_no_email_sent(sent_emails)


def assert_user_state_matches_data(user, data, verified=False):
assert user.username == data['username']
assert user.email == data['email']
Expand Down

0 comments on commit 15e947f

Please sign in to comment.