def test_arctic_auth_custom_app_name(): with patch('pymongo.MongoClient', return_value=MagicMock(), autospec=True), \ patch('arctic.arctic.mongo_retry', autospec=True), \ patch('arctic.arctic.get_auth', autospec=True) as ga: ga.return_value = Credential('db', 'admin_user', 'admin_pass') store = Arctic('cluster', app_name=sentinel.app_name) # do something to trigger lazy arctic init store.list_libraries() assert ga.call_args_list == [ call('cluster', sentinel.app_name, 'admin') ] ga.reset_mock() # Get a 'missing' library with pytest.raises(LibraryNotFoundException): with patch('arctic.arctic.ArcticLibraryBinding.get_library_type', return_value=None, autospec=True): ga.return_value = Credential('db', 'user', 'pass') store._conn['arctic_jblackburn'].name = 'arctic_jblackburn' store['jblackburn.library'] # Creating the library will have attempted to auth against it assert ga.call_args_list == [ call('cluster', sentinel.app_name, 'arctic_jblackburn') ]
def test_arctic_auth(): with patch('pymongo.MongoClient', return_value=MagicMock(), autospec=True), \ patch('arctic.arctic.mongo_retry', autospec=True), \ patch('arctic._cache.Cache._is_not_expired', return_value=True), \ patch('arctic.arctic.get_auth', autospec=True) as ga: ga.return_value = Credential('db', 'admin_user', 'admin_pass') store = Arctic('cluster') # do something to trigger lazy arctic init store.list_libraries() ga.assert_called_once_with('cluster', 'arctic', 'admin') store._adminDB.authenticate.assert_called_once_with( 'admin_user', 'admin_pass') ga.reset_mock() # Get a 'missing' library with pytest.raises(LibraryNotFoundException): with patch('arctic.arctic.ArcticLibraryBinding.get_library_type', return_value=None, autospec=True): ga.return_value = Credential('db', 'user', 'pass') store._conn['arctic_jblackburn'].name = 'arctic_jblackburn' store['jblackburn.library'] # Creating the library will have attempted to auth against it ga.assert_called_once_with('cluster', 'arctic', 'arctic_jblackburn') store._conn['arctic_jblackburn'].authenticate.assert_called_once_with( 'user', 'pass')
def test_init_library_bad_library(mongo_host): with pytest.raises(Exception): with patch('arctic.arctic.get_auth', return_value=Credential('admin', 'adminuser', 'adminpwd', 'admin')), \ patch('pymongo.database.Database.authenticate', return_value=True), \ patch('argparse.ArgumentParser.error', side_effect=Exception): # Create the user agains the current mongo database run_as_main(mil.main, '--host', mongo_host, '--library', 'user')
def test_arctic_repr(): with patch('pymongo.MongoClient', return_value=MagicMock(), autospec=True): with patch('arctic.arctic.mongo_retry', autospec=True): with patch('arctic.arctic.get_auth', autospec=True) as ga: ga.return_value = Credential('db', 'admin_user', 'admin_pass') store = Arctic('cluster') assert str(store) == repr(store)
def test_create_user_auth_fail(mongo_host): stderr = StringIO() with patch('arctic.scripts.arctic_create_user.get_auth', return_value=Credential('admin', 'user', 'pass')), \ patch('pymongo.database.Database.authenticate', return_value=False), \ patch('sys.stderr', stderr): run_as_main(mcu.main, '--host', mongo_host) err = stderr.getvalue() assert 'Failed to authenticate' in err
def test_prune_versions_symbol(mongo_host, library, library_name): with patch('arctic.scripts.arctic_prune_versions.prune_versions', autospec=True) as prune_versions, \ patch('arctic.scripts.utils.get_auth', return_value=Credential('admin', 'adminuser', 'adminpwd')), \ patch('pymongo.database.Database.authenticate', return_value=True): run_as_main(mpv.main, '--host', mongo_host, '--library', library_name, '--symbols', 'sym1,sym2') prune_versions.assert_has_calls([call(ANY, ['sym1', 'sym2'], 10)])
def test_create_user_no_passwd(mongo_host, mongodb): user = '******' pwd = None newpwd = 'newpasswd' stdout = StringIO() with patch('arctic.scripts.arctic_create_user.get_auth', return_value=Credential('admin', 'adminuser', 'adminpwd')), \ patch('pymongo.database.Database.authenticate',return_value=True), \ patch('base64.b64encode',return_value=newpwd), \ patch('sys.stdout', stdout): run_as_main(mcu.main, '--host', mongo_host, user) out = stdout.getvalue() assert '%-16s %s' % (user,newpwd) in out
def test_create_user_dryrun_nodb(mongo_host, mongodb): user = '******' pwd = 'password' stderr = StringIO() stdout = StringIO() with patch('arctic.scripts.arctic_create_user.get_auth', return_value=Credential('admin', 'adminuser', 'adminpwd')), \ patch('pymongo.database.Database.authenticate', return_value=True), \ patch('sys.stderr', stderr), \ patch('sys.stdout', stdout): run_as_main(mcu.main, '--host', mongo_host, user, '--pass', pwd, '--dryrun', '--nodb') out = stdout.getvalue() assert 'DRYRUN: add user %s readonly True nodb True' % (user) in out
def test_create_user_verbose(mongo_host, mongodb): user = '******' pwd = 'password' stderr = StringIO() stdout = StringIO() with patch('arctic.scripts.arctic_create_user.get_auth', return_value=Credential('admin', 'adminuser', 'adminpwd')), \ patch('pymongo.database.Database.authenticate', return_value=True), \ patch('sys.stderr', stderr), \ patch('sys.stdout', stdout): run_as_main(mcu.main, '--host', mongo_host, user, '--pass', pwd, '--verbose') out = stdout.getvalue() assert 'Adding user %s to DB %s' % (user, mongo_host) in out assert 'Adding database arctic_%s to DB %s' % (user, mongo_host) in out
def test_create_user(mongo_host, mongodb): # Create the user agains the current mongo database with patch('arctic.scripts.arctic_create_user.get_auth', return_value=Credential('admin', 'adminuser', 'adminpwd')), \ patch('pymongo.database.Database.authenticate',return_value=True): run_as_main(mcu.main, '--host', mongo_host, 'user', '--pass', 'pass') # Check: # User exists in system user = mongodb.admin.system.users.find_one({'user': '******'}) assert user assert user['readOnly'] == True # User db exists user = mongodb.arctic_user.system.users.find_one({'user': '******'}) assert user assert 'readOnly' not in user or user['readOnly'] == False
def arctic_auth_hook(mongo_host, app, database): return Credential( database="arctic", user=ARCTIC_USER, password=ARCTIC_PASSWD, )