File: test_subsonicupdate.py

package info (click to toggle)
beets 1.6.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,652 kB
  • sloc: python: 39,201; javascript: 7,997; xml: 334; sh: 247; makefile: 117
file content (198 lines) | stat: -rw-r--r-- 5,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""Tests for the 'subsonic' plugin."""


import responses
import unittest

from test import _common
from beets import config
from beetsplug import subsonicupdate
from test.helper import TestHelper
from urllib.parse import parse_qs, urlparse


class ArgumentsMock:
    """Argument mocks for tests."""

    def __init__(self, mode, show_failures):
        """Constructs ArgumentsMock."""
        self.mode = mode
        self.show_failures = show_failures
        self.verbose = 1


def _params(url):
    """Get the query parameters from a URL."""
    return parse_qs(urlparse(url).query)


class SubsonicPluginTest(_common.TestCase, TestHelper):
    """Test class for subsonicupdate."""
    @responses.activate
    def setUp(self):
        """Sets up config and plugin for test."""
        config.clear()
        self.setup_beets()

        config["subsonic"]["user"] = "admin"
        config["subsonic"]["pass"] = "admin"
        config["subsonic"]["url"] = "http://localhost:4040"
        responses.add(
            responses.GET,
            'http://localhost:4040/rest/ping.view',
            status=200,
            body=self.PING_BODY
        )
        self.subsonicupdate = subsonicupdate.SubsonicUpdate()
    PING_BODY = '''
{
    "subsonic-response": {
        "status": "failed",
        "version": "1.15.0"
    }
}
'''
    SUCCESS_BODY = '''
{
    "subsonic-response": {
        "status": "ok",
        "version": "1.15.0",
        "scanStatus": {
            "scanning": true,
            "count": 1000
        }
    }
}
'''

    FAILED_BODY = '''
{
    "subsonic-response": {
        "status": "failed",
        "version": "1.15.0",
        "error": {
            "code": 40,
            "message": "Wrong username or password."
        }
    }
}
'''

    ERROR_BODY = '''
{
    "timestamp": 1599185854498,
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/rest/startScn"
}
'''

    def tearDown(self):
        """Tears down tests."""
        self.teardown_beets()

    @responses.activate
    def test_start_scan(self):
        """Tests success path based on best case scenario."""
        responses.add(
            responses.GET,
            'http://localhost:4040/rest/startScan',
            status=200,
            body=self.SUCCESS_BODY
        )

        self.subsonicupdate.start_scan()

    @responses.activate
    def test_start_scan_failed_bad_credentials(self):
        """Tests failed path based on bad credentials."""
        responses.add(
            responses.GET,
            'http://localhost:4040/rest/startScan',
            status=200,
            body=self.FAILED_BODY
        )

        self.subsonicupdate.start_scan()

    @responses.activate
    def test_start_scan_failed_not_found(self):
        """Tests failed path based on resource not found."""
        responses.add(
            responses.GET,
            'http://localhost:4040/rest/startScan',
            status=404,
            body=self.ERROR_BODY
        )

        self.subsonicupdate.start_scan()

    def test_start_scan_failed_unreachable(self):
        """Tests failed path based on service not available."""
        self.subsonicupdate.start_scan()

    @responses.activate
    def test_url_with_context_path(self):
        """Tests success for included with contextPath."""
        config["subsonic"]["url"] = "http://localhost:4040/contextPath/"

        responses.add(
            responses.GET,
            'http://localhost:4040/contextPath/rest/startScan',
            status=200,
            body=self.SUCCESS_BODY
        )

        self.subsonicupdate.start_scan()

    @responses.activate
    def test_url_with_trailing_forward_slash_url(self):
        """Tests success path based on trailing forward slash."""
        config["subsonic"]["url"] = "http://localhost:4040/"

        responses.add(
            responses.GET,
            'http://localhost:4040/rest/startScan',
            status=200,
            body=self.SUCCESS_BODY
        )

        self.subsonicupdate.start_scan()

    @responses.activate
    def test_url_with_missing_port(self):
        """Tests failed path based on missing port."""
        config["subsonic"]["url"] = "http://localhost/airsonic"

        responses.add(
            responses.GET,
            'http://localhost/airsonic/rest/startScan',
            status=200,
            body=self.SUCCESS_BODY
        )

        self.subsonicupdate.start_scan()

    @responses.activate
    def test_url_with_missing_schema(self):
        """Tests failed path based on missing schema."""
        config["subsonic"]["url"] = "localhost:4040/airsonic"

        responses.add(
            responses.GET,
            'http://localhost:4040/rest/startScan',
            status=200,
            body=self.SUCCESS_BODY
        )

        self.subsonicupdate.start_scan()


def suite():
    """Default test suite."""
    return unittest.TestLoader().loadTestsFromName(__name__)


if __name__ == '__main__':
    unittest.main(defaultTest='suite')