def create_user(orcid, email, name, consumer_key, token=None, allow_push=False): user = User() user.email = email with db.session.begin_nested(): db.session.add(user) oauth_link_external_id(user, {"id": orcid, "method": "orcid"}) if token: with db.session.begin_nested(): db.session.add( RemoteToken.create( user_id=user.id, client_id=consumer_key, token=token, secret=None, extra_data={ "orcid": orcid, "full_name": name, "allow_push": allow_push, }, ))
def create_user(orcid, email, name, consumer_key, token=None, allow_push=False): user = User() user.email = email with db.session.begin_nested(): db.session.add(user) oauth_link_external_id(user, { 'id': orcid, 'method': 'orcid' }) if token: with db.session.begin_nested(): db.session.add( RemoteToken.create( user_id=user.id, client_id=consumer_key, token=token, secret=None, extra_data={ 'orcid': orcid, 'full_name': name, 'allow_push': allow_push, } ) )
def _register_user(name, email, orcid, token): """Add a token to the user, creating the user if doesn't exist. There are multiple possible scenarios: - user exists, has ORCID and token already linked - user exists and has their ORCID linked, but no token is associated - user exists, but doesn't have the ORCID identifier linked - user doesn't exist at all In all the above scenarios this will create the missing parts. Args: name (string): user's name email (string): user's email address orcid (string): user's ORCID identifier token (string): OAUTH authorization token Returns: str: the ORCID associated with the new user if we created one, or the ORCID associated with the user whose ``allow_push`` flag changed state. """ # Try to find an existing user entry user = _find_user_matching(orcid, email) # Make the user if didn't find existing one if not user: with db.session.begin_nested(): user = User() user.email = email db.session.add(user) return _link_user_and_token(user, name, orcid, token)
def create_user(orcid, email, name, consumer_key, token=None, allow_push=False): user = User() user.email = email with db.session.begin_nested(): db.session.add(user) oauth_link_external_id(user, {'id': orcid, 'method': 'orcid'}) if token: with db.session.begin_nested(): db.session.add( RemoteToken.create(user_id=user.id, client_id=consumer_key, token=token, secret=None, extra_data={ 'orcid': orcid, 'full_name': name, 'allow_push': allow_push, }))
def test_unlinked_user_exists(app_with_config, teardown_sample_user): """Add a token to an existing user without a paired ORCID.""" assert_db_has_n_legacy_records(0, SAMPLE_USER) # Register sample user user = User() user.email = SAMPLE_USER['email'] with db.session.begin_nested(): db.session.add(user) # Register the token _register_user(**SAMPLE_USER) # Assert new token assert_db_has_n_legacy_records(1, SAMPLE_USER)
def test_unlinked_user_exists(app_with_config, teardown_sample_user): """Add a token to an existing user without a paired ORCID.""" assert_db_has_n_legacy_tokens(0, SAMPLE_USER) # Register sample user user = User() user.email = SAMPLE_USER['email'] with db.session.begin_nested(): db.session.add(user) # Register the token _register_user(**SAMPLE_USER) # Assert new token assert_db_has_n_legacy_tokens(1, SAMPLE_USER)
def test_isolated_app_session_close_before_commit(users_count, isolated_app): assert User.query.count() == users_count user = User() db.session.add(user) # Closing the session within isolated_app fixture must trigger the rollback. db.session.close() assert User.query.count() == users_count
def test_empty_email_w_existing_user_w_empty_email( mock_legacy_orcid_arrays, mock_orcid_push, assert_user_and_token_models, app_cli_runner, redis_setup, db, es_clear, inspire_record_author, inspire_record_literature, ): user = User(email="") db.session.add(user) orcid = "0000-0002-0942-3697" token = "mytoken" email = "" name = "myname" mock_legacy_orcid_arrays.return_value = ((orcid, token, email, name),) result = app_cli_runner.invoke(import_legacy_orcid_tokens) assert_user_and_token_models( orcid, token, USER_EMAIL_EMPTY_PATTERN.format(orcid), name )
def test_isolated_app_nested_transaction(users_count, isolated_app): assert User.query.count() == users_count user = User() with db.session.begin_nested(): db.session.add(user) assert User.query.get(user.id) assert User.query.count() == users_count + 1
def test_isolated_app_session_flush(users_count, isolated_app): assert User.query.count() == users_count user = User() db.session.add(user) db.session.flush() assert User.query.get(user.id) assert User.query.count() == users_count + 1
def setup(self): with db.session.begin_nested(): self.user = User() self.user.email = "*****@*****.**" db.session.add(self.user) self.orcid = "myorcid" self.token = "mytoken" self.name = "myname"
def setup(self): with db.session.begin_nested(): self.user = User() self.user.email = '*****@*****.**' db.session.add(self.user) self.orcid = 'myorcid' self.token = 'mytoken' self.name = 'myname'
def test_isolated_app_session_commit(users_count, isolated_app): assert User.query.count() == users_count user = User() db.session.add(user) db.session.commit() user.email = '*****@*****.**' db.session.add(user) db.session.commit() new_email = '*****@*****.**' user.email = new_email db.session.merge(user) db.session.commit() user = User.query.get(user.id) assert user.email == new_email assert User.query.count() == users_count + 1
def test_isolated_app_session_close_after_commit(users_count, isolated_app): assert User.query.count() == users_count user = User() db.session.add(user) db.session.commit() id_ = user.id assert User.query.get(id_) # Closing the session within isolated_app fixtures must trigger the rollback. db.session.close() assert User.query.count() == users_count
def test_isolated_app_and_app_together(isolated_app, app): # When using `isolated_app` and `app` fixture together, the db isolation # feature should prevail. user = User() db.session.add(user) db.session.commit() id_ = user.id assert User.query.get(id_) # Closing the session within isolated_app fixture must trigger the rollback. db.session.close() assert not User.query.get(id_)
def _register_user(name, email, orcid, token): """Add a token to the user, creating the user if doesn't exist. There are multiple possible scenarios: - user exists, has ORCID and token already linked - user exists and has their ORCID linked, but no token is associated - user exists, but doesn't have the ORCID identifier linked - user doesn't exist at all In all the above scenarios this will create the missing parts. Args: name (string): user's name email (string): user's email address orcid (string): user's ORCID identifier token (string): OAUTH authorization token Returns: str: the ORCID associated with the new user if we created one, or the ORCID associated with the user whose ``allow_push`` flag changed state. """ if not email: # Generate a (fake) unique email address (because User.email is a # unique field). email = USER_EMAIL_EMPTY_PATTERN.format(orcid) # Try to find an existing user entry user = _find_user_matching(orcid, email) # Make the user if didn't find existing one if not user: with db.session.begin_nested(): user = User() user.email = email user.active = True db.session.add(user) return _link_user_and_token(user, name, orcid, token)
def test_isolated_app_sqlalchemy_signals(isolated_app): @models_committed.connect def model_committed_receiver(sender, changes): for model_instance, change in changes: if isinstance(model_instance, User): global count count += 1 user = User() db.session.add(user) db.session.commit() assert count == 1
def _register_user(name, email, orcid, token): """Add a token to the user, creating the user if doesn't exist. There are multiple possible scenarios: - user exists, has ORCID and token already linked - user exists and has their ORCID linked, but no token is associated - user exists, but doesn't have the ORCID identifier linked - user doesn't exist at all In all the above scenarios this will create the missing parts. Args: name (string): user's name email (string): user's email address orcid (string): user's ORCID identifier token (string): OAUTH authorization token """ # Try to find an existing user entry user_by_orcid = oauth_get_user(orcid) user_by_email = oauth_get_user(None, { 'user': { 'email': email } }) # Make the user if didn't find existing one if not user_by_email and not user_by_orcid: user = User() user.email = email with db.session.begin_nested(): db.session.add(user) else: user = user_by_orcid or user_by_email _link_user_and_token(user, name, orcid, token)
def test_empty_email_w_existing_user_w_empty_email(self): user = User(email='') db.session.add(user) token = 'mytoken' email = '' name = 'myname' self.mock_legacy_orcid_arrays.return_value = ((self.orcid, token, email, name), ) import_legacy_orcid_tokens() self._assert_user_and_token_models( self.orcid, token, USER_EMAIL_EMPTY_PATTERN.format(self.orcid), name) self.mock_logger.exception.assert_not_called()
def test_isolated_app_session_rollback(users_count, isolated_app): assert User.query.count() == users_count user = User() db.session.add(user) db.session.rollback() assert User.query.count() == users_count