def test_import_legacy_orcid_tokens_pushes_on_new_user( mock_get_literature_recids_for_orcid, mock_orcid_push, app_with_config, redis_setup, teardown_sample_user): mock_get_literature_recids_for_orcid.return_value = [4328] push_to_redis(SAMPLE_USER) # Check initial state assert redis_setup.llen('legacy_orcid_tokens') == 1 assert_db_has_n_legacy_tokens(0, SAMPLE_USER) # Migrate import_legacy_orcid_tokens() # Check state after migration assert not redis_setup.llen('legacy_orcid_tokens') assert_db_has_n_legacy_tokens(1, SAMPLE_USER) # Check that we pushed to ORCID mock_orcid_push.apply_async.assert_called_with( queue='orcid_push_legacy_tokens', kwargs={ 'orcid': '0000-0002-1825-0097', 'rec_id': 4328, 'oauth_token': '3d25a708-dae9-48eb-b676-80a2bfb9d35c', }, )
def test_log_exception_when_no_author_record(self): token = 'mytoken' name = 'myname' email = '*****@*****.**' self.mock_legacy_orcid_arrays.return_value = (('inexistentorcid', token, email, name), ) import_legacy_orcid_tokens() assert self.mock_logger.exception.call_count == 1 # Ensure that when no author record is found with that ORCID, then # the exception is logged. # Note: I couldn't find a better way to assert on exception instances. assert 'NoResultFound' in str(self.mock_logger.exception.call_args)
def test_invalid_token(self): token = 'mytoken' email = '*****@*****.**' name = 'myname' cache = push_access_tokens._OrcidInvalidTokensCache(token) cache.write_invalid_token(self.orcid) self.mock_legacy_orcid_arrays.return_value = ((self.orcid, token, email, name), ) import_legacy_orcid_tokens() self.mock_orcid_push.apply_async.assert_not_called() assert not User.query.filter_by(email=email).one_or_none() self.mock_logger.exception.assert_not_called()
def test_empty_email(self): token = 'mytoken' email = '' name = 'myname' self.mock_legacy_orcid_arrays.return_value = ( (self.orcid, token, email, name), ('myotherorcid', 'myothertoken', email, 'othername'), ) import_legacy_orcid_tokens() self._assert_user_and_token_models( self.orcid, token, USER_EMAIL_EMPTY_PATTERN.format(self.orcid), name) assert self.mock_logger.exception.call_count == 1
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_import_multiple_orcid_tokens_no_configuration( app_without_config, redis_setup, teardown_sample_user, teardown_sample_user_2): """Attempt and fail to create new users when configuration missing.""" push_to_redis(SAMPLE_USER_2) push_to_redis(SAMPLE_USER) # Check initial state assert redis_setup.llen('legacy_orcid_tokens') == 2 assert_db_has_n_legacy_records(0, SAMPLE_USER) assert_db_has_n_legacy_records(0, SAMPLE_USER_2) # Migrate import_legacy_orcid_tokens() # Assert state unchanged after migration assert redis_setup.llen('legacy_orcid_tokens') == 2 assert_db_has_n_legacy_records(0, SAMPLE_USER) assert_db_has_n_legacy_records(0, SAMPLE_USER_2)
def test_import_multiple_orcid_tokens_no_user_exists( app_with_config, redis_setup, teardown_sample_user, teardown_sample_user_2): """Create two users and all the associate entries.""" push_to_redis(SAMPLE_USER_2) push_to_redis(SAMPLE_USER) # Check initial state assert redis_setup.llen('legacy_orcid_tokens') == 2 assert_db_has_n_legacy_records(0, SAMPLE_USER) assert_db_has_n_legacy_records(0, SAMPLE_USER_2) # Migrate import_legacy_orcid_tokens() # Check state after migration assert not redis_setup.llen('legacy_orcid_tokens') assert_db_has_n_legacy_records(1, SAMPLE_USER) assert_db_has_n_legacy_records(1, SAMPLE_USER_2)
def test_happy_flow(self): token = 'mytoken' email = '*****@*****.**' name = 'myname' self.mock_legacy_orcid_arrays.return_value = ((self.orcid, token, email, name), ) import_legacy_orcid_tokens() self.mock_orcid_push.apply_async.assert_any_call( queue='orcid_push_legacy_tokens', kwargs={ 'orcid': self.orcid, 'rec_id': self.inspire_record_literature['control_number'], 'oauth_token': token, }, ) self._assert_user_and_token_models(self.orcid, token, email, name) self.mock_logger.exception.assert_not_called()
def test_import_multiple_orcid_tokens_no_configuration(app_without_config, redis_setup, teardown_sample_user, teardown_sample_user_2): """Attempt and fail to create new users when configuration missing.""" push_to_redis(SAMPLE_USER_2) push_to_redis(SAMPLE_USER) # Check initial state assert redis_setup.llen('legacy_orcid_tokens') == 2 assert_db_has_n_legacy_tokens(0, SAMPLE_USER) assert_db_has_n_legacy_tokens(0, SAMPLE_USER_2) # Migrate import_legacy_orcid_tokens() # Assert state unchanged after migration assert redis_setup.llen('legacy_orcid_tokens') == 2 assert_db_has_n_legacy_tokens(0, SAMPLE_USER) assert_db_has_n_legacy_tokens(0, SAMPLE_USER_2)
def test_import_multiple_orcid_tokens_no_user_exists(app_with_config, redis_setup, teardown_sample_user, teardown_sample_user_2): """Create two users and all the associate entries.""" push_to_redis(SAMPLE_USER_2) push_to_redis(SAMPLE_USER) # Check initial state assert redis_setup.llen('legacy_orcid_tokens') == 2 assert_db_has_n_legacy_tokens(0, SAMPLE_USER) assert_db_has_n_legacy_tokens(0, SAMPLE_USER_2) # Migrate import_legacy_orcid_tokens() # Check state after migration assert not redis_setup.llen('legacy_orcid_tokens') assert_db_has_n_legacy_tokens(1, SAMPLE_USER) assert_db_has_n_legacy_tokens(1, SAMPLE_USER_2)
def test_empty_name(self): token = 'mytoken' email = '*****@*****.**' name = '' self.mock_legacy_orcid_arrays.return_value = ( (self.orcid, token, email, name), ('myotherorcid', 'myothertoken', '*****@*****.**', name), ) import_legacy_orcid_tokens() self.mock_orcid_push.apply_async.assert_any_call( queue='orcid_push_legacy_tokens', kwargs={ 'orcid': self.orcid, 'rec_id': self.inspire_record_literature['control_number'], 'oauth_token': token, }, ) self._assert_user_and_token_models(self.orcid, token, email, name) assert self.mock_logger.exception.call_count == 1