Skip to content
Snippets Groups Projects
Commit 607f5665 authored by Andreas Valder's avatar Andreas Valder
Browse files

moved unit tests to own module

parent 97c7aa8b
Branches
Tags
No related merge requests found
...@@ -15,9 +15,9 @@ Hinweis: diese Variante startet eine lokale Testversion der Website, es sind nic ...@@ -15,9 +15,9 @@ Hinweis: diese Variante startet eine lokale Testversion der Website, es sind nic
Alternativ, insbesondere zum Testen der Zugriffsbeschränkungen: Siehe `nginx.example.conf`. Alternativ, insbesondere zum Testen der Zugriffsbeschränkungen: Siehe `nginx.example.conf`.
### Unittests ### Unittests
Tests können mittels `./tests.py` ausgeführt werden. Tests können mittels `./runTests.py` ausgeführt werden.
Coverage Tests können mittels `rm .coverage; python -m coverage run tests.py; python -m coverage html` ausgeführt werden. Dies erstellt einen Ordner `htmlcov` in dem HTML Output liegt. Coverage Tests können mittels `rm .coverage; python -m coverage run runTests.py; python -m coverage html` ausgeführt werden. Dies erstellt einen Ordner `htmlcov` in dem HTML Output liegt.
### Zum Mitmachen: ### Zum Mitmachen:
1. Repo für den eigenen User forken, dafür den "Fork-Button" auf der Website verwenden 1. Repo für den eigenen User forken, dafür den "Fork-Button" auf der Website verwenden
......
#!/usr/bin/env python3
import unittest
import os
import server
def setUp():
server.app.testing = True
def tearDown():
os.unlink(server.app.config['SQLITE_DB'])
if __name__ == '__main__':
setUp()
try:
suite = unittest.defaultTestLoader.discover('./tests/', pattern="*")
unittest.TextTestRunner(verbosity=2, failfast=True).run(suite)
finally:
tearDown()
...@@ -26,7 +26,7 @@ if sys.argv[0].endswith('run.py'): ...@@ -26,7 +26,7 @@ if sys.argv[0].endswith('run.py'):
config['SQLITE_INIT_DATA'] = True config['SQLITE_INIT_DATA'] = True
config['DEBUG'] = True config['DEBUG'] = True
config.from_pyfile('config.py', silent=True) config.from_pyfile('config.py', silent=True)
if sys.argv[0].endswith('tests.py'): if sys.argv[0].endswith('runTests.py'):
print('running in test mode') print('running in test mode')
import tempfile import tempfile
# ensure we always use a clean sqlite db for tests # ensure we always use a clean sqlite db for tests
......
#!/usr/bin/env python3
import os
import unittest
import server
import json
import flask
from flask import url_for
class VideoTestCase(unittest.TestCase):
@classmethod
def tearDownClass(cls):
os.unlink(server.app.config['SQLITE_DB'])
def setUp(self):
server.app.testing = True
self.app = server.app.test_client()
def login(self, c):
with c.session_transaction() as sess:
sess['user'] = {'name': 'videoag', '_csrf_token': 'asd', 'dbid': 72}
sess['_csrf_token'] = 'asd'
def test_index(self):
with self.app as c:
r = c.get('/')
assert r.status_code == 200
self.login(c)
r = c.get('/')
assert r.status_code == 200
def test_courses(self):
with self.app as c:
r = c.get('/courses')
assert r.status_code == 200
self.login(c)
r = c.get('/courses')
assert r.status_code == 200
def test_lecture(self):
with self.app as c:
r = c.get('/15ws-afi/6981')
assert r.status_code == 200
def test_course(self):
with self.app as c:
# normal
r = c.get('/15ws-afi')
assert r.status_code == 200
# not listed
r = c.get('/15ws-bio')
assert r.status_code == 200
# not visible
r = c.get('/15ws-einfprog')
assert r.status_code == 404
self.login(c)
r = c.get('/15ws-afi')
assert r.status_code == 200
r = c.get('/15ws-einfprog')
assert r.status_code == 200
def test_timetable(self):
with self.app as c:
r = c.get('/internal/timetable')
assert r.status_code == 302
self.login(c)
r = c.get('internal/timetable')
assert r.status_code == 200
r = c.get('internal/timetable?date=2016-W05')
assert r.status_code == 200
assert 'AfI' in r.data.decode()
assert 'Progra' in r.data.decode()
assert 'Bio' in r.data.decode()
def test_faq(self):
r = self.app.get('/faq')
assert r.status_code == 200
def test_ical(self):
with self.app as c:
r = c.get('/internal/ical/all')
assert r.status_code == 401
self.login(c)
r = c.get('/internal/ical/all')
assert r.status_code == 200
assert 'Progra' in r.data.decode()
assert 'Vorlesung' in r.data.decode()
def test_sitemap(self):
r = self.app.get('/sitemap.xml')
assert r.status_code == 200
def test_chapters(self):
with self.app as c:
# wrong time format
r = c.post('/internal/newchapter/7011', data={'text':'testchapter A', 'time': 1234})
assert r.status_code == 400
# should be inserted as id 15
r = c.post('/internal/newchapter/7011', data={'text':'testchapter B', 'time': '00:10:00'})
assert r.status_code == 200
# not yet set visible
r = c.get('/internal/chapters/7011')
assert r.status_code == 404
# other lecture
r = c.get('/internal/chapters/7012')
assert r.status_code == 404
self.login(c)
r = c.post('/internal/edit', data={"chapters.15.visible":1,"_csrf_token":"asd"})
assert r.status_code == 200
r = c.get('/internal/chapters/7011')
assert 'testchapter B' in r.data.decode() and not 'testchapter A' in r.data.decode()
def test_search(self):
r = self.app.get('/search?q=Malo')
assert r.status_code == 200
assert 'Mathematische Logik II' in r.data.decode() and '4.1 Der Sequenzenkalkül' in r.data.decode()
r = self.app.get('/search?q=Afi+Stens')
assert r.status_code == 200
assert 'Analysis für Informatiker' in r.data.decode() and 'Höhere Mathematik I' in r.data.decode()
def test_login(self):
# test login page
r = self.app.get('/internal/login')
assert r.status_code == 200
# test successfull login
with self.app as c:
r = c.post('/internal/login', data={'user': 'videoag', 'password': 'videoag', 'ref': '/'})
assert flask.session['user']
assert r.status_code == 302
assert '<a href="/">' in r.data.decode()
# test unsuccessfull login
with self.app as c:
r = c.post('/internal/login', data={'user': 'videoag', 'password': 'asd', 'ref': '/'})
assert flask.session['user']
assert r.status_code == 403
assert 'Login fehlgeschlagen' in r.data.decode()
def test_logout(self):
with self.app as c:
with c.session_transaction() as sess:
sess['user'] = {'foo': 'bar'}
r = c.get('/internal/logout', data={'ref': '/'})
assert not flask.session.get('user')
assert r.status_code == 302
assert '<a href="/">' in r.data.decode()
def test_edit(self):
with self.app as c:
# test auth
r = c.get('/internal/new/courses', data={'title': 'Neue Vera14352345nstaltung totalyrandomcrap', 'responsible': 'foo', 'handle': '2r5sQ46z4w3DFCRT3<F4>DG', '_csrf_token': 'asd'})
assert r.status_code != 200
r = c.get('/internal/changelog')
assert r.status_code == 302
# all other tests are done logged in
self.login(c)
# add course
r = c.get('/internal/new/courses', data={'title': 'Neue Veranstaltung totalyrandomcrap', 'responsible': 'foo', 'handle': '2r5sQDFCRT3DG', '_csrf_token': 'asd'})
assert r.status_code == 200
r = self.app.get('/courses')
assert r.status_code == 200
assert 'Neue Veranstaltung totalyrandomcrap' in r.data.decode() and '2r5sQDFCRT3DG' in r.data.decode()
# rename lecture
r = c.get('internal/edit', data={"lectures.7353.title":"Testtitle","_csrf_token":"asd"})
assert r.status_code == 200
# test if the changelog is written
r = c.get('/internal/changelog')
assert r.status_code == 200
assert 'Testtitle' in r.data.decode() and 'lectures.7353.title' in r.data.decode()
def test_legacyurl(self):
with self.app as c:
r = self.app.get('/site/feed.php?newcourses')
assert r.status_code == 302
assert url_for('courses_feed') in r.data.decode()
r = self.app.get('/?view=faq')
assert r.status_code == 302
assert url_for('faq') in r.data.decode()
r = self.app.get('/site/feed.php?all')
assert r.status_code == 302
assert url_for('feed') in r.data.decode()
r = self.app.get('/?course=16ws-progra')
assert r.status_code == 302
assert url_for('course', handle='16ws-progra') in r.data.decode()
r = self.app.get('/?view=player&lectureid=7319')
assert r.status_code == 302
assert url_for('lecture', id='7319', course='16ws-progra') in r.data.decode()
r = self.app.get('/site/feed.php?16ws-afi')
assert r.status_code == 302
assert url_for('feed', handle='16ws-afi') in r.data.decode()
r = self.app.get('/site/embed.php?lecture=7319')
assert r.status_code == 302
assert url_for('embed', id='7319', course='16ws-progra') in r.data.decode()
r = self.app.get('/site/embed.php?vid=6088')
assert r.status_code == 302
assert url_for('embed', id='4105', course='14ws-ex3') in r.data.decode()
def test_livestream(self):
with self.app as c:
# fails because no ip is sent
r = self.app.get('/internal/streaming/legacy_auth', data={'app': 'live', 'call': 'publish', 'pass': 'caisoh8aht0wuSu', 'lecture': 7011, 'name': '16ss-dsal'})
assert r.status_code == 403
r = self.app.get('/internal/streaming/legacy_auth/testserver', data={'app': 'live', 'call': 'publish', 'pass': 'caisoh8aht0wuSu', 'lecture': 6981, 'name': '15ws-afi'}, headers={'X-Real-IP': '137.226.35.193'})
assert r.status_code == 200
# test for livestream job
r = self.app.post('/internal/jobs/api/worker/test/schedule', data=json.dumps({'jobtypes': ['simple_live_transcode'], 'queues': ['default'], 'apikey': '1'}), content_type='application/json')
assert r.status_code == 200
jobdata = json.loads(json.loads(r.data.decode())['data'])
assert jobdata.get('src') == 'rtmp://testserver/live/15ws-afi'
# test for thumbnail job
import livestreams
livestreams.livestream_thumbnail()
r = self.app.post('/internal/jobs/api/worker/test/schedule', data=json.dumps({'jobtypes': ['thumbnail'], 'queues': ['default'], 'apikey': '1'}), content_type='application/json')
assert r.status_code == 200
jobdata = json.loads(json.loads(r.data.decode())['data'])
assert jobdata.get('lectureid') == '6981'
assert jobdata.get('path') == 'pub/hls/15ws-afi.m3u8'
r = self.app.get('/internal/streaming/legacy_auth/testserver', data={'app': 'live', 'call': 'publish_done', 'pass': 'caisoh8aht0wuSu', 'lecture': 6981, 'name': '15ws-afi'}, headers={'X-Real-IP': '137.226.35.193'})
assert r.status_code == 200
# test if there are more jobs
r = self.app.post('/internal/jobs/api/worker/test/schedule', data=json.dumps({'jobtypes': ['simple_live_transcode'], 'queues': ['default'], 'apikey': '1'}), content_type='application/json')
assert r.status_code == 503
# test if job was cancled
r = self.app.post('/internal/jobs/api/job/1/ping', data={'apikey': '1', 'host': 'test', 'status': '{}', 'state': '{}'})
assert r.status_code == 205
def test_sorter(self):
with self.app as c:
r = self.app.get('/courses')
import sorter
match, fmt = sorter.sort_file('08ws-swt-081118.mp4')
assert len(match) == 1
assert match[0]['id'] == 104
match, fmt = sorter.sort_file('15ss-zkk-extremale-codes-1080p.mp4')
assert len(match) == 1
assert match[0]['id'] == 6095
def test_campusimport(self):
with self.app as c:
self.login(c)
r = self.app.post('/internal/import/257', data={'campus.new.url': 'https://www.campus.rwth-aachen.de/rwth/all/event.asp?gguid=0x4664DBD60E5A02479B53089BF0EB0681&tguid=0x0B473CF286B45B4984CD02565C07D6F8', 'campus.new.type': 'Vorlesung'})
assert r.status_code == 200
r = self.app.get('/internal/import/257/now')
assert r.status_code == 200
def test_cutprogress(self):
with self.app as c:
self.login(c)
r = self.app.get('/internal/cutprogress')
assert r.status_code == 200
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment