Skip to content
Snippets Groups Projects
Commit b28e58d8 authored by Simon Künzel's avatar Simon Künzel
Browse files

py: Added API testing

parent 25c5c785
No related branches found
No related tags found
No related merge requests found
......@@ -14,5 +14,8 @@ if __name__ == '__main__':
try:
suite = unittest.defaultTestLoader.discover('./tests/', pattern="*") #pylint: disable=invalid-name
unittest.TextTestRunner(verbosity=2, failfast=True).run(suite)
suite = unittest.defaultTestLoader.discover("./api_tests/", pattern="*", top_level_dir="./api_tests")
unittest.TextTestRunner(verbosity=2, failfast=True).run(suite)
finally:
tear_down()
import json
from unittest import TestCase
import server
from api.miscellaneous.constants import *
from api.version import API_LATEST_VERSION, get_api_path
def reset_database():
import sqlite3
import os
db = sqlite3.connect(server.config['SQLITE_DB'])
cur = db.cursor()
cur.executescript(open(os.path.join(os.path.dirname(__file__), server.config['DB_API_TEST_DATA'])).read())
db.commit()
db.close()
class ApiTest(TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._client = server.app.test_client()
def setUp(self):
super().setUp()
reset_database()
def moderator_login(self):
self._client.post(path=f"/api/v{API_LATEST_VERSION}/authentication/fsmpi", json={
"username": "videoag",
"password": "videoag"
})
def moderator_logout(self):
self._client.post(path=f"/api/v{API_LATEST_VERSION}/authentication/moderator_logout")
def do_json_request(self,
method: str,
path: str,
request: object or None,
expected_response: object or None = None,
assert_empty_response: bool = False,
use_moderator_login: bool = False,
expected_response_code: int or None = HTTP_200_OK,
api_version: int = API_LATEST_VERSION) -> tuple[int, object]:
if not path.startswith("/"):
raise ValueError("Path must start with /")
if use_moderator_login:
self.moderator_login()
else:
self.moderator_logout()
if request is None:
response = self._client.open(
method=method,
path=get_api_path(api_version, path)
)
else:
response = self._client.open(
method=method,
path=get_api_path(api_version, path),
mimetype="application/json",
data=json.dumps(request)
)
if expected_response_code is not None:
self.assertEqual(response.status_code, expected_response_code)
data = None
if response.mimetype == "application/json":
try:
data = json.loads(response.get_data(as_text=True))
except Exception as e:
raise AssertionError("Malformed json") from e
if expected_response:
if assert_empty_response:
raise ValueError("Cannot assert empty response while expecting a response")
if data is None:
raise AssertionError("Expected response but got none (Content-Type is not 'application/json')")
self.assertDictEqual(expected_response, data)
elif assert_empty_response and data is not None and len(data) > 0:
raise AssertionError("Expected no response but got one: " + data)
return response.status_code, data
def tearDown(self):
super().tearDown()
\ No newline at end of file
DELETE FROM `announcements`;
INSERT INTO `announcements` (`id`, `extid`, `text`, `level`, `visible`, `deleted`, `time_publish`, `time_expire`, `time_created`, `time_updated`, `created_by`) VALUES
(1, NULL, 'Test Ankündigung', 0, 1, 0, '2024-01-26 00:00:00', '2050-03-25 00:00:00', '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(2, NULL, 'Neue Ankündigung', 1, 1, 0, '2024-03-01 00:00:00', NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(3, NULL, 'Versteckte Ankündigung', 1, 0, 0, NULL, NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(4, NULL, 'Upcoming Announcement', 2, 1, 0, '2050-03-22 00:00:00', NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(5, NULL, 'Expired Announcement', 0, 1, 0, '2024-01-26 00:00:00', '2024-02-01 00:00:00', '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0);
DELETE FROM `chapters`;
INSERT INTO `chapters` (`id`, `lecture_id`, `time`, `text`, `visible`, `deleted`, `time_created`, `time_updated`, `created_by`, `submitted_by`) VALUES
(1, 25, 60, 'test1', 1, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0, NULL),
(2, 25, 7200, 'test2', 1, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0, NULL),
(3, 26, 360, 'Hidden', 0, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0, NULL),
(4, 1187, 0, 'Deleted', 1, 1, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0, NULL),
(5, 3, 429, 'Something', 1, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0, NULL);
DELETE FROM `courses_data`;
INSERT INTO `courses_data` (`id`, `visible`, `listed`, `deleted`, `title`, `short`, `handle`, `organizer`, `subject`, `credits`, `created_by`, `time_created`, `time_updated`, `semester`, `settings`, `downloadable`, `embedinvisible`, `description`, `internal`, `responsible`, `feed_url`, `external`, `coursechapters`, `autopublish`, `autovisible`, `profile`, `login_info`) VALUES
(2, 1, 1, 0, 'Berechenbarkeit und Komplexität', 'BuK', '07ws-buk', 'Prof. Vöcking', 'Informatik', 6, 2, '2007-10-01 00:00:00', '0000-00-00 00:00:00', '2007ws', '', 1, 0, 'Seite zur Veranstaltung...', '', '', '', 0, 0, 0, 0, 'default', ''),
(3, 1, 1, 0, 'Diskrete Strukturen', 'Diskrete', '07ws-diskrete', 'Prof. Hiß', 'Informatik', 6, 2, '2007-10-01 00:00:00', '0000-00-00 00:00:00', '2007ws', '', 1, 0, 'Von dieser Vorlesungsreihe fehlen die ersten zwei Monate. Wenn wir die Gelegenheit bekommen, filmen wir gerne nochmal.', '', '', '', 0, 0, 0, 0, 'default', ''),
(13, 0, 1, 0, 'Formale Systeme, Automaten, Prozesse', 'FoSAP', '09ss-fosap', 'Prof. Rossmanith', 'Informatik', 6, 2, '2009-04-01 00:00:00', '0000-00-00 00:00:00', '2009ss', '', 1, 0, 'Seite des Lehrstuhls ...', '', '', '', 0, 0, 0, 0, 'default', ''),
(62, 1, 0, 0, 'Investition und Finanzierung', 'InFin', '11ws-infin', 'Prof. Breuer', 'BWL', 6, 1, '2011-09-22 21:50:23', '2011-10-16 19:36:13', '2011ws', '', 0, 0, 'Seite im Campus ...', '', '', '', 0, 0, 0, 0, 'default', '');
DELETE FROM `featured`;
INSERT INTO `featured` (`id`, `title`, `text`, `internal`, `type`, `param`, `param2`, `order`, `visible`, `deleted`, `time_created`, `time_updated`, `created_by`) VALUES
(1, 'Video AG', 'Wir machen Vorlesungsvideos', '', 'plain', '', '', 1, 1, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(2, 'Image Panel', '', '', 'image', 'https://example.com/image.jpg', '', NULL, 0, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(3, 'Courses Panel', 'Vom Winter 07', '', 'courses', 'semester', '2007ws', NULL, 1, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(4, 'Lecture Panel', 'Watch this!', '', 'video', '3', '', NULL, 1, 0, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(22, 'Deleted', 'Wir machen Vorlesungsvideos', '', 'plain', '', '', 1, 1, 1, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0);
DELETE FROM `formats`;
INSERT INTO `formats` (`id`, `description`, `keywords`, `resolution`, `aspect`, `prio`, `player_prio`, `mimetype`, `options`, `suffix`) VALUES
(1, 'Format unbekannt', '', '', '', -100, 0, 'video/mp4', NULL, NULL),
(4, '1080p', '1080p', '1920x1080', '16:9', 10, 7, 'video/mp4', NULL, '-1080p.mp4'),
(5, '720p', '720p', '1280x720', '16:9', 5, 10, 'video/mp4', NULL, '-720p.mp4');
DELETE FROM `lectures_data`;
INSERT INTO `lectures_data` (`id`, `course_id`, `visible`, `timed_release`, `use_timed_release`, `drehplan`, `deleted`, `title`, `comment`, `internal`, `speaker`, `place`, `time`, `duration`, `time_created`, `time_updated`, `jumplist`, `titlefile`, `live`, `norecording`, `profile`, `stream_settings`, `stream_job`) VALUES
(1, 2, 1, NULL, 0, 'egal', 0, 'Einführung zur Berechenbarkeit', '', '', '', '', '2007-10-19 12:00:00', 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/07ws-buk/07ws-buk-071019-title.jpg', 0, 0, NULL, '', NULL),
(2, 2, 1, NULL, 0, 'egal', 0, 'Einführung zur Berechenbarkeit', '', '', '', '', '2007-10-23 08:30:00', 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/07ws-buk/07ws-buk-071023-title.jpg', 0, 0, NULL, '', NULL),
(3, 2, 1, NULL, 0, 'egal', 0, 'Einführung zur Berechenbarkeit', '', '', '', '', '2007-10-26 12:00:00', 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/07ws-buk/07ws-buk-071026-title.jpg', 0, 0, NULL, '', NULL),
(25, 3, 1, NULL, 0, 'egal', 0, 'Graphentheorie: Grundbegriffe, Datenstrukturen, Algorithmus für Breitensuche', '', '', '', '', '2007-12-11 13:30:00', 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/07ws-diskrete/07ws-diskrete-071211-title.jpg', 0, 0, NULL, '', NULL),
(26, 0, 1, NULL, 0, 'egal', 0, 'Hamiltonkreis, Eulertour, Eulerweg', '', '', '', '', '2007-12-18 13:30:00', 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/07ws-diskrete/07ws-diskrete-071218-title.jpg', 0, 0, NULL, '', NULL),
(29, 3, 1, NULL, 0, 'egal', 0, 'Modulare Arithmetik: Gruppe, Ring, Körper, abelsche Gruppe, Untergruppe, Einheitengruppe. Restklassenringe, Primzahl.', '', '', '', '', '2008-01-17 08:15:00', 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/07ws-diskrete/07ws-diskrete-080117-title.jpg', 0, 0, NULL, '', NULL),
(185, 13, 1, NULL, 0, 'egal', 0, 'Organisatorisches, Motivation, Künstliche Pflanzen, Alphabete, Wörter, Sprachen', 'Sorry für den schlechten Ton', '', '', '', '2009-04-16 10:00:00', 90, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/09ss-fosap/09ss-fosap-090416-title.jpg', 0, 0, NULL, '', NULL),
(186, 13, 1, NULL, 0, 'egal', 0, 'Alphabete, Wörter, Sprachen, Reguläre Ausdrücke', '', '', '', '', '2009-04-21 08:15:00', 45, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/09ss-fosap/09ss-fosap-090421-title.jpg', 0, 0, NULL, '', NULL),
(187, 13, 1, NULL, 0, 'egal', 1, 'Reguläre Ausdrücke, Endliche Automaten', '', '', '', '', '2009-04-23 10:00:00', 90, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/09ss-fosap/09ss-fosap-090423-title.jpg', 0, 0, NULL, '', NULL),
(1186, 62, 1, NULL, 0, 'egal', 0, 'Einführung, I. Grundlagen', '', '', '', 'Aula', '2011-10-10 18:30:00', 90, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/11ws-infin/11ws-infin-111010-title.jpg', 0, 0, NULL, '', NULL),
(1187, 62, 1, NULL, 0, 'egal', 0, '', 'noch kein Titel', '', '', 'Aula', '2011-10-17 18:30:00', 90, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/11ws-infin/11ws-infin-111017-title.jpg', 0, 0, NULL, '', NULL),
(1188, 62, 1, NULL, 0, 'egal', 0, '', 'noch kein Titel', '', '', 'Aula', '2050-10-24 18:30:00', 90, '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', 'pub/11ws-infin/11ws-infin-111024-title.jpg', 0, 0, NULL, '', NULL);
DELETE FROM `perm`;
INSERT INTO `perm` (`id`, `deleted`, `course_id`, `lecture_id`, `video_id`, `type`, `param1`, `param2`, `time_created`, `time_updated`, `created_by`) VALUES
(1, 0, 2, NULL, NULL, 'rwth', NULL, NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(2, 0, 2, NULL, NULL, 'fsmpi', NULL, NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(5, 0, 62, NULL, NULL, 'moodle', '1', NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(6, 1, NULL, 3, NULL, 'password', 't', 't', '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(17, 0, 3, NULL, NULL, 'none', '', '', '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(18, 0, NULL, 29, NULL, 'password', 't', 't', '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(19, 0, NULL, 25, NULL, 'public', NULL, NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0),
(28, 0, NULL, 3, NULL, 'public', NULL, NULL, '2024-01-01 00:00:00', '2024-01-01 00:00:00', 0);
DELETE FROM `videos_data`;
INSERT INTO `videos_data` (`id`, `lecture_id`, `visible`, `deleted`, `downloadable`, `title`, `comment`, `internal`, `path`, `file_modified`, `time_created`, `time_updated`, `created_by`, `file_size`, `video_format`, `hash`, `source`, `duration`) VALUES
(185, 185, 1, 0, 1, '', '', '', 'pub/09ss-fosap/09ss-fosap-090416.mp4', '2009-04-22 23:18:28', '0000-00-00 00:00:00', '2009-04-22 23:18:28', 0, 418148064, 5, '82f4389afa4b0012c3f3c583a097ef76', NULL, 0),
(186, 186, 1, 0, 1, '', '', '', 'pub/09ss-fosap/09ss-fosap-090421.mp4', '2009-05-05 23:35:30', '0000-00-00 00:00:00', '2009-05-05 23:35:30', 0, 246922038, 5, 'a1df9c4f7e220a72137d99589132e9e8', NULL, 0),
(204, 186, 1, 0, 1, '', '', '', 'pub/09ss-fosap/09ss-fosap-090421.f4v', '2009-05-05 23:48:13', '0000-00-00 00:00:00', '2009-05-05 23:48:13', 0, 228716898, 4, 'c4ebf870cdf5691c75fbc5c09fda6a53', NULL, 0),
(214, 187, 1, 1, 1, '', '', '', 'pub/09ss-fosap/09ss-fosap-090423.mp4', '2009-07-06 14:06:32', '0000-00-00 00:00:00', '2009-07-06 14:06:32', 0, 506655824, 5, '1c7e441eae08f04867c282c8f03c1aa2', NULL, 0),
(1366, 1, 1, 0, 1, '', '', '', 'vpnonline/07ws-buk/07ws-buk-071019.mp4', '2011-08-15 05:34:54', '0000-00-00 00:00:00', '2011-08-15 05:57:30', 0, 309620019, 1, 'c33d54c32a64ca2db52da59e2c117e7b', NULL, 0),
(1367, 2, 1, 0, 1, '', '', '', 'vpnonline/07ws-buk/07ws-buk-071023.mp4', '2011-08-15 05:35:37', '0000-00-00 00:00:00', '2011-08-15 05:57:33', 0, 442344305, 1, 'd4a1b0dea093334637034c688d376929', NULL, 0),
(1368, 3, 0, 0, 1, '', '', '', 'vpnonline/07ws-buk/07ws-buk-071026.mp4', '2011-08-15 05:36:29', '0000-00-00 00:00:00', '2011-08-15 05:57:37', 0, 496109745, 1, '6fa56385b454ad06ad20bce95ef70844', NULL, 0),
(1486, 26, 1, 0, 1, '', '', '', 'pub/07ws-diskrete/07ws-diskrete-071218.mp4', '2011-08-23 01:18:41', '0000-00-00 00:00:00', '2011-08-23 02:35:37', 0, 398065123, 1, '879b9fed123cfe08ccd6817c1ab4ecad', NULL, 0),
(1487, 29, 1, 0, 1, '', '', '', 'pub/07ws-diskrete/07ws-diskrete-080117.mp4', '2011-08-23 01:19:24', '0000-00-00 00:00:00', '2011-08-23 02:35:39', 0, 389368926, 1, '0a299de5d132a99fcfcea2621b7925e5', NULL, 0),
(1495, 25, 1, 0, 1, '', '', '', 'pub/07ws-diskrete/07ws-diskrete-071211.mp4', '2011-08-23 04:05:37', '0000-00-00 00:00:00', '2011-08-23 04:07:24', 0, 549478861, 1, '399a5095ed14b5dce2c1bb304c297ddc', NULL, 0),
(1497, 1186, 1, 0, 1, '', '', '', 'vpnonline/11ws-infin/11ws-infin-111010.mp4', '2011-10-12 14:17:09', '0000-00-00 00:00:00', '2011-10-28 11:21:31', 0, 687254584, 5, 'b4eb3b764b763ea86b1800730aac1a2a', NULL, 0),
(1539, 1187, 1, 0, 1, '', '', '', 'vpnonline/11ws-infin/11ws-infin-111017.mp4', '2011-10-19 11:36:40', '2011-10-25 00:48:34', '2011-10-28 11:21:35', 0, 1080865275, 5, 'b0b57f352a1f79de7a69c4ab8f0cef15', NULL, 0),
(1540, 1188, 1, 0, 1, '', '', '', 'vpnonline/11ws-infin/11ws-infin-111024.mp4', '2011-10-25 13:39:00', '2011-10-25 13:47:49', '2011-10-28 11:21:38', 0, 1042222975, 5, '50e67bdf59e74262f6ab3e770da1a7c0', NULL, 0);
DELETE FROM `users`;
INSERT INTO `users` (`id`, `name`, `realname`, `level`, `fsacc`, `last_login`, `calendar_key`, `rfc6238`, `mail_notifications`, `time_updated`, `notify_chapter_submitted`, `notify_new_video`, `notify_edit`) VALUES
(0, 'gustav', 'Gustav Geier', 1, '', NULL, '', '', 1, NULL, 1, 1, 0),
(42, 'gustav42', 'Gustav Geier', 2, '', NULL, '', '', 1, NULL, 1, 1, 0),
(43, 'videoag', 'Video', 1, 'videoag', NULL, '', '', 1, NULL, 1, 1, 0);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment