def test_check_session_with_no_user_id_in_session(self): with patch('app.authentication.authenticator.session_storage') as session_storage: # Given session_storage.has_user_id = Mock(return_value=False) # When user = Authenticator().check_session() # Then self.assertIsNone(user)
def test_check_session_with_user_id_in_session(self): with patch('app.authentication.authenticator.session_storage') as session_storage, \ patch('app.authentication.authenticator.get_questionnaire_store', return_value=MagicMock()): # Given session_storage.has_user_id = Mock(return_value=True) session_storage.get_user_id = Mock(return_value='user_id') session_storage.get_user_ik = Mock(return_value='user_ik') # When user = Authenticator().check_session() # Then self.assertEqual(user.user_id, 'user_id') self.assertEqual(user.user_ik, 'user_ik')
def login(): """ Initial url processing - expects a token parameter and then will authenticate this token. Once authenticated it will be placed in the users session :return: a 302 redirect to the next location for the user """ # logging in again clears any session state if session: session.clear() authenticator = Authenticator() logger.debug("Attempting token authentication") authenticator.jwt_login(request) logger.debug("Token authenticated - linking to session") metadata = get_metadata(current_user) eq_id = metadata["eq_id"] form_type = metadata["form_type"] logger.debug("Requested questionnaire %s for form type %s", eq_id, form_type) if not eq_id or not form_type: logger.error("Missing EQ id %s or form type %s in JWT", eq_id, form_type) raise NotFound json, _ = get_schema(metadata) navigator = PathFinder(json, get_answer_store(current_user), metadata) current_location = navigator.get_latest_location( get_completed_blocks(current_user)) return redirect(current_location.url(metadata))
def login(): """ Initial url processing - expects a token parameter and then will authenticate this token. Once authenticated it will be placed in the users session :return: a 302 redirect to the next location for the user """ # logging in again clears any session state if session: session.clear() authenticator = Authenticator() logger.debug("Attempting token authentication") authenticator.jwt_login(request) logger.debug("Token authenticated - linking to session") metadata = get_metadata(current_user) eq_id = metadata["eq_id"] collection_id = metadata["collection_exercise_sid"] form_type = metadata["form_type"] period_id = metadata["period_id"] logger.debug("Requested questionnaire %s for form type %s", eq_id, form_type) if not eq_id or not form_type: logger.error("Missing EQ id %s or form type %s in JWT", eq_id, form_type) raise NotFound json, schema = get_schema() questionnaire_manager = QuestionnaireManager(schema, json=json) navigator = questionnaire_manager.navigator current_location = navigator.get_latest_location(get_answers(current_user), get_completed_blocks(current_user)) return redirect('/questionnaire/' + eq_id + '/' + form_type + '/' + period_id + '/' + collection_id + '/' + current_location)
def request_load_user(request): logger.debug("Load user %s", request) authenticator = Authenticator() return authenticator.check_session()
def load_user(user_id): logger.debug("Loading user %s", user_id) logger.debug(user_id) authenticator = Authenticator() return authenticator.check_session()