Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Only send verification emails on POST

Roland Geider048dd503838b
diff --git a/wger/core/api/views.py b/wger/core/api/views.py
index d40a4d0b3..bbe8768c6 100644
--- a/wger/core/api/views.py
+++ b/wger/core/api/views.py
@@ -157,10 +157,12 @@ class UserProfileViewSet(viewsets.ModelViewSet):
def destroy(self, request, *args, **kwargs):
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
- @action(detail=False, url_name='verify-email', url_path='verify-email')
+ @action(detail=False, methods=['post'], url_name='verify-email', url_path='verify-email')
def verify_email(self, request):
"""
Verify the user's email address
+
+ POST only, a GET must not send out emails as a side effect
"""
email_obj = request.user.userprofile.get_allauth_email
diff --git a/wger/core/tests/test_verification.py b/wger/core/tests/test_verification.py
index b4daf8fa3..f063627bb 100644
--- a/wger/core/tests/test_verification.py
+++ b/wger/core/tests/test_verification.py
@@ -28,7 +28,10 @@ from django.utils import translation
# Third Party
from allauth.account.adapter import DefaultAccountAdapter
from allauth.account.models import EmailAddress
-from rest_framework.status import HTTP_200_OK
+from rest_framework.status import (
+ HTTP_200_OK,
+ HTTP_405_METHOD_NOT_ALLOWED,
+)
# wger
from wger.core.account_adapter import WgerAccountAdapter
@@ -54,7 +57,7 @@ class EmailVerificationFromAPITestCase(WgerTestCase):
self.user_login('admin')
mail.outbox = []
- response = self.client.get(
+ response = self.client.post(
reverse('userprofile-verify-email'),
)
@@ -74,7 +77,7 @@ class EmailVerificationFromAPITestCase(WgerTestCase):
self.user_login('admin')
mail.outbox = []
- response = self.client.get(reverse('userprofile-verify-email'))
+ response = self.client.post(reverse('userprofile-verify-email'))
self.assertEqual(response.data['status'], 'sent')
self.assertEqual(len(mail.outbox), 1)
@@ -102,7 +105,7 @@ class EmailVerificationFromAPITestCase(WgerTestCase):
self.user_login('trainer1')
mail.outbox = []
- response = self.client.get(
+ response = self.client.post(
reverse('userprofile-verify-email'),
)
@@ -112,6 +115,18 @@ class EmailVerificationFromAPITestCase(WgerTestCase):
# No email was sent
self.assertEqual(len(mail.outbox), 0)
+ def test_get_does_not_send_mail(self):
+ """
+ Sending mail is a side effect, a GET must not trigger it
+ """
+ self.user_login('admin')
+
+ mail.outbox = []
+ response = self.client.get(reverse('userprofile-verify-email'))
+
+ self.assertEqual(response.status_code, HTTP_405_METHOD_NOT_ALLOWED)
+ self.assertEqual(len(mail.outbox), 0)
+
class AccountAdapterNotificationLanguageTestCase(WgerTestCase):
"""
diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.py
index 33ba87005..69d2f7a76 100644
--- a/wger/exercises/tests/test_exercise_translation.py
+++ b/wger/exercises/tests/test_exercise_translation.py
@@ -380,15 +380,23 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTes
self.assertTrue(response.data['non_field_errors'])
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
- def test_set_existing_language(self):
+ def test_set_free_language_is_ignored(self):
"""
- Test that it is possible to set the language if it doesn't duplicate a translation
+ Sending a language that doesn't duplicate a translation passes validation,
+ but the language itself is never changed over the API
"""
+ language_before = Translation.objects.get(pk=self.pk).language_id
+
self.authenticate('trainer1')
response = self.client.patch(self.url_detail, data={'language': 1, 'name': '123456'})
+
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(response.data.get('non_field_errors'))
+ translation = Translation.objects.get(pk=self.pk)
+ self.assertEqual(translation.language_id, language_before)
+ self.assertEqual(translation.name, '123456')
+
def test_edit_only_one_language_per_base(self):
"""
Test that it's not possible to edit a translation to a second language for the same base