- 
                Notifications
    
You must be signed in to change notification settings  - Fork 946
 
Refactor redirect tests to use reload_redirects_with_settings instead of override_settings #16414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            localjo
  wants to merge
  2
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
fix-redirect-tests
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at https://mozilla.org/MPL/2.0/. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| 
     | 
||
| import functools | ||
| import importlib | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
| 
     | 
||
| from django.test import override_settings | ||
| 
     | 
||
| 
     | 
||
| def reload_redirects_with_settings(redirects_module_path: str, middleware_module_path: str, **settings_kwargs): | ||
| """ | ||
| Generic decorator that enables dynamic redirect reloading for testing. | ||
| 
     | 
||
| This decorator: | ||
| 1. Stores the original setting values before any changes | ||
| 2. Overrides specified settings using the same pattern as @override_settings | ||
| 3. Reloads the specified redirects module to pick up new patterns | ||
| 4. Patches the specified middleware module to use the reloaded patterns | ||
| 5. Restores original settings and reloads module again after the test | ||
| 
     | 
||
| This is needed because Django loads redirects once at startup and doesn't | ||
| reload them when @override_settings is used. | ||
| 
     | 
||
| Args: | ||
| redirects_module_path: The redirects module path to reload | ||
| middleware_module_path: The middleware module path to patch | ||
| **settings_kwargs: Settings to override | ||
| 
     | 
||
| Usage: | ||
| @reload_redirects_with_settings( | ||
| 'bedrock.firefox.redirects', | ||
| 'bedrock.redirects.middleware', | ||
| ENABLE_FIREFOX_COM_REDIRECTS=True | ||
| ) | ||
| def test_my_redirect(): | ||
| # Your test code here | ||
| pass | ||
| """ | ||
| 
     | 
||
| def decorator(func: Callable) -> Callable: | ||
| @functools.wraps(func) | ||
| def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
| from django.conf import settings | ||
| 
     | 
||
| original_settings = {} | ||
| for setting_name in settings_kwargs.keys(): | ||
| original_settings[setting_name] = getattr(settings, setting_name, None) | ||
| 
     | 
||
| with override_settings(**settings_kwargs): | ||
| original_get_resolver = _reload_module_and_patch_middleware(redirects_module_path, middleware_module_path) | ||
| try: | ||
| return func(*args, **kwargs) | ||
| finally: | ||
| restore_settings = {name: value for name, value in original_settings.items()} | ||
| with override_settings(**restore_settings): | ||
| _reload_module_and_patch_middleware(redirects_module_path, middleware_module_path) | ||
| middleware_module = importlib.import_module(middleware_module_path) | ||
| middleware_module.get_resolver = original_get_resolver | ||
| 
     | 
||
| return wrapper | ||
| 
     | 
||
| return decorator | ||
| 
     | 
||
| 
     | 
||
| def _reload_module_and_patch_middleware(redirects_module_path: str, middleware_module_path: str) -> Any: | ||
| """ | ||
| Import, reload the specified module, and patch the redirects middleware. | ||
| 
     | 
||
| Args: | ||
| redirects_module_path: The redirects module path to reload | ||
| middleware_module_path: The middleware module path to patch | ||
| 
     | 
||
| Returns: | ||
| The original get_resolver function for restoration | ||
| """ | ||
| middleware_module = importlib.import_module(middleware_module_path) | ||
| 
     | 
||
| redirects_module = importlib.import_module(redirects_module_path) | ||
| importlib.reload(redirects_module) # type: ignore | ||
| original_get_resolver = middleware_module.get_resolver | ||
| 
     | 
||
| def patched_get_resolver(patterns=None): | ||
| if patterns is None: | ||
| patterns = redirects_module.redirectpatterns # type: ignore | ||
| return original_get_resolver(patterns) | ||
| 
     | 
||
| middleware_module.get_resolver = patched_get_resolver | ||
| 
     | 
||
| return original_get_resolver | ||
| 
     | 
||
| 
     | 
||
| def enable_fxc_redirects(): | ||
| """ | ||
| Convenience decorator for Firefox.com redirect testing. | ||
| 
     | 
||
| This is a wrapper around reload_redirects_with_settings that enables | ||
| Firefox.com redirects for the duration of the test regardless of the | ||
| ENABLE_FIREFOX_COM_REDIRECTS environment variable. | ||
| 
     | 
||
| Usage: | ||
| @enable_fxc_redirects() | ||
| def test_my_redirect(): | ||
| # Your test code here | ||
| pass | ||
| """ | ||
| return reload_redirects_with_settings("bedrock.firefox.redirects", "bedrock.redirects.middleware", ENABLE_FIREFOX_COM_REDIRECTS=True) | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.