|
|
@@ -1,9 +1,12 @@
|
|
|
+import csv
|
|
|
+import io
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
from django.contrib.auth.context_processors import PermWrapper
|
|
|
from django.contrib.auth.models import AnonymousUser
|
|
|
from django.test import RequestFactory, TestCase, override_settings
|
|
|
+from django.urls import reverse
|
|
|
from django.utils.html import escape
|
|
|
|
|
|
from core.models import ObjectType
|
|
|
@@ -362,6 +365,28 @@ class CustomLinkRestrictedRenderingTest(TestCase):
|
|
|
# A table built from a list is also serialized in chunks
|
|
|
self.assertEqual([len(call.args[1]) for call in load_objects.call_args_list], [2, 1])
|
|
|
|
|
|
+ @patch('netbox.tables.columns.TEMPLATE_OBJECTS_CHUNK_SIZE', 2)
|
|
|
+ def test_table_export_view(self):
|
|
|
+ custom_link = CustomLink.objects.create(
|
|
|
+ name='Link 1', enabled=True, link_text='x', link_url='/{{ object.slug }}'
|
|
|
+ )
|
|
|
+ custom_link.object_types.set([ObjectType.objects.get_for_model(Site)])
|
|
|
+ # Enough sites that the last page doesn't absorb the others as orphans
|
|
|
+ Site.objects.bulk_create([Site(name=f'Site {i}', slug=f'site-{i}') for i in range(4, 10)])
|
|
|
+ self.client.force_login(self.user)
|
|
|
+
|
|
|
+ # Export all sites, beyond the first page of the table
|
|
|
+ with patch('utilities.template_objects._load_objects', wraps=_load_objects) as load_objects:
|
|
|
+ response = self.client.get(reverse('dcim:site_list'), {
|
|
|
+ 'export': '', 'export_format': 'csv', 'per_page': 1,
|
|
|
+ })
|
|
|
+ content = b''.join(response.streaming_content) if response.streaming else response.content
|
|
|
+ self.assertEqual(response.status_code, 200)
|
|
|
+ rows = csv.DictReader(io.StringIO(content.decode('utf-8-sig')))
|
|
|
+ self.assertEqual([row['Link 1'] for row in rows], [f'/site-{i}' for i in range(1, 10)])
|
|
|
+ # The page is serialized at once, and the records following it in chunks
|
|
|
+ self.assertEqual([len(call.args[1]) for call in load_objects.call_args_list], [1, 2, 2, 2, 2])
|
|
|
+
|
|
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
|
|
def test_table_anonymous_user(self):
|
|
|
custom_link = CustomLink.objects.create(
|