def test_account_is_switched_to_default_if_locked(self): r = RequestFactory() request = r.get('/') request.session = FakeSession() request.session[auth.ACCOUNT_ID_VAR] = LOCKED_ACCOUNT.id with patch("nav.web.auth.Account.objects.get", side_effect=[LOCKED_ACCOUNT, DEFAULT_ACCOUNT]): auth.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): r = RequestFactory() request = r.get('/') request.session = FakeSession() request.session[auth.ACCOUNT_ID_VAR] = return_value = PLAIN_ACCOUNT.id with patch("nav.web.auth.Account.objects.get", return_value=PLAIN_ACCOUNT): auth.ensure_account(request) assert request.account == PLAIN_ACCOUNT assert request.session[auth.ACCOUNT_ID_VAR] == PLAIN_ACCOUNT.id
def test_account_is_set_if_missing(self): r = RequestFactory() request = r.get('/') request.session = {} request.session = FakeSession() with patch("nav.web.auth.Account.objects.get", return_value=DEFAULT_ACCOUNT): auth.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'