Require login for index view

This commit is contained in:
Jan Dittberner 2023-04-14 19:33:44 +02:00
parent a5b65974fb
commit dd67ee91da
3 changed files with 23 additions and 3 deletions

View file

@ -14,7 +14,17 @@ TEST_PASSWORD = "secret"
class IndexViewTest(TestCase): class IndexViewTest(TestCase):
def test_index_view_anonymous(self):
response = self.client.get(reverse("dashboard"))
self.assertRedirects(response, "/accounts/login/?next=/")
def test_index_view(self): def test_index_view(self):
user = User.objects.create(username=TEST_USER)
user.set_password(TEST_PASSWORD)
user.save()
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
response = self.client.get(reverse("dashboard")) response = self.client.get(reverse("dashboard"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "dashboard/index.html") self.assertTemplateUsed(response, "dashboard/index.html")

View file

@ -3,13 +3,14 @@ This module defines the views for the gnuviechadmin customer dashboard.
""" """
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView, TemplateView from django.views.generic import DetailView, TemplateView
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
from hostingpackages.models import CustomerHostingPackage from hostingpackages.models import CustomerHostingPackage
class IndexView(TemplateView): class IndexView(LoginRequiredMixin, TemplateView):
""" """
This is the dashboard view. This is the dashboard view.

View file

@ -18,13 +18,16 @@ from gnuviechadmin.context_processors import navigation
User = get_user_model() User = get_user_model()
TEST_USER = "test"
TEST_PASSWORD = "secret"
class NavigationContextProcessorTest(TestCase): class NavigationContextProcessorTest(TestCase):
EXPECTED_ITEMS = ("webmail_url", "phpmyadmin_url", "phppgadmin_url", "active_item") EXPECTED_ITEMS = ("webmail_url", "phpmyadmin_url", "phppgadmin_url", "active_item")
def test_ajax_request(self): def test_ajax_request(self):
response = self.client.get("/", HTTP_X_REQUESTED_WITH="XMLHttpRequest") response = self.client.get("/accounts/login/", HTTP_X_REQUESTED_WITH="XMLHttpRequest")
for item in self.EXPECTED_ITEMS: for item in self.EXPECTED_ITEMS:
self.assertNotIn(item, response.context) self.assertNotIn(item, response.context)
@ -34,6 +37,12 @@ class NavigationContextProcessorTest(TestCase):
self.assertEqual(context["phppgadmin_url"], settings.GVA_LINK_PHPPGADMIN) self.assertEqual(context["phppgadmin_url"], settings.GVA_LINK_PHPPGADMIN)
def test_index_page_context(self): def test_index_page_context(self):
user = User.objects.create(username=TEST_USER)
user.set_password(TEST_PASSWORD)
user.save()
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
response = self.client.get("/") response = self.client.get("/")
for item in self.EXPECTED_ITEMS: for item in self.EXPECTED_ITEMS:
self.assertIn(item, response.context) self.assertIn(item, response.context)
@ -106,6 +115,6 @@ class NavigationContextProcessorTest(TestCase):
class VersionInfoContextProcessorTest(TestCase): class VersionInfoContextProcessorTest(TestCase):
def test_version_info_in_context(self): def test_version_info_in_context(self):
response = self.client.get("/") response = self.client.get("/accounts/login/")
self.assertIn("gnuviechadmin_version", response.context) self.assertIn("gnuviechadmin_version", response.context)
self.assertEqual(response.context["gnuviechadmin_version"], gvaversion) self.assertEqual(response.context["gnuviechadmin_version"], gvaversion)