Add configurable defaults for gravatarproxy and gravatarredirect

This commit is contained in:
Oliver Falk
2025-10-29 11:30:48 +01:00
parent a07db42ae4
commit aecc8e8477
5 changed files with 148 additions and 4 deletions

View File

@@ -63,7 +63,35 @@ pip install -r requirements.txt
./manage.py test -v3 # Or any other verbosity level you like ./manage.py test -v3 # Or any other verbosity level you like
``` ```
## OpenID Connect authentication with Fedora ## Configuration
### Gravatar Proxy and Redirect Settings
By default, ivatar is configured to work well for public instances like libravatar.org. However, for local or private instances, you may want to disable external requests to Gravatar. You can configure the default behavior by adding these settings to your `config_local.py`:
```python
# Default settings for Gravatar proxy and redirect behavior
# These can be overridden by URL parameters (?gravatarproxy=n&gravatarredirect=n)
# Whether to proxy requests to Gravatar when no local avatar is found (default: True)
DEFAULT_GRAVATARPROXY = False
# Whether to redirect directly to Gravatar when no local avatar is found (default: False)
DEFAULT_GRAVATARREDIRECT = False
# Whether to force default behavior even when a user avatar exists (default: False)
FORCEDEFAULT = False
```
**Use cases:**
- **Private/Local instances**: Set `DEFAULT_GRAVATARPROXY = False` and `DEFAULT_GRAVATARREDIRECT = False` to prevent external requests
- **Gravatar-first instances**: Set `DEFAULT_GRAVATARREDIRECT = True` to redirect to Gravatar instead of proxying
- **Testing/Development**: Set `FORCEDEFAULT = True` to always use default avatars
**Note**: URL parameters (`?gravatarproxy=n`, `?gravatarredirect=y`, `?forcedefault=y`) will always override these configuration defaults.
### OpenID Connect authentication with Fedora
To enable OpenID Connect (OIDC) authentication with Fedora, you must have obtained a `client_id` and `client_secret` pair from the Fedora Infrastructure. To enable OpenID Connect (OIDC) authentication with Fedora, you must have obtained a `client_id` and `client_secret` pair from the Fedora Infrastructure.
Then you must set these values in `config_local.py`: Then you must set these values in `config_local.py`:

View File

