Skip to content

Commit

Permalink
Garante redirects de datasets e tabelas
Browse files Browse the repository at this point in the history
  • Loading branch information
berinhard committed Oct 16, 2020
1 parent 3d5c5ea commit c800f07
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
25 changes: 24 additions & 1 deletion core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,13 +689,36 @@ class DataUrlRedirect(models.Model):

@property
def redirect_map(self):
map = {}
dataset_url_names = [
"core:dataset-detail",
"core:dataset-files-detail",
"api-v1:dataset-detail",
]

return {reverse(n, args=[self.dataset_prev]): reverse(n, args=[self.dataset_dest]) for n in dataset_url_names}
if self.dataset_prev != self.dataset_dest:
map.update(
**{
reverse(n, args=[self.dataset_prev]): reverse(n, args=[self.dataset_dest])
for n in dataset_url_names
}
)

if self.tablename_dest != self.tablename_prev:
table_url_names = [
"core:dataset-table-detail",
"api-v1:dataset-table-data",
]
map.update(
**{
reverse(n, args=[self.dataset_prev, self.tablename_prev]): reverse(
n, args=[self.dataset_dest, self.tablename_dest]
)
for n in table_url_names
}
)

return map

@classmethod
def redirect_from(cls, request):
Expand Down
72 changes: 71 additions & 1 deletion core/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.urls import reverse
from model_bakery import baker

from core.models import DataUrlRedirect, TableFile
from core.models import Dataset, DataUrlRedirect, TableFile
from core.tests.utils import BaseTestCaseWithSampleDataset
from traffic_control.tests.util import TrafficControlClient
from utils.tests import DjangoAssertionsMixin
Expand Down Expand Up @@ -185,3 +185,73 @@ def test_dataset_redirect_fetch_data_from_db_for_api_tabledata(self):
response = self.client.get(url)

self.assertRedirects(response, redirect_url, fetch_redirect_response=False)

def test_dataset_redirect_fetch_data_from_db_for_tabledata_with_table_redirect(self):
ds_redirect = self.dataset_redirects[0]
ds_redirect.tablename_prev = "caso2019"
ds_redirect.tablename_dest = "caso2020"
ds_redirect.save()

url = reverse("core:dataset-table-detail", args=[ds_redirect.dataset_prev, "caso2019"])
redirect_url = reverse("core:dataset-table-detail", args=[ds_redirect.dataset_dest, "caso2020"])

response = self.client.get(url)

self.assertRedirects(response, redirect_url, fetch_redirect_response=False)

def test_dataset_redirect_fetch_data_from_db_for_api_tabledata_with_table_redirect(self):
ds_redirect = self.dataset_redirects[0]
ds_redirect.tablename_prev = "caso2019"
ds_redirect.tablename_dest = "caso2020"
ds_redirect.save()

url = reverse("api-v1:dataset-table-data", args=[ds_redirect.dataset_prev, "caso2019"])
redirect_url = reverse("api-v1:dataset-table-data", args=[ds_redirect.dataset_dest, "caso2020"])

response = self.client.get(url)

self.assertRedirects(response, redirect_url, fetch_redirect_response=False)

def test_table_only_redirect(self):
baker.make(Dataset, slug="covid19")
ds_redirect = self.dataset_redirects[0]
ds_redirect.dataset_prev = "covid19"
ds_redirect.dataset_dest = "covid19"
ds_redirect.tablename_prev = "caso2020"
ds_redirect.tablename_dest = "caso2021"
ds_redirect.save()

url = reverse("core:dataset-table-detail", args=["covid19", "caso2020"])
redirect_url = reverse("core:dataset-table-detail", args=["covid19", "caso2021"])

response = self.client.get(url)

self.assertRedirects(response, redirect_url, fetch_redirect_response=False)

# API as well
url = reverse("api-v1:dataset-table-data", args=["covid19", "caso2020"])
redirect_url = reverse("api-v1:dataset-table-data", args=["covid19", "caso2021"])

def test_avoid_infinite_redirect_if_same_dataset_config(self):
ds_redirect = self.dataset_redirects[0]
ds_redirect.dataset_prev = "foo"
ds_redirect.dataset_dest = "foo"
ds_redirect.save()

url = reverse("core:dataset-detail", args=[ds_redirect.dataset_prev])
response = self.client.get(url)

assert 404 == response.status_code

def test_avoid_infinite_redirect_if_same_dataset_config_in_table(self):
ds_redirect = self.dataset_redirects[0]
ds_redirect.dataset_prev = "foo"
ds_redirect.dataset_dest = "foo"
ds_redirect.tablename_prev = "bar"
ds_redirect.tablename_dest = "bar"
ds_redirect.save()

url = reverse("api-v1:dataset-table-data", args=["foo", "bar"])
response = self.client.get(url)

assert 404 == response.status_code
3 changes: 3 additions & 0 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def dataset_detail(request, slug, tablename=""):
except Table.DoesNotExist:
context = {"message": "Table does not exist"}
try:
redirect_url = DataUrlRedirect.redirect_from(request)
if redirect_url:
return redirect(redirect_url)
# log 404 request only if hidden table exist
hidden_table = dataset.get_table(tablename, allow_hidden=True)
if hidden_table:
Expand Down

0 comments on commit c800f07

Please sign in to comment.