Skip to content

Commit

Permalink
added tests and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
4-dash committed Dec 22, 2024
1 parent 8511a8e commit 284e08d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/feedback/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 34 additions & 1 deletion src/feedback/tests/test_views_intern_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/feedback/views/intern/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 7 additions & 2 deletions src/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -25,4 +26,8 @@
re_path(r'^feedback/', include(("feedback.urls", "feedback"), namespace="feedback")),
)

urlpatterns += staticfiles_urlpatterns()
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))]

0 comments on commit 284e08d

Please sign in to comment.