2022-11-20 01:00:54 -08:00
|
|
|
import pytest
|
2022-11-19 16:58:58 -08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import atheris
|
2022-11-20 01:00:54 -08:00
|
|
|
import bottle
|
2022-11-19 15:14:12 -08:00
|
|
|
from boddle import boddle
|
2022-12-05 01:06:57 -08:00
|
|
|
from unittest.mock import MagicMock
|
2022-11-19 16:58:58 -08:00
|
|
|
|
2022-12-05 01:06:57 -08:00
|
|
|
from groove.webserver import webserver
|
2022-11-20 01:00:54 -08:00
|
|
|
|
|
|
|
|
2022-11-19 16:58:58 -08:00
|
|
|
def test_server():
|
2022-11-19 15:14:12 -08:00
|
|
|
with boddle():
|
2022-11-20 09:28:00 -08:00
|
|
|
webserver.index()
|
2022-11-20 01:00:54 -08:00
|
|
|
assert bottle.response.status_code == 200
|
2022-11-19 16:06:23 -08:00
|
|
|
|
|
|
|
|
2022-12-05 01:06:57 -08:00
|
|
|
@pytest.mark.parametrize('track_id, expected', [
|
|
|
|
('1', 200),
|
|
|
|
('99', 404)
|
|
|
|
])
|
|
|
|
def test_serve_track(monkeypatch, track_id, expected, db):
|
|
|
|
monkeypatch.setattr(webserver.requests, 'verify', MagicMock())
|
|
|
|
with boddle():
|
|
|
|
response = webserver.serve_track('ignored', track_id, db=db)
|
|
|
|
assert response.status_code == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_not_from_theme():
|
|
|
|
with boddle():
|
|
|
|
response = webserver.serve_static('favicon.ico')
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.body.read() == b'favicon.ico\n'
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_from_theme():
|
|
|
|
with boddle():
|
|
|
|
response = webserver.serve_static('test.css')
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.body.read() == b'/* test.css */\n'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('slug, expected', [
|
|
|
|
('non-existent-slug', False),
|
|
|
|
('playlist-one', True),
|
|
|
|
])
|
|
|
|
def test_search_playlist(slug, expected, auth, db):
|
|
|
|
with boddle(auth=auth):
|
|
|
|
response = webserver.search_playlist(slug, db)
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
if expected:
|
|
|
|
assert slug in response.body
|
|
|
|
else:
|
|
|
|
assert response.body == {}
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_with_valid_credentials(auth):
|
|
|
|
with boddle(auth=auth):
|
2022-11-20 09:28:00 -08:00
|
|
|
webserver.build()
|
2022-11-20 01:00:54 -08:00
|
|
|
assert bottle.response.status_code == 200
|
2022-11-19 16:58:58 -08:00
|
|
|
|
|
|
|
|
2022-12-05 01:06:57 -08:00
|
|
|
@pytest.mark.skip
|
2022-11-19 16:58:58 -08:00
|
|
|
def test_auth_random_input():
|
|
|
|
def auth(fuzzed_input):
|
|
|
|
with boddle(auth=(fuzzed_input, fuzzed_input)):
|
2022-11-20 09:28:00 -08:00
|
|
|
response = webserver.build()
|
2022-11-20 01:07:38 -08:00
|
|
|
assert response.status_code == 401
|
2022-11-19 16:58:58 -08:00
|
|
|
atheris.Setup([sys.argv[0], "-atheris_runs=100000"], auth)
|
|
|
|
try:
|
|
|
|
atheris.Fuzz()
|
|
|
|
except SystemExit:
|
|
|
|
pass
|
2022-11-20 01:00:54 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('slug, expected', [
|
|
|
|
('playlist-one', 200),
|
|
|
|
('playlist-two', 200),
|
|
|
|
('playlist-three', 200),
|
|
|
|
('no such playlist', 404),
|
|
|
|
])
|
|
|
|
def test_playlist(db, slug, expected):
|
|
|
|
with boddle():
|
2022-12-05 01:06:57 -08:00
|
|
|
response = webserver.serve_playlist(slug, db)
|
2022-11-20 01:00:54 -08:00
|
|
|
assert response.status_code == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_playlist_on_empty_db(in_memory_db):
|
|
|
|
with boddle():
|
2022-12-05 01:06:57 -08:00
|
|
|
response = webserver.serve_playlist('some-slug', in_memory_db)
|
2022-11-20 01:00:54 -08:00
|
|
|
assert response.status_code == 404
|