Skip to content

Commit

Permalink
Port ensure_account tests to integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stveit committed Mar 4, 2024
1 parent f2970f6 commit 149b11b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
28 changes: 28 additions & 0 deletions tests/integration/web/auth/ensure_account_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from nav.web.auth import logout, Account
from nav.web.auth.utils import ACCOUNT_ID_VAR, set_account, ensure_account
from nav.web.auth.sudo import sudo


def test_account_is_set_if_missing(session_request):
assert not hasattr(session_request, "account")
ensure_account(session_request)
assert ACCOUNT_ID_VAR in session_request.session, 'Account id is not in the session'
assert hasattr(session_request, 'account'), 'Account not set'
assert (
session_request.account.id == session_request.session[ACCOUNT_ID_VAR]
), 'Correct user not set'


def test_account_is_switched_to_default_if_locked(session_request, locked_account):
set_account(session_request, locked_account)
ensure_account(session_request)
default_account = Account.objects.get(id=Account.DEFAULT_ACCOUNT)
assert session_request.session[ACCOUNT_ID_VAR] == default_account.id
assert session_request.account == default_account, 'Correct user not set'


def test_account_is_left_alone_if_ok(session_request, account):
set_account(session_request, account)
ensure_account(session_request)
assert session_request.account == account
assert session_request.session[ACCOUNT_ID_VAR] == account.id
39 changes: 0 additions & 39 deletions tests/unittests/general/web_middleware_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,6 @@ def test_set_account(fake_session):
assert request.session[ACCOUNT_ID_VAR] == DEFAULT_ACCOUNT.id


class TestEnsureAccount(object):
def test_account_is_set_if_missing(self, fake_session):
r = RequestFactory()
request = r.get('/')
request.session = fake_session
with patch("nav.web.auth.Account.objects.get", return_value=DEFAULT_ACCOUNT):
ensure_account(request)
assert (
auth.ACCOUNT_ID_VAR in request.session
), 'Account id is not in the session'
assert hasattr(request, 'account'), 'Account not set'
assert (
request.account.id == request.session[auth.ACCOUNT_ID_VAR]
), 'Correct user not set'

def test_account_is_switched_to_default_if_locked(self, fake_session):
r = RequestFactory()
request = r.get('/')
request.session = fake_session
request.session[auth.ACCOUNT_ID_VAR] = LOCKED_ACCOUNT.id
with patch(
"nav.web.auth.Account.objects.get",
side_effect=[LOCKED_ACCOUNT, DEFAULT_ACCOUNT],
):
ensure_account(request)
assert request.session[auth.ACCOUNT_ID_VAR] == DEFAULT_ACCOUNT.id
assert request.account == DEFAULT_ACCOUNT, 'Correct user not set'

def test_account_is_left_alone_if_ok(self, fake_session):
r = RequestFactory()
request = r.get('/')
request.session = fake_session
request.session[auth.ACCOUNT_ID_VAR] = return_value = PLAIN_ACCOUNT.id
with patch("nav.web.auth.Account.objects.get", return_value=PLAIN_ACCOUNT):
ensure_account(request)
assert request.account == PLAIN_ACCOUNT
assert request.session[auth.ACCOUNT_ID_VAR] == PLAIN_ACCOUNT.id


class TestAuthenticationMiddleware(object):
def test_process_request_logged_in(self, fake_session):
r = RequestFactory()
Expand Down

0 comments on commit 149b11b

Please sign in to comment.