gva/gnuviechadmin/gnuviechadmin/tests/test_contextprocessors.py
Jan Dittberner 3d18392b67 Fix tests for Python 3
- drop Python 2 __future__ imports
- fix tests to handle new Django and Python 3 module names
- reformat changed files with black
2019-01-30 21:27:25 +01:00

112 lines
4.3 KiB
Python

# -*- python -*-
# -*- coding: utf-8 -*-
"""
This module contains tests for :py:mod:`gnuviechadmin.context_processors`.
"""
from unittest.mock import MagicMock
from django.conf import settings
from django.contrib.auth import get_user_model
from django.http import HttpRequest
from django.test import TestCase
from django.urls import reverse
from gnuviechadmin import __version__ as gvaversion
from gnuviechadmin.context_processors import navigation
User = get_user_model()
class NavigationContextProcessorTest(TestCase):
EXPECTED_ITEMS = ("webmail_url", "phpmyadmin_url", "phppgadmin_url", "active_item")
def test_ajax_request(self):
response = self.client.get("/", HTTP_X_REQUESTED_WITH="XMLHttpRequest")
for item in self.EXPECTED_ITEMS:
self.assertNotIn(item, response.context)
def _check_static_urls(self, context):
self.assertEqual(context["webmail_url"], settings.GVA_LINK_WEBMAIL)
self.assertEqual(context["phpmyadmin_url"], settings.GVA_LINK_PHPMYADMIN)
self.assertEqual(context["phppgadmin_url"], settings.GVA_LINK_PHPPGADMIN)
def test_index_page_context(self):
response = self.client.get("/")
for item in self.EXPECTED_ITEMS:
self.assertIn(item, response.context)
self._check_static_urls(response.context)
self.assertEqual(response.context["active_item"], "dashboard")
def test_contact_page_context(self):
response = self.client.get(reverse("contact_form"))
for item in self.EXPECTED_ITEMS:
self.assertIn(item, response.context)
self._check_static_urls(response.context)
self.assertEqual(response.context["active_item"], "contact")
def test_hostingpackage_page_context(self):
User.objects.create_user("test", password="test")
self.client.login(username="test", password="test")
response = self.client.get(reverse("hosting_packages", kwargs={"user": "test"}))
for item in self.EXPECTED_ITEMS:
self.assertIn(item, response.context)
self._check_static_urls(response.context)
self.assertEqual(response.context["active_item"], "hostingpackage")
def _test_page_context_by_viewmodule(self, viewmodule, expecteditem):
request = HttpRequest()
request.resolver_match = MagicMock()
request.resolver_match.func.__module__ = viewmodule
context = navigation(request)
for item in self.EXPECTED_ITEMS:
self.assertIn(item, context)
self._check_static_urls(context)
self.assertEqual(context["active_item"], expecteditem)
def test_osusers_page_context(self):
self._test_page_context_by_viewmodule("osusers.views", "hostingpackage")
def test_userdbs_page_context(self):
self._test_page_context_by_viewmodule("userdbs.views", "hostingpackage")
def test_managemails_page_context(self):
self._test_page_context_by_viewmodule("managemails.views", "hostingpackage")
def test_websites_page_context(self):
self._test_page_context_by_viewmodule("websites.views", "hostingpackage")
def test_domains_page_context(self):
self._test_page_context_by_viewmodule("domains.views", "hostingpackage")
def test_allauth_account_page_context(self):
self._test_page_context_by_viewmodule("allauth.account.views", "account")
def test_allauth_socialaccount_page_context(self):
self._test_page_context_by_viewmodule("allauth.socialaccount.views", "account")
def test_imprint_page_context(self):
response = self.client.get(reverse("imprint"))
for item in self.EXPECTED_ITEMS:
self.assertIn(item, response.context)
self._check_static_urls(response.context)
self.assertEqual(response.context["active_item"], "imprint")
def test_no_resolver_match(self):
request = HttpRequest()
context = navigation(request)
self._check_static_urls(context)
self.assertEqual(context["active_item"], "dashboard")
def test_admin_module(self):
self._test_page_context_by_viewmodule("django.contrib.admin.foo", "dashboard")
class VersionInfoContextProcessorTest(TestCase):
def test_version_info_in_context(self):
response = self.client.get("/")
self.assertIn("gnuviechadmin_version", response.context)
self.assertEqual(response.context["gnuviechadmin_version"], gvaversion)