diff --git a/supysonic/api/scan.py b/supysonic/api/scan.py index 30a2191..7537269 100644 --- a/supysonic/api/scan.py +++ b/supysonic/api/scan.py @@ -1,19 +1,30 @@ -from . import api +# This file is part of Supysonic. +# Supysonic is a Python implementation of the Subsonic server API. +# +# Copyright (C) 2020 Alban 'spl0k' Féron +# 2020 Vincent Ducamps +# +# Distributed under terms of the GNU AGPLv3 license. + from flask import request from flask import current_app + +from ..daemon.client import DaemonClient +from ..daemon.exceptions import DaemonUnavailableError + +from . import api from .user import admin_only from .exceptions import ServerError -from ..daemon.client import DaemonClient @api.route("/startScan.view", methods=["GET", "POST"]) @admin_only def startScan(): try: - daeomonclient = DaemonClient(current_app.config["DAEMON"]["socket"]) - daeomonclient.scan() - scanned = daeomonclient.get_scanning_progress() - except Exception as e: + daemonclient = DaemonClient(current_app.config["DAEMON"]["socket"]) + daemonclient.scan() + scanned = daemonclient.get_scanning_progress() + except DaemonUnavailableError as e: raise ServerError(str(e)) return request.formatter( "scanStatus", @@ -31,7 +42,7 @@ def getScanStatus(): scanned = DaemonClient( current_app.config["DAEMON"]["socket"] ).get_scanning_progress() - except Exception as e: + except DaemonUnavailableError as e: raise ServerError(str(e)) return request.formatter( "scanStatus", diff --git a/tests/api/test_scan.py b/tests/api/test_scan.py index 9e33071..84e3167 100644 --- a/tests/api/test_scan.py +++ b/tests/api/test_scan.py @@ -1,53 +1,55 @@ # This file is part of Supysonic. # Supysonic is a Python implementation of the Subsonic server API. # -# Copyright (C) 2017-2020 Alban 'spl0k' Féron +# Copyright (C) 2020 Alban 'spl0k' Féron # # Distributed under terms of the GNU AGPLv3 license. from pony.orm import db_session +from threading import Thread +from supysonic.daemon.server import Daemon from supysonic.db import Folder from .apitestbase import ApiTestBase -from supysonic.daemon.server import Daemon -from threading import Thread -import logging - -logger = logging.getLogger() - - -class DaemonThread(Thread): - def __init__(self, daemon): - super(DaemonThread, self).__init__(target=daemon.run) - self.daemon = True - self.start() - class ScanTestCase(ApiTestBase): def setUp(self): - super(ScanTestCase, self).setUp(apiVersion="1.16.0") + super().setUp(apiVersion="1.16.0") + + def test_unauthorized(self): + self._make_request("startScan", args={"u": "bob", "p": "B0b"}, error=50) + self._make_request("getScanStatus", args={"u": "bob", "p": "B0b"}, error=50) + + def test_unavailable(self): + self._make_request("startScan", error=0) + self._make_request("getScanStatus", error=0) + + +class ScanWithDaemonTestCase(ApiTestBase): + def setUp(self): + super().setUp(apiVersion="1.16.0") with db_session: Folder(name="Root", root=True, path="tests/assets") + self._daemon = Daemon(self.config) + self._thread = Thread(target=self._daemon.run) + self._thread.start() + + def tearDown(self): + self._daemon.terminate() + self._thread.join() + + super().tearDown() + def test_startScan(self): - self._make_request("startScan", error=0) - daemon = Daemon(self.config) - with db_session: - daemonThread = DaemonThread(daemon) rv, child = self._make_request("startScan", tag="scanStatus") - self.assertTrue(child.get("scanning")) + self.assertEqual(child.get("scanning"), "true") self.assertGreaterEqual(int(child.get("count")), 0) - daemon.terminate() def test_getScanStatus(self): - self._make_request("getScanStatus", error=0) - daemon = Daemon(self.config) - with db_session: - daemonThread = DaemonThread(daemon) rv, child = self._make_request("getScanStatus", tag="scanStatus") - self.assertIn(child.get("scanning"), ["true", "false"]) - self.assertGreaterEqual(int(child.get("count")), 0) - daemon.terminate() + self.assertEqual(child.get("scanning"), "false") + self.assertEqual(int(child.get("count")), 0)