Skip to content

Commit 15e947f

Browse files
committed
Add REGISTER_FLOW_ENABLED setting (#186)
1 parent a7b813e commit 15e947f

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

rest_registration/api/views/register.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def register(request):
3131
'''
3232
Register new user.
3333
'''
34+
if not registration_settings.REGISTER_FLOW_ENABLED:
35+
raise Http404()
3436
serializer_class = registration_settings.REGISTER_SERIALIZER_CLASS
3537
serializer = serializer_class(data=request.data, context={'request': request})
3638
serializer.is_valid(raise_exception=True)

rest_registration/settings_fields.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ def __new__(
6666
]
6767

6868
REGISTER_SETTINGS_FIELDS = [
69+
Field(
70+
'REGISTER_FLOW_ENABLED',
71+
default=True,
72+
help=dedent("""\
73+
If enabled, then users are able to register (create new account).
74+
75+
One can disable it if for instance accounts should not be registered
76+
by external users but rather should be created only by admin user.
77+
"""),
78+
),
6979
Field(
7080
'REGISTER_SERIALIZER_CLASS',
7181
default='rest_registration.api.serializers.DefaultRegisterUserSerializer', # noqa: E501,

tests/api/views/register/test_register.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
from rest_framework import status
1010

1111
from rest_registration.signers.register import RegisterSigner
12-
from tests.helpers.api_views import assert_response_status_is_created
12+
from tests.helpers.api_views import (
13+
assert_response_is_not_found,
14+
assert_response_status_is_created
15+
)
1316
from tests.helpers.constants import REGISTER_VERIFICATION_URL, VERIFICATION_FROM_EMAIL
14-
from tests.helpers.email import assert_one_email_sent, capture_sent_emails
17+
from tests.helpers.email import (
18+
assert_no_email_sent,
19+
assert_one_email_sent,
20+
capture_sent_emails
21+
)
1522
from tests.helpers.settings import override_rest_registration_settings
1623
from tests.helpers.text import assert_one_url_line_in_text
1724
from tests.helpers.timer import capture_time
@@ -432,6 +439,21 @@ def test_ok_when_faulty_verification_template_selector(
432439
assert_valid_register_verification_email(sent_email, user, timer)
433440

434441

442+
@pytest.mark.django_db
443+
@override_rest_registration_settings({
444+
'REGISTER_FLOW_ENABLED': False,
445+
})
446+
def test_fail_when_register_flow_disabled(
447+
settings_with_register_verification,
448+
api_view_provider, api_factory):
449+
data = _get_register_user_data(password='testpassword')
450+
request = api_factory.create_post_request(data)
451+
with capture_sent_emails() as sent_emails:
452+
response = api_view_provider.view_func(request)
453+
assert_response_is_not_found(response)
454+
assert_no_email_sent(sent_emails)
455+
456+
435457
def assert_user_state_matches_data(user, data, verified=False):
436458
assert user.username == data['username']
437459
assert user.email == data['email']

0 commit comments

Comments
 (0)