Debian Codegraph @

test/plugins/test_subsonicupdate.py

raw
1 """Tests for the 'subsonic' plugin."""
2
3 import responses
4
5 from beets import config
6 from beets.test.helper import TestHelper
7 from beetsplug import subsonicupdate
8
9
10 class SubsonicPluginTest(TestHelper):
11 """Test class for subsonicupdate."""
12
13 @responses.activate
14 def setUp(self):
15 """Sets up config and plugin for test."""
16 super().setUp()
17
18 config["subsonic"]["user"] = "admin"
19 config["subsonic"]["pass"] = "admin"
20 config["subsonic"]["url"] = "http://localhost:4040"
21 responses.add(
22 responses.GET,
23 "http://localhost:4040/rest/ping.view",
24 status=200,
25 body=self.PING_BODY,
26 )
27 self.subsonicupdate = subsonicupdate.SubsonicUpdate()
28
29 PING_BODY = """
30 {
31 "subsonic-response": {
32 "status": "failed",
33 "version": "1.15.0"
34 }
35 }
36 """
37 SUCCESS_BODY = """
38 {
39 "subsonic-response": {
40 "status": "ok",
41 "version": "1.15.0",
42 "scanStatus": {
43 "scanning": true,
44 "count": 1000
45 }
46 }
47 }
48 """
49
50 FAILED_BODY = """
51 {
52 "subsonic-response": {
53 "status": "failed",
54 "version": "1.15.0",
55 "error": {
56 "code": 40,
57 "message": "Wrong username or password."
58 }
59 }
60 }
61 """
62
63 ERROR_BODY = """
64 {
65 "timestamp": 1599185854498,
66 "status": 404,
67 "error": "Not Found",
68 "message": "No message available",
69 "path": "/rest/startScn"
70 }
71 """
72
73 @responses.activate
74 def test_start_scan(self):
75 """Tests success path based on best case scenario."""
76 responses.add(
77 responses.GET,
78 "http://localhost:4040/rest/startScan",
79 status=200,
80 body=self.SUCCESS_BODY,
81 )
82
83 self.subsonicupdate.start_scan(self.lib)
84
85 @responses.activate
86 def test_start_scan_failed_bad_credentials(self):
87 """Tests failed path based on bad credentials."""
88 responses.add(
89 responses.GET,
90 "http://localhost:4040/rest/startScan",
91 status=200,
92 body=self.FAILED_BODY,
93 )
94
95 self.subsonicupdate.start_scan(self.lib)
96
97 @responses.activate
98 def test_start_scan_failed_not_found(self):
99 """Tests failed path based on resource not found."""
100 responses.add(
101 responses.GET,
102 "http://localhost:4040/rest/startScan",
103 status=404,
104 body=self.ERROR_BODY,
105 )
106
107 self.subsonicupdate.start_scan(self.lib)
108
109 def test_start_scan_failed_unreachable(self):
110 """Tests failed path based on service not available."""
111 self.subsonicupdate.start_scan(self.lib)
112
113 @responses.activate
114 def test_url_with_context_path(self):
115 """Tests success for included with contextPath."""
116 config["subsonic"]["url"] = "http://localhost:4040/contextPath/"
117
118 responses.add(
119 responses.GET,
120 "http://localhost:4040/contextPath/rest/startScan",
121 status=200,
122 body=self.SUCCESS_BODY,
123 )
124
125 self.subsonicupdate.start_scan(self.lib)
126
127 @responses.activate
128 def test_url_with_trailing_forward_slash_url(self):
129 """Tests success path based on trailing forward slash."""
130 config["subsonic"]["url"] = "http://localhost:4040/"
131
132 responses.add(
133 responses.GET,
134 "http://localhost:4040/rest/startScan",
135 status=200,
136 body=self.SUCCESS_BODY,
137 )
138
139 self.subsonicupdate.start_scan(self.lib)
140
141 @responses.activate
142 def test_url_with_missing_port(self):
143 """Tests failed path based on missing port."""
144 config["subsonic"]["url"] = "http://localhost/airsonic"
145
146 responses.add(
147 responses.GET,
148 "http://localhost/airsonic/rest/startScan",
149 status=200,
150 body=self.SUCCESS_BODY,
151 )
152
153 self.subsonicupdate.start_scan(self.lib)
154
155 @responses.activate
156 def test_url_with_missing_schema(self):
157 """Tests failed path based on missing schema."""
158 config["subsonic"]["url"] = "localhost:4040/airsonic"
159
160 responses.add(
161 responses.GET,
162 "http://localhost:4040/rest/startScan",
163 status=200,
164 body=self.SUCCESS_BODY,
165 )
166
167 self.subsonicupdate.start_scan(self.lib)
168
169 @responses.activate
170 def test_start_scan_failed_non_json_response(self):
171 """Tests failed path based on a non-JSON server response."""
172 responses.add(
173 responses.GET,
174 "http://localhost:4040/rest/startScan",
175 status=503,
176 body="<html>server unavailable</html>",
177 content_type="text/html",
178 )
179
180 with self.assertLogs("beets", level="ERROR") as logs:
181 self.subsonicupdate.start_scan(self.lib)
182
183 assert "Subsonic server returned a non-JSON response" in "\n".join(
184 logs.output
185 )