Skip to content

Commit

Permalink
Redirect /<ads_manager_id>/ads/xx to the correct ads manager if neces…
Browse files Browse the repository at this point in the history
…sary
  • Loading branch information
brmzkw committed Mar 1, 2024
1 parent 8455477 commit 7d35b89
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
19 changes: 17 additions & 2 deletions mesads/app/decorators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import functools

from django.contrib.auth.decorators import login_required
from django.shortcuts import get_list_or_404, get_object_or_404
from django.shortcuts import get_list_or_404, get_object_or_404, redirect
from django.urls import resolve, reverse

from .models import ADSManagerRequest, ADSManagerAdministrator
from .models import ADS, ADSManagerRequest, ADSManagerAdministrator


def ads_manager_required(func):
Expand All @@ -25,6 +26,20 @@ def wrapped(request, manager_id=None, *args, **kwargs):
ads_manager__id=manager_id,
accepted=True,
)
if kwargs.get("ads_id"):
# Make sure the ADS exists if the view is for an ADS
ads = get_object_or_404(ADS, id=kwargs.get("ads_id"))

# If the URL is with the form /xxx/<ads_manager_id>/xxx/<ads_id> but
# <ads_manager_id> has been manually changed by the user and doesn't
# match the actual manager_id, we redirect to the correct URL.
if ads.ads_manager.id != manager_id:
urlpattern = resolve(request.path_info)
url = reverse(
urlpattern.url_name,
kwargs={"manager_id": ads.ads_manager.id, "ads_id": ads.id},
)
return redirect(url, permanent=True)
return func(request, manager_id=manager_id, *args, **kwargs)

return login_required(wrapped)
Expand Down
19 changes: 19 additions & 0 deletions mesads/app/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,25 @@ def test_create_ads_user_invalid_siret(self):
)
self.assertEqual(ADSUser.objects.count(), 0)

def test_get_incorrect_ads_manager(self):
"""If user requests /registre_ads/gestion/xxx/ads/yyy but xxx is an
existing ADSManager, but not the one of the ADS, we want to make sure
the user is redirected to the correct page."""
commune = Commune.objects.create(
insee="xx",
departement="xx",
libelle="xx",
)
ads_manager = ADSManager.objects.create(content_object=commune)
resp = self.admin_client.get(
f"/registre_ads/gestion/{ads_manager.id}/ads/{self.ads.id}",
)
self.assertEqual(resp.status_code, 301)
self.assertEqual(
resp.headers["Location"],
f"/registre_ads/gestion/{self.ads_manager_city35.id}/ads/{self.ads.id}",
)


class TestADSDeleteView(ClientTestCase):
def setUp(self):
Expand Down

0 comments on commit 7d35b89

Please sign in to comment.