From 6a7d097e9f8c0419e74dabc942ad5f9e60d159e5 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Thu, 2 Nov 2023 09:52:24 +0100 Subject: [PATCH] Move create_session_cookie to utils.py It isn't used by anything in auth, only in tests. --- python/nav/web/auth/__init__.py | 23 ----------------------- python/nav/web/auth/utils.py | 24 ++++++++++++++++++++++++ tests/functional/conftest.py | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/python/nav/web/auth/__init__.py b/python/nav/web/auth/__init__.py index 61d2c5197c..c082668a04 100644 --- a/python/nav/web/auth/__init__.py +++ b/python/nav/web/auth/__init__.py @@ -24,8 +24,6 @@ from urllib import parse -from django.conf import settings -from django.contrib.sessions.backends.db import SessionStore from django.urls import reverse from nav.auditlog.models import LogEntry @@ -157,24 +155,3 @@ def logout(request, sudo=False): LogEntry.add_log_entry(account, 'log-out', '{actor} logged out', before=account) _logger.debug('logout: redirect to "/" after logout') return u'/' - - -def create_session_cookie(username): - """Creates an active session for username and returns the resulting - session cookie. - - This is useful to fake login sessions during testing. - - """ - user = Account.objects.get(login=username) - session = SessionStore() - session[ACCOUNT_ID_VAR] = user.id - session.save() - - cookie = { - 'name': settings.SESSION_COOKIE_NAME, - 'value': session.session_key, - 'secure': False, - 'path': '/', - } - return cookie diff --git a/python/nav/web/auth/utils.py b/python/nav/web/auth/utils.py index 70004ee19c..3bfbf0ef8b 100644 --- a/python/nav/web/auth/utils.py +++ b/python/nav/web/auth/utils.py @@ -16,6 +16,9 @@ import logging +from django.conf import settings +from django.contrib.sessions.backends.db import SessionStore + from nav.models.profiles import Account @@ -66,3 +69,24 @@ def authorization_not_required(fullpath): if fullpath.startswith(url): _logger.debug('authorization_not_required: %s', url) return True + + +def create_session_cookie(username): + """Creates an active session for username and returns the resulting + session cookie. + + This is useful to fake login sessions during testing. + + """ + user = Account.objects.get(login=username) + session = SessionStore() + session[ACCOUNT_ID_VAR] = user.id + session.save() + + cookie = { + 'name': settings.SESSION_COOKIE_NAME, + 'value': session.session_key, + 'secure': False, + 'path': '/', + } + return cookie diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index f974f3316c..0281323a3e 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -66,7 +66,7 @@ def selenium(selenium, base_url): bootstrap_django(__file__) - from nav.web.auth import create_session_cookie + from nav.web.auth.utils import create_session_cookie selenium.implicitly_wait(10) wait = WebDriverWait(selenium, 10)