# encoding=utf-8 # # Copyright 2012 Nebula, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import copy from django.conf import settings from django import http import six from horizon import exceptions from horizon import middleware from horizon import tabs as horizon_tabs from horizon.test import helpers as test from horizon.test.unit.tables.test_tables import MyTable from horizon.test.unit.tables.test_tables import TEST_DATA class BaseTestTab(horizon_tabs.Tab): def get_context_data(self, request): return {"tab": self} class TabOne(BaseTestTab): slug = "tab_one" name = "Tab One" template_name = "_tab.html" class TabDelayed(BaseTestTab): slug = "tab_delayed" name = "Delayed Tab" template_name = "_tab.html" preload = False class TabDisabled(BaseTestTab): slug = "tab_disabled" name = "Disabled Tab" template_name = "_tab.html" def enabled(self, request): return False class TabDisallowed(BaseTestTab): slug = "tab_disallowed" name = "Disallowed Tab" template_name = "_tab.html" def allowed(self, request): return False class Group(horizon_tabs.TabGroup): slug = "tab_group" tabs = (TabOne, TabDelayed, TabDisabled, TabDisallowed) sticky = True def tabs_not_available(self): self._assert_tabs_not_available = True class TabWithTable(horizon_tabs.TableTab): table_classes = (MyTable,) name = "Tab With My Table" slug = "tab_with_table" template_name = "horizon/common/_detail_table.html" def get_my_table_data(self): return TEST_DATA class RecoverableErrorTab(horizon_tabs.Tab): name = "Recoverable Error Tab" slug = "recoverable_error_tab" template_name = "_tab.html" def get_context_data(self, request): # Raise a known recoverable error. exc = exceptions.AlreadyExists("Recoverable!", horizon_tabs.Tab) exc.silence_logging = True raise exc class RedirectExceptionTab(horizon_tabs.Tab): name = "Redirect Exception Tab" slug = "redirect_exception_tab" template_name = "_tab.html" url = settings.TESTSERVER + settings.LOGIN_URL def get_context_data(self, request): # Raise a known recoverable error. exc = exceptions.Http302(self.url) exc.silence_logging = True raise exc class TableTabGroup(horizon_tabs.TabGroup): slug = "tab_group" tabs = [TabWithTable] class TabWithTableView(horizon_tabs.TabbedTableView): tab_group_class = TableTabGroup template_name = "tab_group.html" class TabTests(test.TestCase): def test_tab_group_basics(self): tg = Group(self.request) # Test tab instantiation/attachment to tab group, and get_tabs method tabs = tg.get_tabs() # "tab_disallowed" should NOT be in this list. self.assertQuerysetEqual(tabs, ['', '', '']) # Test get_id self.assertEqual("tab_group", tg.get_id()) # get_default_classes self.assertEqual(horizon_tabs.base.CSS_TAB_GROUP_CLASSES, tg.get_default_classes()) # Test get_tab self.assertEqual("tab_one", tg.get_tab("tab_one").slug) # Test selected is None w/o GET input self.assertIsNone(tg.selected) # Test get_selected_tab is None w/o GET input self.assertIsNone(tg.get_selected_tab()) def test_tab_group_active_tab(self): tg = Group(self.request) # active tab w/o selected self.assertEqual(tg.get_tabs()[0], tg.active) # active tab w/ selected self.request.GET['tab'] = "tab_group__tab_delayed" tg = Group(self.request) self.assertEqual(tg.get_tab('tab_delayed'), tg.active) # active tab w/ invalid selected self.request.GET['tab'] = "tab_group__tab_invalid" tg = Group(self.request) self.assertEqual(tg.get_tabs()[0], tg.active) # active tab w/ disallowed selected self.request.GET['tab'] = "tab_group__tab_disallowed" tg = Group(self.request) self.assertEqual(tg.get_tabs()[0], tg.active) # active tab w/ disabled selected self.request.GET['tab'] = "tab_group__tab_disabled" tg = Group(self.request) self.assertEqual(tg.get_tabs()[0], tg.active) # active tab w/ non-empty garbage selected # Note: this entry does not contain the '__' SEPARATOR string. self.request.GET['tab'] = "