def test_set_account(): r = RequestFactory() request = r.get('/') request.session = FakeSession() auth._set_account(request, DEFAULT_ACCOUNT) assert 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[ ACCOUNT_ID_VAR], 'Correct user not set' assert request.session[ACCOUNT_ID_VAR] == DEFAULT_ACCOUNT.id
def test_process_request_log_in_remote_user(self): r = RequestFactory() fake_request = r.get('/') fake_request.session = FakeSession() with patch('nav.web.auth.ensure_account', side_effect=auth._set_account(fake_request, DEFAULT_ACCOUNT)): with patch('nav.web.auth.get_remote_username', return_value=PLAIN_ACCOUNT.login): with patch('nav.web.auth.login_remote_user', side_effect=auth._set_account( fake_request, PLAIN_ACCOUNT)): AuthenticationMiddleware().process_request(fake_request) assert fake_request.account == PLAIN_ACCOUNT assert fake_request.session[ ACCOUNT_ID_VAR] == PLAIN_ACCOUNT.id
def test_process_request_logged_in(self): r = RequestFactory() fake_request = r.get('/') fake_request.session = FakeSession(ACCOUNT_ID_VAR=PLAIN_ACCOUNT.id) with patch('nav.web.auth.ensure_account', side_effect=auth._set_account(fake_request, PLAIN_ACCOUNT)): AuthenticationMiddleware().process_request(fake_request) assert fake_request.account == PLAIN_ACCOUNT assert fake_request.session[ ACCOUNT_ID_VAR] == fake_request.account.id
def test_process_request_set_sudoer(self): r = RequestFactory() fake_request = r.get('/') fake_request.session = FakeSession(ACCOUNT_ID_VAR=PLAIN_ACCOUNT.id, SUDOER_ID_VAR=SUDO_ACCOUNT.id) with patch('nav.web.auth.ensure_account', side_effect=auth._set_account(fake_request, PLAIN_ACCOUNT)): with patch('nav.web.auth.get_sudoer', return_value=SUDO_ACCOUNT): AuthenticationMiddleware().process_request(fake_request) assert getattr(fake_request.account, 'sudo_operator', None) == SUDO_ACCOUNT
def test_process_request_not_logged_in(self): r = RequestFactory() fake_request = r.get('/') fake_request.session = FakeSession() with patch( 'nav.web.auth.ensure_account', side_effect=auth._set_account(fake_request, DEFAULT_ACCOUNT), ): with patch('nav.web.auth.get_remote_username', return_value=None): AuthenticationMiddleware(lambda x: x).process_request( fake_request) assert fake_request.account == DEFAULT_ACCOUNT assert fake_request.session[ ACCOUNT_ID_VAR] == fake_request.account.id
def test_process_request_switch_users(self): r = RequestFactory() fake_request = r.get('/') fake_request.session = FakeSession() with patch( 'nav.web.auth.ensure_account', side_effect=auth._set_account(fake_request, PLAIN_ACCOUNT), ): with patch( 'nav.web.auth.get_remote_username', return_value=ANOTHER_PLAIN_ACCOUNT.login, ): with patch( 'nav.web.auth.login_remote_user', side_effect=auth._set_account(fake_request, ANOTHER_PLAIN_ACCOUNT), ): with patch('nav.web.auth.logout'): AuthenticationMiddleware().process_request( fake_request) assert fake_request.account == ANOTHER_PLAIN_ACCOUNT assert (ACCOUNT_ID_VAR in fake_request.session and fake_request.session[ACCOUNT_ID_VAR] == ANOTHER_PLAIN_ACCOUNT.id)