Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Remove unused fields from the user profile and wire up the notification_language

Roland Geider9337bcf8a354
diff --git a/wger/core/account_adapter.py b/wger/core/account_adapter.py
index 67cfc119b..91fcb6f14 100644
--- a/wger/core/account_adapter.py
+++ b/wger/core/account_adapter.py
@@ -25,6 +25,9 @@ from django.utils import (
# Third Party
from allauth.account.adapter import DefaultAccountAdapter
+# wger
+from wger.core.models import UserProfile
+
class WgerAccountAdapter(DefaultAccountAdapter):
"""Wger Account Adapter for allauth"""
@@ -59,9 +62,26 @@ class WgerAccountAdapter(DefaultAccountAdapter):
return user
+ def get_notification_language(self, context):
+ """
+ Return the locale code of the recipient's preferred notification
+ language, or None if it can't be determined from the email context.
+ """
+ user = context.get('user')
+ if user is None or user.pk is None:
+ return None
+
+ profile = (
+ UserProfile.objects.filter(user=user)
+ .select_related('notification_language')
+ .first()
+ )
+ return profile.notification_language.short_name if profile else None
+
def send_mail(self, template_prefix, email, context):
"""
- Add token expiration info to the email context.
+ Add token expiration info to the email context and render the email in
+ the recipient's preferred notification language.
Note that we add this to the context of all emails, even if this
doesn't make sense for some of them. However, this is acceptable since it's
@@ -72,4 +92,11 @@ class WgerAccountAdapter(DefaultAccountAdapter):
expire_days = settings.ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS
context['expire_hours'] = expire_days * 24
context['expire_date'] = timezone.now() + timedelta(days=expire_days)
- super().send_mail(template_prefix, email, context)
+
+ # Emails without a known recipient keep the request's active language.
+ language = self.get_notification_language(context)
+ if language:
+ with translation.override(language):
+ super().send_mail(template_prefix, email, context)
+ else:
+ super().send_mail(template_prefix, email, context)
diff --git a/wger/core/api/serializers.py b/wger/core/api/serializers.py
index 8a831a473..5e43edf0c 100644
--- a/wger/core/api/serializers.py
+++ b/wger/core/api/serializers.py
@@ -58,8 +58,6 @@ class UserprofileSerializer(serializers.ModelSerializer):
'weight_rounding',
'repetitions_rounding',
'is_temporary',
- 'show_comments',
- 'show_english_ingredients',
'workout_reminder_active',
'workout_reminder',
'workout_duration',
@@ -78,7 +76,6 @@ class UserprofileSerializer(serializers.ModelSerializer):
'freetime_intensity',
'calories',
'weight_unit',
- 'ro_access',
'num_days_weight_reminder',
)
diff --git a/wger/core/fixtures/test-user-data.json b/wger/core/fixtures/test-user-data.json
index 6777837c5..e614f0972 100644
--- a/wger/core/fixtures/test-user-data.json
+++ b/wger/core/fixtures/test-user-data.json
@@ -480,8 +480,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 1,
"gender": "1",
"age": 30,
@@ -500,7 +498,6 @@
"notification_language": 1,
"gym": 1,
"weight_unit": "kg",
- "ro_access": true,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -511,8 +508,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 2,
"gender": "1",
"age": 20,
@@ -531,7 +526,6 @@
"notification_language": 2,
"gym": 1,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 3,
"added_by": null,
"can_add_user": true
@@ -542,8 +536,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": true,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 3,
"gender": "2",
"age": 50,
@@ -562,7 +554,6 @@
"notification_language": 2,
"gym": 2,
"weight_unit": "kg",
- "ro_access": true,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -573,8 +564,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 4,
"gender": "1",
"age": 20,
@@ -593,7 +582,6 @@
"notification_language": 2,
"gym": 1,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": 2,
"can_add_user": false
@@ -604,8 +592,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 5,
"gender": "1",
"age": 20,
@@ -624,7 +610,6 @@
"notification_language": 2,
"gym": 1,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": 2,
"can_add_user": false
@@ -635,8 +620,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 6,
"gender": "1",
"age": 20,
@@ -655,7 +638,6 @@
"notification_language": 2,
"gym": 1,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -666,8 +648,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 7,
"gender": "1",
"age": 20,
@@ -686,7 +666,6 @@
"notification_language": 2,
"gym": 2,
"weight_unit": "lb",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -697,8 +676,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 8,
"gender": "1",
"age": 20,
@@ -717,7 +694,6 @@
"notification_language": 2,
"gym": 2,
"weight_unit": "lb",
- "ro_access": true,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -728,8 +704,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 9,
"gender": "1",
"age": 20,
@@ -748,7 +722,6 @@
"notification_language": 2,
"gym": 1,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -759,8 +732,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 10,
"gender": "1",
"age": 20,
@@ -779,7 +750,6 @@
"notification_language": 2,
"gym": 1,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -790,8 +760,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 11,
"gender": "1",
"age": 20,
@@ -810,7 +778,6 @@
"notification_language": 2,
"gym": 2,
"weight_unit": "lb",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -821,8 +788,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 12,
"gender": "1",
"age": 20,
@@ -841,7 +806,6 @@
"notification_language": 2,
"gym": 1,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -852,8 +816,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 13,
"gender": "1",
"age": 20,
@@ -872,7 +834,6 @@
"notification_language": 2,
"gym": 2,
"weight_unit": "lb",
- "ro_access": true,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -884,8 +845,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -903,7 +862,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -915,8 +873,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -934,7 +890,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -946,8 +901,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -965,7 +918,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -977,8 +929,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -996,7 +946,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -1008,8 +957,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -1027,7 +974,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -1039,8 +985,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -1058,7 +1002,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "lb",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -1070,8 +1013,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -1089,7 +1030,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "lb",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -1101,8 +1041,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -1120,7 +1058,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -1132,8 +1069,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -1151,7 +1086,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -1163,8 +1097,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -1182,7 +1114,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
@@ -1194,8 +1125,6 @@
"fields": {
"workout_reminder": 20,
"height": 180,
- "show_english_ingredients": false,
- "show_comments": false,
"workout_duration": 8,
"sleep_hours": 7,
"freetime_hours": 8,
@@ -1213,7 +1142,6 @@
"sport_hours": 6,
"is_temporary": false,
"weight_unit": "kg",
- "ro_access": false,
"num_days_weight_reminder": 0,
"added_by": null,
"can_add_user": false
diff --git a/wger/core/fixtures/users.json b/wger/core/fixtures/users.json
index ca0ed0184..b7979ff07 100644
--- a/wger/core/fixtures/users.json
+++ b/wger/core/fixtures/users.json
@@ -38,8 +38,6 @@
"model": "core.userprofile",
"fields": {
"is_temporary": false,
- "show_comments": false,
- "show_english_ingredients": false,
"user": 1,
"gym": 1
}
diff --git a/wger/core/forms.py b/wger/core/forms.py
index 5b9a1c85c..549621309 100644
--- a/wger/core/forms.py
+++ b/wger/core/forms.py
@@ -143,14 +143,11 @@ class UserPreferencesForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = (
- 'show_comments',
- 'show_english_ingredients',
'workout_reminder_active',
'workout_reminder',
'workout_duration',
'notification_language',
'weight_unit',
- 'ro_access',
'num_days_weight_reminder',
'birthdate',
'height',
@@ -180,23 +177,20 @@ class UserPreferencesForm(forms.ModelForm):
),
'birthdate',
'height',
- HTML('<hr>'),
- ),
- Fieldset(
- _('Workout reminders'),
- 'workout_reminder_active',
- 'workout_reminder',
- 'workout_duration',
- HTML('<hr>'),
+ # HTML('<hr>'),
),
+ # Fieldset(
+ # _('Workout reminders'),
+ # 'workout_reminder_active',
+ # 'workout_reminder',
+ # 'workout_duration',
+ # HTML('<hr>'),
+ # ),
Fieldset(
_('Other settings'),
- 'ro_access',
'notification_language',
'weight_unit',
- 'show_comments',
- 'show_english_ingredients',
- 'num_days_weight_reminder',
+ # 'num_days_weight_reminder',
),
ButtonHolder(Submit('submit', _('Save'), css_class='btn-success btn-block')),
)
diff --git a/wger/core/migrations/0025_remove_unused_fields_in_userprofile.py b/wger/core/migrations/0025_remove_unused_fields_in_userprofile.py
new file mode 100644
index 000000000..05d51ed96
--- /dev/null
+++ b/wger/core/migrations/0025_remove_unused_fields_in_userprofile.py
@@ -0,0 +1,25 @@
+# Generated by Django 6.0.5 on 2026-06-15 13:13
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('core', '0024_backfill_emailaddress'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='userprofile',
+ name='ro_access',
+ ),
+ migrations.RemoveField(
+ model_name='userprofile',
+ name='show_comments',
+ ),
+ migrations.RemoveField(
+ model_name='userprofile',
+ name='show_english_ingredients',
+ ),
+ ]
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py
index 45ba44c3f..a57e5a151 100644
--- a/wger/core/models/profile.py
+++ b/wger/core/models/profile.py
@@ -115,28 +115,6 @@ class UserProfile(models.Model):
# User preferences
#
- show_comments = models.BooleanField(
- verbose_name=_('Show exercise comments'),
- help_text=_('Check to show exercise comments on the workout view'),
- default=True,
- )
- """
- Show exercise comments on workout view
- """
-
- # Also show ingredients in english while composing a nutritional plan
- # (obviously this is only meaningful if the user has a language other than english)
- show_english_ingredients = models.BooleanField(
- verbose_name=_('Also use ingredients in English'),
- help_text=_(
- """Check to also show ingredients in English while creating
-a nutritional plan. These ingredients are extracted from a list provided
-by the US Department of Agriculture. It is extremely complete, with around
-7000 entries, but can be somewhat overwhelming and make the search difficult."""
- ),
- default=True,
- )
-
workout_reminder_active = models.BooleanField(
verbose_name=_('Activate workout reminders'),
help_text=_(
@@ -337,17 +315,6 @@ by the US Department of Agriculture. It is extremely complete, with around
)
"""Preferred weight unit"""
- ro_access = models.BooleanField(
- verbose_name=_('Allow external access'),
- help_text=_(
- 'Allow external users to access your workouts and '
- 'logs in a read-only mode. You need to set this '
- 'before you can share links e.g. to social media.'
- ),
- default=False,
- )
- """Allow anonymous read-only access"""
-
num_days_weight_reminder = models.IntegerField(
verbose_name=_('Automatic reminders for weight entries'),
help_text=_('Number of days after the last weight entry (enter 0 to deactivate)'),
diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.py
index eaae7bf25..389086c77 100644
--- a/wger/core/tests/test_preferences.py
+++ b/wger/core/tests/test_preferences.py
@@ -39,8 +39,6 @@ class PreferencesTestCase(WgerTestCase):
def setUp(self):
super().setUp()
self.form_data = {
- 'show_comments': True,
- 'show_english_ingredients': True,
'first_name': '',
'last_name': '',
'workout_reminder_active': True,
@@ -62,9 +60,6 @@ class PreferencesTestCase(WgerTestCase):
self.user_login('test')
response = self.client.get(reverse('core:user:preferences'))
- profile = User.objects.get(username='test').userprofile
- self.assertFalse(profile.show_comments)
-
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed('preferences.html')
@@ -78,7 +73,6 @@ class PreferencesTestCase(WgerTestCase):
response = self.client.get(reverse('core:user:preferences'))
user = User.objects.get(username='test')
profile = user.userprofile
- self.assertTrue(profile.show_english_ingredients)
self.assertTrue(profile.workout_reminder_active)
self.assertEqual(profile.workout_reminder, 30)
self.assertEqual(profile.workout_duration, 12)
@@ -90,7 +84,6 @@ class PreferencesTestCase(WgerTestCase):
reverse('core:user:preferences'),
{
**self.form_data,
- 'show_comments': False,
'workout_reminder': 22,
'workout_duration': 10,
'weight_unit': 'lb',
@@ -101,8 +94,8 @@ class PreferencesTestCase(WgerTestCase):
self.assertEqual(response.status_code, 302)
response = self.client.get(reverse('core:user:preferences'))
profile = response.context['user'].userprofile
- self.assertFalse(profile.show_comments)
- self.assertTrue(profile.show_english_ingredients)
+ self.assertEqual(profile.workout_reminder, 22)
+ self.assertEqual(profile.weight_unit, 'lb')
def test_email_is_not_editable_from_preferences(self):
"""
diff --git a/wger/core/tests/test_verification.py b/wger/core/tests/test_verification.py
index 6e91525c7..b4daf8fa3 100644
--- a/wger/core/tests/test_verification.py
+++ b/wger/core/tests/test_verification.py
@@ -16,18 +16,22 @@
import json
import logging
import re
+from unittest import mock
# Django
from django.contrib.auth.models import User
from django.core import mail
from django.test import override_settings
from django.urls import reverse
+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
# wger
+from wger.core.account_adapter import WgerAccountAdapter
from wger.core.tests.base_testcase import WgerTestCase
@@ -109,6 +113,39 @@ class EmailVerificationFromAPITestCase(WgerTestCase):
self.assertEqual(len(mail.outbox), 0)
+class AccountAdapterNotificationLanguageTestCase(WgerTestCase):
+ """
+ allauth emails are rendered in the recipient's notification language
+ (UserProfile.notification_language), not the request's active language.
+ """
+
+ def _capture_send_language(self, email, context):
+ """Record the active language at the moment super().send_mail runs."""
+ captured = {}
+
+ def fake_super(self, template_prefix, recipient, ctx):
+ captured['language'] = translation.get_language()
+
+ with mock.patch.object(DefaultAccountAdapter, 'send_mail', fake_super):
+ with translation.override('en'):
+ WgerAccountAdapter().send_mail('account/email/foo', email, context)
+
+ return captured.get('language')
+
+ def test_email_rendered_in_users_notification_language(self):
+ user = User.objects.get(username='admin')
+ # language pk=1 is German in the fixtures
+ user.userprofile.notification_language_id = 1
+ user.userprofile.save()
+
+ language = self._capture_send_language(user.email, {'user': user})
+ self.assertEqual(language, 'de')
+
+ def test_falls_back_to_active_language_without_recipient(self):
+ language = self._capture_send_language('[email protected]', {})
+ self.assertEqual(language, 'en')
+
+
class UserProfileEmailReadOnlyTestCase(WgerTestCase):
"""
Email is read-only on the userprofile endpoint; clients change addresses
diff --git a/wger/utils/helpers.py b/wger/utils/helpers.py
index a38cead56..2dbe7530a 100644
--- a/wger/utils/helpers.py
+++ b/wger/utils/helpers.py
@@ -28,11 +28,8 @@ from functools import wraps
from typing import Any
# Django
-from django.contrib.auth.models import User
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
-from django.http import Http404
-from django.shortcuts import get_object_or_404
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
@@ -99,35 +96,6 @@ def password_generator(length=15):
return ''.join(secrets.choice(chars) for _ in range(length))
-def check_access(request_user, username=None):
- """
- Small helper function to check that the current (possibly unauthenticated)
- user can access a URL that the owner user shared the link.
-
- Raises Http404 in case of error (no read-only access allowed)
-
- :param request_user: the user in the current request
- :param username: the username
- :return: a tuple: (is_owner, user)
- """
-
- if username:
- user = get_object_or_404(User, username=username)
- if request_user.username == username:
- user = request_user
- elif not user.userprofile.ro_access:
- raise Http404('You are not allowed to access this page.')
-
- # If there is no user_pk, just show the user his own data
- else:
- if not request_user.is_authenticated:
- raise Http404('You are not allowed to access this page.')
- user = request_user
-
- is_owner = request_user == user
- return is_owner, user
-
-
def normalize_decimal(d: Decimal):
"""
Normalizes a decimal input
diff --git a/wger/utils/tests/test_check_access.py b/wger/utils/tests/test_check_access.py
deleted file mode 100644
index b1c9b139c..000000000
--- a/wger/utils/tests/test_check_access.py
+++ /dev/null
@@ -1,60 +0,0 @@
-# This file is part of wger Workout Manager.
-#
-# wger Workout Manager is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# wger Workout Manager is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-
-# Django
-from django.contrib.auth.models import (
- AnonymousUser,
- User,
-)
-from django.http import Http404
-
-# wger
-from wger.core.tests.base_testcase import WgerTestCase
-from wger.utils.helpers import check_access
-
-
-class CheckAccessTestCase(WgerTestCase):
- """
- Test the "check_access" helper function
- """
-
- def test_helper(self):
- """
- Test the helper function
- """
-
- user_share = User.objects.get(pk=1)
- self.assertTrue(user_share.userprofile.ro_access)
-
- user_no_share = User.objects.get(pk=2)
- self.assertFalse(user_no_share.userprofile.ro_access)
-
- anon = AnonymousUser()
-
- # Logged out user
- self.assertEqual(check_access(anon, 'admin'), (False, user_share))
- self.assertRaises(Http404, check_access, anon, 'test')
- self.assertRaises(Http404, check_access, anon, 'not_a_username')
- self.assertRaises(Http404, check_access, anon)
-
- # Logged in user
- self.assertEqual(check_access(user_share, 'admin'), (True, user_share))
- self.assertRaises(Http404, check_access, user_share, 'test')
- self.assertEqual(check_access(user_share), (True, user_share))
- self.assertRaises(Http404, check_access, user_share, 'not_a_username')
-
- self.assertEqual(check_access(user_no_share, 'admin'), (False, user_share))
- self.assertEqual(check_access(user_no_share, 'test'), (True, user_no_share))
- self.assertEqual(check_access(user_no_share), (True, user_no_share))
- self.assertRaises(Http404, check_access, user_no_share, 'not_a_username')