Skip to content

Commit

Permalink
Move create_session_cookie to utils.py
Browse files Browse the repository at this point in the history
It isn't used by anything in auth, only in tests.
  • Loading branch information
hmpf committed Nov 2, 2023
1 parent f1191b7 commit 6a7d097
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
23 changes: 0 additions & 23 deletions python/nav/web/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
24 changes: 24 additions & 0 deletions python/nav/web/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6a7d097

Please sign in to comment.