Mikhail Korobov avatar Mikhail Korobov committed 5618345

A very basic test for custom menu

Comments (0)

Files changed (3)

test_proj/menu.py

+from django.core.urlresolvers import reverse
+from django.utils.translation import ugettext_lazy as _
+from admin_tools.menu import items, Menu
+
+# to activate your custom menu add the following to your settings.py:
+#
+# ADMIN_TOOLS_MENU = 'test_proj.menu.CustomMenu'
+
+class CustomMenu(Menu):
+    """
+    Custom Menu for test_proj admin site.
+    """
+    def __init__(self, **kwargs):
+        Menu.__init__(self, **kwargs)
+        self.children += [
+            items.MenuItem(_('Dashboard'), reverse('admin:index')),
+            items.Bookmarks(),
+            items.AppList(
+                _('Applications'),
+                exclude_list=('django.contrib',)
+            ),
+            items.AppList(
+                _('Administration'),
+                include_list=('django.contrib',)
+            ),
+            items.ModelList(
+                'Test app menu',
+                ['test_app.models.Bar', 'django.contrib.auth.*']
+            )
+        ]
+
+    def init_with_context(self, context):
+        """
+        Use this method if you need to access the request context.
+        """
+        pass

test_proj/settings.py

 FIXTURE_DIRS = ['fixtures']
 
 ADMIN_TOOLS_INDEX_DASHBOARD = 'test_proj.dashboard.CustomIndexDashboard'
+ADMIN_TOOLS_MENU = 'test_proj.menu.CustomMenu'
 
 INSTALLED_APPS = [
     'admin_tools',

test_proj/test_app/tests.py

 from django.test import TestCase
 
 class AdminBasicTest(TestCase):
+
+    def index_page(self, username='staff', password='123'):
+        self.assertTrue(self.client.login(username=username, password=password))
+        return self.client.get('/admin/')
+
     def test_admin_loads(self):
-        self.assertTrue(self.client.login(username='staff', password='123'))
-        res = self.client.get('/admin/')
-        self.assertEqual(res.status_code, 200)
+        self.assertEqual(self.index_page().status_code, 200)
 
     def test_permissions(self):
-        self.assertTrue(self.client.login(username='staff', password='123'))
-        res = self.client.get('/admin/')
-        self.assertContains(res, 'Foos')
-        self.assertNotContains(res, 'Bars')
-        self.assertNotContains(res, 'Users')
+        index = self.index_page()
+        self.assertContains(index, 'Foos')
+        self.assertNotContains(index, 'Bars')
+        self.assertNotContains(index, 'Users')
+        self.assertNotContains(index, 'Test app menu')
 
-        self.assertTrue(self.client.login(username='superuser', password='123'))
-        res = self.client.get('/admin/')
-        self.assertContains(res, 'Users', 2) # menu and dashboard items
+        super_index = self.index_page('superuser', '123')
+        self.assertContains(super_index, 'Users', 3) # menu and dashboard items
+        self.assertContains(super_index)
+        self.assertNotContains(super_index, 'Test app menu')
 
     def test_app_index(self):
         self.client.login(username='staff', password='123')
         res = self.client.get('/admin/test_app/')
         self.assertContains(res, 'Foos')
         self.assertContains(res, 'Bars')
-        self.assertContains(res, 'Users', 1) # only item from menu
+        self.assertContains(res, 'Users', 2) # only items from menu
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.