Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request Uninett#2976 from Uninett/test/selenium-login-driver
Browse files Browse the repository at this point in the history
Replace session fakery by login simulation in Selenium functional tests
  • Loading branch information
lunkwill42 authored Sep 17, 2024
2 parents 2e57819 + 984516f commit 08741f4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 58 deletions.
24 changes: 0 additions & 24 deletions python/nav/web/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
"""
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 @@ -87,24 +84,3 @@ 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
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""pytest setup and fixtures common for all tests, regardless of suite"""

import os

import pytest


@pytest.fixture(scope='session')
def admin_username():
return os.environ.get('ADMINUSERNAME', 'admin')


@pytest.fixture(scope='session')
def admin_password():
return os.environ.get('ADMINPASSWORD', 'admin')
40 changes: 16 additions & 24 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def pytest_configure(config):
subprocess.check_call([SCRIPT_CREATE_DB])
start_gunicorn()

# Bootstrap Django config
from nav.bootstrap import bootstrap_django

bootstrap_django('pytest')


def pytest_unconfigure(config):
stop_gunicorn()
Expand Down Expand Up @@ -57,40 +62,27 @@ def stop_gunicorn():


@pytest.fixture
def selenium(selenium, base_url):
def selenium(selenium, base_url, admin_username, admin_password):
"""Fixture to initialize the selenium web driver with a NAV session logged
in as the admin user.
"""
from nav.bootstrap import bootstrap_django

bootstrap_django(__file__)

from nav.web.auth.utils import create_session_cookie

selenium.implicitly_wait(10)
wait = WebDriverWait(selenium, 10)

cookie = create_session_cookie(USERNAME)
# visit a non-existent URL just to set the site context for cookies
selenium.get('{}/images/400'.format(base_url))
wait.until(EC.text_to_be_present_in_element((By.TAG_NAME, "h1"), "Not found"))

print("Cookies after first fetch: {!r}".format(selenium.get_cookies()))
selenium.delete_all_cookies()
print("Setting session cookie for {}: {!r}".format(USERNAME, cookie))
selenium.add_cookie(cookie)
# Cookie modification is also _non-blocking_ in Selenium, so we need to
# wait for the cookie to become present in the browser before we continue!
wait.until(_session_cookie_is_present(cookie))

print("Cookies after set, before refresh: {!r}".format(selenium.get_cookies()))
selenium.refresh()
# visit the login page and submit the login form
selenium.get(f"{base_url}/index/login")
wait.until(EC.text_to_be_present_in_element((By.TAG_NAME, "label"), "Username"))

print("Cookies after refresh: {!r}".format(selenium.get_cookies()))
username = selenium.find_element(By.ID, "id_username")
password = selenium.find_element(By.ID, "id_password")
username.send_keys(admin_username)
password.send_keys(admin_password)
selenium.find_element(By.NAME, "submit").click()
wait.until(EC.url_changes("/"))

# Yield logged-in session to the test
yield selenium
print("Cookies after test: {!r}".format(selenium.get_cookies()))


class _session_cookie_is_present(object):
Expand Down
10 changes: 0 additions & 10 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,3 @@ def admin_account(db):
from nav.models.profiles import Account

yield Account.objects.get(id=Account.ADMIN_ACCOUNT)


@pytest.fixture(scope='session')
def admin_username():
return os.environ.get('ADMINUSERNAME', 'admin')


@pytest.fixture(scope='session')
def admin_password():
return os.environ.get('ADMINPASSWORD', 'admin')

0 comments on commit 08741f4

Please sign in to comment.