From faf2660c9f09707e8b18db13db02db895d072b3c Mon Sep 17 00:00:00 2001
From: Martin Burchell <martinb@aptivate.org>
Date: Wed, 5 Aug 2015 18:07:07 +0100
Subject: [PATCH] Modified render_tab to pass settings to tab

---
 .../tabbed_page/templatetags/render_tab.py    |  3 +-
 django/website/tabbed_page/tests/factories.py |  1 +
 .../website/tabbed_page/tests/render_tests.py | 66 ++++++++-----------
 django/website/tabbed_page/tests/test_tab.py  |  2 -
 4 files changed, 32 insertions(+), 40 deletions(-)

diff --git a/django/website/tabbed_page/templatetags/render_tab.py b/django/website/tabbed_page/templatetags/render_tab.py
index 89c5b8ab..0e83ed88 100644
--- a/django/website/tabbed_page/templatetags/render_tab.py
+++ b/django/website/tabbed_page/templatetags/render_tab.py
@@ -8,6 +8,7 @@ def render_tab(tab_instance):
 
     template_name = tab.template_name
 
-    context = tab.get_context_data()
+    settings = tab_instance.settings
+    context = tab.get_context_data(**settings)
 
     return render_to_string(template_name, context)
diff --git a/django/website/tabbed_page/tests/factories.py b/django/website/tabbed_page/tests/factories.py
index 8c63caac..3b504da3 100644
--- a/django/website/tabbed_page/tests/factories.py
+++ b/django/website/tabbed_page/tests/factories.py
@@ -17,3 +17,4 @@ class TabInstanceFactory(DjangoModelFactory):
 
     name = fuzzy.FuzzyText()
     label = fuzzy.FuzzyText()
+    settings = {}
diff --git a/django/website/tabbed_page/tests/render_tests.py b/django/website/tabbed_page/tests/render_tests.py
index ece92116..2084d2f9 100644
--- a/django/website/tabbed_page/tests/render_tests.py
+++ b/django/website/tabbed_page/tests/render_tests.py
@@ -15,61 +15,53 @@ class MockTabInstance(object):
     pass
 
 
-@pytest.fixture
-def render_to_string_method():
-    return 'tabbed_page.templatetags.render_tab.render_to_string'
-
-
-def get_mock_render_to_string_parameter(mock, parameter):
-    """ Helper function to return arguments a mock instance
-        of render_to_string has been invoked with.
-
-        Args:
-            mock: A Mock object
-            parameter: either 'template_name' or 'context'
-        Returns:
-            The value of the given parameter for the last invocation
-            of the given mock with the render_to_string signature.
-        Raises:
-            ValueError: If parameter is not 'template_name' or 'context'
-    """
-    if parameter == 'template_name':
-        return mock.call_args[0][0]
-    elif parameter == 'context':
-        return mock.call_args[0][1]
-    else:
-        raise ValueError()
+render_to_string_method = 'tabbed_page.templatetags.render_tab.render_to_string'
 
 
 @pytest.mark.django_db
-def test_uses_template_name(render_to_string_method):
+@patch(render_to_string_method)
+def test_uses_template_name(mock_render):
     tab = TestTab(template_name='test-tab-template')
     register_tab('test-tab', tab)
 
     page = TabbedPageFactory()
     tab_instance = TabInstanceFactory(page=page, name='test-tab')
 
-    with patch(render_to_string_method) as mock:
-        render_tab(tab_instance)
-        template_name = get_mock_render_to_string_parameter(
-            mock, 'template_name'
-        )
-    assert template_name == 'test-tab-template'
+    render_tab(tab_instance)
+
+    mock_render.assert_called_once_with('test-tab-template', {})
 
 
 @pytest.mark.django_db
-def test_uses_context(render_to_string_method):
+@patch(render_to_string_method)
+def test_uses_context(mock_render):
     test_context = {'is_test_tab': True}
 
     tab = TestTab(context=test_context)
     register_tab('test-tab', tab)
 
     page = TabbedPageFactory()
+
     tab_instance = TabInstanceFactory(page=page, name='test-tab')
 
-    with patch(render_to_string_method) as mock:
+    render_tab(tab_instance)
+
+    mock_render.assert_called_once_with(None, test_context)
+
+
+@pytest.mark.django_db
+@patch(render_to_string_method)
+def test_settings_passed_to_widget_get_context_data(render_to_string_method):
+    with patch.object(TestTab, 'get_context_data') as mock_get_context:
+        tab = TestTab()
+        register_tab('test-tab', tab)
+
+        page = TabbedPageFactory()
+        columns = ['body', 'timestamp', 'network_provider']
+        settings = {'columns': columns}
+        tab_instance = TabInstanceFactory(page=page,
+                                          name='test-tab',
+                                          settings=settings)
         render_tab(tab_instance)
-        actual_context = get_mock_render_to_string_parameter(
-            mock, 'context'
-        )
-    assert actual_context == test_context
+
+    mock_get_context.assert_called_once_with(columns=columns)
diff --git a/django/website/tabbed_page/tests/test_tab.py b/django/website/tabbed_page/tests/test_tab.py
index 1355afab..59e3f8e5 100644
--- a/django/website/tabbed_page/tests/test_tab.py
+++ b/django/website/tabbed_page/tests/test_tab.py
@@ -13,8 +13,6 @@ class TestTab(object):
     def get_context_data(self, **kwargs):
         context = getattr(self, 'context', {})
 
-        context['kwargs'] = kwargs
-
         return context
 
 
-- 
GitLab