From 284e08d8737f4dd87df91ac54369b69334dee7d1 Mon Sep 17 00:00:00 2001 From: 4-dash <120916864+4-dash@users.noreply.github.com> Date: Sun, 22 Dec 2024 20:22:40 +0400 Subject: [PATCH] added tests and minor changes --- src/feedback/tests/__init__.py | 6 ++-- src/feedback/tests/test_views_intern_auth.py | 35 +++++++++++++++++++- src/feedback/views/intern/auth.py | 3 +- src/settings.py | 2 +- src/urls.py | 9 +++-- 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/feedback/tests/__init__.py b/src/feedback/tests/__init__.py index a333674..1292c71 100644 --- a/src/feedback/tests/__init__.py +++ b/src/feedback/tests/__init__.py @@ -25,8 +25,10 @@ def suite(): suite.addTest(unittest.TestLoader().loadTestsFromName(test_module)) return suite -LOGIN_END = f'/{get_language()}/intern/' -INDEX_END = f'/{get_language()}/intern/uebersicht/' +LOGIN_END = f'/{get_language()}/feedback/intern/' +INDEX_END = f'/{get_language()}/feedback/intern/uebersicht/' +AUTH_END = f'/{get_language()}/feedback/intern/auth_user/' TESTSERVER_BEGIN = '' LOGIN_URL = TESTSERVER_BEGIN+LOGIN_END INDEX_URL = TESTSERVER_BEGIN+INDEX_END +AUTH_URL = TESTSERVER_BEGIN+AUTH_END diff --git a/src/feedback/tests/test_views_intern_auth.py b/src/feedback/tests/test_views_intern_auth.py index 334fa6b..bf20f75 100644 --- a/src/feedback/tests/test_views_intern_auth.py +++ b/src/feedback/tests/test_views_intern_auth.py @@ -9,12 +9,16 @@ from feedback import tests from feedback.models import Veranstaltung from django.utils.translation import get_language - +from django.contrib.auth.models import User +from django.urls import reverse class InternAuthTest(NonSuTestMixin, TestCase): def setUp(self): super(InternAuthTest, self).setUp() _, self.v = get_veranstaltung('vu') + self.username = 'supers1' + self.password = 'pw1' + self.user = User.objects.create_user(username=self.username, password=self.password) def test_login_logged_in(self): self.client.login(username='supers', password='pw') @@ -33,6 +37,35 @@ def test_login_debug_auth(self): self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], tests.INDEX_URL) + def test_auth_user(self) : + response = self.client.get(tests.AUTH_URL) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'registration/login.html') + + def test_auth_user_post_valid(self) : + response = self.client.post(tests.AUTH_URL, {'username':self.username, 'password':self.password}) + self.assertRedirects(response, tests.LOGIN_URL, target_status_code=302) + + login_required_url = reverse('feedback:intern-index') + + response = self.client.get(login_required_url) + self.assertEqual(response.status_code, 302) + + self.assertTrue(response['Location'].split('?')[0].endswith(tests.LOGIN_URL)) + self.assertTrue(response['Location'].split('?')[1].endswith(login_required_url)) + + def test_auth_post_invalid_credentials(self) : + response = self.client.post(tests.AUTH_URL,{'username': self.username,'password': 'wrongpassword'}) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'registration/login.html') + self.assertContains(response, "Invalid Password or Username") + + def test_auth_user_post_missing_data(self): + response = self.client.post(tests.AUTH_URL, {}) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, 'registration/login.html') + self.assertContains(response, "Invalid Password or Username") + def test_rechte_uebernehmen(self): path = f'/{get_language()}/feedback/intern/rechte_uebernehmen/' self.do_non_su_test(path) diff --git a/src/feedback/views/intern/auth.py b/src/feedback/views/intern/auth.py index 19eb9d7..0b629a2 100644 --- a/src/feedback/views/intern/auth.py +++ b/src/feedback/views/intern/auth.py @@ -68,9 +68,10 @@ def auth_user(request) : auth.login(request, user) return HttpResponseRedirect(reverse('feedback:auth-login')) - elif request.method == "GET" : + elif request.method == "GET" and not request.user.is_authenticated : return render(request, 'registration/login.html') + return HttpResponseRedirect(reverse('feedback:auth-login')) @require_safe def login(request): diff --git a/src/settings.py b/src/settings.py index c7e8793..fd5cc47 100644 --- a/src/settings.py +++ b/src/settings.py @@ -112,7 +112,7 @@ 'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'feedback.auth.FSDebugRemoteUserMiddleware', - # 'whitenoise.middleware.WhiteNoiseMiddleware', # useful when testing with DEBUG=False, Note:first pip install whitenoise + # 'whitenoise.middleware.WhiteNoiseMiddleware', # while DEBUG=False servers static files, Note:first pip install whitenoise ] if not TESTING: MIDDLEWARE += 'debug_toolbar.middleware.DebugToolbarMiddleware' diff --git a/src/urls.py b/src/urls.py index c6e20e2..2dd1ccc 100644 --- a/src/urls.py +++ b/src/urls.py @@ -4,10 +4,11 @@ from django.contrib import admin from django.contrib.auth.models import User -from django.urls import re_path, reverse_lazy +from django.urls import re_path, reverse_lazy, path from django.conf.urls.i18n import i18n_patterns from django.views.generic.base import RedirectView from django.contrib.staticfiles.urls import staticfiles_urlpatterns +from django.conf import settings # Admin-Seiten konfigurieren admin.autodiscover() @@ -25,4 +26,8 @@ re_path(r'^feedback/', include(("feedback.urls", "feedback"), namespace="feedback")), ) -urlpatterns += staticfiles_urlpatterns() \ No newline at end of file +urlpatterns += staticfiles_urlpatterns() + +if not settings.DEBUG : + # don't forget to put sample favicon.ico in static files + urlpatterns += [path('favicon.ico', RedirectView.as_view(url='/feedback/static/img/favicon.ico', permanent=True))]