def setUp(self): super(PlaylistTestCase, self).setUp() with db_session: folder = Folder(name="Root", path="tests/assets", root=True) artist = Artist(name="Artist!") album = Album(name="Album!", artist=artist) track = Track( path="tests/assets/23bytes", title="23bytes", artist=artist, album=album, folder=folder, root_folder=folder, duration=2, disc=1, number=1, bitrate=320, last_modification=0, ) playlist = Playlist(name="Playlist!", user=User.get(name="alice")) for _ in range(4): playlist.add(track) self.playlistid = playlist.id
def test_delete_playlist(self): # check params self._make_request('deletePlaylist', error=10) self._make_request('deletePlaylist', {'id': 'string'}, error=0) self._make_request('deletePlaylist', {'id': str(uuid.uuid4())}, error=70) # delete unowned when not admin with db_session: playlist = Playlist.select( lambda p: p.user.name == 'alice').first() self._make_request('deletePlaylist', { 'u': 'bob', 'p': 'B0b', 'id': str(playlist.id) }, error=50) self.assertPlaylistCountEqual(3) # delete owned self._make_request('deletePlaylist', {'id': str(playlist.id)}, skip_post=True) self.assertPlaylistCountEqual(2) self._make_request('deletePlaylist', {'id': str(playlist.id)}, error=70) self.assertPlaylistCountEqual(2) # delete unowned when admin with db_session: playlist = Playlist.get(lambda p: p.user.name == 'bob') self._make_request('deletePlaylist', {'id': str(playlist.id)}, skip_post=True) self.assertPlaylistCountEqual(1)
def test_delete_playlist(self): # check params self._make_request("deletePlaylist", error=10) self._make_request("deletePlaylist", {"id": "string"}, error=0) self._make_request("deletePlaylist", {"id": str(uuid.uuid4())}, error=70) # delete unowned when not admin with db_session: playlist = Playlist.select( lambda p: p.user.name == "alice").first() self._make_request("deletePlaylist", { "u": "bob", "p": "B0b", "id": str(playlist.id) }, error=50) self.assertPlaylistCountEqual(3) # delete owned self._make_request("deletePlaylist", {"id": str(playlist.id)}, skip_post=True) self.assertPlaylistCountEqual(2) self._make_request("deletePlaylist", {"id": str(playlist.id)}, error=70) self.assertPlaylistCountEqual(2) # delete unowned when admin with db_session: playlist = Playlist.get(lambda p: p.user.name == "bob") self._make_request("deletePlaylist", {"id": str(playlist.id)}, skip_post=True) self.assertPlaylistCountEqual(1)
def test_get_playlist(self): # missing param self._make_request("getPlaylist", error=10) # invalid id self._make_request("getPlaylist", {"id": 1234}, error=0) # unknown self._make_request("getPlaylist", {"id": str(uuid.uuid4())}, error=70) # other's private from non admin with db_session: playlist = Playlist.get(lambda p: not p.public and p.user.name == "alice") self._make_request( "getPlaylist", {"u": "bob", "p": "B0b", "id": str(playlist.id)}, error=50 ) # standard rv, child = self._make_request("getPlaylists", tag="playlists") self._make_request("getPlaylist", {"id": child[0].get("id")}, tag="playlist") rv, child = self._make_request( "getPlaylist", {"id": child[1].get("id")}, tag="playlist" ) self.assertEqual(child.get("songCount"), "2") self.assertEqual( self._xpath(child, "count(./entry)"), 2 ) # don't count children, there may be 'allowedUser's (even though not supported by supysonic) self.assertEqual(child.get("duration"), "4") self.assertEqual(child[0].get("title"), "One") self.assertTrue( child[1].get("title") in ("Two", "Three") ) # depending on 'getPlaylists' result ordering
def create_playlist(): # Only(?) method where the android client uses form data rather than GET params playlist_id, name = map( lambda x: request.args.get(x) or request.form.get(x), ['playlistId', 'name']) # songId actually doesn't seem to be required songs = request.args.getlist('songId') or request.form.getlist('songId') try: playlist_id = uuid.UUID(playlist_id) if playlist_id else None songs = set(map(uuid.UUID, songs)) except: return request.error_formatter(0, 'Invalid parameter') if playlist_id: playlist = store.get(Playlist, playlist_id) if not playlist: return request.error_formatter(70, 'Unknwon playlist') if playlist.user_id != request.user.id and not request.user.admin: return request.error_formatter( 50, "You're not allowed to modify a playlist that isn't yours") playlist.tracks.clear() if name: playlist.name = name elif name: playlist = Playlist() playlist.user_id = request.user.id playlist.name = name store.add(playlist) else: return request.error_formatter(10, 'Missing playlist id or name') for sid in songs: track = store.get(Track, sid) if not track: return request.error_formatter(70, 'Unknown song') playlist.tracks.add(track) store.commit() return request.formatter({})
def test_delete(self): self._login("bob", "B0b") rv = self.client.get("/playlist/del/string", follow_redirects=True) self.assertIn("Invalid", rv.data) rv = self.client.get("/playlist/del/" + str(uuid.uuid4()), follow_redirects=True) self.assertIn("Unknown", rv.data) rv = self.client.get("/playlist/del/" + str(self.playlistid), follow_redirects=True) self.assertIn("not allowed", rv.data) with db_session: self.assertEqual(Playlist.select().count(), 1) self._logout() self._login("alice", "Alic3") rv = self.client.get("/playlist/del/" + str(self.playlistid), follow_redirects=True) self.assertIn("deleted", rv.data) with db_session: self.assertEqual(Playlist.select().count(), 0)
def create_playlist(): # Only(?) method where the android client uses form data rather than GET params playlist_id, name = map(request.values.get, [ 'playlistId', 'name' ]) # songId actually doesn't seem to be required songs = request.values.getlist('songId') try: playlist_id = uuid.UUID(playlist_id) if playlist_id else None songs = map(uuid.UUID, songs) except: return request.error_formatter(0, 'Invalid parameter') if playlist_id: playlist = store.get(Playlist, playlist_id) if not playlist: return request.error_formatter(70, 'Unknwon playlist') if playlist.user_id != request.user.id and not request.user.admin: return request.error_formatter(50, "You're not allowed to modify a playlist that isn't yours") playlist.clear() if name: playlist.name = name elif name: playlist = Playlist() playlist.user_id = request.user.id playlist.name = name store.add(playlist) else: return request.error_formatter(10, 'Missing playlist id or name') for sid in songs: track = store.get(Track, sid) if not track: store.rollback() return request.error_formatter(70, 'Unknown song') playlist.add(track) store.commit() return request.formatter({})
def setUp(self): super(PlaylistTestCase, self).setUp() folder = Folder() folder.name = 'Root' folder.path = 'tests/assets' folder.root = True artist = Artist() artist.name = 'Artist!' album = Album() album.name = 'Album!' album.artist = artist track = Track() track.path = 'tests/assets/23bytes' track.title = '23bytes' track.artist = artist track.album = album track.folder = folder track.root_folder = folder track.duration = 2 track.disc = 1 track.number = 1 track.content_type = 'audio/mpeg' track.bitrate = 320 track.last_modification = 0 playlist = Playlist() playlist.name = 'Playlist!' playlist.user = self.store.find(User, User.name == 'alice').one() for _ in range(4): playlist.add(track) self.store.add(track) self.store.add(playlist) self.store.commit() self.playlist = playlist
def test_delete_playlist(self): # check params self._make_request('deletePlaylist', error = 10) self._make_request('deletePlaylist', { 'id': 'string' }, error = 0) self._make_request('deletePlaylist', { 'id': str(uuid.uuid4()) }, error = 70) # delete unowned when not admin with db_session: playlist = Playlist.select(lambda p: p.user.name == 'alice').first() self._make_request('deletePlaylist', { 'u': 'bob', 'p': 'B0b', 'id': str(playlist.id) }, error = 50) self.assertPlaylistCountEqual(3); # delete owned self._make_request('deletePlaylist', { 'id': str(playlist.id) }, skip_post = True) self.assertPlaylistCountEqual(2); self._make_request('deletePlaylist', { 'id': str(playlist.id) }, error = 70) self.assertPlaylistCountEqual(2); # delete unowned when admin with db_session: playlist = Playlist.get(lambda p: p.user.name == 'bob') self._make_request('deletePlaylist', { 'id': str(playlist.id) }, skip_post = True) self.assertPlaylistCountEqual(1);
def test_update_playlist(self): self._make_request('updatePlaylist', error = 10) self._make_request('updatePlaylist', { 'playlistId': 1234 }, error = 0) self._make_request('updatePlaylist', { 'playlistId': str(uuid.uuid4()) }, error = 70) with db_session: playlist = Playlist.select(lambda p: p.user.name == 'alice').order_by(Playlist.created).first() pid = str(playlist.id) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIdToAdd': 'string' }, error = 0) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIndexToRemove': 'string' }, error = 0) name = str(playlist.name) self._make_request('updatePlaylist', { 'u': 'bob', 'p': 'B0b', 'playlistId': pid, 'name': 'new name' }, error = 50) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertEqual(child.get('name'), name) self.assertEqual(self._xpath(child, 'count(./entry)'), 2) self._make_request('updatePlaylist', { 'playlistId': pid, 'name': 'new name' }, skip_post = True) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertEqual(child.get('name'), 'new name') self.assertEqual(self._xpath(child, 'count(./entry)'), 2) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIndexToRemove': [ -1, 2 ] }, skip_post = True) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertEqual(self._xpath(child, 'count(./entry)'), 2) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIndexToRemove': [ 0, 2 ] }, skip_post = True) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertEqual(self._xpath(child, 'count(./entry)'), 1) self.assertEqual(self._find(child, './entry').get('title'), 'Three') with db_session: songs = { s.title: str(s.id) for s in Track.select() } self._make_request('updatePlaylist', { 'playlistId': pid, 'songIdToAdd': [ songs['One'], songs['Two'], songs['Two'] ] }, skip_post = True) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertSequenceEqual(self._xpath(child, './entry/@title'), [ 'Three', 'One', 'Two', 'Two' ]) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIndexToRemove': [ 2, 1 ] }, skip_post = True) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertSequenceEqual(self._xpath(child, './entry/@title'), [ 'Three', 'Two' ]) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIdToAdd': songs['One'] }, skip_post = True) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIndexToRemove': [ 1, 1 ] }, skip_post = True) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertSequenceEqual(self._xpath(child, './entry/@title'), [ 'Three', 'One' ]) self._make_request('updatePlaylist', { 'playlistId': pid, 'songIdToAdd': str(uuid.uuid4()) }, error = 70) rv, child = self._make_request('getPlaylist', { 'id': pid }, tag = 'playlist') self.assertEqual(self._xpath(child, 'count(./entry)'), 2)
def test_create_playlist(self): self._make_request('createPlaylist', error = 10) self._make_request('createPlaylist', { 'name': 'wrongId', 'songId': 'abc' }, error = 0) self._make_request('createPlaylist', { 'name': 'unknownId', 'songId': str(uuid.uuid4()) }, error = 70) self._make_request('createPlaylist', { 'playlistId': 'abc' }, error = 0) self._make_request('createPlaylist', { 'playlistId': str(uuid.uuid4()) }, error = 70) # create self._make_request('createPlaylist', { 'name': 'new playlist' }, skip_post = True) rv, child = self._make_request('getPlaylists', tag = 'playlists') self.assertEqual(len(child), 3) playlist = self._find(child, "./playlist[@name='new playlist']") self.assertEqual(len(playlist), 0) # "update" newly created self._make_request('createPlaylist', { 'playlistId': playlist.get('id') }) rv, child = self._make_request('getPlaylists', tag = 'playlists') self.assertEqual(len(child), 3) # renaming self._make_request('createPlaylist', { 'playlistId': playlist.get('id'), 'name': 'renamed' }) rv, child = self._make_request('getPlaylists', tag = 'playlists') self.assertEqual(len(child), 3) self.assertIsNone(self._find(child, "./playlist[@name='new playlist']")) playlist = self._find(child, "./playlist[@name='renamed']") self.assertIsNotNone(playlist) # update from other user self._make_request('createPlaylist', { 'u': 'bob', 'p': 'B0b', 'playlistId': playlist.get('id') }, error = 50) # create more useful playlist with db_session: songs = { s.title: str(s.id) for s in Track.select() } self._make_request('createPlaylist', { 'name': 'songs', 'songId': list(map(lambda s: songs[s], [ 'Three', 'One', 'Two' ])) }, skip_post = True) with db_session: playlist = Playlist.get(name = 'songs') self.assertIsNotNone(playlist) rv, child = self._make_request('getPlaylist', { 'id': str(playlist.id) }, tag = 'playlist') self.assertEqual(child.get('songCount'), '3') self.assertEqual(self._xpath(child, 'count(./entry)'), 3) self.assertEqual(child[0].get('title'), 'Three') self.assertEqual(child[1].get('title'), 'One') self.assertEqual(child[2].get('title'), 'Two') # update self._make_request('createPlaylist', { 'playlistId': str(playlist.id), 'songId': songs['Two'] }, skip_post = True) rv, child = self._make_request('getPlaylist', { 'id': str(playlist.id) }, tag = 'playlist') self.assertEqual(child.get('songCount'), '1') self.assertEqual(self._xpath(child, 'count(./entry)'), 1) self.assertEqual(child[0].get('title'), 'Two')
def upload_file(): if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #return redirect(url_for('uploaded_file', filename=filename)) playlist = Playlist() playlist.user_id = uuid.UUID(session.get('userid')) playlist.name = filename store.add(playlist) tracks = [] with open(os.path.join(app.config['UPLOAD_FOLDER'], filename)) as f: for line in f: if line[0] != '#': line = line.rstrip() line = line.decode('utf-8') tracks.append(line) track = store.find(Track, Track.path == line).one() if track: playlist.tracks.add(track) store.commit() return render_template('upload.html', tracks = tracks) return '''
def test_get_playlist(self): # missing param self._make_request('getPlaylist', error=10) # invalid id self._make_request('getPlaylist', {'id': 1234}, error=0) # unknown self._make_request('getPlaylist', {'id': str(uuid.uuid4())}, error=70) # other's private from non admin with db_session: playlist = Playlist.get( lambda p: not p.public and p.user.name == 'alice') self._make_request('getPlaylist', { 'u': 'bob', 'p': 'B0b', 'id': str(playlist.id) }, error=50) # standard rv, child = self._make_request('getPlaylists', tag='playlists') self._make_request('getPlaylist', {'id': child[0].get('id')}, tag='playlist') rv, child = self._make_request('getPlaylist', {'id': child[1].get('id')}, tag='playlist') self.assertEqual(child.get('songCount'), '2') self.assertEqual( self._xpath(child, 'count(./entry)'), 2 ) # don't count children, there may be 'allowedUser's (even though not supported by supysonic) self.assertEqual(child.get('duration'), '4') self.assertEqual(child[0].get('title'), 'One') self.assertTrue( child[1].get('title') == 'Two' or child[1].get('title') == 'Three') # depending on 'getPlaylists' result ordering
def test_get_playlist(self): # missing param self._make_request('getPlaylist', error = 10) # invalid id self._make_request('getPlaylist', { 'id': 1234 }, error = 0) # unknown self._make_request('getPlaylist', { 'id': str(uuid.uuid4()) }, error = 70) # other's private from non admin with db_session: playlist = Playlist.get(lambda p: not p.public and p.user.name == 'alice') self._make_request('getPlaylist', { 'u': 'bob', 'p': 'B0b', 'id': str(playlist.id) }, error = 50) # standard rv, child = self._make_request('getPlaylists', tag = 'playlists') self._make_request('getPlaylist', { 'id': child[0].get('id') }, tag = 'playlist') rv, child = self._make_request('getPlaylist', { 'id': child[1].get('id') }, tag = 'playlist') self.assertEqual(child.get('songCount'), '2') self.assertEqual(self._xpath(child, 'count(./entry)'), 2) # don't count children, there may be 'allowedUser's (even though not supported by supysonic) self.assertEqual(child.get('duration'), '4') self.assertEqual(child[0].get('title'), 'One') self.assertTrue(child[1].get('title') == 'Two' or child[1].get('title') == 'Three') # depending on 'getPlaylists' result ordering
def setUp(self): super(PlaylistTestCase, self).setUp() root = Folder() root.root = True root.name = 'Root folder' root.path = 'tests/assets' self.store.add(root) artist = Artist() artist.name = 'Artist' album = Album() album.name = 'Album' album.artist = artist songs = {} for num, song in enumerate([ 'One', 'Two', 'Three', 'Four' ]): track = Track() track.disc = 1 track.number = num track.title = song track.duration = 2 track.album = album track.artist = artist track.bitrate = 320 track.path = 'tests/assets/empty' track.content_type = 'audio/mpeg' track.last_modification = 0 track.root_folder = root track.folder = root self.store.add(track) songs[song] = track users = { u.name: u for u in self.store.find(User) } playlist = Playlist() playlist.user = users['alice'] playlist.name = "Alice's" playlist.add(songs['One']) playlist.add(songs['Three']) self.store.add(playlist) playlist = Playlist() playlist.user = users['alice'] playlist.public = True playlist.name = "Alice's public" playlist.add(songs['One']) playlist.add(songs['Two']) self.store.add(playlist) playlist = Playlist() playlist.user = users['bob'] playlist.name = "Bob's" playlist.add(songs['Two']) playlist.add(songs['Four']) self.store.add(playlist) self.store.commit()
def assertPlaylistCountEqual(self, count): self.assertEqual(Playlist.select().count(), count)
def setUp(self): super(PlaylistTestCase, self).setUp() with db_session: root = Folder(root=True, name='Root folder', path='tests/assets') artist = Artist(name='Artist') album = Album(name='Album', artist=artist) songs = {} for num, song in enumerate(['One', 'Two', 'Three', 'Four']): track = Track(disc=1, number=num, title=song, duration=2, album=album, artist=artist, bitrate=320, path='tests/assets/' + song, last_modification=0, root_folder=root, folder=root) songs[song] = track users = {u.name: u for u in User.select()} playlist = Playlist(user=users['alice'], name="Alice's") playlist.add(songs['One']) playlist.add(songs['Three']) playlist = Playlist(user=users['alice'], public=True, name="Alice's public") playlist.add(songs['One']) playlist.add(songs['Two']) playlist = Playlist(user=users['bob'], name="Bob's") playlist.add(songs['Two']) playlist.add(songs['Four'])
def setUp(self): super(PlaylistTestCase, self).setUp() root = Folder() root.root = True root.name = 'Root folder' root.path = 'tests/assets' self.store.add(root) artist = Artist() artist.name = 'Artist' album = Album() album.name = 'Album' album.artist = artist songs = {} for num, song in enumerate(['One', 'Two', 'Three', 'Four']): track = Track() track.disc = 1 track.number = num track.title = song track.duration = 2 track.album = album track.artist = artist track.bitrate = 320 track.path = 'tests/assets/empty' track.content_type = 'audio/mpeg' track.last_modification = 0 track.root_folder = root track.folder = root self.store.add(track) songs[song] = track users = {u.name: u for u in self.store.find(User)} playlist = Playlist() playlist.user = users['alice'] playlist.name = "Alice's" playlist.add(songs['One']) playlist.add(songs['Three']) self.store.add(playlist) playlist = Playlist() playlist.user = users['alice'] playlist.public = True playlist.name = "Alice's public" playlist.add(songs['One']) playlist.add(songs['Two']) self.store.add(playlist) playlist = Playlist() playlist.user = users['bob'] playlist.name = "Bob's" playlist.add(songs['Two']) playlist.add(songs['Four']) self.store.add(playlist) self.store.commit()
def setUp(self): super(PlaylistTestCase, self).setUp() with db_session: root = Folder(root = True, name = 'Root folder', path = 'tests/assets') artist = Artist(name = 'Artist') album = Album(name = 'Album', artist = artist) songs = {} for num, song in enumerate([ 'One', 'Two', 'Three', 'Four' ]): track = Track( disc = 1, number = num, title = song, duration = 2, album = album, artist = artist, bitrate = 320, path = 'tests/assets/' + song, last_modification = 0, root_folder = root, folder = root ) songs[song] = track users = { u.name: u for u in User.select() } playlist = Playlist(user = users['alice'], name = "Alice's") playlist.add(songs['One']) playlist.add(songs['Three']) playlist = Playlist(user = users['alice'], public = True, name = "Alice's public") playlist.add(songs['One']) playlist.add(songs['Two']) playlist = Playlist(user = users['bob'], name = "Bob's") playlist.add(songs['Two']) playlist.add(songs['Four'])
def test_create_playlist(self): self._make_request("createPlaylist", error=10) self._make_request("createPlaylist", { "name": "wrongId", "songId": "abc" }, error=0) self._make_request( "createPlaylist", { "name": "unknownId", "songId": str(uuid.uuid4()) }, error=70, ) self._make_request("createPlaylist", {"playlistId": "abc"}, error=0) self._make_request("createPlaylist", {"playlistId": str(uuid.uuid4())}, error=70) # create self._make_request("createPlaylist", {"name": "new playlist"}, skip_post=True) rv, child = self._make_request("getPlaylists", tag="playlists") self.assertEqual(len(child), 3) playlist = self._find(child, "./playlist[@name='new playlist']") self.assertEqual(len(playlist), 0) # "update" newly created self._make_request("createPlaylist", {"playlistId": playlist.get("id")}) rv, child = self._make_request("getPlaylists", tag="playlists") self.assertEqual(len(child), 3) # renaming self._make_request("createPlaylist", { "playlistId": playlist.get("id"), "name": "renamed" }) rv, child = self._make_request("getPlaylists", tag="playlists") self.assertEqual(len(child), 3) self.assertIsNone(self._find(child, "./playlist[@name='new playlist']")) playlist = self._find(child, "./playlist[@name='renamed']") self.assertIsNotNone(playlist) # update from other user self._make_request( "createPlaylist", { "u": "bob", "p": "B0b", "playlistId": playlist.get("id") }, error=50, ) # create more useful playlist with db_session: songs = {s.title: str(s.id) for s in Track.select()} self._make_request( "createPlaylist", { "name": "songs", "songId": list(map(lambda s: songs[s], ["Three", "One", "Two"])), }, skip_post=True, ) with db_session: playlist = Playlist.get(name="songs") self.assertIsNotNone(playlist) rv, child = self._make_request("getPlaylist", {"id": str(playlist.id)}, tag="playlist") self.assertEqual(child.get("songCount"), "3") self.assertEqual(self._xpath(child, "count(./entry)"), 3) self.assertEqual(child[0].get("title"), "Three") self.assertEqual(child[1].get("title"), "One") self.assertEqual(child[2].get("title"), "Two") # update self._make_request( "createPlaylist", { "playlistId": str(playlist.id), "songId": songs["Two"] }, skip_post=True, ) rv, child = self._make_request("getPlaylist", {"id": str(playlist.id)}, tag="playlist") self.assertEqual(child.get("songCount"), "1") self.assertEqual(self._xpath(child, "count(./entry)"), 1) self.assertEqual(child[0].get("title"), "Two")
def setUp(self): super(PlaylistTestCase, self).setUp() with db_session: root = Folder(root=True, name="Root folder", path="tests/assets") artist = Artist(name="Artist") album = Album(name="Album", artist=artist) songs = {} for num, song in enumerate(["One", "Two", "Three", "Four"]): track = Track( disc=1, number=num, title=song, duration=2, album=album, artist=artist, bitrate=320, path="tests/assets/" + song, last_modification=0, root_folder=root, folder=root, ) songs[song] = track users = {u.name: u for u in User.select()} playlist = Playlist(user=users["alice"], name="Alice's") playlist.add(songs["One"]) playlist.add(songs["Three"]) playlist = Playlist(user=users["alice"], public=True, name="Alice's public") playlist.add(songs["One"]) playlist.add(songs["Two"]) playlist = Playlist(user=users["bob"], name="Bob's") playlist.add(songs["Two"]) playlist.add(songs["Four"])
def test_update_playlist(self): self._make_request("updatePlaylist", error=10) self._make_request("updatePlaylist", {"playlistId": 1234}, error=0) self._make_request("updatePlaylist", {"playlistId": str(uuid.uuid4())}, error=70) with db_session: playlist = ( Playlist.select(lambda p: p.user.name == "alice").order_by( Playlist.created).first()) pid = str(playlist.id) self._make_request("updatePlaylist", { "playlistId": pid, "songIdToAdd": "string" }, error=0) self._make_request( "updatePlaylist", { "playlistId": pid, "songIndexToRemove": "string" }, error=0, ) name = str(playlist.name) self._make_request( "updatePlaylist", { "u": "bob", "p": "B0b", "playlistId": pid, "name": "new name" }, error=50, ) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertEqual(child.get("name"), name) self.assertEqual(self._xpath(child, "count(./entry)"), 2) self._make_request("updatePlaylist", { "playlistId": pid, "name": "new name" }, skip_post=True) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertEqual(child.get("name"), "new name") self.assertEqual(self._xpath(child, "count(./entry)"), 2) self._make_request( "updatePlaylist", { "playlistId": pid, "songIndexToRemove": [-1, 2] }, skip_post=True, ) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertEqual(self._xpath(child, "count(./entry)"), 2) self._make_request( "updatePlaylist", { "playlistId": pid, "songIndexToRemove": [0, 2] }, skip_post=True, ) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertEqual(self._xpath(child, "count(./entry)"), 1) self.assertEqual(self._find(child, "./entry").get("title"), "Three") with db_session: songs = {s.title: str(s.id) for s in Track.select()} self._make_request( "updatePlaylist", { "playlistId": pid, "songIdToAdd": [songs["One"], songs["Two"], songs["Two"]], }, skip_post=True, ) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertSequenceEqual(self._xpath(child, "./entry/@title"), ["Three", "One", "Two", "Two"]) self._make_request( "updatePlaylist", { "playlistId": pid, "songIndexToRemove": [2, 1] }, skip_post=True, ) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertSequenceEqual(self._xpath(child, "./entry/@title"), ["Three", "Two"]) self._make_request( "updatePlaylist", { "playlistId": pid, "songIdToAdd": songs["One"] }, skip_post=True, ) self._make_request( "updatePlaylist", { "playlistId": pid, "songIndexToRemove": [1, 1] }, skip_post=True, ) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertSequenceEqual(self._xpath(child, "./entry/@title"), ["Three", "One"]) self._make_request( "updatePlaylist", { "playlistId": pid, "songIdToAdd": str(uuid.uuid4()) }, error=70, ) rv, child = self._make_request("getPlaylist", {"id": pid}, tag="playlist") self.assertEqual(self._xpath(child, "count(./entry)"), 2)