def test_authors_and_interpreters(self): # insert test author for further testing rv = utils._post_author(self.app, name='Axl Rose') assert rv.status_code == 201 data = json.loads(rv.data) author_id = data['link'].split('/')[1] # insert test interpreter for further testing rv = utils._post_interpreter(self.app, name='Guns n Roses') assert rv.status_code == 201 data = json.loads(rv.data) interpreter_id = data['link'].split('/')[1] # test wrong author insert rv = utils._post_song(self.app, title='Welcome to the Jungle', lauthors=['000000000000000000000000']) assert rv.status_code == 404 assert b'"code":"does_not_exist"' in rv.data rv = utils._post_song(self.app, title='Welcome to the Jungle', mauthors=['000000000000000000000000']) assert rv.status_code == 404 assert b'"code":"does_not_exist"' in rv.data # test wrong interpreter insert rv = utils._post_song(self.app, title='Welcome to the Jungle', interpreters=['000000000000000000000000']) assert rv.status_code == 404 assert b'"code":"does_not_exist"' in rv.data # test correct insert rv = utils._post_song(self.app, title='Welcome to the Jungle', lauthors=[author_id], mauthors=[author_id], interpreters=[interpreter_id]) assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_id = data['id'] # text correct song edit request rv = self.app.get('/api/v1/songs/{}'.format(song_id)) assert rv.status_code == 200 data = json.loads(rv.data) assert interpreter_id in data['interpreters'] assert author_id in data['authors']['music'] assert author_id in data['authors']['lyrics'] # clean the database self.mongo_client.drop_database(self.db_name)
def test_song_ordering(self): song_ids = [] rv = utils._post_song(self.app, title='Live and Let Die') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_ids.append(data['id']) rv = utils._post_song(self.app, title='Kashmir') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_ids.append(data['id']) rv = utils._post_song(self.app, title='Pažitka') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_ids.append(data['id']) # get sorted songs with get request rv = self.app.get('/api/v1/songs?order=title') assert rv.status_code == 200 res = json.loads(rv.data) res_ids = [x['id'] for x in res['data']] assert res_ids[0] == song_ids[1] and res_ids[1] == song_ids[ 0] and res_ids[2] == song_ids[2] # get sorted songs with get request rv = self.app.get('/api/v1/songs?order=title_desc') assert rv.status_code == 200 res = json.loads(rv.data) res_ids = [x['id'] for x in res['data']] assert res_ids[0] == song_ids[2] and res_ids[1] == song_ids[ 0] and res_ids[2] == song_ids[1] # clean the database self.mongo_client.drop_database(self.db_name)
def test_song_variant_basics(self): # add test song into the database rv = utils._post_song(self.app, title='Back in Black', description='Variant test song') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_id = data['id'] variant_id = data['variants'][0]['id'] # get song variant with get request rv = self.app.get('/api/v1/songs/{}/variants'.format(song_id)) assert rv.status_code == 200 # get id of song variant res = json.loads(rv.data) assert variant_id == res[0]['id'] # check get request on selected song variant rv = self.app.get('/api/v1/songs/{}/variants/{}'.format( song_id, variant_id)) assert rv.status_code == 200 variant = json.loads(rv.data) assert variant['id'] == variant_id assert variant['description'] == "Variant test song" # clean the database self.mongo_client.drop_database(self.db_name)
def test_duplication(self): # insert test song into the database rv = utils._post_song(self.app, title='Invincible', description='Variant test song') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_id = data['id'] variant_id = data['variants'][0]['id'] # duplicate variant rv = self.app.get('/api/v1/songs/{}/variants/{}/duplicate'.format( song_id, variant_id)) assert rv.status_code == 200 # try to duplicate wrong song rv = self.app.get('/api/v1/songs/{}/variants/{}/duplicate'.format( song_id, '000000000000000000000000')) assert rv.status_code == 404 assert b'"code":"does_not_exist"' in rv.data # clean the database self.mongo_client.drop_database(self.db_name)
def test_put_request(self): # insert test song into the database rv = utils._post_song(self.app, title='Panic Station', description='Variant test song') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_id = data['id'] variant_id = data['variants'][0]['id'] # test wrong song variant id rv = utils._put_song_variant(self.app, song_id, '000000000000000000000000') assert rv.status_code == 404 # test missing fields rv = self.app.put('/api/v1/songs/{}/variants/{}'.format( song_id, variant_id), content_type='application/json', data=json.dumps(dict(field="field"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data # test missing text field rv = self.app.put('/api/v1/songs/{}/variants/{}'.format( song_id, variant_id), content_type='application/json', data=json.dumps(dict(description=""))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"text"' in rv.data # test missing description field rv = self.app.put('/api/v1/songs/{}/variants/{}'.format( song_id, variant_id), content_type='application/json', data=json.dumps(dict(text="[verse]"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"description"' in rv.data # test correct song variant edit request rv = self.app.put('/api/v1/songs/{}/variants/{}'.format( song_id, variant_id), content_type='application/json', data=json.dumps( dict(title="Variant title", text="[verse] Don't panic", description="It works"))) assert rv.status_code == 200 data = json.loads(rv.data) assert data['text'] == "[verse] Don't panic" assert data['description'] == "It works" # clean the database self.mongo_client.drop_database(self.db_name)
def test_permissions(self): # insert test song for further testing rv = utils._post_song(self.app, title='Bubák') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_id = data['id'] variant_id = data['variants'][0]['id'] # test wrong view permission values rv = utils._put_song_variant(self.app, song_id, variant_id, visibility=2) assert rv.status_code == 422 assert b'"code":"wrong_value"' in rv.data assert b'"data":"visibility"' in rv.data rv = utils._put_song_variant(self.app, song_id, variant_id, visibility='abc') assert rv.status_code == 422 assert b'"code":"wrong_value"' in rv.data assert b'"data":"visibility"' in rv.data # test correct permission change rv = utils._put_song_variant(self.app, song_id, variant_id, visibility=PERMISSION.PUBLIC) assert rv.status_code == 200 # test no change rv = utils._put_song_variant(self.app, song_id, variant_id, visibility=PERMISSION.PUBLIC) assert rv.status_code == 200 # test wrong permission change rv = utils._put_song_variant(self.app, song_id, variant_id, visibility=PERMISSION.PRIVATE) assert rv.status_code == 422 assert b'"code":"wrong_value"' in rv.data assert b'"data":"visibility"' in rv.data # clean the database self.mongo_client.drop_database(self.db_name)
def test_song_validation(self): # test nested repetition rv = utils._post_song(self.app, title='Kokos', text='[verse] |: |: Kulda :| :|') assert rv.status_code == 422 assert b'"code":"compilation_error"' in rv.data # test nested repetition end before start rv = utils._post_song(self.app, title='Kokos', text='[verse] |: Kulda :| :|') assert rv.status_code == 422 assert b'"code":"compilation_error"' in rv.data # test chords inside rec rv = utils._post_song(self.app, title='Kokos', text='[rec] Kulda [A]') assert rv.status_code == 422 assert b'"code":"compilation_error"' in rv.data # test unknown tag rv = utils._post_song(self.app, title='Kokos', text='[versex]') assert rv.status_code == 422 assert b'"code":"compilation_error"' in rv.data
def test_song_basics(self): # check empty database get request rv = self.app.get('/api/v1/songs') assert rv.status_code == 200 assert b'[]' in rv.data # add song into the database rv = utils._post_song(self.app, title='Back in Black') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data # get songs with get request rv = self.app.get('/api/v1/songs') assert rv.status_code == 200 # get id of one of the songs res = json.loads(rv.data) song_id = res['data'][0]['id'] # check get request on selected song rv = self.app.get('/api/v1/songs/{}'.format(song_id)) assert rv.status_code == 200 song = json.loads(rv.data) assert song['title'] == 'Back in Black' assert song['id'] == song_id # test put (edit) request rv = utils._put_song(self.app, song_id, title='Back in White') assert rv.status_code == 200 song = json.loads(rv.data) assert song['title'] == 'Back in White' assert song['id'] == song_id # clean the database self.mongo_client.drop_database(self.db_name)
def test_put_request(self): # insert test song for further testing rv = utils._post_song(self.app, title='Panic Station') assert rv.status_code == 201 song = json.loads(rv.data) song_id = song['id'] # test wrong song id rv = utils._put_song(self.app, '000000000000000000000000', title='Panic Station') assert rv.status_code == 404 # test missing fields rv = self.app.put('/api/v1/songs/{}'.format(song_id), content_type='application/json', data=json.dumps(dict(field="field"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data # test missing title field rv = self.app.put('/api/v1/songs/{}'.format(song_id), content_type='application/json', data=json.dumps( dict(authors={ 'lyrics': [], 'music': [] }, interpreters=[]))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"title"' in rv.data # test missing authors field rv = self.app.put('/api/v1/songs/{}'.format(song_id), content_type='application/json', data=json.dumps( dict(title="Madness", interpreters=[]))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"authors"' in rv.data # test missing interpreters field rv = self.app.put('/api/v1/songs/{}'.format(song_id), content_type='application/json', data=json.dumps( dict(title="Madness", authors={ 'lyrics': [], 'music': [] }))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"interpreters"' in rv.data # test correct song edit request rv = self.app.put('/api/v1/songs/{}'.format(song_id), content_type='application/json', data=json.dumps( dict(title="Madness", authors={ 'lyrics': [], 'music': [] }, interpreters=[]))) assert rv.status_code == 200 data = json.loads(rv.data) assert data['title'] == "Madness" # clean the database self.mongo_client.drop_database(self.db_name)
def test_simple_requests(self): # insert test songbook for further testing rv = utils._post_songbook(self.app, title='My songbook') assert rv.status_code == 201 songbook = json.loads(rv.data) songbook_id = songbook['id'] # test songbook title endpoint (missing title) rv = self.app.put('/api/v1/songbooks/{}/title'.format(songbook_id), content_type='application/json', data=json.dumps(dict(field="field"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"title"' in rv.data # test songbook title endpoint (no title) rv = utils._put_songbook_title(self.app, songbook_id, title='') assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"title"' in rv.data # test songbook title endpoint (correct title) rv = utils._put_songbook_title(self.app, songbook_id, title='Other songbook') assert rv.status_code == 200 songbook = json.loads(rv.data) assert songbook['title'] == 'Other songbook' assert songbook['id'] == songbook_id # test songbook songs endpoint (missing songs array) rv = self.app.put('/api/v1/songbooks/{}/songs'.format(songbook_id), content_type='application/json', data=json.dumps(dict(field="field"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"songs"' in rv.data # test songbook songs endpoint (wrong songs) rv = utils._put_songbook_songs(self.app, songbook_id, songs=[{ 'variant_id': '000000000000000000000000' }]) assert rv.status_code == 404 assert b'"code":"does_not_exist"' in rv.data # insert songs into the database for further testing variant_ids = [] rv = utils._post_song(self.app) data = json.loads(rv.data) variant_ids.append(data['variants'][0]['id']) rv = utils._post_song(self.app) data = json.loads(rv.data) variant_ids.append(data['variants'][0]['id']) # test songbook songs endpoint (correct insertion) rv = utils._put_songbook_songs(self.app, songbook_id, songs=[{ 'variant_id': variant_ids[0] }, { 'variant_id': variant_ids[1] }]) assert rv.status_code == 200 assert variant_ids[0].encode() in rv.data assert variant_ids[1].encode() in rv.data rv = utils._put_songbook_songs(self.app, songbook_id, songs=[{ 'variant_id': variant_ids[0] }]) assert rv.status_code == 200 assert variant_ids[0].encode() in rv.data assert variant_ids[1].encode() not in rv.data rv = utils._put_songbook_songs(self.app, songbook_id, songs=[{ 'variant_id': variant_ids[1] }]) assert rv.status_code == 200 assert variant_ids[0].encode() not in rv.data assert variant_ids[1].encode() in rv.data # test songbook options endpoint (missing options) rv = self.app.put('/api/v1/songbooks/{}/options'.format(songbook_id), content_type='application/json', data=json.dumps(dict(field="field"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"options"' in rv.data # test songbook options endpoint (wrong format option) rv = utils._put_songbook_options(self.app, songbook_id, options={'format': "B4"}) assert rv.status_code == 422 assert b'"code":"wrong_value"' in rv.data assert b'"data":"format"' in rv.data # test songbook options endpoint (correct change) rv = utils._put_songbook_options(self.app, songbook_id, options={ 'format': OPTIONS.FORMAT.A5, 'chorded': False }) res = json.loads(rv.data) assert rv.status_code == 200 assert res['options']['format'] == OPTIONS.FORMAT.A5 assert res['options']['chorded'] == False # clean the database self.mongo_client.drop_database(self.db_name)
def test_variants_in_songbook(self): # insert test songbook for further testing rv = utils._post_songbook(self.app, title='My songbook') assert rv.status_code == 201 songbook = json.loads(rv.data) songbook_id = songbook['id'] # insert test song into the database rv = utils._post_song(self.app, title='Panic Station', description='First variant') assert rv.status_code == 201 variant_ids = [] data = json.loads(rv.data) song_id = data['id'] variant_ids.append(data['variants'][0]['id']) # insert one more variant for this song rv = self.app.post('/api/v1/songs/{}/variants'.format(song_id), content_type='application/json', data=json.dumps( dict(title="Variant title", text="[verse]", description="Second variant"))) assert rv.status_code == 201 data = json.loads(rv.data) variant_ids.append(data['id']) # get songbooks with get request rv = self.app.get('/api/v1/songbooks/{}'.format(songbook_id)) assert rv.status_code == 200 songbook = json.loads(rv.data) # put one variant into songbook songbook['songs'] = [{'variant_id': variant_ids[1]}] rv = self.app.put('/api/v1/songbooks/{}'.format(songbook_id), content_type='application/json', data=json.dumps(songbook)) assert rv.status_code == 200 assert variant_ids[0].encode() not in rv.data assert variant_ids[1].encode() in rv.data # test both songs songbook['songs'] = [{ 'variant_id': variant_ids[0] }, { 'variant_id': variant_ids[1] }] rv = self.app.put('/api/v1/songbooks/{}'.format(songbook_id), content_type='application/json', data=json.dumps(songbook)) assert rv.status_code == 200 assert variant_ids[0].encode() in rv.data assert variant_ids[1].encode() in rv.data # test songbook duplication rv = self.app.get('/api/v1/songbooks/{}/duplicate'.format(songbook_id)) assert rv.status_code == 201 assert songbook_id.encode() not in rv.data assert b'"link":"songbooks/' in rv.data # clean the database self.mongo_client.drop_database(self.db_name)
def test_post_requests(self): # insert test song into the database rv = utils._post_song(self.app, title='Back in Black', description='Variant test song') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data data = json.loads(rv.data) song_id = data['id'] variant_id = data['variants'][0]['id'] # test json request error rv = self.app.post('/api/v1/songs/{}/variants'.format(song_id)) assert rv.status_code == 400 # test missing fields rv = self.app.post('/api/v1/songs/{}/variants'.format(song_id), content_type='application/json', data=json.dumps(dict(field="field"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data # test missing text field rv = self.app.post('/api/v1/songs/{}/variants'.format(song_id), content_type='application/json', data=json.dumps( dict(description="Missing text variant"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"text"' in rv.data # test missing description field rv = self.app.post('/api/v1/songs/{}/variants'.format(song_id), content_type='application/json', data=json.dumps( dict(text="Missing description variant"))) assert rv.status_code == 422 assert b'"code":"missing_field"' in rv.data assert b'"data":"description"' in rv.data # test non-existent song variant insertion rv = self.app.post( '/api/v1/songs/{}/variants'.format('000000000000000000000000'), content_type='application/json', data=json.dumps( dict(text="[verse] Test song", description="Test song"))) assert rv.status_code == 404 assert b'"code":"does_not_exist"' in rv.data # test correct song variant insertion rv = self.app.post('/api/v1/songs/{}/variants'.format(song_id), content_type='application/json', data=json.dumps( dict(title="Variant title", text="[verse] Test song", description="Test song"))) assert rv.status_code == 201 data = json.loads(rv.data) variant_id = data['id'] # check get request on inserted song variant rv = self.app.get('/api/v1/songs/{}/variants/{}'.format( song_id, variant_id)) assert rv.status_code == 200 variant = json.loads(rv.data) assert variant['text'] == "[verse] Test song" assert variant['id'] == variant_id assert variant['description'] == "Test song" # clean the database self.mongo_client.drop_database(self.db_name)
def test_deletion(self): # insert test song into the database rv = utils._post_song(self.app, title='Panic Station', description='First variant') assert rv.status_code == 201 assert b'"created":"' in rv.data assert b'"id":"' in rv.data variant_ids = [] data = json.loads(rv.data) song_id = data['id'] variant_ids.append(data['variants'][0]['id']) # insert one more variant for this song rv = self.app.post('/api/v1/songs/{}/variants'.format(song_id), content_type='application/json', data=json.dumps( dict(title="Another variant", text="[verse]", description="Second variant"))) assert rv.status_code == 201 data = json.loads(rv.data) variant_ids.append(data['id']) # try to delete nonexistent song variant from the database rv = self.app.delete('/api/v1/songs/{}/variants/{}'.format( song_id, '000000000000000000000000')) assert rv.status_code == 404 # delete variant of song rv = self.app.delete('/api/v1/songs/{}/variants/{}'.format( song_id, variant_ids[0])) assert rv.status_code == 204 # check, that song is really deleted rv = self.app.get('/api/v1/songs/{}/variants'.format(song_id)) data = json.loads(rv.data) assert len(data) == 1 # check that variant cannot be found via its id rv = self.app.get('/api/v1/songs/{}/variants/{}'.format( song_id, variant_ids[0])) assert rv.status_code == 404 assert b'Song variant was not found' in rv.data # delete second (last) variant rv = self.app.delete('/api/v1/songs/{}/variants/{}'.format( song_id, variant_ids[1])) assert rv.status_code == 204 # check correct deletion of entire song rv = self.app.get('/api/v1/songs/{}'.format(song_id)) assert rv.status_code == 404 assert b'"code":"does_not_exist"' in rv.data # clean the database self.mongo_client.drop_database(self.db_name)
def test_songbook_export(self): # insert test songbook for further testing rv = utils._post_songbook(self.app, title="Linkin Park songbook") assert rv.status_code == 201 songbook = json.loads(rv.data) songbook_id = songbook['id'] # insert test songs for further testing variant_ids = [] rv = utils._post_song( self.app, title="Numb", text="[verse][F#m]I'm tired of being what you [D]want me to be\n" "[A]Feeling so faithless\n" "Lost [E]under the surface\n" "[F#m]I don't know what you're ex[D]pecting of me\n" "[A]Put under the pressure\n" "of [E]walking in your [D]shoes\n" "[rec]Caught in the undertow, just caught in the undertow\n") assert rv.status_code == 201 song = json.loads(rv.data) variant_ids.append(song['variants'][0]['id']) rv = utils._post_song(self.app, title="Given Up", text="[verse][Em]Wake in a sweat again\n" "[G]Another day's been laid to [C]waste\n" "In my dis[D]grace\n" "[Em]Stuck in my head again\n" "[G]Feels like I'll never leave this [C]place\n" "There's no es[D]cape\n" "I'm my [Em]own worst [G]ene[C]my\n") assert rv.status_code == 201 song = json.loads(rv.data) variant_ids.append(song['variants'][0]['id']) # get current songbook rv = self.app.get('/api/v1/songbooks/{}'.format(songbook_id)) songbook = json.loads(rv.data) # insert songs into the songbook songbook['songs'] = [{ 'variant_id': variant_ids[0] }, { 'variant_id': variant_ids[1] }] rv = self.app.put('/api/v1/songbooks/{}/songs'.format(songbook_id), content_type='application/json', data=json.dumps(songbook)) assert rv.status_code == 200 # export test songbook as pdf rv = self.app.get('/api/v1/songbooks/{}'.format(songbook_id), headers={'Accept': 'application/pdf'}) assert rv.status_code == 200 assert b'download/' in rv.data # check correct json structure data = json.loads(rv.data) assert 'link' in data # delete generated file link = json.loads(rv.data)['link'] filename = str(link).split('/')[1] os.remove(os.path.join('./songs/done', filename)) # clean the database self.mongo_client.drop_database(self.db_name)