<commit_before>#!/usr/bin/env python
# coding: utf-8
#
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.

import uuid

from pony.orm import db_session

from supysonic.db import Folder, Artist, Album, Track, Playlist, User

from .apitestbase import ApiTestBase

class PlaylistTestCase(ApiTestBase):
    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,
                    content_type = 'audio/mpeg',
                    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_get_playlists(self):
        # get own playlists
        rv, child = self._make_request('getPlaylists', tag = 'playlists')
        self.assertEqual(len(child), 2)
        self.assertEqual(child[0].get('owner'), 'alice')
        self.assertEqual(child[1].get('owner'), 'alice')

        # get own and public
        rv, child = self._make_request('getPlaylists', { 'u': 'bob', 'p': 'B0b' }, tag = 'playlists')
        self.assertEqual(len(child), 2)
        self.assertTrue(child[0].get('owner') == 'alice' or child[1].get('owner') == 'alice')
        self.assertTrue(child[0].get('owner') == 'bob' or child[1].get('owner') == 'bob')
        self.assertIsNotNone(self._find(child, "./playlist[@owner='alice'][@public='true']"))

        # get other
        rv, child = self._make_request('getPlaylists', { 'username': 'bob' }, tag = 'playlists')
        self.assertEqual(len(child), 1)
        self.assertEqual(child[0].get('owner'), 'bob')

        # get other when not admin
        self._make_request('getPlaylists',  { 'u': 'bob', 'p': 'B0b', 'username': 'alice' }, error = 50)

        # get from unknown user
        self._make_request('getPlaylists', { 'username': 'johndoe' }, error = 70)

    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 == False 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')
        rv, child = self._make_request('getPlaylist', { 'id': child[0].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_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')<commit_msg>Fixed playlists tests<commit_after>#!/usr/bin/env python
# coding: utf-8
#
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.

import uuid

from pony.orm import db_session

from supysonic.db import Folder, Artist, Album, Track, Playlist, User

from .apitestbase import ApiTestBase

class PlaylistTestCase(ApiTestBase):
    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,
                    content_type = 'audio/mpeg',
                    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_get_playlists(self):
        # get own playlists
        rv, child = self._make_request('getPlaylists', tag = 'playlists')
        self.assertEqual(len(child), 2)
        self.assertEqual(child[0].get('owner'), 'alice')
        self.assertEqual(child[1].get('owner'), 'alice')

        # get own and public
        rv, child = self._make_request('getPlaylists', { 'u': 'bob', 'p': 'B0b' }, tag = 'playlists')
        self.assertEqual(len(child), 2)
        self.assertTrue(child[0].get('owner') == 'alice' or child[1].get('owner') == 'alice')
        self.assertTrue(child[0].get('owner') == 'bob' or child[1].get('owner') == 'bob')
        self.assertIsNotNone(self._find(child, "./playlist[@owner='alice'][@public='true']"))

        # get other
        rv, child = self._make_request('getPlaylists', { 'username': 'bob' }, tag = 'playlists')
        self.assertEqual(len(child), 1)
        self.assertEqual(child[0].get('owner'), 'bob')

        # get other when not admin
        self._make_request('getPlaylists',  { 'u': 'bob', 'p': 'B0b', 'username': 'alice' }, error = 50)

        # get from unknown user
        self._make_request('getPlaylists', { 'username': 'johndoe' }, error = 70)

    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_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')
{'fraction_non_alphanumeric': 0.1567902375889803, 'fraction_numerical': 0.012110566700563928, 'mean_word_length': 2.5134784020785967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1975054', 'n_tokens_mistral': 3419, 'n_tokens_neox': 3232, 'n_words': 757}