Skip to content

Commit c7b897d

Browse files
committed
fix: handle missing file parameter in /api/loadfile
This commit also introduces a test that calls post endpoints with wrong args.
1 parent 411ca7b commit c7b897d

File tree

3 files changed

+207
-1
lines changed

3 files changed

+207
-1
lines changed

tests/snapshots/snap_tests.py

+152
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,158 @@
66

77
snapshots = Snapshot()
88

9+
snapshots["TestsRequests.test_post_wrong_args[add-&-foo] 1"] = {
10+
"message": "Parameter name contains invalid characters"
11+
}
12+
13+
snapshots["TestsRequests.test_post_wrong_args[add-foo-&] 1"] = {
14+
"message": "Parameter needs to be an integer or float"
15+
}
16+
17+
snapshots["TestsRequests.test_post_wrong_args[add_audio_delay-&-None] 1"] = {
18+
"message": "Parameter needs to be an integer or float"
19+
}
20+
21+
snapshots["TestsRequests.test_post_wrong_args[add_chapter-&-None] 1"] = {
22+
"message": "Parameter needs to be an integer or float"
23+
}
24+
25+
snapshots["TestsRequests.test_post_wrong_args[add_sub_delay-&-None] 1"] = {
26+
"message": "Parameter needs to be an integer or float"
27+
}
28+
29+
snapshots["TestsRequests.test_post_wrong_args[add_volume-&-None] 1"] = {
30+
"message": "Parameter needs to be an integer or float"
31+
}
32+
33+
snapshots["TestsRequests.test_post_wrong_args[cycle-&-foo] 1"] = {
34+
"message": "Parameter name contains invalid characters"
35+
}
36+
37+
snapshots["TestsRequests.test_post_wrong_args[cycle-foo-&] 1"] = {
38+
"message": 'Cycle paramater is not "up" or "down"'
39+
}
40+
41+
snapshots["TestsRequests.test_post_wrong_args[loadfile-None-None] 1"] = {
42+
"message": "No url provided!"
43+
}
44+
45+
snapshots["TestsRequests.test_post_wrong_args[loadfile-http://foo-invalid] 1"] = {
46+
"message": "Invalid mode: 'foo'"
47+
}
48+
49+
snapshots["TestsRequests.test_post_wrong_args[loop_file-&-None] 1"] = {
50+
"message": "Invalid parameter!"
51+
}
52+
53+
snapshots["TestsRequests.test_post_wrong_args[loop_file-None-None] 1"] = {
54+
"message": "Invalid parameter!"
55+
}
56+
57+
snapshots["TestsRequests.test_post_wrong_args[loop_playlist-&-None] 1"] = {
58+
"message": "Invalid parameter!"
59+
}
60+
61+
snapshots["TestsRequests.test_post_wrong_args[multiply-&-23] 1"] = {
62+
"message": "Parameter name contains invalid characters"
63+
}
64+
65+
snapshots["TestsRequests.test_post_wrong_args[multiply-23-&] 1"] = {
66+
"message": "Parameter needs to be an integer or float"
67+
}
68+
69+
snapshots["TestsRequests.test_post_wrong_args[multiply-23-None] 1"] = {
70+
"message": "Parameter needs to be an integer or float"
71+
}
72+
73+
snapshots["TestsRequests.test_post_wrong_args[playlist_jump-&-None] 1"] = {
74+
"message": "Parameter needs to be an integer or float"
75+
}
76+
77+
snapshots["TestsRequests.test_post_wrong_args[playlist_jump-None-None] 1"] = {
78+
"message": "Parameter needs to be an integer or float"
79+
}
80+
81+
snapshots["TestsRequests.test_post_wrong_args[playlist_move-&-23] 1"] = {
82+
"message": "Parameter needs to be an integer or float"
83+
}
84+
85+
snapshots["TestsRequests.test_post_wrong_args[playlist_move-23-&] 1"] = {
86+
"message": "Parameter needs to be an integer or float"
87+
}
88+
89+
snapshots["TestsRequests.test_post_wrong_args[playlist_move-23-None] 1"] = {
90+
"message": "Parameter needs to be an integer or float"
91+
}
92+
93+
snapshots["TestsRequests.test_post_wrong_args[playlist_move_up-&-None] 1"] = {
94+
"message": "Parameter needs to be an integer or float"
95+
}
96+
97+
snapshots["TestsRequests.test_post_wrong_args[playlist_move_up-None-None] 1"] = {
98+
"message": "Parameter needs to be an integer or float"
99+
}
100+
101+
snapshots["TestsRequests.test_post_wrong_args[playlist_remove-&-None] 1"] = {
102+
"message": "Parameter needs to be an integer or float"
103+
}
104+
105+
snapshots["TestsRequests.test_post_wrong_args[playlist_remove-None-None] 1"] = {
106+
"message": "Parameter needs to be an integer or float"
107+
}
108+
109+
snapshots["TestsRequests.test_post_wrong_args[seek-None-None] 1"] = {
110+
"message": "Parameter needs to be an integer or float"
111+
}
112+
113+
snapshots["TestsRequests.test_post_wrong_args[seek-g-None] 1"] = {
114+
"message": "Parameter needs to be an integer or float"
115+
}
116+
117+
snapshots["TestsRequests.test_post_wrong_args[set-&-foo] 1"] = {
118+
"message": "Parameter name contains invalid characters"
119+
}
120+
121+
snapshots["TestsRequests.test_post_wrong_args[set-foo- ] 1"] = {
122+
"message": "Parameter value contains invalid characters"
123+
}
124+
125+
snapshots["TestsRequests.test_post_wrong_args[set_audio_delay-&-None] 1"] = {
126+
"message": "Parameter needs to be an integer or float"
127+
}
128+
129+
snapshots["TestsRequests.test_post_wrong_args[set_position-&-None] 1"] = {
130+
"message": "Parameter needs to be an integer or float"
131+
}
132+
133+
snapshots["TestsRequests.test_post_wrong_args[set_position-None-None] 1"] = {
134+
"message": "Parameter needs to be an integer or float"
135+
}
136+
137+
snapshots["TestsRequests.test_post_wrong_args[set_sub_delay-&-None] 1"] = {
138+
"message": "Parameter needs to be an integer or float"
139+
}
140+
141+
snapshots["TestsRequests.test_post_wrong_args[set_volume-&-None] 1"] = {
142+
"message": "Parameter needs to be an integer or float"
143+
}
144+
145+
snapshots["TestsRequests.test_post_wrong_args[speed_adjust-&-None] 1"] = {
146+
"message": "Parameter needs to be an integer or float"
147+
}
148+
149+
snapshots["TestsRequests.test_post_wrong_args[speed_set-&-None] 1"] = {
150+
"message": "Parameter needs to be an integer or float"
151+
}
152+
153+
snapshots["TestsRequests.test_post_wrong_args[toggle-&-None] 1"] = {
154+
"message": "Parameter name contains invalid characters"
155+
}
156+
157+
snapshots["TestsRequests.test_post_wrong_args[toggle-None-None] 1"] = {
158+
"message": "Parameter name contains invalid characters"
159+
}
160+
9161
snapshots["test_status 1"] = {
10162
"audio-delay": 0,
11163
"audio-devices": [

tests/tests.py

+54
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,60 @@ def test_post(mpv_instance, endpoint, arg, key, value, invert_actual):
132132

133133
assert send(endpoint, arg=arg, status=key) == value
134134

135+
@staticmethod
136+
@pytest.mark.parametrize(
137+
"endpoint,arg,arg2",
138+
[
139+
("seek", "g", None),
140+
("seek", None, None),
141+
("add", "&", "foo"),
142+
("add", "foo", "&"),
143+
("cycle", "&", "foo"),
144+
("cycle", "foo", "&"),
145+
("multiply", "&", "23"),
146+
("multiply", "23", "&"),
147+
("multiply", "23", None),
148+
("set", "&", "foo"),
149+
("set", "foo", " "),
150+
("toggle", "&", None),
151+
("toggle", None, None),
152+
("set_position", "&", None),
153+
("set_position", None, None),
154+
("playlist_jump", "&", None),
155+
("playlist_jump", None, None),
156+
("playlist_remove", "&", None),
157+
("playlist_remove", None, None),
158+
("playlist_move", "&", "23"),
159+
("playlist_move", "23", "&"),
160+
("playlist_move", "23", None),
161+
("playlist_move_up", "&", None),
162+
("playlist_move_up", None, None),
163+
("loop_file", "&", None),
164+
("loop_file", None, None),
165+
("loop_playlist", "&", None),
166+
("add_volume", "&", None),
167+
("set_volume", "&", None),
168+
("add_sub_delay", "&", None),
169+
("set_sub_delay", "&", None),
170+
("add_audio_delay", "&", None),
171+
("set_audio_delay", "&", None),
172+
("speed_set", "&", None),
173+
("speed_adjust", "&", None),
174+
("add_chapter", "&", None),
175+
("loadfile", None, None),
176+
("loadfile", "http://foo", "invalid"),
177+
],
178+
)
179+
def test_post_wrong_args(mpv_instance, snapshot, endpoint, arg, arg2):
180+
send(endpoint, arg=arg, arg2=arg2, expect=400)
181+
api = f"api/{endpoint}"
182+
for a in [arg, arg2]:
183+
if a is not None:
184+
api += f"/{a}"
185+
response = requests.post(get_uri(api))
186+
assert response.status_code == 400
187+
snapshot.assert_match(response.json())
188+
135189
@staticmethod
136190
@pytest.mark.parametrize(
137191
"endpoint,arg,position",

webui.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ local commands = {
389389
end,
390390

391391
loadfile = function(uri, mode)
392-
if uri == nil or type(uri) ~= "string" then
392+
if uri == "" or type(uri) ~= "string" then
393393
return true, false, "No url provided!"
394394
end
395395
if mode ~= nil and

0 commit comments

Comments
 (0)