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_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_linked_user_with_token_exists(app_with_config, teardown_sample_user):
    """Ignore token, if already has one."""
    assert_db_has_n_legacy_records(0, SAMPLE_USER)

    # Register sample user
    _register_user(**SAMPLE_USER)

    # Check state after migration
    assert_db_has_n_legacy_records(1, SAMPLE_USER)

    # Register the same user with another token
    _register_user(**SAMPLE_USER_EDITED)

    # Assert token unchanged
    assert_db_has_n_legacy_records(1, SAMPLE_USER)
    assert 0 == RemoteToken.query.filter_by(token=SAMPLE_USER_EDITED).count()
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)

    # 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)

    # Assert token unchanged
    assert_db_has_n_legacy_tokens(1, SAMPLE_USER)
    assert 0 == RemoteToken.query.filter_by(token=SAMPLE_USER_EDITED).count()
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)

    # 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_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_records(0, SAMPLE_USER)

    # Register sample user
    _register_user(**SAMPLE_USER)

    # Check state after migration
    assert_db_has_n_legacy_records(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)

    # Assert new token
    assert_db_has_n_legacy_records(1, SAMPLE_USER_EDITED)