Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Properly block registration via the REST API when the appropriate setting is set

Roland Geider5df4ed9677a9
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index 055f7ff97..197ad4156 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -343,6 +343,12 @@ class UserAPIRegistrationViewSet(viewsets.ViewSet):
},
)
def post(self, request):
+ if not settings.WGER_SETTINGS['ALLOW_REGISTRATION']:
+ return Response(
+ {'message': 'Registration is not allowed on this instance'},
+ status=status.HTTP_200_OK,
+ )
+
data = request.data
serializer = self.serializer_class(data=data)
serializer.is_valid(raise_exception=True)
diff --git a/wger/core/tests/test_registration.py b/wger/core/tests/test_registration.py
index 3804229f8..fe27835d4 100644
--- a/wger/core/tests/test_registration.py
+++ b/wger/core/tests/test_registration.py
@@ -14,11 +14,21 @@
# Standard Library
import logging
+from io import StringIO
# Django
from django.contrib.auth.models import User
+from django.core.management import call_command
from django.urls import reverse
+# Third Party
+from rest_framework.authtoken.models import Token
+from rest_framework.status import (
+ HTTP_200_OK,
+ HTTP_201_CREATED,
+ HTTP_400_BAD_REQUEST,
+)
+
# wger
from wger.core.forms import (
RegistrationForm,
@@ -32,7 +42,7 @@ logger = logging.getLogger(__name__)
class RegistrationTestCase(WgerTestCase):
"""
- Tests registering a new user
+ Tests registering a new user via the registration form
"""
def test_registration_captcha(self):
@@ -200,3 +210,160 @@ class RegistrationTestCase(WgerTestCase):
count_after = User.objects.count()
self.assertEqual(response.status_code, 302)
self.assertEqual(count_before, count_after)
+
+
+class ApiRegistrationTestCase(WgerTestCase):
+ def test_post_valid_api_user_creation(self):
+ """Successfully register a user via the REST API"""
+
+ # Act
+ count_before = User.objects.count()
+ response = self.client.post(
+ reverse('api_register'),
+ {'username': 'restapi', 'email': '[email protected]', 'password': 'AekaiLe0ga'},
+ )
+ count_after = User.objects.count()
+
+ # Assert
+ self.assertEqual(response.status_code, HTTP_201_CREATED)
+ token = Token.objects.get(user__username='restapi')
+ self.assertEqual(response.data['message'], 'api user successfully registered')
+ self.assertEqual(response.data['token'], token.key)
+ self.assertEqual(count_after, count_before + 1)
+
+ def test_post_valid_api_user_creation_no_email(self):
+ """Successfully register a user via the REST API without providing an email"""
+
+ # Act
+ count_before = User.objects.count()
+ response = self.client.post(
+ reverse('api_register'),
+ {'username': 'restapi', 'password': 'AekaiLe0ga'},
+ )
+ count_after = User.objects.count()
+
+ # Assert
+ self.assertEqual(response.status_code, HTTP_201_CREATED)
+ token = Token.objects.get(user__username='restapi')
+ self.assertEqual(response.data['message'], 'api user successfully registered')
+ self.assertEqual(response.data['token'], token.key)
+ self.assertEqual(count_after, count_before + 1)
+
+ def test_post_unsuccessfully_registration_no_username(self):
+ """Test unsuccessful registration (weak password)"""
+
+ # Act
+ response = self.client.post(
+ reverse('api_register'),
+ {'password': 'AekaiLe0ga'},
+ )
+
+ # Assert
+ self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)
+
+ def test_post_unsuccessfully_registration_invalid_email(self):
+ """Test unsuccessful registration (invalid email)"""
+
+ # Act
+ response = self.client.post(
+ reverse('api_register'),
+ {'username': 'restapi', 'email': 'example.com', 'password': 'AekaiLe0ga'},
+ )
+
+ # Assert
+ self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)
+
+ def test_post_unsuccessfully_registration_invalid_email_2(self):
+ """Test unsuccessful registration (email already exists)"""
+
+ # Act
+ response = self.client.post(
+ reverse('api_register'),
+ {'username': 'restapi', 'email': '[email protected]', 'password': 'AekaiLe0ga'},
+ )
+
+ # Assert
+ self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)
+
+ def test_post_valid_data_registration_deactivated(self):
+ """When the registration is deactivated no users can be registered via the API"""
+
+ with self.settings(
+ WGER_SETTINGS={
+ 'ALLOW_GUEST_USERS': False,
+ 'ALLOW_REGISTRATION': False,
+ }
+ ):
+ # Act
+ count_before = User.objects.count()
+ response = self.client.post(
+ reverse('api_register'),
+ {'username': 'restapi', 'email': '[email protected]', 'password': 'AekaiLe0ga'},
+ )
+ count_after = User.objects.count()
+
+ # Assert
+ self.assertEqual(response.status_code, HTTP_200_OK)
+ self.assertEqual(count_before, count_after)
+
+ def test_post_invalid_data_registration_deactivated(self):
+ """When the registration is deactivated the data sent is ignored"""
+
+ with self.settings(
+ WGER_SETTINGS={
+ 'ALLOW_GUEST_USERS': False,
+ 'ALLOW_REGISTRATION': False,
+ }
+ ):
+ # Act
+ count_before = User.objects.count()
+ response = self.client.post(
+ reverse('api_register'),
+ {'foo': 'bar', 'bar': True, 'answer': 42},
+ )
+ count_after = User.objects.count()
+
+ # Assert
+ self.assertEqual(response.status_code, HTTP_200_OK)
+ self.assertEqual(count_before, count_after)
+
+
+class CreateUserCommand(WgerTestCase):
+ def setUp(self):
+ super(CreateUserCommand, self).setUp()
+ self.out = StringIO()
+
+ def test_access_api_user_creation(self):
+ """Tests giving permission to register users via API"""
+
+ user = User.objects.get(username='admin')
+ self.assertFalse(user.userprofile.can_add_user)
+
+ call_command('add-user-rest', 'admin', stdout=self.out, no_color=True)
+ self.assertEqual('admin is now ALLOWED to add users via the API\n', self.out.getvalue())
+
+ user = User.objects.get(username='admin')
+ self.assertTrue(user.userprofile.can_add_user)
+
+ def test_revoke_access_api_user_creation(self):
+ """Tests revoking permission to register users via API"""
+
+ user = User.objects.get(username='test')
+ self.assertTrue(user.userprofile.can_add_user)
+
+ call_command('add-user-rest', 'test', disable=True, stdout=self.out, no_color=True)
+ self.assertEqual(
+ 'test is now DISABLED from adding users via the API\n',
+ self.out.getvalue(),
+ )
+
+ user = User.objects.get(username='test')
+ self.assertFalse(user.userprofile.can_add_user)
+
+ def test_access_get_api_users(self):
+ """Tests listing created users via API"""
+
+ call_command('list-users-api', stdout=self.out, no_color=True)
+ self.assertIn('Users created by test:', self.out.getvalue())
+ self.assertIn('- trainer1', self.out.getvalue())
+ self.assertIn('- trainer2', self.out.getvalue())
diff --git a/wger/core/tests/test_user_rest_registration.py b/wger/core/tests/test_user_rest_registration.py
deleted file mode 100644
index fefdd5f63..000000000
--- a/wger/core/tests/test_user_rest_registration.py
+++ /dev/null
@@ -1,142 +0,0 @@
-# Standard Library
-from io import StringIO
-
-# Django
-from django.contrib.auth.models import User
-from django.core.management import call_command
-from django.urls import reverse
-
-# Third Party
-from rest_framework.authtoken.models import Token
-from rest_framework.status import (
- HTTP_201_CREATED,
- HTTP_400_BAD_REQUEST,
-)
-
-# wger
-from wger.core.tests.base_testcase import WgerTestCase
-
-
-class CreateUserCommand(WgerTestCase):
- def setUp(self):
- super(CreateUserCommand, self).setUp()
- self.out = StringIO()
-
- def test_access_api_user_creation(self):
- """Tests giving permission to register users via API"""
-
- user = User.objects.get(username='admin')
- self.assertFalse(user.userprofile.can_add_user)
-
- call_command('add-user-rest', 'admin', stdout=self.out, no_color=True)
- self.assertEqual('admin is now ALLOWED to add users via the API\n', self.out.getvalue())
-
- user = User.objects.get(username='admin')
- self.assertTrue(user.userprofile.can_add_user)
-
- def test_revoke_access_api_user_creation(self):
- """Tests revoking permission to register users via API"""
-
- user = User.objects.get(username='test')
- self.assertTrue(user.userprofile.can_add_user)
-
- call_command('add-user-rest', 'test', disable=True, stdout=self.out, no_color=True)
- self.assertEqual(
- 'test is now DISABLED from adding users via the API\n',
- self.out.getvalue(),
- )
-
- user = User.objects.get(username='test')
- self.assertFalse(user.userprofile.can_add_user)
-
- def test_access_get_api_users(self):
- """Tests listing created users via API"""
-
- call_command('list-users-api', stdout=self.out, no_color=True)
- self.assertIn('Users created by test:', self.out.getvalue())
- self.assertIn('- trainer1', self.out.getvalue())
- self.assertIn('- trainer2', self.out.getvalue())
-
- def test_post_valid_api_user_creation(self):
- """Successfully register a user via the REST API"""
-
- self.user_login('test')
- user = User.objects.get(username='test')
- token = Token.objects.get(user=user)
- count_before = User.objects.count()
-
- response = self.client.post(
- reverse('api_register'),
- {'username': 'restapi', 'email': '[email protected]', 'password': 'AekaiLe0ga'},
- )
- count_after = User.objects.count()
- self.assertEqual(response.status_code, HTTP_201_CREATED)
-
- new_user = User.objects.get(username='restapi')
- token = Token.objects.get(user=new_user)
- self.assertEqual(response.data['message'], 'api user successfully registered')
- self.assertEqual(response.data['token'], token.key)
- self.assertEqual(count_after, count_before + 1)
-
- def test_post_valid_api_user_creation_no_email(self):
- """Successfully register a user via the REST API without providing an email"""
-
- self.user_login('test')
- user = User.objects.get(username='test')
- token = Token.objects.get(user=user)
- count_before = User.objects.count()
-
- response = self.client.post(
- reverse('api_register'),
- {'username': 'restapi', 'password': 'AekaiLe0ga'},
- )
- count_after = User.objects.count()
- self.assertEqual(response.status_code, HTTP_201_CREATED)
-
- new_user = User.objects.get(username='restapi')
- token = Token.objects.get(user=new_user)
- self.assertEqual(response.data['message'], 'api user successfully registered')
- self.assertEqual(response.data['token'], token.key)
- self.assertEqual(count_after, count_before + 1)
-
- def test_post_unsuccessfully_registration_no_username(self):
- """Test unsuccessful registration (weak password)"""
-
- self.user_login('test')
- user = User.objects.get(username='test')
- token = Token.objects.get(user=user)
-
- response = self.client.post(
- reverse('api_register'),
- {'password': 'AekaiLe0ga'},
- )
-
- self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)
-
- def test_post_unsuccessfully_registration_invalid_email(self):
- """Test unsuccessful registration (invalid email)"""
-
- self.user_login('test')
- user = User.objects.get(username='test')
- token = Token.objects.get(user=user)
-
- response = self.client.post(
- reverse('api_register'),
- {'username': 'restapi', 'email': 'example.com', 'password': 'AekaiLe0ga'},
- )
-
- self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)
-
- def test_post_unsuccessfully_registration_invalid_email_2(self):
- """Test unsuccessful registration (email already exists)"""
-
- self.user_login('test')
- user = User.objects.get(username='test')
- token = Token.objects.get(user=user)
-
- response = self.client.post(
- reverse('api_register'),
- {'username': 'restapi', 'email': '[email protected]', 'password': 'AekaiLe0ga'},
- )
-
- self.assertEqual(response.status_code, HTTP_400_BAD_REQUEST)