@@ -196,6 +196,12 @@ ALLOWED_EXTERNAL_OPENID_REDIRECT_DOMAINS = [
DEFAULT_AVATAR_SIZE = 80 DEFAULT_AVATAR_SIZE = 80
# Default settings for Gravatar proxy and redirect behavior
# These can be overridden by URL parameters
DEFAULT_GRAVATARPROXY = True
DEFAULT_GRAVATARREDIRECT = False
FORCEDEFAULT = False
LANGUAGES = ( LANGUAGES = (
("de", _("Deutsch")), ("de", _("Deutsch")),
("en", _("English")), ("en", _("English")),

View File

@@ -48,3 +48,18 @@ import os
# Bluesky integration credentials # Bluesky integration credentials
# BLUESKY_IDENTIFIER = "your-bluesky-handle" # BLUESKY_IDENTIFIER = "your-bluesky-handle"
# BLUESKY_APP_PASSWORD = "your-app-password" # BLUESKY_APP_PASSWORD = "your-app-password"
# Gravatar proxy and redirect settings
# These control the default behavior when no local avatar is found
# URL parameters (?gravatarproxy=n&gravatarredirect=y) will override these defaults
# For private/local instances that should not make external requests:
# DEFAULT_GRAVATARPROXY = False
# DEFAULT_GRAVATARREDIRECT = False
# For instances that prefer direct Gravatar redirects:
# DEFAULT_GRAVATARREDIRECT = True
# DEFAULT_GRAVATARPROXY = False
# For testing/development (always use default avatars):
# FORCEDEFAULT = True

View File

@@ -0,0 +1,90 @@
"""
Unit tests for configurable default settings
"""
import unittest
from unittest.mock import patch
import os
import django
os.environ["DJANGO_SETTINGS_MODULE"] = "ivatar.settings"
django.setup()
from django.test import TestCase
class ConfigurableDefaultsTestCase(TestCase):
"""
Test cases for configurable default settings for gravatarproxy, gravatarredirect, and forcedefault
"""
def test_config_imports_successfully(self):
"""
Test that the new configuration options can be imported successfully
"""
try:
from ivatar.settings import (
DEFAULT_GRAVATARPROXY,
DEFAULT_GRAVATARREDIRECT,
FORCEDEFAULT,
)
# Test that they have the expected default values
self.assertTrue(isinstance(DEFAULT_GRAVATARPROXY, bool))
self.assertTrue(isinstance(DEFAULT_GRAVATARREDIRECT, bool))
self.assertTrue(isinstance(FORCEDEFAULT, bool))
except ImportError as e:
self.fail(f"Failed to import configuration settings: {e}")
def test_views_imports_config_successfully(self):
"""
Test that views.py can import the new configuration options
"""
try:
from ivatar.views import (
DEFAULT_GRAVATARPROXY,
DEFAULT_GRAVATARREDIRECT,
FORCEDEFAULT,
)
# Test that they have the expected default values
self.assertTrue(isinstance(DEFAULT_GRAVATARPROXY, bool))
self.assertTrue(isinstance(DEFAULT_GRAVATARREDIRECT, bool))
self.assertTrue(isinstance(FORCEDEFAULT, bool))
except ImportError as e:
self.fail(f"Failed to import configuration settings in views: {e}")
@patch("ivatar.settings.DEFAULT_GRAVATARPROXY", False)
@patch("ivatar.settings.DEFAULT_GRAVATARREDIRECT", True)
@patch("ivatar.settings.FORCEDEFAULT", True)
def test_config_values_can_be_overridden(self):
"""
Test that configuration values can be overridden (mocked for testing)
"""
from ivatar import settings
# These should reflect the patched values
self.assertFalse(settings.DEFAULT_GRAVATARPROXY)
self.assertTrue(settings.DEFAULT_GRAVATARREDIRECT)
self.assertTrue(settings.FORCEDEFAULT)
def test_default_values_are_correct(self):
"""
Test that the default values match the issue requirements
"""
from ivatar.settings import (
DEFAULT_GRAVATARPROXY,
DEFAULT_GRAVATARREDIRECT,
FORCEDEFAULT,
)
# Based on the issue, these should be the defaults for public instances
self.assertTrue(
DEFAULT_GRAVATARPROXY
) # Default should be True for public instances
self.assertFalse(DEFAULT_GRAVATARREDIRECT) # Default should be False
self.assertFalse(FORCEDEFAULT) # Default should be False
if __name__ == "__main__":
unittest.main()

View File

@@ -33,6 +33,11 @@ from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE
from ivatar.settings import CACHE_RESPONSE from ivatar.settings import CACHE_RESPONSE
from ivatar.settings import CACHE_IMAGES_MAX_AGE from ivatar.settings import CACHE_IMAGES_MAX_AGE
from ivatar.settings import TRUSTED_DEFAULT_URLS from ivatar.settings import TRUSTED_DEFAULT_URLS
from ivatar.settings import (
DEFAULT_GRAVATARPROXY,
DEFAULT_GRAVATARREDIRECT,
FORCEDEFAULT,
)
from .ivataraccount.models import ConfirmedEmail, ConfirmedOpenId from .ivataraccount.models import ConfirmedEmail, ConfirmedOpenId
from .ivataraccount.models import UnconfirmedEmail, UnconfirmedOpenId from .ivataraccount.models import UnconfirmedEmail, UnconfirmedOpenId
from .ivataraccount.models import Photo from .ivataraccount.models import Photo
@@ -144,9 +149,9 @@ class AvatarImageView(TemplateView):
imgformat = "png" imgformat = "png"
obj = None obj = None
default = None default = None
forcedefault = False forcedefault = FORCEDEFAULT
gravatarredirect = False gravatarredirect = DEFAULT_GRAVATARREDIRECT
gravatarproxy = True gravatarproxy = DEFAULT_GRAVATARPROXY
uri = request.build_absolute_uri() uri = request.build_absolute_uri()
# Check the cache first # Check the cache first