Adding database support, basic reads
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import pytest
|
||||
|
||||
import groove.db
|
||||
|
||||
from sqlalchemy import create_engine, insert
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def in_memory_db():
|
||||
"""
|
||||
An (empty) in-memory SQLite3 database
|
||||
"""
|
||||
engine = create_engine('sqlite:///:memory:', future=True)
|
||||
Session = sessionmaker(bind=engine, future=True)
|
||||
session = Session()
|
||||
groove.db.metadata.create_all(bind=engine)
|
||||
yield session
|
||||
session.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def db(in_memory_db):
|
||||
"""
|
||||
Populate the in-memory sqlite database with fixture data.
|
||||
"""
|
||||
|
||||
# create tracks
|
||||
query = insert(groove.db.track)
|
||||
in_memory_db.execute(query, [
|
||||
{'id': 1, 'relpath': '/UNKLE/Psyence Fiction/01 Guns Blazing (Drums of Death, Part 1).flac'},
|
||||
{'id': 2, 'relpath': '/UNKLE/Psyence Fiction/02 UNKLE (Main Title Theme).flac'},
|
||||
{'id': 3, 'relpath': '/UNKLE/Psyence Fiction/03 Bloodstain.flac'}
|
||||
])
|
||||
|
||||
# create playlists
|
||||
query = insert(groove.db.playlist)
|
||||
in_memory_db.execute(query, [
|
||||
{'id': 1, 'name': 'playlist one', 'description': 'the first one', 'slug': 'playlist-one'},
|
||||
{'id': 2, 'name': 'playlist two', 'description': 'the second one', 'slug': 'playlist-two'},
|
||||
{'id': 3, 'name': 'playlist three', 'description': 'the threerd one', 'slug': 'playlist-three'}
|
||||
])
|
||||
|
||||
# populate the playlists
|
||||
query = insert(groove.db.entry)
|
||||
in_memory_db.execute(query, [
|
||||
{'playlist_id': '1', 'track': '1', 'track_id': '1'},
|
||||
{'playlist_id': '1', 'track': '2', 'track_id': '2'},
|
||||
{'playlist_id': '1', 'track': '3', 'track_id': '3'},
|
||||
|
||||
{'playlist_id': '2', 'track': '1', 'track_id': '1'},
|
||||
|
||||
{'playlist_id': '3', 'track': '6', 'track_id': '2'},
|
||||
{'playlist_id': '3', 'track': '2', 'track_id': '3'},
|
||||
])
|
||||
yield in_memory_db
|
||||
+32
-4
@@ -1,31 +1,59 @@
|
||||
import pytest
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
import atheris
|
||||
import bottle
|
||||
from boddle import boddle
|
||||
|
||||
from groove import ondemand
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope='session')
|
||||
def init_db():
|
||||
ondemand.initialize()
|
||||
|
||||
|
||||
def test_server():
|
||||
with boddle():
|
||||
assert ondemand.index() == 'Groovy.'
|
||||
ondemand.index()
|
||||
assert bottle.response.status_code == 200
|
||||
|
||||
|
||||
def test_auth_with_valid_credentials():
|
||||
with boddle(auth=(os.environ.get('USERNAME'), os.environ.get('PASSWORD'))):
|
||||
assert ondemand.admin() == 'Authenticated. Groovy.'
|
||||
ondemand.build()
|
||||
assert bottle.response.status_code == 200
|
||||
|
||||
|
||||
def test_auth_random_input():
|
||||
|
||||
def auth(fuzzed_input):
|
||||
with boddle(auth=(fuzzed_input, fuzzed_input)):
|
||||
result = ondemand.admin()
|
||||
assert result.body == 'Access denied'
|
||||
response = ondemand.build()
|
||||
assert response.status_code == 403
|
||||
|
||||
atheris.Setup([sys.argv[0], "-atheris_runs=100000"], auth)
|
||||
try:
|
||||
atheris.Fuzz()
|
||||
except SystemExit:
|
||||
pass
|
||||
|
||||
|
||||
@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():
|
||||
response = ondemand.get_playlist(slug, db)
|
||||
assert response.status_code == expected
|
||||
|
||||
|
||||
def test_playlist_on_empty_db(in_memory_db):
|
||||
with boddle():
|
||||
response = ondemand.get_playlist('some-slug', in_memory_db)
|
||||
assert response.status_code == 404
|
||||
|
||||
Reference in New Issue
Block a user