def test_linked_user_without_token_exists(app_with_config, teardown_sample_user_edited): """Add a token to an existing user with an ORCID paired.""" assert_db_has_n_legacy_tokens(0, SAMPLE_USER) # Register sample user _register_user(**SAMPLE_USER) db.session.commit() # Check state after migration assert_db_has_n_legacy_tokens(1, SAMPLE_USER) # Remove token and remote account RemoteToken.query.filter_by(access_token=SAMPLE_USER["token"]).delete() user_id = ( db.session.query(UserIdentity.id_user) .filter(UserIdentity.id == SAMPLE_USER["orcid"]) .subquery() ) RemoteAccount.query.filter(RemoteAccount.user_id.in_(user_id)).delete( synchronize_session="fetch" ) # Register the same user with another token _register_user(**SAMPLE_USER_EDITED) db.session.commit() # Assert new token assert_db_has_n_legacy_tokens(1, SAMPLE_USER_EDITED)
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) db.session.commit() # Assert new token assert_db_has_n_legacy_tokens(1, SAMPLE_USER)
def test_find_user_matching(app_with_config, teardown_sample_user, orcid, email): """Add a token to an existing user with an ORCID paired.""" assert_db_has_n_legacy_tokens(0, SAMPLE_USER) # Register sample user _register_user(**SAMPLE_USER) db.session.commit() # Check state after migration assert_db_has_n_legacy_tokens(1, SAMPLE_USER) # Remove token and remote account user_by_orcid = _find_user_matching(orcid, email) # Assert the user found is the one added assert user_by_orcid.email == SAMPLE_USER["email"] assert User.query.filter_by(email=SAMPLE_USER["email"]).count() == 1
def test_linked_user_with_token_exists(app_with_config, teardown_sample_user): """Ignore token, if already has one.""" assert_db_has_n_legacy_tokens(0, SAMPLE_USER) # Register sample user _register_user(**SAMPLE_USER) db.session.commit() # Check state after migration assert_db_has_n_legacy_tokens(1, SAMPLE_USER) # Register the same user with another token _register_user(**SAMPLE_USER_EDITED) db.session.commit() # Assert token unchanged assert_db_has_n_legacy_tokens(1, SAMPLE_USER) assert 0 == RemoteToken.query.filter_by(token=SAMPLE_USER_EDITED).count()
def _import_legacy_orcid_tokens(): if get_value(current_app.config, "ORCID_APP_CREDENTIALS.consumer_key") is None: secho("consumer key for ORCID_APP_CREDENTIALS is None.", fg="yellow") return for user_data in legacy_orcid_arrays(): secho(f"Processing {user_data}") try: orcid, token, email, name = user_data if push_access_tokens.is_access_token_invalid(token): secho(f"Token {token} is invalid. Skipping push.", fg="yellow") continue orcid_to_push = _register_user(name, email, orcid, token) if orcid_to_push: recids = get_literature_recids_for_orcid(orcid_to_push) if not recids: secho("No records to push.") continue for recid in recids: secho( f"Pushing orcid: {orcid_push}\trecid: {recid}\t token: {token}" ) orcid_push.apply_async( queue="orcid_push_legacy_tokens", kwargs={ "orcid": orcid_to_push, "rec_id": recid, "oauth_token": token, }, ) else: secho("Cannot link user and token.", fg="yellow") except SQLAlchemyError as ex: secho(str(ex), fg="red") secho("No more data to process.") db.session.commit()