def __init__(self, argv): self.gus = GUS(api_key=environ('GUSREGON_API_KEY'), sandbox=environ('GUSREGON_SANDBOX', True)) self.s = requests.Session() self.argv = argv self.args = self.get_build_args(argv[1:]) self.s.auth = (self.args.user, self.args.password)
def __init__(self, argv): self.gus = GUS( api_key=environ("GUSREGON_API_KEY"), sandbox=environ("GUSREGON_SANDBOX", True), ) self.s = requests.Session() self.argv = argv self.args = self.get_build_args(argv[1:]) self.s.auth = (self.args.user, self.args.password) self.done_count = 0 self.total_count = None
def test_basic_do_get_with_wrong_playlist_id_returns_client_error(self): self.playlist = Stub(entries=[Stub()]) self.rb.get_playlists.return_value = [self.playlist] result = handle_request(self.app, environ('/rest/playlists/1'), self.response) self.response.assert_called_with('400 Bad Request: there is no playlist with id 1', [('Content-type', 'text/html; charset=UTF-8')]) self.rb.get_playlists.assert_called_with()
def test_utils_environ_delete_implicit(self): self.assertTrue('TOTO' not in os.environ) # Paranoid os.environ['TOTO'] = 'tutu' self.assertEqual('tutu', os.environ['TOTO']) with utils.environ('TOTO'): self.assertTrue('TOTO' not in os.environ) self.assertEqual('tutu', os.environ['TOTO'])
def test_utils_environ_existent_empty_val(self): self.assertTrue('TOTO' not in os.environ) # Paranoid os.environ['TOTO'] = 'tutu' self.assertEqual('tutu', os.environ['TOTO']) with utils.environ('TOTO', ''): self.assertEqual('', os.environ['TOTO']) self.assertEqual('tutu', os.environ['TOTO'])
def test_play_invalid_source_fails(self): self.rb.get_playlists.return_value = [self.playlist] self.rb.play_source.return_value = False result = handle_request(self.app, environ('/rest/playlists', post_data='action=play_source&source=10'), self.response) self.response.assert_called_with('400 Bad Request: there is no playlist with id 10', [('Content-type', 'text/html; charset=UTF-8')])
def test_play_with_no_source_fails(self): self.rb.get_playlists.return_value = [self.playlist] self.rb.play_source.return_value = False result = handle_request(self.app, environ('/rest/playlists', post_data='action=play_source'), self.response) self.response.assert_called_with('400 Bad Request: no "source" parameter', [('Content-type', 'text/html; charset=UTF-8')])
def test_get_invalid_query(self): self.rb.query.side_effect = InvalidQueryException('Invalid query') result = handle_request(self.app, environ('/rest/search/song'), self.response) self.response.assert_called_with('400 Bad Request: Invalid query', [('Content-type', 'text/html; charset=UTF-8')]) self.rb.query.assert_called_with({'type': 'song'})
def test_basic_do_get(self): self.rb.library.artists = {'values' : {'a guy' : 55}, 'max': 55} result = handle_request(self.app, environ('/rest/library/artists'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) expected = json.loads('{"values": {"a guy": 55}, "max": 55}') returned = json.loads(result) self.assertEquals(expected, returned)
def test_openstack_out_get_field_ok(self): f = self.get_field(['false']) self.assertEqual(None, f) f = self.get_field(['true']) self.assertEqual(None, f) with utils.environ('OS_PARAMS', '--insecure'): f = self.get_field(['-c', '666', '--', 'neutron', 'agent-list']) self.assertEqual(None, f)
def test_post_search(self): self.rb.query.return_value = [self.entry] result = handle_request(self.app, environ('/rest/search', post_data=''), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) expected = json.loads('{ "entries" : [ { "duration" : "duration" , "location" : "location" , "last_played" : "last_played" , "album" : "album" , "title" : "title" , "genre" : "genre" , "year" : "year" , "rating" : "rating" , "id" : "id" , "track_number" : "track_number" , "play_count" : "play_count" , "bitrate" : "bitrate" , "artist" : "artist" } ] }') returned = json.loads(result) self.assertEquals(expected, returned) self.rb.query.assert_called_with({})
def test_basic_do_get(self): self.queue.get_play_queue.return_value = [Stub(id=1), Stub(id=2), Stub(id=3)] result = handle_request(self.app, environ('/rest/queue'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) returned = json.loads(result) self.queue.get_play_queue.assert_called_with() for index, entry in enumerate(returned['entries'], 1): self.assertEquals(index, entry['id'])
def test_query_with_params_and_limit(self): self.rb.query.return_value = [self.entry for i in range(5)] result = handle_request(self.app, environ('/rest/search/song/limit/10/first/5'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) returned = json.loads(result) self.assertEquals(5, len(returned['entries'])) self.rb.query.assert_called_with({ 'type' : 'song', 'limit' : '10', 'first' : '5' })
def test_basic_do_get_with_playlist_returns_right_element(self): self.playlist = Stub(entries=[Stub()]) self.rb.get_playlists.return_value = [self.playlist] result = handle_request(self.app, environ('/rest/playlists/0'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) expected = json.loads('{ "id" : "id" , "name" : "name" , "type" : "source_type", "entries" : [{"last_played": "last_played", "title": "title", "genre": "genre", "album": "album", "bitrate": "bitrate", "track_number": "track_number", "id": "id", "duration": "duration", "year": "year", "play_count": "play_count", "location": "location", "artist": "artist", "rating": "rating"}]}') returned = json.loads(result) self.assertEquals(expected, returned) self.rb.get_playlists.assert_called_with()
def test_get_search_returns_empty_set(self): self.rb.query.return_value = [] result = handle_request(self.app, environ('/rest/search'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) returned = json.loads(result) self.assertEquals({}, returned) self.rb.query.assert_called_with({})
def test_get_status_when_not_playing_works(self): self.rb.get_playing_status.return_value = False self.rb.get_play_order.return_value = 'bla' self.rb.get_mute.return_value = True self.rb.get_volume.return_value = 1 result = handle_request(self.app, environ('/rest/status'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) expected = json.loads('{ "playing_order" : "bla" , "volume" : 1, "muted" : true, "playing" : false }') returned = json.loads(result) self.assertEquals(expected, returned)
def test_play_playlist_fails(self): self.rb.get_playlists.return_value = [self.playlist] self.rb.play_source.return_value = False result = handle_request(self.app, environ('/rest/playlists', post_data='action=play_source&playlist=0'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) expected = json.loads('{ "result" : "BAD" }') returned = json.loads(result) self.assertEquals(expected, returned) self.rb.play_source.assert_called_with(self.playlist)
def test_post_search_with_post_params(self): self.rb.query.return_value = [self.entry] post_data = '&'.join(('album=calabaza', 'title=oruga', 'artist=uno', 'type=song', 'genre=classic', 'rating=4', 'first=1', 'limit=10')) result = handle_request(self.app, environ('/rest/search', post_data=post_data), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) expected = json.loads('{ "entries" : [ { "duration" : "duration" , "location" : "location" , "last_played" : "last_played" , "album" : "album" , "title" : "title" , "genre" : "genre" , "year" : "year" , "rating" : "rating" , "id" : "id" , "track_number" : "track_number" , "play_count" : "play_count" , "bitrate" : "bitrate" , "artist" : "artist" } ] }') returned = json.loads(result) self.assertEquals(expected, returned) self.rb.query.assert_called_with({'album': 'calabaza', 'rating': '4', 'title': 'oruga', 'artist': 'uno', 'limit': '10', 'genre': 'classic', 'type': 'song', 'first': '1'})
def ci(window = True, local = False): args = [git, 'commit', '-a', '-v'] status() env = dict() editor = 'git_message_editor' if window is not True: editor += ' -nw' env['GIT_EDITOR'] = editor with environ(env), sourcedir(): print('Starting git commit') with subprocess.Popen( args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) as proc: message, errmsg = proc.communicate() code = proc.returncode message = message.decode() for m in message.splitlines(): print(' [git] {}'.format(m)) if code == 0: print('git commit finished.') else: if message.find('nothing to commit, working directory clean') > 0: print('Done.') else: print('git commit failed.') return if local: return with sourcedir(): args = [git, 'push'] try: print('Starting git push') subprocess.check_call(args) print('git push finished.') except: print('#'*24) print('### git push failed! ###') print('#'*24)
def ci(window=True, local=False): args = [git, 'commit', '-a', '-v'] status() env = dict() editor = 'git_message_editor' if window is not True: editor += ' -nw' env['GIT_EDITOR'] = editor with environ(env), sourcedir(source): print('Starting git commit') with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc: message, errmsg = proc.communicate() code = proc.returncode message = message.decode() for m in message.splitlines(): print(' [git] {}'.format(m)) if code == 0: print('git commit finished.') else: if message.find('nothing to commit, working directory clean') > 0: print('Done.') else: print('git commit failed.') return if local: return with sourcedir(source): args = [git, 'push'] try: print('Starting git push') subprocess.check_call(args) print('git push finished.') except: print('#' * 24) print('### git push failed! ###') print('#' * 24)
def test_get_status_when_playing_works(self): self.rb.get_playing_status.return_value = True self.rb.get_play_order.return_value = 'bla' self.rb.get_mute.return_value = False self.rb.get_volume.return_value = 1 self.rb.get_playing_entry.return_value = self.entry self.rb.get_playing_time.return_value = 10 result = handle_request(self.app, environ('/rest/status'), self.response) self.response.assert_called_with('200 OK', [('Content-type', 'application/json; charset=UTF-8'), ('Cache-Control: ', 'no-cache; must-revalidate')]) expected = json.loads('''{ "playing_entry" : { "artist" : "artist" , "title" : "title" , "duration" : "duration" , "genre" : "genre" , "id" : "id" , "year" : "year" , "bitrate" : "bitrate" , "location" : "location" , "rating" : "rating" , "last_played" : "last_played" , "play_count" : "play_count" , "track_number" : "track_number" , "album" : "album" }, "playing_order" : "bla" , "muted" : false, "volume" : 1, "playing" : true, "playing_time" : 10 }''') returned = json.loads(result) self.assertEquals(expected, returned)
def test_basic_do_post(self): result = handle_request(self.app, environ('/rest/queue', post_data='bla=1'), self.response) self.response.assert_called_with('405 method POST not allowed', [('Content-type', 'text/html; charset=UTF-8')])
def test_play_with_no_action_fails(self): result = handle_request(self.app, environ('/rest/playlists', post_data='source=1'), self.response) self.response.assert_called_with('400 Bad Request: no "action" parameter', [('Content-type', 'text/html; charset=UTF-8')])
def test_glance_imglist(self): self.assertTrue(glance.glance_run('image-list')) with utils.environ('OS_PASSWORD', 'not_the_one'): self.assertFalse(glance.glance_run('image-list'))
def test_glance_ok(self): self.assertTrue(glance.glance_ok()) with utils.devnull('stderr'): with utils.environ('PATH', 'not_the_one'): self.assertFalse(glance.glance_ok()) self.assertTrue(glance.glance_ok())
def test_utils_environ_delete_explicit(self): self.assertTrue('TOTO' not in os.environ) # Paranoid with utils.environ('TOTO', None): # This is not redundant with the above or below identical lines self.assertTrue('TOTO' not in os.environ) self.assertTrue('TOTO' not in os.environ)
def test_invalid_search_type_fails(self): self.rb.query.return_value = [self.entry] result = handle_request(self.app, environ('/rest/library/calabaza'), self.response) self.response.assert_called_with('400 Bad Request: Invalid library filter "calabaza"', [('Content-type', 'text/html; charset=UTF-8')])
def test_basic_do_get_without_playlists_returns_error(self): self.rb.get_playlists.return_value = [] result = handle_request(self.app, environ('/rest/playlists/0'), self.response) self.response.assert_called_with('404 NOT FOUND', [('Content-type', 'text/html; charset=UTF-8')]) self.rb.get_playlists.assert_called_with()
def test_glance_main_fail_listing_ids(self): with utils.environ('OS_PASSWORD', ''): self.assertFalse(glance.main([]))
def test_get_without_parameters_fails(self): self.rb.query.return_value = [self.entry] result = handle_request(self.app, environ('/rest/library'), self.response) self.response.assert_called_with('404 NOT FOUND', [('Content-type', 'text/html; charset=UTF-8')])
def test_utils_environ_not_existent_val(self): self.assertTrue('TOTO' not in os.environ) # Paranoid with utils.environ('TOTO', 'titi'): self.assertEqual('titi', os.environ['TOTO']) self.assertTrue('TOTO' not in os.environ)
""" import argparse import sys import requests import csv from gusregon import GUS from utils import environ from insert_institution import normalize_jst from urllib.parse import urljoin if not bool(environ("GUSREGON_SANDBOX")): sys.stderr.write( "You are using sandbox mode for the REGON database. Data may be incorrect. " "Set the environemnt variable GUSREGON_API_KEY correctly." ) class Command: REQUIRED_FIELDS = ["name", "email", "tags"] def __init__(self, argv): self.gus = GUS( api_key=environ("GUSREGON_API_KEY"), sandbox=environ("GUSREGON_SANDBOX", True), ) self.s = requests.Session()
def test_utils_environ_delete_implicit_nonexistent(self): self.assertTrue('TOTO' not in os.environ) # Paranoid with utils.environ('TOTO'): self.assertTrue('TOTO' not in os.environ) self.assertTrue('TOTO' not in os.environ)
def test_utils_run_not_in_path(self): with utils.environ('PATH', ''): self.assertFalse(utils.run(['true'])[0])
def test_post_invalid_action(self): result = handle_request(self.app, environ('/rest/playlists', post_data='action=invalid&playlist=0'), self.response) self.response.assert_called_with('400 Bad Request: Unknown action invalid', [('Content-type', 'text/html; charset=UTF-8')])
import sys import requests import unicodecsv as csv from gusregon import GUS from tqdm import trange from utils import environ from insert_institution import normalize_jst try: from urlparse import urljoin except ImportError: from urllib.parse import urljoin if not bool(environ('GUSREGON_SANDBOX')): sys.stderr.write( "You are using sandbox mode for the REGON database. Data may be incorrect. " "Set the environemnt variable GUSREGON_API_KEY correctly.") class Command(object): REQUIRED_FIELDS = [ 'name', 'email', 'regon', 'terc', 'regon_parent', 'tags' ] def __init__(self, argv): self.gus = GUS(api_key=environ('GUSREGON_API_KEY'), sandbox=environ('GUSREGON_SANDBOX', True)) self.s = requests.Session() self.argv = argv
def test_utils_environ_not_existent_no_val(self): self.assertTrue('TOTO' not in os.environ) # Paranoid with utils.environ('TOTO'): # This is not redundant with the above or below identical lines self.assertTrue('TOTO' not in os.environ) self.assertTrue('TOTO' not in os.environ)
def test_glance_ids(self): self.assertEqual(set(), glance.glance_ids('XXX')) with utils.environ('OS_PASSWORD', 'not_the_one'): self.assertEqual(set(), glance.glance_ids('XXX'))
def test_glancing_main_glance_availabilityFail(self): with environ('PATH'): self.assertFalse(glancing.main([os.devnull]))