test_misc.py 9.5 KB
Newer Older
Andreas Valder's avatar
Andreas Valder committed
1
2
import os
import unittest
3
import json
4
import server
Andreas Valder's avatar
Andreas Valder committed
5
import flask
Andreas Valder's avatar
Andreas Valder committed
6
from flask import url_for
Andreas Valder's avatar
Andreas Valder committed
7
8

class VideoTestCase(unittest.TestCase):
9
10
	def tearDown(self):
		pass
Andreas Valder's avatar
Andreas Valder committed
11
12
	
	def setUp(self):
13
		server.app.testing = True
Andreas Valder's avatar
Andreas Valder committed
14
15
		self.app = server.app.test_client()

Andreas Valder's avatar
Andreas Valder committed
16
17
18
19
20
	def login(self, c):
		with c.session_transaction() as sess:
			sess['user'] = {'name': 'videoag', '_csrf_token': 'asd', 'dbid': 72}
			sess['_csrf_token'] = 'asd'

Andreas Valder's avatar
Andreas Valder committed
21
	def test_index(self):
Andreas Valder's avatar
Andreas Valder committed
22
23
24
25
26
27
28
		with self.app as c:
			r = c.get('/')
			assert r.status_code == 200

			self.login(c)
			r = c.get('/')
			assert r.status_code == 200
Andreas Valder's avatar
Andreas Valder committed
29
30
	
	def test_courses(self):
Andreas Valder's avatar
Andreas Valder committed
31
32
33
34
35
36
37
		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
38
39
40
41
42
	
	def test_lecture(self):
		with self.app as c:
			r = c.get('/15ws-afi/6981')
			assert r.status_code == 200
Andreas Valder's avatar
Andreas Valder committed
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	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

Andreas Valder's avatar
Andreas Valder committed
64
65
66
67
68
69
70
71
72
73
74
75
76
	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()
Andreas Valder's avatar
Andreas Valder committed
77
78
79
80
	
	def test_faq(self):
		r = self.app.get('/faq')
		assert r.status_code == 200
Andreas Valder's avatar
Andreas Valder committed
81
82
83
84
85
86
87
88
89
90
91

	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()
Andreas Valder's avatar
Andreas Valder committed
92
93
94
95
96
	
	def test_sitemap(self):
		r = self.app.get('/sitemap.xml')
		assert r.status_code == 200
	
Andreas Valder's avatar
Andreas Valder committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	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()

Andreas Valder's avatar
Andreas Valder committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
	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()
Andreas Valder's avatar
Andreas Valder committed
142
143
144
145
146
147
148
149
150

	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()
Andreas Valder's avatar
Andreas Valder committed
151
	
152
153
	def test_edit(self):
		with self.app as c:
154
155
156
157
			# 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

Andreas Valder's avatar
Andreas Valder committed
158
159
160
			r = c.get('/internal/changelog')
			assert r.status_code == 302

161
			# all other tests are done logged in
Andreas Valder's avatar
Andreas Valder committed
162
			self.login(c)
163

164
165
166
167
168
169
170
			# 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()

Andreas Valder's avatar
Andreas Valder committed
171
172
173
174
175
176
177
178
179
180
			# 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()


Andreas Valder's avatar
Andreas Valder committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	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()
Andreas Valder's avatar
Andreas Valder committed
198

Andreas Valder's avatar
Andreas Valder committed
199
200
201
202
203
204
205
206
			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()

Andreas Valder's avatar
Andreas Valder committed
207
			r = self.app.get('/site/embed.php?lecture=7319')
Andreas Valder's avatar
Andreas Valder committed
208
			assert r.status_code == 302
Andreas Valder's avatar
Andreas Valder committed
209
			assert url_for('embed', id='7319', course='16ws-progra') in r.data.decode()
Andreas Valder's avatar
Andreas Valder committed
210

Andreas Valder's avatar
Andreas Valder committed
211
			r = self.app.get('/site/embed.php?vid=6088')
Andreas Valder's avatar
Andreas Valder committed
212
			assert r.status_code == 302
Andreas Valder's avatar
Andreas Valder committed
213
			assert url_for('embed', id='4105', course='14ws-ex3') in r.data.decode()
Andreas Valder's avatar
Andreas Valder committed
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	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

250
251
252
253
254
255
256
257
258
259
260
261
262
	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

263
#	@unittest.skip("too slow")
264
265
266
267
268
269
270
	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

Julian Rother's avatar
Julian Rother committed
271
272
273
			r = self.app.post('/internal/import/257', data={'campus.new.url': 'https://online.rwth-aachen.de/RWTHonline/pl/ui/%24ctx/wbLv.wbShowLVDetail?pStpSpNr=269474&pSpracheNr=1', 'campus.new.type': 'Übung'})
			assert r.status_code == 200

274
275
276
			r = self.app.get('/internal/import/257/now')
			assert r.status_code == 200

277
278
279
280
281
	def test_cutprogress(self):
		with self.app as c:
			self.login(c)
			r = self.app.get('/internal/cutprogress')
			assert r.status_code == 200